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

Commit 5c85cd0

Browse files
committed
Get size from object rather than database
1 parent 3d05daf commit 5c85cd0

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

file.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ 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"`
2827
Size int64 `bson:"size" json:"size"`
28+
ThumbnailSize int64 `bson:"thumbnail_size,omitempty" json:"thumbnail_size,omitempty"`
2929
Filename string `bson:"filename,omitempty" json:"filename,omitempty"`
3030
Width int `bson:"width,omitempty" json:"width,omitempty"`
3131
Height int `bson:"height,omitempty" json:"height,omitempty"`
@@ -428,7 +428,7 @@ func (f *File) GenerateThumbnail() error {
428428
return err
429429
}
430430

431-
obj, err := f.GetObject(false)
431+
obj, _, err := f.GetObject(false)
432432
if err != nil {
433433
sentry.CaptureException(err)
434434
return err
@@ -547,25 +547,42 @@ func (f *File) GenerateThumbnail() error {
547547
return nil
548548
}
549549

550-
func (f *File) GetObject(thumbnail bool) (*minio.Object, error) {
550+
func (f *File) GetObject(thumbnail bool) (*minio.Object, *minio.ObjectInfo, error) {
551551
objName := f.Hash
552552
if thumbnail && f.Bucket == "attachments" && (strings.HasPrefix(f.Mime, "image/") || strings.HasPrefix(f.Mime, "video/")) {
553553
// Generate thumbnail if one doesn't exist yet
554554
if f.ThumbnailMime == "" || f.ThumbnailSize == 0 {
555555
if err := f.GenerateThumbnail(); err != nil {
556-
return nil, err
556+
return nil, nil, err
557557
}
558558
}
559559

560560
objName += "_thumbnail"
561561
}
562562

563-
return s3Clients[s3RegionOrder[0]].GetObject(
563+
// Get object
564+
obj, err := s3Clients[s3RegionOrder[0]].GetObject(
564565
ctx,
565566
f.Bucket,
566567
objName,
567568
minio.GetObjectOptions{},
568569
)
570+
if err != nil {
571+
return nil, nil, err
572+
}
573+
574+
// Get object info (used for size)
575+
objInfo, err := s3Clients[s3RegionOrder[0]].StatObject(
576+
ctx,
577+
f.Bucket,
578+
objName,
579+
minio.StatObjectOptions{},
580+
)
581+
if err != nil {
582+
return nil, nil, err
583+
}
584+
585+
return obj, &objInfo, nil
569586
}
570587

571588
func (f *File) Delete() error {

router.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func downloadFile(w http.ResponseWriter, r *http.Request) {
101101
} else if strings.HasPrefix(f.Mime, "video/") && r.URL.Query().Has("thumbnail") {
102102
thumbnail = true
103103
}
104-
obj, err := f.GetObject(thumbnail)
104+
obj, objInfo, err := f.GetObject(thumbnail)
105105
if err != nil {
106106
sentry.CaptureException(err)
107107
http.Error(w, "Failed to get object", http.StatusInternalServerError)
@@ -111,11 +111,10 @@ 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))
115114
} else {
116115
w.Header().Set("Content-Type", f.Mime)
117-
w.Header().Set("Content-Length", strconv.FormatInt(f.Size, 10))
118116
}
117+
w.Header().Set("Content-Length", strconv.FormatInt(objInfo.Size, 10))
119118
w.Header().Set("ETag", f.Id)
120119
w.Header().Set("Cache-Control", "pbulic, max-age=31536000") // 1 year cache (files should never change)
121120
filename := chi.URLParam(r, "*")

0 commit comments

Comments
 (0)