2018-09-24 21:14:15 +02:00
|
|
|
package api
|
2018-09-24 11:27:46 +02:00
|
|
|
|
|
|
|
import (
|
2019-12-05 21:06:53 +01:00
|
|
|
"fmt"
|
2018-10-31 07:14:33 +01:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2019-05-16 08:41:16 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/util"
|
2019-05-02 14:10:05 +02:00
|
|
|
|
2018-09-24 11:27:46 +02:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/gin-gonic/gin/binding"
|
2019-12-05 19:21:35 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2018-09-24 11:27:46 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
|
|
|
)
|
|
|
|
|
2018-11-06 10:43:59 +01:00
|
|
|
// GET /api/v1/photos
|
2018-11-06 10:28:44 +01:00
|
|
|
//
|
|
|
|
// Query:
|
2018-11-06 10:43:59 +01:00
|
|
|
// q: string Query string
|
2019-06-05 18:25:20 +02:00
|
|
|
// label: string Label
|
2018-11-06 10:43:59 +01:00
|
|
|
// cat: string Category
|
|
|
|
// country: string Country code
|
|
|
|
// camera: int Camera 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
|
2019-05-06 23:18:10 +02:00
|
|
|
func GetPhotos(router *gin.RouterGroup, conf *config.Config) {
|
2018-09-24 11:27:46 +02:00
|
|
|
router.GET("/photos", func(c *gin.Context) {
|
2019-12-05 19:21:35 +01:00
|
|
|
var f form.PhotoSearch
|
2019-05-16 08:41:16 +02:00
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
search := photoprism.NewSearch(conf.OriginalsPath(), conf.Db())
|
2019-12-05 19:21:35 +01:00
|
|
|
err := c.MustBindWith(&f, binding.Form)
|
2019-05-16 08:41:16 +02:00
|
|
|
|
2019-01-15 14:00:42 +01:00
|
|
|
if err != nil {
|
2019-05-16 08:41:16 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": util.UcFirst(err.Error())})
|
2019-01-15 14:00:42 +01:00
|
|
|
return
|
|
|
|
}
|
2018-09-24 11:27:46 +02:00
|
|
|
|
2019-12-05 19:21:35 +01:00
|
|
|
result, err := search.Photos(f)
|
2019-12-08 22:45:45 +01:00
|
|
|
|
2018-09-24 11:27:46 +02:00
|
|
|
if err != nil {
|
2019-05-16 08:41:16 +02:00
|
|
|
c.AbortWithStatusJSON(400, gin.H{"error": util.UcFirst(err.Error())})
|
2019-01-15 14:00:42 +01:00
|
|
|
return
|
2018-09-24 11:27:46 +02:00
|
|
|
}
|
|
|
|
|
2019-12-05 19:21:35 +01:00
|
|
|
c.Header("X-Result-Count", strconv.Itoa(f.Count))
|
|
|
|
c.Header("X-Result-Offset", strconv.Itoa(f.Offset))
|
2019-05-16 08:41:16 +02:00
|
|
|
|
2018-09-24 11:27:46 +02:00
|
|
|
c.JSON(http.StatusOK, result)
|
|
|
|
})
|
2018-09-27 08:59:53 +02:00
|
|
|
}
|
2018-10-31 03:33:18 +01:00
|
|
|
|
2019-12-05 21:06:53 +01:00
|
|
|
// 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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-04 16:47:47 +01:00
|
|
|
// POST /api/v1/photos/:uuid/like
|
2018-11-06 10:28:44 +01:00
|
|
|
//
|
|
|
|
// Parameters:
|
2019-12-04 16:47:47 +01:00
|
|
|
// uuid: string PhotoUUID as returned by the API
|
2019-05-06 23:18:10 +02:00
|
|
|
func LikePhoto(router *gin.RouterGroup, conf *config.Config) {
|
2019-12-04 16:47:47 +01:00
|
|
|
router.POST("/photos/:uuid/like", func(c *gin.Context) {
|
2019-11-12 05:49:10 +01:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
search := photoprism.NewSearch(conf.OriginalsPath(), conf.Db())
|
2019-12-04 16:47:47 +01:00
|
|
|
m, err := search.FindPhotoByUUID(c.Param("uuid"))
|
2019-04-26 04:25:00 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2019-05-16 08:41:16 +02:00
|
|
|
c.AbortWithStatusJSON(404, gin.H{"error": util.UcFirst(err.Error())})
|
2019-04-26 04:25:00 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-04 16:47:47 +01:00
|
|
|
m.PhotoFavorite = true
|
|
|
|
conf.Db().Save(&m)
|
2019-05-16 08:41:16 +02:00
|
|
|
|
2019-12-04 16:47:47 +01:00
|
|
|
c.JSON(http.StatusOK, gin.H{"photo": m})
|
2018-10-31 03:33:18 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-04 16:47:47 +01:00
|
|
|
// DELETE /api/v1/photos/:uuid/like
|
2018-11-06 10:28:44 +01:00
|
|
|
//
|
|
|
|
// Parameters:
|
2019-12-04 16:47:47 +01:00
|
|
|
// uuid: string PhotoUUID as returned by the API
|
2019-05-06 23:18:10 +02:00
|
|
|
func DislikePhoto(router *gin.RouterGroup, conf *config.Config) {
|
2019-12-04 16:47:47 +01:00
|
|
|
router.DELETE("/photos/:uuid/like", func(c *gin.Context) {
|
2019-11-12 05:49:10 +01:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
search := photoprism.NewSearch(conf.OriginalsPath(), conf.Db())
|
2019-12-04 16:47:47 +01:00
|
|
|
m, err := search.FindPhotoByUUID(c.Param("uuid"))
|
2019-04-26 04:25:00 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2019-05-16 08:41:16 +02:00
|
|
|
c.AbortWithStatusJSON(404, gin.H{"error": util.UcFirst(err.Error())})
|
2019-04-26 04:25:00 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-04 16:47:47 +01:00
|
|
|
m.PhotoFavorite = false
|
|
|
|
conf.Db().Save(&m)
|
2019-05-16 08:41:16 +02:00
|
|
|
|
2019-12-04 16:47:47 +01:00
|
|
|
c.JSON(http.StatusOK, gin.H{"photo": m})
|
2018-10-31 03:33:18 +01:00
|
|
|
})
|
|
|
|
}
|