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"
|
2022-03-30 20:36:25 +02:00
|
|
|
|
2020-07-04 12:54:35 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
2022-09-28 09:01:17 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2019-12-05 19:21:35 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2022-03-30 20:36:25 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
2021-09-18 15:32:39 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/search"
|
2022-03-30 20:36:25 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
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.
|
2022-09-30 00:42:19 +02:00
|
|
|
// See form.SearchPhotos for supported search params and data types.
|
2021-08-29 16:16:49 +02:00
|
|
|
//
|
2018-11-06 10:43:59 +01:00
|
|
|
// GET /api/v1/photos
|
2021-09-18 15:32:39 +02:00
|
|
|
func SearchPhotos(router *gin.RouterGroup) {
|
2022-03-30 20:36:25 +02:00
|
|
|
// searchPhotos checking authorization and parses the search request.
|
2022-09-28 09:01:17 +02:00
|
|
|
searchForm := func(c *gin.Context) (f form.SearchPhotos, s *entity.Session, err error) {
|
|
|
|
s = AuthAny(c, acl.ResourcePhotos, acl.Permissions{acl.ActionSearch, acl.ActionView, acl.AccessShared})
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Abort if permission was not granted.
|
|
|
|
if s.Abort(c) {
|
|
|
|
return f, s, i18n.Error(i18n.ErrForbidden)
|
2020-01-22 13:43:07 +01:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Abort if request params are invalid.
|
|
|
|
if err = c.MustBindWith(&f, binding.Form); err != nil {
|
2022-09-30 00:42:19 +02:00
|
|
|
event.AuditWarn([]string{ClientIP(c), "session %s", string(acl.ResourcePhotos), "form invalid", "%s"}, s.RefID, err)
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortBadRequest(c)
|
2022-09-28 09:01:17 +02:00
|
|
|
return f, s, err
|
2019-01-15 14:00:42 +01:00
|
|
|
}
|
2018-09-24 11:27:46 +02:00
|
|
|
|
2022-10-04 02:57:15 +02:00
|
|
|
settings := service.Config().Settings()
|
|
|
|
|
2022-09-30 00:42:19 +02:00
|
|
|
// Ignore private flag if feature is disabled.
|
2022-10-04 02:57:15 +02:00
|
|
|
if !settings.Features.Private {
|
2022-08-01 15:57:19 +02:00
|
|
|
f.Public = false
|
2020-06-25 14:54:04 +02:00
|
|
|
}
|
|
|
|
|
2022-10-04 02:57:15 +02:00
|
|
|
// Ignore private flag if feature is disabled.
|
|
|
|
if f.Scope == "" &&
|
|
|
|
settings.Features.Review &&
|
|
|
|
acl.Resources.Deny(acl.ResourcePhotos, s.User().AclRole(), acl.ActionManage) {
|
|
|
|
f.Quality = 3
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
return f, s, nil
|
2022-03-30 20:36:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// defaultHandler a standard JSON result with all fields.
|
|
|
|
defaultHandler := func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
f, s, err := searchForm(c)
|
2022-03-30 20:36:25 +02:00
|
|
|
|
|
|
|
// Abort if authorization or form are invalid.
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-30 19:15:10 +02:00
|
|
|
// Find matching pictures.
|
2022-09-30 00:42:19 +02:00
|
|
|
result, count, err := search.UserPhotos(f, s)
|
2019-12-08 22:45:45 +01:00
|
|
|
|
2022-09-30 19:15:10 +02:00
|
|
|
// Ok?
|
2018-09-24 11:27:46 +02:00
|
|
|
if err != nil {
|
2022-09-30 00:42:19 +02:00
|
|
|
event.AuditWarn([]string{ClientIP(c), "session %s", string(acl.ResourcePhotos), "search", "%s"}, s.RefID, 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
|
|
|
}
|
|
|
|
|
2022-03-30 20:36:25 +02:00
|
|
|
// Add response headers.
|
|
|
|
AddCountHeader(c, count)
|
|
|
|
AddLimitHeader(c, f.Count)
|
|
|
|
AddOffsetHeader(c, f.Offset)
|
|
|
|
AddTokenHeaders(c)
|
|
|
|
|
2022-09-30 19:15:10 +02:00
|
|
|
// Return as JSON.
|
2022-03-30 20:36:25 +02:00
|
|
|
c.JSON(http.StatusOK, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
// viewHandler returns a photo viewer formatted result.
|
|
|
|
viewHandler := func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
f, s, err := searchForm(c)
|
2022-03-30 20:36:25 +02:00
|
|
|
|
|
|
|
// Abort if authorization or form are invalid.
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
conf := service.Config()
|
2022-08-01 15:57:19 +02:00
|
|
|
|
2022-09-30 00:42:19 +02:00
|
|
|
result, count, err := search.UserPhotosViewerResults(f, s, conf.ContentUri(), conf.ApiUri(), conf.PreviewToken(), conf.DownloadToken())
|
2022-03-30 20:36:25 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2022-09-30 00:42:19 +02:00
|
|
|
event.AuditWarn([]string{ClientIP(c), "session %s", string(acl.ResourcePhotos), "view", "%s"}, s.RefID, err)
|
2022-03-30 20:36:25 +02:00
|
|
|
AbortBadRequest(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add response headers.
|
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
|
|
|
|
2022-09-30 19:15:10 +02:00
|
|
|
// Return as JSON.
|
2018-09-24 11:27:46 +02:00
|
|
|
c.JSON(http.StatusOK, result)
|
2022-03-30 20:36:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Register route handlers.
|
|
|
|
router.GET("/photos", defaultHandler)
|
|
|
|
router.GET("/photos/view", viewHandler)
|
2018-09-27 08:59:53 +02:00
|
|
|
}
|