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

Commit

Permalink
Get size from object rather than database
Browse files Browse the repository at this point in the history
  • Loading branch information
tnix100 committed Nov 6, 2024
1 parent 3d05daf commit 5c85cd0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
27 changes: 22 additions & 5 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ type File struct {
Bucket string `bson:"bucket" json:"-"`
Mime string `bson:"mime" json:"mime"`
ThumbnailMime string `bson:"thumbnail_mime,omitempty" json:"thumbnail_mime,omitempty"`
ThumbnailSize int64 `bson:"thumbnail_size,omitempty" json:"thumbnail_size,omitempty"`
Size int64 `bson:"size" json:"size"`
ThumbnailSize int64 `bson:"thumbnail_size,omitempty" json:"thumbnail_size,omitempty"`
Filename string `bson:"filename,omitempty" json:"filename,omitempty"`
Width int `bson:"width,omitempty" json:"width,omitempty"`
Height int `bson:"height,omitempty" json:"height,omitempty"`
Expand Down Expand Up @@ -428,7 +428,7 @@ func (f *File) GenerateThumbnail() error {
return err
}

obj, err := f.GetObject(false)
obj, _, err := f.GetObject(false)
if err != nil {
sentry.CaptureException(err)
return err
Expand Down Expand Up @@ -547,25 +547,42 @@ func (f *File) GenerateThumbnail() error {
return nil
}

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

objName += "_thumbnail"
}

return s3Clients[s3RegionOrder[0]].GetObject(
// Get object
obj, err := s3Clients[s3RegionOrder[0]].GetObject(
ctx,
f.Bucket,
objName,
minio.GetObjectOptions{},
)
if err != nil {
return nil, nil, err
}

// Get object info (used for size)
objInfo, err := s3Clients[s3RegionOrder[0]].StatObject(
ctx,
f.Bucket,
objName,
minio.StatObjectOptions{},
)
if err != nil {
return nil, nil, err
}

return obj, &objInfo, nil
}

func (f *File) Delete() error {
Expand Down
5 changes: 2 additions & 3 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func downloadFile(w http.ResponseWriter, r *http.Request) {
} else if strings.HasPrefix(f.Mime, "video/") && r.URL.Query().Has("thumbnail") {
thumbnail = true
}
obj, err := f.GetObject(thumbnail)
obj, objInfo, err := f.GetObject(thumbnail)
if err != nil {
sentry.CaptureException(err)
http.Error(w, "Failed to get object", http.StatusInternalServerError)
Expand All @@ -111,11 +111,10 @@ func downloadFile(w http.ResponseWriter, r *http.Request) {
// Set response headers
if thumbnail {
w.Header().Set("Content-Type", f.ThumbnailMime)
w.Header().Set("Content-Length", strconv.FormatInt(f.ThumbnailSize, 10))
} else {
w.Header().Set("Content-Type", f.Mime)
w.Header().Set("Content-Length", strconv.FormatInt(f.Size, 10))
}
w.Header().Set("Content-Length", strconv.FormatInt(objInfo.Size, 10))
w.Header().Set("ETag", f.Id)
w.Header().Set("Cache-Control", "pbulic, max-age=31536000") // 1 year cache (files should never change)
filename := chi.URLParam(r, "*")
Expand Down

0 comments on commit 5c85cd0

Please sign in to comment.