d8e0364dbb
This needs to be very well tested and discussed, as these changes can lead to private photos being accidentally published. Thank you! Signed-off-by: Michael Mayer <michael@photoprism.app>
111 lines
2.4 KiB
Go
111 lines
2.4 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gin-gonic/gin/binding"
|
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
|
"github.com/photoprism/photoprism/internal/form"
|
|
"github.com/photoprism/photoprism/internal/search"
|
|
"github.com/photoprism/photoprism/internal/service"
|
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
|
"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.
|
|
//
|
|
// GET /api/v1/geo
|
|
//
|
|
// See form.SearchPhotosGeo for supported search params and data types.
|
|
func SearchGeo(router *gin.RouterGroup) {
|
|
handler := func(c *gin.Context) {
|
|
s := Auth(SessionID(c), acl.ResourcePhotos, acl.ActionSearch)
|
|
|
|
if s.Invalid() {
|
|
AbortUnauthorized(c)
|
|
return
|
|
}
|
|
|
|
var f form.SearchPhotosGeo
|
|
|
|
err := c.MustBindWith(&f, binding.Form)
|
|
|
|
if err != nil {
|
|
AbortBadRequest(c)
|
|
return
|
|
}
|
|
|
|
// Limit results to a specific album?
|
|
if f.Album == "" {
|
|
// Do nothing.
|
|
} else if a, err := entity.CachedAlbumByUID(f.Album); err != nil {
|
|
AbortAlbumNotFound(c)
|
|
return
|
|
} else {
|
|
f.Filter = a.AlbumFilter
|
|
}
|
|
|
|
// Parse query string and filter.
|
|
if err = f.ParseQueryString(); err != nil {
|
|
log.Debugf("search: %s", err)
|
|
AbortBadRequest(c)
|
|
return
|
|
}
|
|
|
|
conf := service.Config()
|
|
|
|
// Guests may only see public content.
|
|
if s.Guest() {
|
|
if f.Album == "" || !s.HasShare(f.Album) {
|
|
AbortUnauthorized(c)
|
|
return
|
|
}
|
|
|
|
f.Public = true
|
|
f.Private = false
|
|
f.Archived = false
|
|
f.Review = false
|
|
} else if !conf.Settings().Features.Private {
|
|
f.Public = false
|
|
}
|
|
|
|
// Find matching pictures.
|
|
photos, err := search.PhotosGeo(f)
|
|
|
|
if err != nil {
|
|
log.Warnf("search: %s", err)
|
|
AbortBadRequest(c)
|
|
return
|
|
}
|
|
|
|
// Add response headers.
|
|
AddTokenHeaders(c)
|
|
|
|
var resp []byte
|
|
|
|
// Render JSON response.
|
|
switch clean.Token(c.Param("format")) {
|
|
case "view":
|
|
conf := service.Config()
|
|
resp, err = photos.ViewerJSON(conf.ContentUri(), conf.ApiUri(), conf.PreviewToken(), conf.DownloadToken())
|
|
default:
|
|
resp, err = photos.GeoJSON()
|
|
}
|
|
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(400, gin.H{"error": txt.UpperFirst(err.Error())})
|
|
return
|
|
}
|
|
|
|
c.Data(http.StatusOK, "application/json", resp)
|
|
}
|
|
|
|
// Register route handlers.
|
|
router.GET("/geo", handler)
|
|
router.GET("/geo/:format", handler)
|
|
}
|