Skip to content
This repository was archived by the owner on Dec 2, 2024. It is now read-only.

Commit 3d05daf

Browse files
committed
Add thumbnail sizes
1 parent 781338c commit 3d05daf

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

file.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type File struct {
2424
Bucket string `bson:"bucket" json:"-"`
2525
Mime string `bson:"mime" json:"mime"`
2626
ThumbnailMime string `bson:"thumbnail_mime,omitempty" json:"thumbnail_mime,omitempty"`
27+
ThumbnailSize int64 `bson:"thumbnail_size,omitempty" json:"thumbnail_size,omitempty"`
2728
Size int64 `bson:"size" json:"size"`
2829
Filename string `bson:"filename,omitempty" json:"filename,omitempty"`
2930
Width int `bson:"width,omitempty" json:"width,omitempty"`
@@ -514,25 +515,30 @@ func (f *File) GenerateThumbnail() error {
514515
}
515516

516517
// Upload thumbnail
517-
if _, err := s3Clients[s3RegionOrder[0]].FPutObject(
518+
uploadInfo, err := s3Clients[s3RegionOrder[0]].FPutObject(
518519
ctx,
519520
f.Bucket,
520521
fmt.Sprint(f.Hash, "_thumbnail"),
521522
fmt.Sprint(ingestDir, "/thumbnail.", format),
522523
minio.PutObjectOptions{
523524
ContentType: fmt.Sprint("image/", format),
524525
},
525-
); err != nil {
526+
)
527+
if err != nil {
526528
sentry.CaptureException(err)
527529
return err
528530
}
529531

530532
// Update file details
531533
f.ThumbnailMime = fmt.Sprint("image/", format)
534+
f.ThumbnailSize = uploadInfo.Size
532535
if _, err := db.Collection("files").UpdateMany(
533536
context.TODO(),
534537
bson.M{"hash": f.Hash, "bucket": f.Bucket},
535-
bson.M{"$set": bson.M{"thumbnail_mime": f.ThumbnailMime}},
538+
bson.M{"$set": bson.M{
539+
"thumbnail_mime": f.ThumbnailMime,
540+
"thumbnail_size": f.ThumbnailSize,
541+
}},
536542
); err != nil {
537543
sentry.CaptureException(err)
538544
return err
@@ -545,7 +551,7 @@ func (f *File) GetObject(thumbnail bool) (*minio.Object, error) {
545551
objName := f.Hash
546552
if thumbnail && f.Bucket == "attachments" && (strings.HasPrefix(f.Mime, "image/") || strings.HasPrefix(f.Mime, "video/")) {
547553
// Generate thumbnail if one doesn't exist yet
548-
if f.ThumbnailMime == "" {
554+
if f.ThumbnailMime == "" || f.ThumbnailSize == 0 {
549555
if err := f.GenerateThumbnail(); err != nil {
550556
return nil, err
551557
}

router.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,11 @@ func downloadFile(w http.ResponseWriter, r *http.Request) {
111111
// Set response headers
112112
if thumbnail {
113113
w.Header().Set("Content-Type", f.ThumbnailMime)
114+
w.Header().Set("Content-Length", strconv.FormatInt(f.ThumbnailSize, 10))
114115
} else {
115116
w.Header().Set("Content-Type", f.Mime)
117+
w.Header().Set("Content-Length", strconv.FormatInt(f.Size, 10))
116118
}
117-
w.Header().Set("Content-Length", strconv.FormatInt(f.Size, 10))
118119
w.Header().Set("ETag", f.Id)
119120
w.Header().Set("Cache-Control", "pbulic, max-age=31536000") // 1 year cache (files should never change)
120121
filename := chi.URLParam(r, "*")

0 commit comments

Comments
 (0)