mirror of
https://github.com/photoprism/photoprism.git
synced 2025-12-12 00:34:13 +01:00
error chacks and minor api refactoring (#92)
* error chacks and minor api refactoring * consistant naming
This commit is contained in:
committed by
Michael Mayer
parent
74dc8be598
commit
4edfc4fa4c
@@ -28,64 +28,63 @@ import (
|
|||||||
func GetPhotos(router *gin.RouterGroup, conf photoprism.Config) {
|
func GetPhotos(router *gin.RouterGroup, conf photoprism.Config) {
|
||||||
router.GET("/photos", func(c *gin.Context) {
|
router.GET("/photos", func(c *gin.Context) {
|
||||||
var form forms.PhotoSearchForm
|
var form forms.PhotoSearchForm
|
||||||
|
|
||||||
search := photoprism.NewSearch(conf.OriginalsPath(), conf.Db())
|
search := photoprism.NewSearch(conf.OriginalsPath(), conf.Db())
|
||||||
|
err := c.MustBindWith(&form, binding.Form)
|
||||||
c.MustBindWith(&form, binding.Form)
|
if err != nil {
|
||||||
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
result, err := search.Photos(form)
|
result, err := search.Photos(form)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithStatusJSON(400, gin.H{"error": err.Error()})
|
c.AbortWithStatusJSON(400, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Header("x-result-count", strconv.Itoa(form.Count))
|
c.Header("x-result-count", strconv.Itoa(form.Count))
|
||||||
c.Header("x-result-offset", strconv.Itoa(form.Offset))
|
c.Header("x-result-offset", strconv.Itoa(form.Offset))
|
||||||
|
|
||||||
c.JSON(http.StatusOK, result)
|
c.JSON(http.StatusOK, result)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// POST /api/v1/photos/:photoId/like
|
// POST /api/v1/photos/:id/like
|
||||||
//
|
//
|
||||||
// Parameters:
|
// Parameters:
|
||||||
// photoId: int Photo ID as returned by the API
|
// id: int Photo ID as returned by the API
|
||||||
func LikePhoto(router *gin.RouterGroup, conf photoprism.Config) {
|
func LikePhoto(router *gin.RouterGroup, conf photoprism.Config) {
|
||||||
router.POST("/photos/:photoId/like", func(c *gin.Context) {
|
router.POST("/photos/:id/like", func(c *gin.Context) {
|
||||||
search := photoprism.NewSearch(conf.OriginalsPath(), conf.Db())
|
search := photoprism.NewSearch(conf.OriginalsPath(), conf.Db())
|
||||||
|
photoID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||||
photoId, err := strconv.ParseUint(c.Param("photoId"), 10, 64)
|
if err != nil {
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
photo := search.FindPhotoByID(photoId)
|
|
||||||
photo.PhotoFavorite = true
|
|
||||||
conf.Db().Save(&photo)
|
|
||||||
c.JSON(http.StatusOK, http.Response{})
|
|
||||||
} else {
|
|
||||||
log.Printf("could not find image for id: %s", err.Error())
|
log.Printf("could not find image for id: %s", err.Error())
|
||||||
c.Data(http.StatusNotFound, "image", []byte(""))
|
c.Data(http.StatusNotFound, "image", []byte(""))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
photo := search.FindPhotoByID(photoID)
|
||||||
|
photo.PhotoFavorite = true
|
||||||
|
conf.Db().Save(&photo)
|
||||||
|
c.JSON(http.StatusOK, http.Response{})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// DELETE /api/v1/photos/:photoId/like
|
// DELETE /api/v1/photos/:photoId/like
|
||||||
//
|
//
|
||||||
// Parameters:
|
// Parameters:
|
||||||
// photoId: int Photo ID as returned by the API
|
// id: int Photo ID as returned by the API
|
||||||
func DislikePhoto(router *gin.RouterGroup, conf photoprism.Config) {
|
func DislikePhoto(router *gin.RouterGroup, conf photoprism.Config) {
|
||||||
router.DELETE("/photos/:photoId/like", func(c *gin.Context) {
|
router.DELETE("/photos/:id/like", func(c *gin.Context) {
|
||||||
search := photoprism.NewSearch(conf.OriginalsPath(), conf.Db())
|
search := photoprism.NewSearch(conf.OriginalsPath(), conf.Db())
|
||||||
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||||
photoId, err := strconv.ParseUint(c.Param("photoId"), 10, 64)
|
if err != nil {
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
photo := search.FindPhotoByID(photoId)
|
|
||||||
photo.PhotoFavorite = false
|
|
||||||
conf.Db().Save(&photo)
|
|
||||||
c.JSON(http.StatusOK, http.Response{})
|
|
||||||
} else {
|
|
||||||
log.Printf("could not find image for id: %s", err.Error())
|
log.Printf("could not find image for id: %s", err.Error())
|
||||||
c.Data(http.StatusNotFound, "image", []byte(""))
|
c.Data(http.StatusNotFound, "image", []byte(""))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
photo := search.FindPhotoByID(id)
|
||||||
|
photo.PhotoFavorite = false
|
||||||
|
conf.Db().Save(&photo)
|
||||||
|
c.JSON(http.StatusOK, http.Response{})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,45 +26,45 @@ func GetThumbnail(router *gin.RouterGroup, conf photoprism.Config) {
|
|||||||
fileHash := c.Param("hash")
|
fileHash := c.Param("hash")
|
||||||
thumbnailType := c.Param("type")
|
thumbnailType := c.Param("type")
|
||||||
size, err := strconv.Atoi(c.Param("size"))
|
size, err := strconv.Atoi(c.Param("size"))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("invalid size: %s", c.Param("size"))
|
log.Printf("invalid size: %s", c.Param("size"))
|
||||||
c.Data(400, "image/svg+xml", photoIconSvg)
|
c.Data(400, "image/svg+xml", photoIconSvg)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
search := photoprism.NewSearch(conf.OriginalsPath(), conf.Db())
|
search := photoprism.NewSearch(conf.OriginalsPath(), conf.Db())
|
||||||
|
|
||||||
file := search.FindFileByHash(fileHash)
|
file := search.FindFileByHash(fileHash)
|
||||||
|
|
||||||
fileName := fmt.Sprintf("%s/%s", conf.OriginalsPath(), file.FileName)
|
fileName := fmt.Sprintf("%s/%s", conf.OriginalsPath(), file.FileName)
|
||||||
|
|
||||||
if mediaFile, err := photoprism.NewMediaFile(fileName); err == nil {
|
mediaFile, err := photoprism.NewMediaFile(fileName)
|
||||||
switch thumbnailType {
|
if err != nil {
|
||||||
case "fit":
|
|
||||||
if thumbnail, err := mediaFile.GetThumbnail(conf.ThumbnailsPath(), size); err == nil {
|
|
||||||
c.File(thumbnail.GetFilename())
|
|
||||||
} else {
|
|
||||||
log.Printf("could not create thumbnail: %s", err.Error())
|
|
||||||
c.Data(400, "image/svg+xml", photoIconSvg)
|
|
||||||
}
|
|
||||||
case "square":
|
|
||||||
if thumbnail, err := mediaFile.GetSquareThumbnail(conf.ThumbnailsPath(), size); err == nil {
|
|
||||||
c.File(thumbnail.GetFilename())
|
|
||||||
} else {
|
|
||||||
log.Printf("could not create square thumbnail: %s", err.Error())
|
|
||||||
c.Data(400, "image/svg+xml", photoIconSvg)
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
log.Printf("unknown thumbnail type: %s", thumbnailType)
|
|
||||||
c.Data(400, "image/svg+xml", photoIconSvg)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
log.Printf("could not find image for thumbnail: %s", err.Error())
|
log.Printf("could not find image for thumbnail: %s", err.Error())
|
||||||
c.Data(404, "image/svg+xml", photoIconSvg)
|
c.Data(404, "image/svg+xml", photoIconSvg)
|
||||||
|
|
||||||
// Set missing flag so that the file doesn't show up in search results anymore
|
// Set missing flag so that the file doesn't show up in search results anymore
|
||||||
file.FileMissing = true
|
file.FileMissing = true
|
||||||
conf.Db().Save(&file)
|
conf.Db().Save(&file)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch thumbnailType {
|
||||||
|
case "fit":
|
||||||
|
if thumbnail, err := mediaFile.GetThumbnail(conf.ThumbnailsPath(), size); err == nil {
|
||||||
|
c.File(thumbnail.GetFilename())
|
||||||
|
} else {
|
||||||
|
log.Printf("could not create thumbnail: %s", err.Error())
|
||||||
|
c.Data(400, "image/svg+xml", photoIconSvg)
|
||||||
|
}
|
||||||
|
case "square":
|
||||||
|
if thumbnail, err := mediaFile.GetSquareThumbnail(conf.ThumbnailsPath(), size); err == nil {
|
||||||
|
c.File(thumbnail.GetFilename())
|
||||||
|
} else {
|
||||||
|
log.Printf("could not create square thumbnail: %s", err.Error())
|
||||||
|
c.Data(400, "image/svg+xml", photoIconSvg)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
log.Printf("unknown thumbnail type: %s", thumbnailType)
|
||||||
|
c.Data(400, "image/svg+xml", photoIconSvg)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package commands
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/photoprism/photoprism/internal/context"
|
"github.com/photoprism/photoprism/internal/context"
|
||||||
"github.com/photoprism/photoprism/internal/photoprism"
|
"github.com/photoprism/photoprism/internal/photoprism"
|
||||||
@@ -20,7 +19,7 @@ func convertAction(ctx *cli.Context) error {
|
|||||||
conf := context.NewConfig(ctx)
|
conf := context.NewConfig(ctx)
|
||||||
|
|
||||||
if err := conf.CreateDirectories(); err != nil {
|
if err := conf.CreateDirectories(); err != nil {
|
||||||
log.Fatal(err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Converting RAW images in %s to JPEG...\n", conf.OriginalsPath())
|
fmt.Printf("Converting RAW images in %s to JPEG...\n", conf.OriginalsPath())
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package commands
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/araddon/dateparse"
|
"github.com/araddon/dateparse"
|
||||||
"github.com/photoprism/photoprism/internal/context"
|
"github.com/photoprism/photoprism/internal/context"
|
||||||
@@ -42,7 +41,7 @@ func exportAction(ctx *cli.Context) error {
|
|||||||
conf := context.NewConfig(ctx)
|
conf := context.NewConfig(ctx)
|
||||||
|
|
||||||
if err := conf.CreateDirectories(); err != nil {
|
if err := conf.CreateDirectories(); err != nil {
|
||||||
log.Fatal(err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
before := ctx.String("before")
|
before := ctx.String("before")
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package commands
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/photoprism/photoprism/internal/context"
|
"github.com/photoprism/photoprism/internal/context"
|
||||||
"github.com/photoprism/photoprism/internal/photoprism"
|
"github.com/photoprism/photoprism/internal/photoprism"
|
||||||
@@ -20,7 +19,7 @@ func importAction(ctx *cli.Context) error {
|
|||||||
conf := context.NewConfig(ctx)
|
conf := context.NewConfig(ctx)
|
||||||
|
|
||||||
if err := conf.CreateDirectories(); err != nil {
|
if err := conf.CreateDirectories(); err != nil {
|
||||||
log.Fatal(err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
conf.MigrateDb()
|
conf.MigrateDb()
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package commands
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/photoprism/photoprism/internal/context"
|
"github.com/photoprism/photoprism/internal/context"
|
||||||
"github.com/photoprism/photoprism/internal/photoprism"
|
"github.com/photoprism/photoprism/internal/photoprism"
|
||||||
@@ -20,7 +19,7 @@ func indexAction(ctx *cli.Context) error {
|
|||||||
conf := context.NewConfig(ctx)
|
conf := context.NewConfig(ctx)
|
||||||
|
|
||||||
if err := conf.CreateDirectories(); err != nil {
|
if err := conf.CreateDirectories(); err != nil {
|
||||||
log.Fatal(err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
conf.MigrateDb()
|
conf.MigrateDb()
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package commands
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/photoprism/photoprism/internal/context"
|
"github.com/photoprism/photoprism/internal/context"
|
||||||
"github.com/photoprism/photoprism/internal/photoprism"
|
"github.com/photoprism/photoprism/internal/photoprism"
|
||||||
@@ -34,7 +33,7 @@ func thumbnailsAction(ctx *cli.Context) error {
|
|||||||
conf := context.NewConfig(ctx)
|
conf := context.NewConfig(ctx)
|
||||||
|
|
||||||
if err := conf.CreateDirectories(); err != nil {
|
if err := conf.CreateDirectories(); err != nil {
|
||||||
log.Fatal(err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Creating thumbnails in %s...\n", conf.ThumbnailsPath())
|
fmt.Printf("Creating thumbnails in %s...\n", conf.ThumbnailsPath())
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ func FindOriginalsByDate(originalsPath string, after time.Time, before time.Time
|
|||||||
}
|
}
|
||||||
|
|
||||||
mediaFile, err := NewMediaFile(filename)
|
mediaFile, err := NewMediaFile(filename)
|
||||||
|
|
||||||
if err != nil || !mediaFile.IsJpeg() {
|
if err != nil || !mediaFile.IsJpeg() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -374,7 +374,7 @@ func (m *MediaFile) GetRelatedFiles() (result MediaFiles, mainFile *MediaFile, e
|
|||||||
mainFile = resultFile
|
mainFile = resultFile
|
||||||
} else if resultFile.IsRaw() {
|
} else if resultFile.IsRaw() {
|
||||||
mainFile = resultFile
|
mainFile = resultFile
|
||||||
} else if resultFile.IsJpeg() && resultFile.IsJpeg() && len(mainFile.GetFilename()) > len(resultFile.GetFilename()) {
|
} else if resultFile.IsJpeg() && len(mainFile.GetFilename()) > len(resultFile.GetFilename()) {
|
||||||
mainFile = resultFile
|
mainFile = resultFile
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -117,7 +117,7 @@ func (s *Search) Photos(form forms.PhotoSearchForm) ([]PhotoSearchResult, error)
|
|||||||
Group("photos.id, files.id")
|
Group("photos.id, files.id")
|
||||||
|
|
||||||
if form.Query != "" {
|
if form.Query != "" {
|
||||||
likeString := "%"+strings.ToLower(form.Query)+"%"
|
likeString := "%" + strings.ToLower(form.Query) + "%"
|
||||||
q = q.Where("tags.tag_label LIKE ? OR LOWER(photo_title) LIKE ?", likeString, likeString)
|
q = q.Where("tags.tag_label LIKE ? OR LOWER(photo_title) LIKE ?", likeString, likeString)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,7 +201,6 @@ func (s *Search) FindFileByID(id string) (file models.File) {
|
|||||||
// FindFileByHash finds a file with a given hash string.
|
// FindFileByHash finds a file with a given hash string.
|
||||||
func (s *Search) FindFileByHash(fileHash string) (file models.File) {
|
func (s *Search) FindFileByHash(fileHash string) (file models.File) {
|
||||||
s.db.Where("file_hash = ?", fileHash).First(&file)
|
s.db.Where("file_hash = ?", fileHash).First(&file)
|
||||||
|
|
||||||
return file
|
return file
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user