error chacks and minor api refactoring (#92)
* error chacks and minor api refactoring * consistant naming
This commit is contained in:
parent
74dc8be598
commit
4edfc4fa4c
|
@ -28,64 +28,63 @@ import (
|
|||
func GetPhotos(router *gin.RouterGroup, conf photoprism.Config) {
|
||||
router.GET("/photos", func(c *gin.Context) {
|
||||
var form forms.PhotoSearchForm
|
||||
|
||||
search := photoprism.NewSearch(conf.OriginalsPath(), conf.Db())
|
||||
|
||||
c.MustBindWith(&form, binding.Form)
|
||||
err := c.MustBindWith(&form, binding.Form)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
result, err := search.Photos(form)
|
||||
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(400, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.Header("x-result-count", strconv.Itoa(form.Count))
|
||||
c.Header("x-result-offset", strconv.Itoa(form.Offset))
|
||||
|
||||
c.JSON(http.StatusOK, result)
|
||||
})
|
||||
}
|
||||
|
||||
// POST /api/v1/photos/:photoId/like
|
||||
// POST /api/v1/photos/:id/like
|
||||
//
|
||||
// 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) {
|
||||
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())
|
||||
|
||||
photoId, err := strconv.ParseUint(c.Param("photoId"), 10, 64)
|
||||
|
||||
if err == nil {
|
||||
photo := search.FindPhotoByID(photoId)
|
||||
photo.PhotoFavorite = true
|
||||
conf.Db().Save(&photo)
|
||||
c.JSON(http.StatusOK, http.Response{})
|
||||
} else {
|
||||
photoID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
log.Printf("could not find image for id: %s", err.Error())
|
||||
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
|
||||
//
|
||||
// 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) {
|
||||
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())
|
||||
|
||||
photoId, err := strconv.ParseUint(c.Param("photoId"), 10, 64)
|
||||
|
||||
if err == nil {
|
||||
photo := search.FindPhotoByID(photoId)
|
||||
photo.PhotoFavorite = false
|
||||
conf.Db().Save(&photo)
|
||||
c.JSON(http.StatusOK, http.Response{})
|
||||
} else {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
log.Printf("could not find image for id: %s", err.Error())
|
||||
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")
|
||||
thumbnailType := c.Param("type")
|
||||
size, err := strconv.Atoi(c.Param("size"))
|
||||
|
||||
if err != nil {
|
||||
log.Printf("invalid size: %s", c.Param("size"))
|
||||
c.Data(400, "image/svg+xml", photoIconSvg)
|
||||
return
|
||||
}
|
||||
|
||||
search := photoprism.NewSearch(conf.OriginalsPath(), conf.Db())
|
||||
|
||||
file := search.FindFileByHash(fileHash)
|
||||
|
||||
fileName := fmt.Sprintf("%s/%s", conf.OriginalsPath(), file.FileName)
|
||||
|
||||
if mediaFile, err := photoprism.NewMediaFile(fileName); err == nil {
|
||||
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)
|
||||
}
|
||||
} else {
|
||||
mediaFile, err := photoprism.NewMediaFile(fileName)
|
||||
if err != nil {
|
||||
log.Printf("could not find image for thumbnail: %s", err.Error())
|
||||
c.Data(404, "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
|
||||
}
|
||||
|
||||
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 (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/photoprism/photoprism/internal/context"
|
||||
"github.com/photoprism/photoprism/internal/photoprism"
|
||||
|
@ -20,7 +19,7 @@ func convertAction(ctx *cli.Context) error {
|
|||
conf := context.NewConfig(ctx)
|
||||
|
||||
if err := conf.CreateDirectories(); err != nil {
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("Converting RAW images in %s to JPEG...\n", conf.OriginalsPath())
|
||||
|
|
|
@ -2,7 +2,6 @@ package commands
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/araddon/dateparse"
|
||||
"github.com/photoprism/photoprism/internal/context"
|
||||
|
@ -42,7 +41,7 @@ func exportAction(ctx *cli.Context) error {
|
|||
conf := context.NewConfig(ctx)
|
||||
|
||||
if err := conf.CreateDirectories(); err != nil {
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
|
||||
before := ctx.String("before")
|
||||
|
|
|
@ -2,7 +2,6 @@ package commands
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/photoprism/photoprism/internal/context"
|
||||
"github.com/photoprism/photoprism/internal/photoprism"
|
||||
|
@ -20,7 +19,7 @@ func importAction(ctx *cli.Context) error {
|
|||
conf := context.NewConfig(ctx)
|
||||
|
||||
if err := conf.CreateDirectories(); err != nil {
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
|
||||
conf.MigrateDb()
|
||||
|
|
|
@ -2,7 +2,6 @@ package commands
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/photoprism/photoprism/internal/context"
|
||||
"github.com/photoprism/photoprism/internal/photoprism"
|
||||
|
@ -20,7 +19,7 @@ func indexAction(ctx *cli.Context) error {
|
|||
conf := context.NewConfig(ctx)
|
||||
|
||||
if err := conf.CreateDirectories(); err != nil {
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
|
||||
conf.MigrateDb()
|
||||
|
|
|
@ -2,7 +2,6 @@ package commands
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/photoprism/photoprism/internal/context"
|
||||
"github.com/photoprism/photoprism/internal/photoprism"
|
||||
|
@ -34,7 +33,7 @@ func thumbnailsAction(ctx *cli.Context) error {
|
|||
conf := context.NewConfig(ctx)
|
||||
|
||||
if err := conf.CreateDirectories(); err != nil {
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
if err != nil || !mediaFile.IsJpeg() {
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -374,7 +374,7 @@ func (m *MediaFile) GetRelatedFiles() (result MediaFiles, mainFile *MediaFile, e
|
|||
mainFile = resultFile
|
||||
} else if resultFile.IsRaw() {
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
@ -117,7 +117,7 @@ func (s *Search) Photos(form forms.PhotoSearchForm) ([]PhotoSearchResult, error)
|
|||
Group("photos.id, files.id")
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
|
@ -201,7 +201,6 @@ func (s *Search) FindFileByID(id string) (file models.File) {
|
|||
// FindFileByHash finds a file with a given hash string.
|
||||
func (s *Search) FindFileByHash(fileHash string) (file models.File) {
|
||||
s.db.Where("file_hash = ?", fileHash).First(&file)
|
||||
|
||||
return file
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue