2021-11-26 13:59:10 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/gin-gonic/gin/binding"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
2022-09-30 00:42:19 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2021-11-26 13:59:10 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2022-10-15 21:54:11 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/get"
|
2021-11-26 13:59:10 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/search"
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2021-11-26 13:59:10 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
|
|
|
)
|
|
|
|
|
2021-11-26 21:10:52 +01:00
|
|
|
// SearchGeo finds photos and returns results as JSON, so they can be displayed on a map or in a viewer.
|
2022-09-30 00:42:19 +02:00
|
|
|
// See form.SearchPhotosGeo for supported search params and data types.
|
2021-11-26 13:59:10 +01:00
|
|
|
//
|
|
|
|
// GET /api/v1/geo
|
|
|
|
func SearchGeo(router *gin.RouterGroup) {
|
2021-11-26 21:10:52 +01:00
|
|
|
handler := func(c *gin.Context) {
|
2022-09-30 00:42:19 +02:00
|
|
|
s := AuthAny(c, acl.ResourcePlaces, acl.Permissions{acl.ActionSearch, acl.ActionView, acl.AccessShared})
|
2021-11-26 13:59:10 +01:00
|
|
|
|
2022-09-30 00:42:19 +02:00
|
|
|
// Abort if permission was not granted.
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2021-11-26 13:59:10 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-30 00:42:19 +02:00
|
|
|
var err error
|
2022-09-30 19:15:10 +02:00
|
|
|
var f form.SearchPhotosGeo
|
2021-11-26 13:59:10 +01:00
|
|
|
|
2022-09-30 00:42:19 +02:00
|
|
|
// Abort if request params are invalid.
|
|
|
|
if err = c.MustBindWith(&f, binding.Form); err != nil {
|
|
|
|
event.AuditWarn([]string{ClientIP(c), "session %s", string(acl.ResourcePlaces), "form invalid", "%s"}, s.RefID, err)
|
2022-08-01 15:57:19 +02:00
|
|
|
AbortBadRequest(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
conf := get.Config()
|
2022-10-04 02:57:15 +02:00
|
|
|
settings := conf.Settings()
|
2022-08-01 15:57:19 +02:00
|
|
|
|
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-09-30 00:42:19 +02:00
|
|
|
f.Public = false
|
2021-11-26 13:59:10 +01: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
|
|
|
|
}
|
|
|
|
|
2021-11-26 13:59:10 +01:00
|
|
|
// Find matching pictures.
|
2022-09-30 00:42:19 +02:00
|
|
|
photos, err := search.UserPhotosGeo(f, s)
|
2021-11-26 13:59:10 +01:00
|
|
|
|
2022-09-30 19:15:10 +02:00
|
|
|
// Ok?
|
2021-11-26 13:59:10 +01:00
|
|
|
if err != nil {
|
2022-09-30 00:42:19 +02:00
|
|
|
event.AuditWarn([]string{ClientIP(c), "session %s", string(acl.ResourcePlaces), "search", "%s"}, s.RefID, err)
|
2021-11-26 13:59:10 +01:00
|
|
|
AbortBadRequest(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-30 20:36:25 +02:00
|
|
|
// Add response headers.
|
2022-09-30 00:42:19 +02:00
|
|
|
AddCountHeader(c, len(photos))
|
|
|
|
AddLimitHeader(c, f.Count)
|
|
|
|
AddOffsetHeader(c, f.Offset)
|
2022-10-13 22:11:02 +02:00
|
|
|
AddTokenHeaders(c, s)
|
2022-03-30 20:36:25 +02:00
|
|
|
|
2021-11-26 21:10:52 +01:00
|
|
|
var resp []byte
|
|
|
|
|
|
|
|
// Render JSON response.
|
2022-04-15 09:42:07 +02:00
|
|
|
switch clean.Token(c.Param("format")) {
|
2021-11-26 21:10:52 +01:00
|
|
|
case "view":
|
2022-10-13 22:11:02 +02:00
|
|
|
resp, err = photos.ViewerJSON(conf.ContentUri(), conf.ApiUri(), s.PreviewToken, s.DownloadToken)
|
2021-11-26 21:10:52 +01:00
|
|
|
default:
|
|
|
|
resp, err = photos.GeoJSON()
|
|
|
|
}
|
2021-11-26 13:59:10 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
c.AbortWithStatusJSON(400, gin.H{"error": txt.UpperFirst(err.Error())})
|
2021-11-26 13:59:10 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Data(http.StatusOK, "application/json", resp)
|
2021-11-26 21:10:52 +01:00
|
|
|
}
|
|
|
|
|
2022-03-30 20:36:25 +02:00
|
|
|
// Register route handlers.
|
2021-11-26 21:10:52 +01:00
|
|
|
router.GET("/geo", handler)
|
|
|
|
router.GET("/geo/:format", handler)
|
2021-11-26 13:59:10 +01:00
|
|
|
}
|