27ca260942
Signed-off-by: Michael Mayer <michael@liquidbytes.net>
150 lines
3.9 KiB
Go
150 lines
3.9 KiB
Go
package api
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
|
"github.com/photoprism/photoprism/internal/event"
|
|
"github.com/photoprism/photoprism/internal/util"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gin-gonic/gin/binding"
|
|
"github.com/photoprism/photoprism/internal/form"
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
|
)
|
|
|
|
// GET /api/v1/photos
|
|
//
|
|
// Query:
|
|
// q: string Query string
|
|
// label: string Label
|
|
// cat: string Category
|
|
// country: string Country code
|
|
// camera: int UpdateCamera ID
|
|
// order: string Sort order
|
|
// count: int Max result count (required)
|
|
// offset: int Result offset
|
|
// before: date Find photos taken before (format: "2006-01-02")
|
|
// after: date Find photos taken after (format: "2006-01-02")
|
|
// favorites: bool Find favorites only
|
|
func GetPhotos(router *gin.RouterGroup, conf *config.Config) {
|
|
router.GET("/photos", func(c *gin.Context) {
|
|
var f form.PhotoSearch
|
|
|
|
search := photoprism.NewSearch(conf.OriginalsPath(), conf.Db())
|
|
err := c.MustBindWith(&f, binding.Form)
|
|
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": util.UcFirst(err.Error())})
|
|
return
|
|
}
|
|
|
|
result, err := search.Photos(f)
|
|
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(400, gin.H{"error": util.UcFirst(err.Error())})
|
|
return
|
|
}
|
|
|
|
c.Header("X-Result-Count", strconv.Itoa(f.Count))
|
|
c.Header("X-Result-Offset", strconv.Itoa(f.Offset))
|
|
|
|
c.JSON(http.StatusOK, result)
|
|
})
|
|
}
|
|
|
|
// GET /api/v1/photos/:uuid/download
|
|
//
|
|
// Parameters:
|
|
// uuid: string PhotoUUID as returned by the API
|
|
func GetPhotoDownload(router *gin.RouterGroup, conf *config.Config) {
|
|
router.GET("/photos/:uuid/download", func(c *gin.Context) {
|
|
search := photoprism.NewSearch(conf.OriginalsPath(), conf.Db())
|
|
file, err := search.FindFileByPhotoUUID(c.Param("uuid"))
|
|
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(404, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
fileName := fmt.Sprintf("%s/%s", conf.OriginalsPath(), file.FileName)
|
|
|
|
if !util.Exists(fileName) {
|
|
log.Errorf("could not find original: %s", c.Param("uuid"))
|
|
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
|
|
}
|
|
|
|
downloadFileName := file.DownloadFileName()
|
|
|
|
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", downloadFileName))
|
|
|
|
c.File(fileName)
|
|
})
|
|
}
|
|
|
|
// POST /api/v1/photos/:uuid/like
|
|
//
|
|
// Parameters:
|
|
// uuid: string PhotoUUID as returned by the API
|
|
func LikePhoto(router *gin.RouterGroup, conf *config.Config) {
|
|
router.POST("/photos/:uuid/like", func(c *gin.Context) {
|
|
if Unauthorized(c, conf) {
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
return
|
|
}
|
|
|
|
search := photoprism.NewSearch(conf.OriginalsPath(), conf.Db())
|
|
m, err := search.FindPhotoByUUID(c.Param("uuid"))
|
|
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(404, gin.H{"error": util.UcFirst(err.Error())})
|
|
return
|
|
}
|
|
|
|
m.PhotoFavorite = true
|
|
conf.Db().Save(&m)
|
|
|
|
event.Publish("count.favorites", event.Data{
|
|
"count": 1,
|
|
})
|
|
|
|
c.JSON(http.StatusOK, gin.H{"photo": m})
|
|
})
|
|
}
|
|
|
|
// DELETE /api/v1/photos/:uuid/like
|
|
//
|
|
// Parameters:
|
|
// uuid: string PhotoUUID as returned by the API
|
|
func DislikePhoto(router *gin.RouterGroup, conf *config.Config) {
|
|
router.DELETE("/photos/:uuid/like", func(c *gin.Context) {
|
|
if Unauthorized(c, conf) {
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
return
|
|
}
|
|
|
|
search := photoprism.NewSearch(conf.OriginalsPath(), conf.Db())
|
|
m, err := search.FindPhotoByUUID(c.Param("uuid"))
|
|
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(404, gin.H{"error": util.UcFirst(err.Error())})
|
|
return
|
|
}
|
|
|
|
m.PhotoFavorite = false
|
|
conf.Db().Save(&m)
|
|
|
|
event.Publish("count.favorites", event.Data{
|
|
"count": -1,
|
|
})
|
|
|
|
c.JSON(http.StatusOK, gin.H{"photo": m})
|
|
})
|
|
}
|