website/pkg/models/backfill/backfill_filesize.go
Noah Petherbridge 8e4bb85934 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.
2022-12-06 21:50:42 -08:00

65 lines
1.5 KiB
Go

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
}