API: Add download package to allow temporary file downloads #127 #1090

This is necessary to provide external services with temporary access to
specific media files without giving them permanent access, a regular
download, or an access token.

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer
2025-04-07 23:56:35 +02:00
parent 0f76186663
commit 42897bde35
9 changed files with 165 additions and 19 deletions

View File

@@ -42,7 +42,7 @@ func GetThumb(router *gin.RouterGroup) {
start := time.Now()
conf := get.Config()
download := c.Query("download") != ""
attachment := c.Query("download") != ""
fileHash, cropArea := crop.ParseThumb(clean.Token(c.Param("thumb")))
// Is cropped thumbnail?
@@ -72,7 +72,7 @@ func GetThumb(router *gin.RouterGroup) {
// Add HTTP cache header.
AddImmutableCacheHeader(c)
if download {
if attachment {
c.FileAttachment(fileName, cropName.Jpeg())
} else {
c.File(fileName)
@@ -118,7 +118,7 @@ func GetThumb(router *gin.RouterGroup) {
// Add HTTP cache header.
AddImmutableCacheHeader(c)
if download {
if attachment {
c.FileAttachment(cached.FileName, cached.ShareName)
} else {
c.File(cached.FileName)
@@ -128,7 +128,7 @@ func GetThumb(router *gin.RouterGroup) {
}
// Return existing thumbs straight away.
if !download {
if !attachment {
if fileName, err := size.ResolvedName(fileHash, conf.ThumbCachePath()); err == nil {
// Add HTTP cache header.
AddImmutableCacheHeader(c)
@@ -188,7 +188,7 @@ func GetThumb(router *gin.RouterGroup) {
}
// Use original file if thumb size exceeds limit, see https://github.com/photoprism/photoprism/issues/157
if size.ExceedsLimit() && !download {
if size.ExceedsLimit() && !attachment {
log.Debugf("%s: using original, size exceeds limit (width %d, height %d)", logPrefix, size.Width, size.Height)
// Add HTTP cache header.
@@ -228,7 +228,7 @@ func GetThumb(router *gin.RouterGroup) {
AddImmutableCacheHeader(c)
// Return requested content.
if download {
if attachment {
c.FileAttachment(thumbName, f.DownloadName(DownloadName(c), 0))
} else {
c.File(thumbName)