chore(files): use absolute file path to retrieve and save files
(cherry picked from commit c2b116de70df335a6a354cf2b3595632c8b49ff1)
This commit is contained in:
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user