error chacks and minor api refactoring (#92)

* error chacks and minor api refactoring

* consistant naming
This commit is contained in:
Vedhavyas Singareddi 2019-01-15 14:00:42 +01:00 committed by Michael Mayer
parent 74dc8be598
commit 4edfc4fa4c
10 changed files with 58 additions and 66 deletions

View file

@ -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{})
})
}

View file

@ -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)
}
})
}

View file

@ -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())

View file

@ -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")

View file

@ -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()

View file

@ -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()

View file

@ -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())

View file

@ -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
}

View file

@ -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
}

View file

@ -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
}