mirror of
https://github.com/photoprism/photoprism.git
synced 2025-12-12 00:34:13 +01:00
Backend: Refactor API source structure
Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/photoprism/photoprism/internal/entity"
|
||||
"github.com/photoprism/photoprism/internal/event"
|
||||
"github.com/photoprism/photoprism/internal/form"
|
||||
"github.com/photoprism/photoprism/internal/photoprism"
|
||||
"github.com/photoprism/photoprism/internal/repo"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -392,3 +393,59 @@ func DownloadAlbum(router *gin.RouterGroup, conf *config.Config) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
// POST /api/v1/albums/:uuid/thumbnail/:type
|
||||
//
|
||||
// Parameters:
|
||||
// uuid: string Album UUID
|
||||
// type: string Thumbnail type, see photoprism.ThumbnailTypes
|
||||
func AlbumThumbnail(router *gin.RouterGroup, conf *config.Config) {
|
||||
router.GET("/albums/:uuid/thumbnail/:type", func(c *gin.Context) {
|
||||
typeName := c.Param("type")
|
||||
uuid := c.Param("uuid")
|
||||
|
||||
thumbType, ok := photoprism.ThumbnailTypes[typeName]
|
||||
|
||||
if !ok {
|
||||
log.Errorf("invalid type: %s", typeName)
|
||||
c.Data(http.StatusBadRequest, "image/svg+xml", photoIconSvg)
|
||||
return
|
||||
}
|
||||
|
||||
r := repo.New(conf.OriginalsPath(), conf.Db())
|
||||
|
||||
file, err := r.FindAlbumThumbByUUID(uuid)
|
||||
|
||||
if err != nil {
|
||||
log.Debugf("album has no photos yet, using generic thumb image: %s", uuid)
|
||||
c.Data(http.StatusOK, "image/svg+xml", albumIconSvg)
|
||||
return
|
||||
}
|
||||
|
||||
fileName := path.Join(conf.OriginalsPath(), file.FileName)
|
||||
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -57,3 +57,21 @@ func TestDislikeAlbum(t *testing.T) {
|
||||
assert.Equal(t, http.StatusNotFound, result.Code)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
func TestAlbumThumbnail(t *testing.T) {
|
||||
t.Run("invalid type", func(t *testing.T) {
|
||||
app, router, ctx := NewApiTest()
|
||||
AlbumThumbnail(router, ctx)
|
||||
result := PerformRequest(app, "GET", "/api/v1/albums/1/thumbnail/xxx")
|
||||
|
||||
assert.Equal(t, http.StatusBadRequest, result.Code)
|
||||
})
|
||||
t.Run("album has no photo (because is not existing)", func(t *testing.T) {
|
||||
app, router, ctx := NewApiTest()
|
||||
AlbumThumbnail(router, ctx)
|
||||
result := PerformRequest(app, "GET", "/api/v1/albums/987-986435/thumbnail/tile_500")
|
||||
|
||||
assert.Equal(t, http.StatusOK, result.Code)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
"github.com/photoprism/photoprism/internal/config"
|
||||
"github.com/photoprism/photoprism/internal/form"
|
||||
"github.com/photoprism/photoprism/internal/photoprism"
|
||||
"github.com/photoprism/photoprism/internal/repo"
|
||||
"github.com/photoprism/photoprism/internal/util"
|
||||
)
|
||||
@@ -91,3 +94,62 @@ func DislikeLabel(router *gin.RouterGroup, conf *config.Config) {
|
||||
c.JSON(http.StatusOK, http.Response{})
|
||||
})
|
||||
}
|
||||
|
||||
// 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)
|
||||
c.Data(http.StatusBadRequest, "image/svg+xml", photoIconSvg)
|
||||
return
|
||||
}
|
||||
|
||||
r := repo.New(conf.OriginalsPath(), conf.Db())
|
||||
|
||||
// log.Infof("Searching for label slug: %s", c.Param("slug"))
|
||||
|
||||
file, err := r.FindLabelThumbBySlug(c.Param("slug"))
|
||||
|
||||
// log.Infof("Label thumb file: %#v", file)
|
||||
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": util.UcFirst(err.Error())})
|
||||
return
|
||||
}
|
||||
|
||||
fileName := path.Join(conf.OriginalsPath(), file.FileName)
|
||||
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -58,3 +58,20 @@ func TestDislikeLabel(t *testing.T) {
|
||||
assert.Equal(t, http.StatusNotFound, result.Code)
|
||||
})
|
||||
}
|
||||
|
||||
func TestLabelThumbnail(t *testing.T) {
|
||||
t.Run("invalid type", func(t *testing.T) {
|
||||
app, router, ctx := NewApiTest()
|
||||
LabelThumbnail(router, ctx)
|
||||
result := PerformRequest(app, "GET", "/api/v1/labels/dog/thumbnail/xxx")
|
||||
|
||||
assert.Equal(t, http.StatusBadRequest, result.Code)
|
||||
})
|
||||
t.Run("invalid label", func(t *testing.T) {
|
||||
app, router, ctx := NewApiTest()
|
||||
LabelThumbnail(router, ctx)
|
||||
result := PerformRequest(app, "GET", "/api/v1/labels/xxx/thumbnail/tile_500")
|
||||
|
||||
assert.Equal(t, http.StatusNotFound, result.Code)
|
||||
})
|
||||
}
|
||||
|
||||
67
internal/api/photo_thumbnail.go
Normal file
67
internal/api/photo_thumbnail.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path"
|
||||
|
||||
"github.com/photoprism/photoprism/internal/config"
|
||||
"github.com/photoprism/photoprism/internal/repo"
|
||||
"github.com/photoprism/photoprism/internal/util"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/photoprism/photoprism/internal/photoprism"
|
||||
)
|
||||
|
||||
// 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")
|
||||
|
||||
thumbType, ok := photoprism.ThumbnailTypes[typeName]
|
||||
|
||||
if !ok {
|
||||
log.Errorf("invalid type: %s", typeName)
|
||||
c.Data(http.StatusBadRequest, "image/svg+xml", photoIconSvg)
|
||||
return
|
||||
}
|
||||
|
||||
r := repo.New(conf.OriginalsPath(), conf.Db())
|
||||
file, err := r.FindFileByHash(fileHash)
|
||||
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
fileName := path.Join(conf.OriginalsPath(), file.FileName)
|
||||
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
25
internal/api/photo_thumbnail_test.go
Normal file
25
internal/api/photo_thumbnail_test.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGetThumbnail(t *testing.T) {
|
||||
t.Run("invalid type", func(t *testing.T) {
|
||||
app, router, ctx := NewApiTest()
|
||||
GetThumbnail(router, ctx)
|
||||
result := PerformRequest(app, "GET", "/api/v1/thumbnails/1/xxx")
|
||||
|
||||
assert.Equal(t, http.StatusBadRequest, result.Code)
|
||||
})
|
||||
t.Run("invalid hash", func(t *testing.T) {
|
||||
app, router, ctx := NewApiTest()
|
||||
GetThumbnail(router, ctx)
|
||||
result := PerformRequest(app, "GET", "/api/v1/thumbnails/1/tile_500")
|
||||
|
||||
assert.Equal(t, http.StatusNotFound, result.Code)
|
||||
})
|
||||
}
|
||||
@@ -1,178 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path"
|
||||
|
||||
"github.com/photoprism/photoprism/internal/config"
|
||||
"github.com/photoprism/photoprism/internal/repo"
|
||||
"github.com/photoprism/photoprism/internal/util"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/photoprism/photoprism/internal/photoprism"
|
||||
)
|
||||
|
||||
// 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")
|
||||
|
||||
thumbType, ok := photoprism.ThumbnailTypes[typeName]
|
||||
|
||||
if !ok {
|
||||
log.Errorf("invalid type: %s", typeName)
|
||||
c.Data(http.StatusBadRequest, "image/svg+xml", photoIconSvg)
|
||||
return
|
||||
}
|
||||
|
||||
r := repo.New(conf.OriginalsPath(), conf.Db())
|
||||
file, err := r.FindFileByHash(fileHash)
|
||||
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
fileName := path.Join(conf.OriginalsPath(), file.FileName)
|
||||
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 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)
|
||||
c.Data(http.StatusBadRequest, "image/svg+xml", photoIconSvg)
|
||||
return
|
||||
}
|
||||
|
||||
r := repo.New(conf.OriginalsPath(), conf.Db())
|
||||
|
||||
// log.Infof("Searching for label slug: %s", c.Param("slug"))
|
||||
|
||||
file, err := r.FindLabelThumbBySlug(c.Param("slug"))
|
||||
|
||||
// log.Infof("Label thumb file: %#v", file)
|
||||
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": util.UcFirst(err.Error())})
|
||||
return
|
||||
}
|
||||
|
||||
fileName := path.Join(conf.OriginalsPath(), file.FileName)
|
||||
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/* ********** Albums ******** */
|
||||
|
||||
func AlbumThumbnail(router *gin.RouterGroup, conf *config.Config) {
|
||||
router.GET("/albums/:uuid/thumbnail/:type", func(c *gin.Context) {
|
||||
typeName := c.Param("type")
|
||||
uuid := c.Param("uuid")
|
||||
|
||||
thumbType, ok := photoprism.ThumbnailTypes[typeName]
|
||||
|
||||
if !ok {
|
||||
log.Errorf("invalid type: %s", typeName)
|
||||
c.Data(http.StatusBadRequest, "image/svg+xml", photoIconSvg)
|
||||
return
|
||||
}
|
||||
|
||||
r := repo.New(conf.OriginalsPath(), conf.Db())
|
||||
|
||||
file, err := r.FindAlbumThumbByUUID(uuid)
|
||||
|
||||
if err != nil {
|
||||
log.Debugf("album has no photos yet, using generic thumb image: %s", uuid)
|
||||
c.Data(http.StatusOK, "image/svg+xml", albumIconSvg)
|
||||
return
|
||||
}
|
||||
|
||||
fileName := path.Join(conf.OriginalsPath(), file.FileName)
|
||||
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGetThumbnail(t *testing.T) {
|
||||
t.Run("invalid type", func(t *testing.T) {
|
||||
app, router, ctx := NewApiTest()
|
||||
GetThumbnail(router, ctx)
|
||||
result := PerformRequest(app, "GET", "/api/v1/thumbnails/1/xxx")
|
||||
|
||||
assert.Equal(t, http.StatusBadRequest, result.Code)
|
||||
})
|
||||
t.Run("invalid hash", func(t *testing.T) {
|
||||
app, router, ctx := NewApiTest()
|
||||
GetThumbnail(router, ctx)
|
||||
result := PerformRequest(app, "GET", "/api/v1/thumbnails/1/tile_500")
|
||||
|
||||
assert.Equal(t, http.StatusNotFound, result.Code)
|
||||
})
|
||||
}
|
||||
|
||||
func TestLabelThumbnail(t *testing.T) {
|
||||
t.Run("invalid type", func(t *testing.T) {
|
||||
app, router, ctx := NewApiTest()
|
||||
LabelThumbnail(router, ctx)
|
||||
result := PerformRequest(app, "GET", "/api/v1/labels/dog/thumbnail/xxx")
|
||||
|
||||
assert.Equal(t, http.StatusBadRequest, result.Code)
|
||||
})
|
||||
t.Run("invalid label", func(t *testing.T) {
|
||||
app, router, ctx := NewApiTest()
|
||||
LabelThumbnail(router, ctx)
|
||||
result := PerformRequest(app, "GET", "/api/v1/labels/xxx/thumbnail/tile_500")
|
||||
|
||||
assert.Equal(t, http.StatusNotFound, result.Code)
|
||||
})
|
||||
}
|
||||
|
||||
func TestAlbumThumbnail(t *testing.T) {
|
||||
t.Run("invalid type", func(t *testing.T) {
|
||||
app, router, ctx := NewApiTest()
|
||||
AlbumThumbnail(router, ctx)
|
||||
result := PerformRequest(app, "GET", "/api/v1/albums/1/thumbnail/xxx")
|
||||
|
||||
assert.Equal(t, http.StatusBadRequest, result.Code)
|
||||
})
|
||||
t.Run("album has no photo (because is not existing)", func(t *testing.T) {
|
||||
app, router, ctx := NewApiTest()
|
||||
AlbumThumbnail(router, ctx)
|
||||
result := PerformRequest(app, "GET", "/api/v1/albums/987-986435/thumbnail/tile_500")
|
||||
|
||||
assert.Equal(t, http.StatusOK, result.Code)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user