photoprism/internal/api/download.go

84 lines
1.9 KiB
Go
Raw Normal View History

2019-05-14 16:16:35 +00:00
package api
import (
"fmt"
"net/http"
2019-05-14 16:16:35 +00:00
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/service"
"github.com/photoprism/photoprism/internal/photoprism"
"github.com/photoprism/photoprism/internal/query"
"github.com/photoprism/photoprism/pkg/fs"
"github.com/photoprism/photoprism/pkg/txt"
2019-05-14 16:16:35 +00:00
"github.com/gin-gonic/gin"
)
// TODO: GET /api/v1/dl/file/:hash
// TODO: GET /api/v1/dl/photo/:uid
// TODO: GET /api/v1/dl/album/:uid
// GET /api/v1/dl/:hash
2019-05-14 16:16:35 +00:00
//
// Parameters:
// hash: string The file hash as returned by the search API
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
}
2019-05-14 16:16:35 +00:00
fileHash := c.Param("hash")
f, err := query.FileByHash(fileHash)
2019-05-14 16:16:35 +00:00
if err != nil {
c.AbortWithStatusJSON(404, gin.H{"error": err.Error()})
return
}
fileName := photoprism.FileName(f.FileRoot, f.FileName)
2019-05-14 16:16:35 +00:00
if !fs.FileExists(fileName) {
log.Errorf("download: file %s is missing", txt.Quote(f.FileName))
c.Data(404, "image/svg+xml", brokenIconSvg)
// Set missing flag so that the file doesn't show up in search results anymore.
logError("download", f.Update("FileMissing", true))
2019-05-14 16:16:35 +00:00
return
}
name := entity.DownloadNameFile
switch c.Query("name") {
case "file":
name = entity.DownloadNameFile
case "share":
name = entity.DownloadNameShare
case "original":
name = entity.DownloadNameOriginal
default:
name = service.Config().Settings().Download.Name
}
var downloadName string
switch name {
case entity.DownloadNameFile:
downloadName = f.Base()
case entity.DownloadNameOriginal:
downloadName = f.OriginalBase()
default:
downloadName = f.ShareBase()
}
2019-05-14 16:16:35 +00:00
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", downloadName))
2019-05-14 16:16:35 +00:00
c.File(fileName)
})
}