2019-05-14 18:16:35 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-12-13 16:25:47 +01:00
|
|
|
"path"
|
2019-05-14 18:16:35 +02:00
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2020-01-05 14:18:40 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2020-01-13 11:07:09 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2019-05-14 18:16:35 +02:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2019-12-05 19:21:35 +01:00
|
|
|
// TODO: GET /api/v1/dl/file/:hash
|
|
|
|
// TODO: GET /api/v1/dl/photo/:uuid
|
|
|
|
// TODO: GET /api/v1/dl/album/:uuid
|
|
|
|
|
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")
|
|
|
|
|
2020-03-28 15:29:17 +01:00
|
|
|
q := query.New(conf.Db())
|
|
|
|
f, err := q.FileByHash(fileHash)
|
2019-05-14 18:16:35 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(404, gin.H{"error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-06 14:32:15 +01:00
|
|
|
fileName := path.Join(conf.OriginalsPath(), f.FileName)
|
2019-05-14 18:16:35 +02:00
|
|
|
|
2020-01-12 14:00:56 +01:00
|
|
|
if !fs.FileExists(fileName) {
|
2019-05-14 18:16:35 +02:00
|
|
|
log.Errorf("could not find original: %s", fileHash)
|
|
|
|
c.Data(404, "image/svg+xml", photoIconSvg)
|
|
|
|
|
|
|
|
// Set missing flag so that the file doesn't show up in search results anymore
|
2020-01-06 14:32:15 +01:00
|
|
|
f.FileMissing = true
|
|
|
|
conf.Db().Save(&f)
|
2019-05-14 18:16:35 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-01 12:00:45 +02:00
|
|
|
downloadFileName := f.ShareFileName()
|
2019-05-14 18:16:35 +02:00
|
|
|
|
|
|
|
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", downloadFileName))
|
|
|
|
|
|
|
|
c.File(fileName)
|
|
|
|
})
|
|
|
|
}
|