2019-05-14 18:16:35 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-05-27 19:38:40 +02:00
|
|
|
"net/http"
|
2019-05-14 18:16:35 +02:00
|
|
|
|
2021-12-14 18:34:52 +01:00
|
|
|
"github.com/gin-gonic/gin"
|
2020-12-16 11:59:16 +01:00
|
|
|
|
2021-12-14 18:34:52 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2020-06-07 10:09:35 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
2020-05-08 15:41:01 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2021-12-14 18:34:52 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2020-01-13 11:07:09 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2019-05-14 18:16:35 +02:00
|
|
|
)
|
|
|
|
|
2019-12-05 19:21:35 +01:00
|
|
|
// TODO: GET /api/v1/dl/file/:hash
|
2020-05-23 20:58:58 +02:00
|
|
|
// TODO: GET /api/v1/dl/photo/:uid
|
|
|
|
// TODO: GET /api/v1/dl/album/:uid
|
2019-12-05 19:21:35 +01:00
|
|
|
|
2021-01-27 21:30:10 +01:00
|
|
|
// DownloadName returns the download file name type.
|
|
|
|
func DownloadName(c *gin.Context) entity.DownloadName {
|
|
|
|
switch c.Query("name") {
|
|
|
|
case "file":
|
|
|
|
return entity.DownloadNameFile
|
|
|
|
case "share":
|
|
|
|
return entity.DownloadNameShare
|
|
|
|
case "original":
|
|
|
|
return entity.DownloadNameOriginal
|
|
|
|
default:
|
|
|
|
return service.Config().Settings().Download.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-10 17:02:09 +02:00
|
|
|
// GetDownload returns the raw file data.
|
|
|
|
//
|
2020-05-27 19:38:40 +02:00
|
|
|
// GET /api/v1/dl/:hash
|
2019-05-14 18:16:35 +02:00
|
|
|
//
|
|
|
|
// Parameters:
|
2022-08-10 16:09:21 +02:00
|
|
|
//
|
|
|
|
// hash: string The file hash as returned by the files/photos endpoint
|
2020-06-25 14:54:04 +02:00
|
|
|
func GetDownload(router *gin.RouterGroup) {
|
2020-05-27 19:38:40 +02:00
|
|
|
router.GET("/dl/:hash", func(c *gin.Context) {
|
2020-06-25 14:54:04 +02:00
|
|
|
if InvalidDownloadToken(c) {
|
2020-05-27 19:38:40 +02:00
|
|
|
c.Data(http.StatusForbidden, "image/svg+xml", brokenIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
fileHash := clean.Token(c.Param("hash"))
|
2019-05-14 18:16:35 +02:00
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
f, err := query.FileByHash(fileHash)
|
2019-05-14 18:16:35 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(404, gin.H{"error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-07 10:09:35 +02:00
|
|
|
fileName := photoprism.FileName(f.FileRoot, f.FileName)
|
2019-05-14 18:16:35 +02:00
|
|
|
|
2020-01-12 14:00:56 +01:00
|
|
|
if !fs.FileExists(fileName) {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Errorf("download: file %s is missing", clean.Log(f.FileName))
|
2020-05-25 19:10:44 +02:00
|
|
|
c.Data(404, "image/svg+xml", brokenIconSvg)
|
|
|
|
|
|
|
|
// Set missing flag so that the file doesn't show up in search results anymore.
|
2020-05-28 16:26:22 +02:00
|
|
|
logError("download", f.Update("FileMissing", true))
|
2019-05-14 18:16:35 +02:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-27 21:30:10 +01:00
|
|
|
c.FileAttachment(fileName, f.DownloadName(DownloadName(c), 0))
|
2019-05-14 18:16:35 +02:00
|
|
|
})
|
|
|
|
}
|