2018-09-24 21:14:15 +02:00
|
|
|
package api
|
2018-09-24 11:27:46 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-06-13 20:26:01 +02:00
|
|
|
"net/http"
|
2019-12-13 16:25:47 +01:00
|
|
|
"path"
|
2018-10-31 07:14:33 +01:00
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2019-12-11 07:37:39 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/repo"
|
2019-05-13 18:01:50 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/util"
|
2019-05-02 14:10:05 +02:00
|
|
|
|
2018-10-31 07:14:33 +01:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
2018-09-24 11:27:46 +02:00
|
|
|
)
|
|
|
|
|
2019-05-13 18:01:50 +02:00
|
|
|
// GET /api/v1/thumbnails/:hash/:type
|
2018-11-06 10:28:44 +01:00
|
|
|
//
|
|
|
|
// Parameters:
|
2018-11-06 10:43:59 +01:00
|
|
|
// hash: string The file hash as returned by the search API
|
2019-05-13 18:01:50 +02:00
|
|
|
// type: string Thumbnail type, see photoprism.ThumbnailTypes
|
2019-05-06 23:18:10 +02:00
|
|
|
func GetThumbnail(router *gin.RouterGroup, conf *config.Config) {
|
2019-05-13 18:01:50 +02:00
|
|
|
router.GET("/thumbnails/:hash/:type", func(c *gin.Context) {
|
2018-09-24 11:27:46 +02:00
|
|
|
fileHash := c.Param("hash")
|
2019-05-13 18:01:50 +02:00
|
|
|
typeName := c.Param("type")
|
|
|
|
|
|
|
|
thumbType, ok := photoprism.ThumbnailTypes[typeName]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
log.Errorf("invalid type: %s", typeName)
|
2019-06-13 20:26:01 +02:00
|
|
|
c.Data(http.StatusBadRequest, "image/svg+xml", photoIconSvg)
|
2019-01-15 14:00:42 +01:00
|
|
|
return
|
2018-09-24 11:27:46 +02:00
|
|
|
}
|
|
|
|
|
2019-12-11 07:37:39 +01:00
|
|
|
r := repo.New(conf.OriginalsPath(), conf.Db())
|
|
|
|
file, err := r.FindFileByHash(fileHash)
|
2019-04-26 04:25:00 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2019-06-13 20:26:01 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
2019-04-26 04:25:00 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-13 16:25:47 +01:00
|
|
|
fileName := path.Join(conf.OriginalsPath(), file.FileName)
|
2018-09-24 11:27:46 +02:00
|
|
|
|
2019-05-13 18:01:50 +02:00
|
|
|
if !util.Exists(fileName) {
|
|
|
|
log.Errorf("could not find original for thumbnail: %s", fileName)
|
2019-06-13 20:26:01 +02:00
|
|
|
c.Data(http.StatusNotFound, "image/svg+xml", photoIconSvg)
|
2018-09-27 12:10:19 +02:00
|
|
|
|
|
|
|
// Set missing flag so that the file doesn't show up in search results anymore
|
|
|
|
file.FileMissing = true
|
2019-05-06 23:18:10 +02:00
|
|
|
conf.Db().Save(&file)
|
2019-01-15 14:00:42 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-13 18:01:50 +02:00
|
|
|
if thumbnail, err := photoprism.ThumbnailFromFile(fileName, file.FileHash, conf.ThumbnailsPath(), thumbType.Width, thumbType.Height, thumbType.Options...); err == nil {
|
2019-05-16 04:03:55 +02:00
|
|
|
if c.Query("download") != "" {
|
|
|
|
downloadFileName := file.DownloadFileName()
|
|
|
|
|
|
|
|
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", downloadFileName))
|
|
|
|
}
|
|
|
|
|
2019-05-13 18:01:50 +02:00
|
|
|
c.File(thumbnail)
|
|
|
|
} else {
|
|
|
|
log.Errorf("could not create thumbnail: %s", err)
|
2019-06-13 20:26:01 +02:00
|
|
|
c.Data(http.StatusBadRequest, "image/svg+xml", photoIconSvg)
|
2018-09-24 11:27:46 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-06-09 04:37:02 +02:00
|
|
|
|
|
|
|
// GET /api/v1/labels/:slug/thumbnail/:type
|
|
|
|
//
|
|
|
|
// Example: /api/v1/labels/cheetah/thumbnail/tile_500
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// slug: string Label slug name
|
|
|
|
// type: string Thumbnail type, see photoprism.ThumbnailTypes
|
|
|
|
func LabelThumbnail(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.GET("/labels/:slug/thumbnail/:type", func(c *gin.Context) {
|
|
|
|
typeName := c.Param("type")
|
|
|
|
|
|
|
|
thumbType, ok := photoprism.ThumbnailTypes[typeName]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
log.Errorf("invalid type: %s", typeName)
|
2019-06-13 20:26:01 +02:00
|
|
|
c.Data(http.StatusBadRequest, "image/svg+xml", photoIconSvg)
|
2019-06-09 04:37:02 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-11 07:37:39 +01:00
|
|
|
r := repo.New(conf.OriginalsPath(), conf.Db())
|
2019-06-09 04:37:02 +02:00
|
|
|
|
|
|
|
// log.Infof("Searching for label slug: %s", c.Param("slug"))
|
|
|
|
|
2019-12-11 07:37:39 +01:00
|
|
|
file, err := r.FindLabelThumbBySlug(c.Param("slug"))
|
2019-06-09 04:37:02 +02:00
|
|
|
|
|
|
|
// log.Infof("Label thumb file: %#v", file)
|
|
|
|
|
|
|
|
if err != nil {
|
2019-06-13 20:26:01 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": util.UcFirst(err.Error())})
|
2019-06-09 04:37:02 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-13 16:25:47 +01:00
|
|
|
fileName := path.Join(conf.OriginalsPath(), file.FileName)
|
2019-06-09 04:37:02 +02:00
|
|
|
|
|
|
|
if !util.Exists(fileName) {
|
|
|
|
log.Errorf("could not find original for thumbnail: %s", fileName)
|
2019-06-13 20:26:01 +02:00
|
|
|
c.Data(http.StatusNotFound, "image/svg+xml", photoIconSvg)
|
2019-06-09 04:37:02 +02:00
|
|
|
|
|
|
|
// Set missing flag so that the file doesn't show up in search results anymore
|
|
|
|
file.FileMissing = true
|
|
|
|
conf.Db().Save(&file)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if thumbnail, err := photoprism.ThumbnailFromFile(fileName, file.FileHash, conf.ThumbnailsPath(), thumbType.Width, thumbType.Height, thumbType.Options...); err == nil {
|
|
|
|
if c.Query("download") != "" {
|
|
|
|
downloadFileName := file.DownloadFileName()
|
|
|
|
|
|
|
|
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", downloadFileName))
|
|
|
|
}
|
|
|
|
|
|
|
|
c.File(thumbnail)
|
|
|
|
} else {
|
|
|
|
log.Errorf("could not create thumbnail: %s", err)
|
2019-06-13 20:26:01 +02:00
|
|
|
c.Data(http.StatusBadRequest, "image/svg+xml", photoIconSvg)
|
2019-06-09 04:37:02 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-06-17 21:45:06 +02:00
|
|
|
|
|
|
|
/* ********** Albums ******** */
|
|
|
|
|
|
|
|
func AlbumThumbnail(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.GET("/albums/:uuid/thumbnail/:type", func(c *gin.Context) {
|
|
|
|
typeName := c.Param("type")
|
2019-06-18 06:37:10 +02:00
|
|
|
uuid := c.Param("uuid")
|
2019-06-17 21:45:06 +02:00
|
|
|
|
|
|
|
thumbType, ok := photoprism.ThumbnailTypes[typeName]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
log.Errorf("invalid type: %s", typeName)
|
|
|
|
c.Data(http.StatusBadRequest, "image/svg+xml", photoIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-11 07:37:39 +01:00
|
|
|
r := repo.New(conf.OriginalsPath(), conf.Db())
|
2019-06-17 21:45:06 +02:00
|
|
|
|
2019-12-11 07:37:39 +01:00
|
|
|
file, err := r.FindAlbumThumbByUUID(uuid)
|
2019-06-17 21:45:06 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2019-06-18 06:37:10 +02:00
|
|
|
log.Debugf("album has no photos yet, using generic thumb image: %s", uuid)
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", albumIconSvg)
|
2019-06-17 21:45:06 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-13 16:25:47 +01:00
|
|
|
fileName := path.Join(conf.OriginalsPath(), file.FileName)
|
2019-06-17 21:45:06 +02:00
|
|
|
|
|
|
|
if !util.Exists(fileName) {
|
|
|
|
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
|
|
|
|
file.FileMissing = true
|
|
|
|
conf.Db().Save(&file)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if thumbnail, err := photoprism.ThumbnailFromFile(fileName, file.FileHash, conf.ThumbnailsPath(), thumbType.Width, thumbType.Height, thumbType.Options...); err == nil {
|
|
|
|
if c.Query("download") != "" {
|
|
|
|
downloadFileName := file.DownloadFileName()
|
|
|
|
|
|
|
|
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", downloadFileName))
|
|
|
|
}
|
|
|
|
|
|
|
|
c.File(thumbnail)
|
|
|
|
} else {
|
|
|
|
log.Errorf("could not create thumbnail: %s", err)
|
|
|
|
c.Data(http.StatusBadRequest, "image/svg+xml", photoIconSvg)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|