mirror of
https://github.com/photoprism/photoprism.git
synced 2025-10-06 17:27:20 +08:00
77 lines
2.0 KiB
Go
77 lines
2.0 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/photoprism/photoprism/internal/config/customize"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/photoprism/photoprism/internal/entity/query"
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
|
"github.com/photoprism/photoprism/internal/photoprism/get"
|
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
|
)
|
|
|
|
// TODO: GET /api/v1/dl/file/:hash
|
|
// TODO: GET /api/v1/dl/photo/:uid
|
|
// TODO: GET /api/v1/dl/album/:uid
|
|
|
|
// DownloadName returns the download file name type.
|
|
func DownloadName(c *gin.Context) customize.DownloadName {
|
|
switch c.Query("name") {
|
|
case "file":
|
|
return customize.DownloadNameFile
|
|
case "share":
|
|
return customize.DownloadNameShare
|
|
case "original":
|
|
return customize.DownloadNameOriginal
|
|
default:
|
|
return get.Config().Settings().Download.Name
|
|
}
|
|
}
|
|
|
|
// GetDownload returns the raw file data.
|
|
//
|
|
// @Summary returns the raw file data
|
|
// @Id GetDownload
|
|
// @Tags Images, Files
|
|
// @Produce application/octet-stream
|
|
// @Failure 403,404 {file} image/svg+xml
|
|
// @Success 200 {file} application/octet-stream
|
|
// @Param hash path string true "File Hash"
|
|
// @Router /api/v1/dl/{hash} [get]
|
|
func GetDownload(router *gin.RouterGroup) {
|
|
router.GET("/dl/:hash", func(c *gin.Context) {
|
|
if InvalidDownloadToken(c) {
|
|
c.Data(http.StatusForbidden, "image/svg+xml", brokenIconSvg)
|
|
return
|
|
}
|
|
|
|
fileHash := clean.Token(c.Param("hash"))
|
|
|
|
f, err := query.FileByHash(fileHash)
|
|
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(404, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
fileName := photoprism.FileName(f.FileRoot, f.FileName)
|
|
|
|
if !fs.FileExists(fileName) {
|
|
log.Errorf("download: file %s is missing", clean.Log(f.FileName))
|
|
c.Data(404, "image/svg+xml", brokenIconSvg)
|
|
|
|
// Set missing flag so that the file doesn't show up in search results anymore.
|
|
logErr("download", f.Update("FileMissing", true))
|
|
|
|
return
|
|
}
|
|
|
|
c.FileAttachment(fileName, f.DownloadName(DownloadName(c), 0))
|
|
})
|
|
}
|