Skip to content

Commit

Permalink
refactor: optimize file retrieval in GetFile handler
Browse files Browse the repository at this point in the history
  • Loading branch information
krau committed Nov 20, 2024
1 parent 6ec7d57 commit 3914aa8
Showing 1 changed file with 45 additions and 40 deletions.
85 changes: 45 additions & 40 deletions api/restful/routers/picture/handlers.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package picture

import (
"bytes"
"errors"
"io"
"net/http"

"github.com/krau/ManyACG/adapter"
"github.com/krau/ManyACG/common"
"github.com/krau/ManyACG/sources"
"go.mongodb.org/mongo-driver/bson/primitive"

"github.com/krau/ManyACG/service"
"github.com/krau/ManyACG/storage"
Expand All @@ -21,51 +16,61 @@ import (

func GetFile(ctx *gin.Context) {
picture := ctx.MustGet("picture").(*types.Picture)

file, err := storage.GetFileStream(ctx, picture.StorageInfo.Original)
data, err := storage.GetFile(ctx, picture.StorageInfo.Original)
if err != nil {
common.Logger.Errorf("Failed to get file: %v", err)
common.GinErrorResponse(ctx, err, http.StatusInternalServerError, "Failed to get file")
return
}
defer file.Close()
mimeType := mimetype.Detect(data)
ctx.Data(http.StatusOK, mimeType.String(), data)

var mimeBuf bytes.Buffer
tee := io.TeeReader(io.LimitReader(file, 512), &mimeBuf)
// GetFileStream:
//
// file, err := storage.GetFileStream(ctx, picture.StorageInfo.Original)
// if err != nil {
// common.Logger.Errorf("Failed to get file: %v", err)
// common.GinErrorResponse(ctx, err, http.StatusInternalServerError, "Failed to get file")
// return
// }
// defer file.Close()

_, err = io.Copy(io.Discard, tee)
if err != nil {
common.Logger.Errorf("Failed to read for mime detection: %v", err)
common.GinErrorResponse(ctx, err, http.StatusInternalServerError, "Failed to process file")
return
}
// var mimeBuf bytes.Buffer
// tee := io.TeeReader(io.LimitReader(file, 512), &mimeBuf)

mimeType := mimetype.Detect(mimeBuf.Bytes())
ctx.Header("Content-Type", mimeType.String())
artworkID, _ := primitive.ObjectIDFromHex(picture.ArtworkID)
artwork, err := service.GetArtworkByID(ctx, artworkID, adapter.LoadNone())
if err != nil {
common.Logger.Errorf("Failed to get artwork: %v", err)
common.GinErrorResponse(ctx, err, http.StatusInternalServerError, "Failed to get artwork")
return
}
fileName, _ := sources.GetFileName(artwork, picture)
if fileName != "" {
ctx.Header("Content-Disposition", "inline; filename="+fileName)
}
// _, err = io.Copy(io.Discard, tee)
// if err != nil {
// common.Logger.Errorf("Failed to read for mime detection: %v", err)
// common.GinErrorResponse(ctx, err, http.StatusInternalServerError, "Failed to process file")
// return
// }

if _, err := io.Copy(ctx.Writer, &mimeBuf); err != nil {
common.Logger.Errorf("Failed to write mime buffer: %v", err)
common.GinErrorResponse(ctx, err, http.StatusInternalServerError, "Failed to send file")
return
}
// mimeType := mimetype.Detect(mimeBuf.Bytes())
// ctx.Header("Content-Type", mimeType.String())
// artworkID, _ := primitive.ObjectIDFromHex(picture.ArtworkID)
// artwork, err := service.GetArtworkByID(ctx, artworkID, adapter.LoadNone())
// if err != nil {
// common.Logger.Errorf("Failed to get artwork: %v", err)
// common.GinErrorResponse(ctx, err, http.StatusInternalServerError, "Failed to get artwork")
// return
// }
// fileName, _ := sources.GetFileName(artwork, picture)
// if fileName != "" {
// ctx.Header("Content-Disposition", "inline; filename="+fileName)
// }

_, err = io.Copy(ctx.Writer, file)
if err != nil {
common.Logger.Errorf("Failed to copy file: %v", err)
common.GinErrorResponse(ctx, err, http.StatusInternalServerError, "Failed to send file")
return
}
// if _, err := io.Copy(ctx.Writer, &mimeBuf); err != nil {
// common.Logger.Errorf("Failed to write mime buffer: %v", err)
// common.GinErrorResponse(ctx, err, http.StatusInternalServerError, "Failed to send file")
// return
// }

// _, err = io.Copy(ctx.Writer, file)
// if err != nil {
// common.Logger.Errorf("Failed to copy file: %v", err)
// common.GinErrorResponse(ctx, err, http.StatusInternalServerError, "Failed to send file")
// return
// }
}

func RandomPicture(ctx *gin.Context) {
Expand Down

0 comments on commit 3914aa8

Please sign in to comment.