From bf08dc2585dde71b222a3b4b5ab2b7921b35b72e Mon Sep 17 00:00:00 2001 From: kolaente Date: Thu, 5 Sep 2024 15:03:32 +0200 Subject: [PATCH] chore(files): use absolute file path to retrieve and save files (cherry picked from commit c2b116de70df335a6a354cf2b3595632c8b49ff1) --- pkg/files/files.go | 17 +++++++++------ pkg/routes/api/v1/task_attachment.go | 32 ++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/pkg/files/files.go b/pkg/files/files.go index 033799f10..1c2f018fa 100644 --- a/pkg/files/files.go +++ b/pkg/files/files.go @@ -20,6 +20,7 @@ import ( "errors" "io" "os" + "path/filepath" "strconv" "time" @@ -52,17 +53,21 @@ type File struct { } // TableName is the table name for the files table -func (File) TableName() string { +func (*File) TableName() string { return "files" } -func (f *File) getFileName() string { - return config.FilesBasePath.GetString() + "/" + strconv.FormatInt(f.ID, 10) +func (f *File) getAbsoluteFilePath() string { + return filepath.Join( + config.ServiceRootpath.GetString(), + config.FilesBasePath.GetString(), + strconv.FormatInt(f.ID, 10), + ) } // LoadFileByID returns a file by its ID func (f *File) LoadFileByID() (err error) { - f.File, err = afs.Open(f.getFileName()) + f.File, err = afs.Open(f.getAbsoluteFilePath()) return } @@ -137,7 +142,7 @@ func (f *File) Delete() (err error) { return ErrFileDoesNotExist{FileID: f.ID} } - err = afs.Remove(f.getFileName()) + err = afs.Remove(f.getAbsoluteFilePath()) if err != nil { var perr *os.PathError if errors.As(err, &perr) { @@ -155,7 +160,7 @@ func (f *File) Delete() (err error) { // Save saves a file to storage func (f *File) Save(fcontent io.Reader) (err error) { - err = afs.WriteReader(f.getFileName(), fcontent) + err = afs.WriteReader(f.getAbsoluteFilePath(), fcontent) if err != nil { return } diff --git a/pkg/routes/api/v1/task_attachment.go b/pkg/routes/api/v1/task_attachment.go index 6499cbb73..dcf1979f0 100644 --- a/pkg/routes/api/v1/task_attachment.go +++ b/pkg/routes/api/v1/task_attachment.go @@ -18,9 +18,10 @@ package v1 import ( "net/http" + "strings" "code.vikunja.io/api/pkg/db" - + "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/models" auth2 "code.vikunja.io/api/pkg/modules/auth" "code.vikunja.io/api/pkg/web/handler" @@ -116,6 +117,8 @@ func UploadTaskAttachment(c echo.Context) error { // @Produce octet-stream // @Param id path int true "Task ID" // @Param attachmentID path int true "Attachment ID" +// @Param preview query string false "If set to true, a preview image will be returned if the attachment is an image." +// @Param size query string false "The size of the preview image. Can be sm = 100px, md = 200px, lg = 400px or xl = 800px." // @Security JWTKeyAuth // @Success 200 {file} blob "The attachment file." // @Failure 403 {object} models.Message "No access to this task." @@ -154,7 +157,24 @@ func GetTaskAttachment(c echo.Context) error { return handler.HandleHTTPError(err) } - // Open an send the file to the client + // Reading the 'preview' query parameter + preview := c.QueryParam("preview") == "true" + previewSize := models.PreviewSize(c.QueryParam("size")) + if previewSize == "" { + previewSize = models.PreviewMedium + } + + // If the preview query parameter is set and the preview was already generated and cached, return the cached preview image + if preview && strings.HasPrefix(taskAttachment.File.Mime, "image") { + previewFileBytes := taskAttachment.GetPreviewFromCache(previewSize) + if previewFileBytes != nil { + log.Debugf("Cached attachment image preview found for task attachment %v", taskAttachment.ID) + + return c.Blob(http.StatusOK, "image/png", previewFileBytes) + } + } + + // Open and send the file to the client err = taskAttachment.File.LoadFileByID() if err != nil { _ = s.Rollback() @@ -166,6 +186,14 @@ func GetTaskAttachment(c echo.Context) error { return handler.HandleHTTPError(err) } + // If a preview is requested and the preview was not cached, we create the preview and cache it + if preview { + previewFileBytes := taskAttachment.GenerateAndSavePreviewToCache(previewSize) + if previewFileBytes != nil { + return c.Blob(http.StatusOK, "image/png", previewFileBytes) + } + } + http.ServeContent(c.Response(), c.Request(), taskAttachment.File.Name, taskAttachment.File.Created, taskAttachment.File.File) return nil }