2019-12-16 11:04:49 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"path"
|
2020-05-25 19:10:44 +02:00
|
|
|
"path/filepath"
|
2019-12-16 11:04:49 +01:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-25 19:10:44 +02:00
|
|
|
// Find fallback if file is not a JPEG image.
|
|
|
|
if f.NoJPEG() {
|
2020-05-23 20:58:58 +02:00
|
|
|
f, err = query.FileByPhotoUID(f.PhotoUID)
|
2020-05-16 17:07:44 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2020-05-25 19:10:44 +02:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", fileIconSvg)
|
2020-05-16 17:07:44 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 19:10:44 +02:00
|
|
|
// Return SVG icon as placeholder if file has errors.
|
2020-01-29 17:41:51 +01:00
|
|
|
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-25 19:10:44 +02:00
|
|
|
log.Errorf("photo: file %s is missing", 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
|
|
|
|
2020-05-25 19:10:44 +02:00
|
|
|
// Set missing flag so that the file doesn't show up in search results anymore.
|
|
|
|
report("photo", f.Update("FileMissing", true))
|
2020-05-13 15:36:42 +02:00
|
|
|
|
2020-05-25 19:10:44 +02:00
|
|
|
if f.AllFilesMissing() {
|
2020-05-13 15:36:42 +02:00
|
|
|
log.Infof("photo: deleting photo, all files missing for %s", txt.Quote(f.FileName))
|
|
|
|
|
2020-05-25 19:10:44 +02:00
|
|
|
report("photo", f.RelatedPhoto().Delete(false))
|
2020-05-13 15:36:42 +02:00
|
|
|
}
|
2020-05-25 19:10:44 +02:00
|
|
|
|
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)
|
2020-05-25 19:10:44 +02:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", brokenIconSvg)
|
|
|
|
return
|
|
|
|
} else if thumbnail == "" {
|
|
|
|
log.Errorf("photo: thumbnail name for %s is empty - bug?", filepath.Base(fileName))
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", brokenIconSvg)
|
2020-05-05 15:42:54 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|