2018-09-24 21:14:15 +02:00
|
|
|
package api
|
2018-09-24 11:27:46 +02:00
|
|
|
|
|
|
|
import (
|
2018-10-31 07:14:33 +01:00
|
|
|
"net/http"
|
|
|
|
|
2018-09-24 11:27:46 +02:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/gin-gonic/gin/binding"
|
2020-07-04 12:54:35 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
2019-12-05 19:21:35 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2021-09-18 15:32:39 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/search"
|
2018-09-24 11:27:46 +02:00
|
|
|
)
|
|
|
|
|
2021-09-18 15:32:39 +02:00
|
|
|
// SearchPhotos searches the pictures index and returns the result as JSON.
|
2021-08-29 16:16:49 +02:00
|
|
|
//
|
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
|
2019-12-09 08:04:41 +01:00
|
|
|
// camera: int UpdateCamera ID
|
2018-11-06 10:43:59 +01:00
|
|
|
// 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")
|
2020-05-14 19:03:12 +02:00
|
|
|
// favorite: bool Find favorites only
|
2021-09-18 15:32:39 +02:00
|
|
|
func SearchPhotos(router *gin.RouterGroup) {
|
2018-09-24 11:27:46 +02:00
|
|
|
router.GET("/photos", func(c *gin.Context) {
|
2020-06-25 14:54:04 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourcePhotos, acl.ActionSearch)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2020-01-22 13:43:07 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-26 13:59:10 +01:00
|
|
|
var f form.SearchPhotos
|
2019-05-16 08:41:16 +02:00
|
|
|
|
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 {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortBadRequest(c)
|
2019-01-15 14:00:42 +01:00
|
|
|
return
|
|
|
|
}
|
2018-09-24 11:27:46 +02:00
|
|
|
|
2020-06-26 12:16:13 +02:00
|
|
|
// Guests may only see public content in shared albums.
|
|
|
|
if s.Guest() {
|
|
|
|
if f.Album == "" || !s.HasShare(f.Album) {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2020-06-26 12:16:13 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-16 15:26:54 +01:00
|
|
|
f.UID = ""
|
2022-03-24 18:30:59 +01:00
|
|
|
f.Albums = ""
|
2020-06-26 12:16:13 +02:00
|
|
|
f.Public = true
|
|
|
|
f.Private = false
|
|
|
|
f.Hidden = false
|
|
|
|
f.Archived = false
|
|
|
|
f.Review = false
|
2020-06-25 14:54:04 +02:00
|
|
|
}
|
|
|
|
|
2021-09-18 15:32:39 +02:00
|
|
|
result, count, err := search.Photos(f)
|
2019-12-08 22:45:45 +01:00
|
|
|
|
2018-09-24 11:27:46 +02:00
|
|
|
if err != nil {
|
2021-09-17 15:52:25 +02:00
|
|
|
log.Warnf("search: %s", err)
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortBadRequest(c)
|
2019-01-15 14:00:42 +01:00
|
|
|
return
|
2018-09-24 11:27:46 +02:00
|
|
|
}
|
|
|
|
|
2021-01-08 09:02:30 +01:00
|
|
|
AddCountHeader(c, count)
|
|
|
|
AddLimitHeader(c, f.Count)
|
|
|
|
AddOffsetHeader(c, f.Offset)
|
|
|
|
AddTokenHeaders(c)
|
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
|
|
|
}
|