2019-12-16 11:04:49 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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-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"
|
2020-01-13 11:07:09 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2020-05-03 18:00:50 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
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 {
|
2020-05-03 18:00:50 +02:00
|
|
|
log.Errorf("photo: invalid thumb type %s", txt.Quote(typeName))
|
2020-05-05 15:42:54 +02:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", photoIconSvg)
|
2019-12-16 11:04:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
f, err := query.FileByHash(fileHash)
|
2019-12-16 11:04:49 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-05-05 15:42:54 +02:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", photoIconSvg)
|
2020-01-29 17:41:51 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.FileError != "" {
|
2020-05-05 15:42:54 +02:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", brokenIconSvg)
|
2019-12-16 11:04:49 +01:00
|
|
|
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-12 14:00:56 +01:00
|
|
|
if !fs.FileExists(fileName) {
|
2020-05-13 15:36:42 +02:00
|
|
|
log.Errorf("photo: could not find original for %s", txt.Quote(f.FileName))
|
2020-05-05 15:42:54 +02:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", photoIconSvg)
|
2019-12-16 11:04:49 +01:00
|
|
|
|
|
|
|
// 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
|
2020-05-13 15:36:42 +02:00
|
|
|
|
|
|
|
if err := f.Save(); err != nil {
|
|
|
|
log.Errorf("photo: %s", err)
|
|
|
|
} else if f.AllFilesMissing() {
|
|
|
|
log.Infof("photo: deleting photo, all files missing for %s", txt.Quote(f.FileName))
|
|
|
|
|
|
|
|
if err := f.Photo.Delete(false); err != nil {
|
|
|
|
log.Errorf("photo: %s", err)
|
|
|
|
}
|
|
|
|
}
|
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
|
2020-01-13 11:07:09 +01:00
|
|
|
if thumbType.ExceedsLimit() && c.Query("download") == "" {
|
2020-01-08 19:51:21 +01:00
|
|
|
log.Debugf("photo: using original, thumbnail size exceeds limit (width %d, height %d)", thumbType.Width, thumbType.Height)
|
|
|
|
|
|
|
|
c.File(fileName)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-05 15:42:54 +02:00
|
|
|
var thumbnail string
|
2019-12-16 11:04:49 +01:00
|
|
|
|
2020-05-05 17:04:13 +02:00
|
|
|
if conf.ThumbUncached() || thumbType.OnDemand() {
|
|
|
|
thumbnail, err = thumb.FromFile(fileName, f.FileHash, conf.ThumbPath(), thumbType.Width, thumbType.Height, thumbType.Options...)
|
2019-12-16 11:04:49 +01:00
|
|
|
} else {
|
2020-05-05 17:04:13 +02:00
|
|
|
thumbnail, err = thumb.FromCache(fileName, f.FileHash, conf.ThumbPath(), thumbType.Width, thumbType.Height, thumbType.Options...)
|
2020-05-05 15:42:54 +02:00
|
|
|
}
|
2020-02-02 00:31:09 +01:00
|
|
|
|
2020-05-05 18:26:44 +02:00
|
|
|
if err != nil {
|
2020-05-05 15:42:54 +02:00
|
|
|
log.Errorf("photo: %s", err)
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", photoIconSvg)
|
|
|
|
return
|
|
|
|
}
|
2020-01-29 17:41:51 +01:00
|
|
|
|
2020-05-05 15:42:54 +02:00
|
|
|
if c.Query("download") != "" {
|
2020-05-13 15:36:42 +02:00
|
|
|
c.FileAttachment(thumbnail, f.ShareFileName())
|
|
|
|
} else {
|
|
|
|
c.File(thumbnail)
|
2019-12-16 11:04:49 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|