2019-12-16 11:04:49 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"path"
|
|
|
|
|
2020-01-06 14:32:15 +01:00
|
|
|
"github.com/gin-gonic/gin"
|
2019-12-16 11:04:49 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2020-01-06 14:32:15 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/file"
|
2020-01-05 14:18:40 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2020-01-06 14:32:15 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/thumb"
|
2019-12-16 11:04:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// GET /api/v1/thumbnails/:hash/:type
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// hash: string The file hash as returned by the search API
|
|
|
|
// type: string Thumbnail type, see photoprism.ThumbnailTypes
|
|
|
|
func GetThumbnail(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.GET("/thumbnails/:hash/:type", func(c *gin.Context) {
|
|
|
|
fileHash := c.Param("hash")
|
|
|
|
typeName := c.Param("type")
|
|
|
|
|
2020-01-06 14:32:15 +01:00
|
|
|
thumbType, ok := thumb.Types[typeName]
|
2019-12-16 11:04:49 +01:00
|
|
|
|
|
|
|
if !ok {
|
|
|
|
log.Errorf("invalid type: %s", typeName)
|
|
|
|
c.Data(http.StatusBadRequest, "image/svg+xml", photoIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-05 14:18:40 +01:00
|
|
|
q := query.New(conf.OriginalsPath(), conf.Db())
|
2020-01-06 14:32:15 +01:00
|
|
|
f, err := q.FindFileByHash(fileHash)
|
2019-12-16 11:04:49 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-06 14:32:15 +01:00
|
|
|
fileName := path.Join(conf.OriginalsPath(), f.FileName)
|
2019-12-16 11:04:49 +01:00
|
|
|
|
2020-01-06 14:32:15 +01:00
|
|
|
if !file.Exists(fileName) {
|
2019-12-16 11:04:49 +01:00
|
|
|
log.Errorf("could not find original for thumbnail: %s", fileName)
|
|
|
|
c.Data(http.StatusNotFound, "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-12-16 11:04:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-08 19:51:21 +01:00
|
|
|
// Use original file if thumb size exceeds limit, see https://github.com/photoprism/photoprism/issues/157
|
|
|
|
if thumbType.Height > thumb.MaxHeight || thumbType.Width > thumb.MaxWidth {
|
|
|
|
log.Debugf("photo: using original, thumbnail size exceeds limit (width %d, height %d)", thumbType.Width, thumbType.Height)
|
|
|
|
|
2019-12-16 11:04:49 +01:00
|
|
|
if c.Query("download") != "" {
|
2020-01-08 19:51:21 +01:00
|
|
|
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", f.DownloadFileName()))
|
|
|
|
}
|
2019-12-16 11:04:49 +01:00
|
|
|
|
2020-01-08 19:51:21 +01:00
|
|
|
c.File(fileName)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if thumbnail, err := thumb.FromFile(fileName, f.FileHash, conf.ThumbnailsPath(), thumbType.Width, thumbType.Height, thumbType.Options...); err == nil {
|
|
|
|
if c.Query("download") != "" {
|
|
|
|
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", f.DownloadFileName()))
|
2019-12-16 11:04:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
c.File(thumbnail)
|
|
|
|
} else {
|
|
|
|
log.Errorf("could not create thumbnail: %s", err)
|
|
|
|
c.Data(http.StatusBadRequest, "image/svg+xml", photoIconSvg)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|