photoprism/internal/api/download.go

53 lines
1.2 KiB
Go
Raw Normal View History

2019-05-14 18:16:35 +02:00
package api
import (
"fmt"
"path"
2019-05-14 18:16:35 +02:00
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/query"
"github.com/photoprism/photoprism/pkg/fs"
"github.com/photoprism/photoprism/pkg/txt"
2019-05-14 18:16:35 +02: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
2019-05-14 18:16:35 +02:00
// GET /api/v1/download/:hash
//
// Parameters:
// hash: string The file hash as returned by the search API
func GetDownload(router *gin.RouterGroup, conf *config.Config) {
router.GET("/download/:hash", func(c *gin.Context) {
fileHash := c.Param("hash")
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
}
fileName := path.Join(conf.OriginalsPath(), f.FileName)
2019-05-14 18:16:35 +02: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.
report("download", f.Update("FileMissing", true))
2019-05-14 18:16:35 +02:00
return
}
downloadFileName := f.ShareFileName()
2019-05-14 18:16:35 +02:00
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", downloadFileName))
c.File(fileName)
})
}