2021-01-08 12:20:41 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2021-09-05 11:05:33 +02:00
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/crop"
|
2021-01-08 12:20:41 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
|
|
|
"github.com/photoprism/photoprism/internal/thumb"
|
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
|
|
|
)
|
|
|
|
|
2021-09-02 23:47:37 +02:00
|
|
|
// GetThumb returns a thumbnail image matching the hash and type.
|
|
|
|
//
|
2021-01-08 12:20:41 +01:00
|
|
|
// GET /api/v1/t/:hash/:token/:type
|
|
|
|
//
|
|
|
|
// Parameters:
|
2021-01-08 12:52:26 +01:00
|
|
|
// hash: string sha1 file hash
|
|
|
|
// token: string url security token, see config
|
|
|
|
// type: string thumb type, see thumb.Types
|
2021-01-08 12:20:41 +01:00
|
|
|
func GetThumb(router *gin.RouterGroup) {
|
|
|
|
router.GET("/t/:hash/:token/:type", func(c *gin.Context) {
|
|
|
|
if InvalidPreviewToken(c) {
|
|
|
|
c.Data(http.StatusForbidden, "image/svg+xml", brokenIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
conf := service.Config()
|
|
|
|
fileHash := c.Param("hash")
|
|
|
|
typeName := c.Param("type")
|
2021-01-08 12:52:26 +01:00
|
|
|
download := c.Query("download") != ""
|
2021-01-08 12:20:41 +01:00
|
|
|
|
|
|
|
thumbType, ok := thumb.Types[typeName]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
log.Errorf("thumbs: invalid type %s", txt.Quote(typeName))
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", photoIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if thumbType.ExceedsSize() && !conf.ThumbUncached() {
|
|
|
|
typeName, thumbType = thumb.Find(conf.ThumbSize())
|
|
|
|
|
|
|
|
if typeName == "" {
|
|
|
|
log.Errorf("thumbs: invalid size %d", conf.ThumbSize())
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", photoIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-08 12:52:26 +01:00
|
|
|
cache := service.ThumbCache()
|
|
|
|
cacheKey := CacheKey("thumbs", fileHash, typeName)
|
2021-01-08 12:20:41 +01:00
|
|
|
|
2021-01-08 12:52:26 +01:00
|
|
|
if cacheData, ok := cache.Get(cacheKey); ok {
|
2021-02-06 16:30:30 +01:00
|
|
|
log.Debugf("api: cache hit for %s [%s]", cacheKey, time.Since(start))
|
2021-01-08 12:20:41 +01:00
|
|
|
|
2021-01-08 12:52:26 +01:00
|
|
|
cached := cacheData.(ThumbCache)
|
2021-01-08 12:20:41 +01:00
|
|
|
|
|
|
|
if !fs.FileExists(cached.FileName) {
|
|
|
|
log.Errorf("thumbs: %s not found", fileHash)
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", brokenIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
AddThumbCacheHeader(c)
|
|
|
|
|
|
|
|
if c.Query("download") != "" {
|
|
|
|
c.FileAttachment(cached.FileName, cached.ShareName)
|
|
|
|
} else {
|
|
|
|
c.File(cached.FileName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-08 12:52:26 +01:00
|
|
|
// Return existing thumbs straight away.
|
|
|
|
if !download {
|
2021-09-03 17:42:37 +02:00
|
|
|
if fileName, err := thumb.FileName(fileHash, conf.ThumbPath(), thumbType.Width, thumbType.Height, thumbType.Options...); err == nil && fs.FileExists(fileName) {
|
2021-01-08 12:52:26 +01:00
|
|
|
c.File(fileName)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query index for file infos.
|
2021-01-08 12:20:41 +01:00
|
|
|
f, err := query.FileByHash(fileHash)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", photoIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find fallback if file is not a JPEG image.
|
|
|
|
if f.NoJPEG() {
|
|
|
|
f, err = query.FileByPhotoUID(f.PhotoUID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", fileIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return SVG icon as placeholder if file has errors.
|
|
|
|
if f.FileError != "" {
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", brokenIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fileName := photoprism.FileName(f.FileRoot, f.FileName)
|
|
|
|
|
|
|
|
if !fs.FileExists(fileName) {
|
|
|
|
log.Errorf("thumbs: file %s is missing", txt.Quote(f.FileName))
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", brokenIconSvg)
|
|
|
|
|
|
|
|
// Set missing flag so that the file doesn't show up in search results anymore.
|
2021-01-08 12:52:26 +01:00
|
|
|
logError("thumbs", f.Update("FileMissing", true))
|
2021-01-08 12:20:41 +01:00
|
|
|
|
|
|
|
if f.AllFilesMissing() {
|
|
|
|
log.Infof("thumbs: deleting photo, all files missing for %s", txt.Quote(f.FileName))
|
|
|
|
|
2021-01-08 12:52:26 +01:00
|
|
|
logError("thumbs", f.RelatedPhoto().Delete(false))
|
2021-01-08 12:20:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use original file if thumb size exceeds limit, see https://github.com/photoprism/photoprism/issues/157
|
|
|
|
if thumbType.ExceedsSizeUncached() && c.Query("download") == "" {
|
|
|
|
log.Debugf("thumbs: using original, size exceeds limit (width %d, height %d)", thumbType.Width, thumbType.Height)
|
|
|
|
|
|
|
|
AddThumbCacheHeader(c)
|
|
|
|
c.File(fileName)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var thumbnail string
|
|
|
|
|
|
|
|
if conf.ThumbUncached() || thumbType.OnDemand() {
|
2021-02-21 22:53:25 +01:00
|
|
|
thumbnail, err = thumb.FromFile(fileName, f.FileHash, conf.ThumbPath(), thumbType.Width, thumbType.Height, f.FileOrientation, thumbType.Options...)
|
2021-01-08 12:20:41 +01:00
|
|
|
} else {
|
|
|
|
thumbnail, err = thumb.FromCache(fileName, f.FileHash, conf.ThumbPath(), thumbType.Width, thumbType.Height, thumbType.Options...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("thumbs: %s", err)
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", brokenIconSvg)
|
|
|
|
return
|
|
|
|
} else if thumbnail == "" {
|
|
|
|
log.Errorf("thumbs: %s has empty thumb name - bug?", filepath.Base(fileName))
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", brokenIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-27 21:30:10 +01:00
|
|
|
cache.SetDefault(cacheKey, ThumbCache{thumbnail, f.ShareBase(0)})
|
2021-01-08 12:52:26 +01:00
|
|
|
log.Debugf("cached %s [%s]", cacheKey, time.Since(start))
|
2021-01-08 12:20:41 +01:00
|
|
|
|
|
|
|
AddThumbCacheHeader(c)
|
|
|
|
|
2021-01-08 12:52:26 +01:00
|
|
|
if download {
|
2021-01-27 21:30:10 +01:00
|
|
|
c.FileAttachment(thumbnail, f.DownloadName(DownloadName(c), 0))
|
2021-01-08 12:20:41 +01:00
|
|
|
} else {
|
|
|
|
c.File(thumbnail)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-09-03 17:42:37 +02:00
|
|
|
|
|
|
|
// GetThumbCrop returns a cropped thumbnail image matching the hash and type.
|
|
|
|
//
|
|
|
|
// GET /api/v1/t/:hash/:token/:type/:area
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// hash: string sha1 file hash
|
|
|
|
// token: string url security token, see config
|
|
|
|
// type: string thumb type, see thumb.Types
|
|
|
|
// area: string image area identifier, e.g. 022004010015
|
|
|
|
func GetThumbCrop(router *gin.RouterGroup) {
|
|
|
|
router.GET("/t/:hash/:token/:type/:area", func(c *gin.Context) {
|
|
|
|
if InvalidPreviewToken(c) {
|
|
|
|
c.Data(http.StatusForbidden, "image/svg+xml", brokenIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
conf := service.Config()
|
|
|
|
fileHash := c.Param("hash")
|
|
|
|
typeName := c.Param("type")
|
|
|
|
cropArea := c.Param("area")
|
|
|
|
download := c.Query("download") != ""
|
|
|
|
|
|
|
|
thumbType, ok := thumb.Types[typeName]
|
|
|
|
|
|
|
|
if !ok || len(thumbType.Options) < 1 {
|
|
|
|
log.Errorf("thumbs: invalid type %s", txt.Quote(typeName))
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", photoIconSvg)
|
|
|
|
return
|
|
|
|
} else if thumbType.Options[0] != thumb.ResampleCrop {
|
|
|
|
log.Errorf("thumbs: invalid crop %s", txt.Quote(typeName))
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", photoIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fileName, err := crop.FromCache(fileHash, conf.ThumbPath(), thumbType.Width, thumbType.Height, cropArea)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("thumbs: %s", err)
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", brokenIconSvg)
|
|
|
|
return
|
|
|
|
} else if fileName == "" {
|
|
|
|
log.Errorf("thumbs: empty file name, potential bug")
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", brokenIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
AddThumbCacheHeader(c)
|
|
|
|
|
|
|
|
if download {
|
|
|
|
c.FileAttachment(fileName, typeName+fs.JpegExt)
|
|
|
|
} else {
|
|
|
|
c.File(fileName)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|