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"
|
|
|
|
"github.com/photoprism/photoprism/internal/search"
|
2021-11-26 21:10:52 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
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-04-14 08:39:52 +02:00
|
|
|
var f form.SearchPhotosGeo
|
2022-09-30 00:42:19 +02:00
|
|
|
var err error
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
conf := service.Config()
|
|
|
|
|
2022-09-30 00:42:19 +02:00
|
|
|
// Ignore private flag if feature is disabled.
|
|
|
|
if !conf.Settings().Features.Private {
|
|
|
|
f.Public = false
|
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
|
|
|
|
|
|
|
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-03-30 20:36:25 +02:00
|
|
|
AddTokenHeaders(c)
|
|
|
|
|
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":
|
|
|
|
resp, err = photos.ViewerJSON(conf.ContentUri(), conf.ApiUri(), conf.PreviewToken(), conf.DownloadToken())
|
|
|
|
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
|
|
|
}
|