photoprism/internal/api/photos_search_geo.go

94 lines
2.4 KiB
Go
Raw Normal View History

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"
"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"
"github.com/photoprism/photoprism/internal/service"
"github.com/photoprism/photoprism/pkg/clean"
2021-11-26 13:59:10 +01:00
"github.com/photoprism/photoprism/pkg/txt"
)
// SearchGeo finds photos and returns results as JSON, so they can be displayed on a map or in a viewer.
// 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) {
handler := func(c *gin.Context) {
s := AuthAny(c, acl.ResourcePlaces, acl.Permissions{acl.ActionSearch, acl.ActionView, acl.AccessShared})
2021-11-26 13:59:10 +01:00
// Abort if permission was not granted.
if s.Abort(c) {
2021-11-26 13:59:10 +01:00
return
}
var err error
var f form.SearchPhotosGeo
2021-11-26 13:59:10 +01: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)
AbortBadRequest(c)
return
}
conf := service.Config()
settings := conf.Settings()
// Ignore private flag if feature is disabled.
if !settings.Features.Private {
f.Public = false
2021-11-26 13:59:10 +01: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.
photos, err := search.UserPhotosGeo(f, s)
2021-11-26 13:59:10 +01:00
// Ok?
2021-11-26 13:59:10 +01:00
if err != nil {
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
}
// Add response headers.
AddCountHeader(c, len(photos))
AddLimitHeader(c, f.Count)
AddOffsetHeader(c, f.Offset)
AddTokenHeaders(c)
var resp []byte
// Render JSON response.
switch clean.Token(c.Param("format")) {
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 {
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)
}
// Register route handlers.
router.GET("/geo", handler)
router.GET("/geo/:format", handler)
2021-11-26 13:59:10 +01:00
}