Raise photo cap to 100, fix filesize storage

The photo upload limit for user profiles is raised from 24 to 100.

The bug about Filesize not saving to the database for Photos and
CommentPhotos (storing zeroes in the DB) has been fixed. Run the
`nonshy backfill filesizes` to populate your existing database.
This commit is contained in:
Noah Petherbridge 2022-12-06 21:50:42 -08:00
parent fd0ff31d3e
commit 8e4bb85934
7 changed files with 91 additions and 1 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
/web/static/photos
database.sqlite
settings.json
pgdump.sql

View File

@ -8,6 +8,7 @@ import (
"code.nonshy.com/nonshy/website/pkg/config"
"code.nonshy.com/nonshy/website/pkg/log"
"code.nonshy.com/nonshy/website/pkg/models"
"code.nonshy.com/nonshy/website/pkg/models/backfill"
"code.nonshy.com/nonshy/website/pkg/redis"
"github.com/urfave/cli/v2"
"gorm.io/driver/postgres"
@ -133,6 +134,27 @@ func main() {
},
},
},
{
Name: "backfill",
Usage: "One-off maintenance tasks and data backfills for database migrations",
Subcommands: []*cli.Command{
{
Name: "filesizes",
Usage: "repopulate Filesizes on all photos and comment_photos which have a zero stored in the DB",
Action: func(c *cli.Context) error {
initdb(c)
log.Info("Running BackfillFilesizes()")
err := backfill.BackfillFilesizes()
if err != nil {
return err
}
return nil
},
},
},
},
},
}

View File

@ -86,7 +86,7 @@ const (
// Quotas for uploaded photos.
PhotoQuotaUncertified = 6
PhotoQuotaCertified = 24
PhotoQuotaCertified = 100
)
// Forum settings

View File

@ -0,0 +1,64 @@
package backfill
import (
"os"
"code.nonshy.com/nonshy/website/pkg/log"
"code.nonshy.com/nonshy/website/pkg/models"
"code.nonshy.com/nonshy/website/pkg/photo"
)
// BackfillFilesizes finds Photos and CommentPhotos which have a zero Filesize and recomputes them.
func BackfillFilesizes() error {
// Find Photos with a filesize of zero.
var (
photos []*models.Photo
commentPhotos []*models.CommentPhoto
)
// Query for photos w/ zero filesize.
result := models.DB.Model(&models.Photo{}).Where("filesize = 0").Find(&photos)
if result.Error != nil {
return result.Error
}
// And CommentPhotos too.
result = models.DB.Model(&models.CommentPhoto{}).Where("filesize = 0").Find(&commentPhotos)
if result.Error != nil {
return result.Error
}
// Do the Photos.
for i, row := range photos {
// Set the filesize from disk.
if stat, err := os.Stat(photo.DiskPath(row.Filename)); err == nil {
row.Filesize = stat.Size()
}
log.Info("[%d of %d] Update photo %d: %s filesize=%d",
i+1, len(photos), row.ID, row.Filename, row.Filesize,
)
if err := row.Save(); err != nil {
return err
}
}
// And same for the CommentPhotos.
for i, row := range commentPhotos {
// Set the filesize from disk.
if stat, err := os.Stat(photo.DiskPath(row.Filename)); err == nil {
row.Filesize = stat.Size()
}
log.Info("[%d of %d] Update comment_photo %d: %s filesize=%d",
i+1, len(photos), row.ID, row.Filename, row.Filesize,
)
if err := row.Save(); err != nil {
return err
}
}
return nil
}

View File

@ -22,6 +22,7 @@ func CreateCommentPhoto(tmpl CommentPhoto) (*CommentPhoto, error) {
CommentID: tmpl.CommentID,
UserID: tmpl.UserID,
Filename: tmpl.Filename,
Filesize: tmpl.Filesize,
}
result := DB.Create(p)

View File

@ -0,0 +1 @@
package models

View File

@ -60,6 +60,7 @@ func CreatePhoto(tmpl Photo) (*Photo, error) {
UserID: tmpl.UserID,
Filename: tmpl.Filename,
CroppedFilename: tmpl.CroppedFilename,
Filesize: tmpl.Filesize,
Caption: tmpl.Caption,
Visibility: tmpl.Visibility,
Gallery: tmpl.Gallery,