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/form"
|
|
|
|
"github.com/photoprism/photoprism/internal/search"
|
2021-11-26 21:10:52 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
2022-03-30 20:36:25 +02:00
|
|
|
|
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.
|
2021-11-26 13:59:10 +01:00
|
|
|
//
|
|
|
|
// GET /api/v1/geo
|
2022-03-30 20:36:25 +02:00
|
|
|
//
|
2022-04-14 08:39:52 +02:00
|
|
|
// See form.SearchPhotosGeo for supported search params and data types.
|
2021-11-26 13:59:10 +01:00
|
|
|
func SearchGeo(router *gin.RouterGroup) {
|
2021-11-26 21:10:52 +01:00
|
|
|
handler := func(c *gin.Context) {
|
2021-11-26 13:59:10 +01:00
|
|
|
s := Auth(SessionID(c), acl.ResourcePhotos, acl.ActionSearch)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
|
|
|
AbortUnauthorized(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-14 08:39:52 +02:00
|
|
|
var f form.SearchPhotosGeo
|
2021-11-26 13:59:10 +01:00
|
|
|
|
|
|
|
err := c.MustBindWith(&f, binding.Form)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
AbortBadRequest(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find matching pictures.
|
2022-04-14 14:13:54 +02:00
|
|
|
photos, err := search.PhotosGeo(f)
|
2021-11-26 13:59:10 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("search: %s", err)
|
|
|
|
AbortBadRequest(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-30 20:36:25 +02:00
|
|
|
// Add response headers.
|
|
|
|
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":
|
|
|
|
conf := service.Config()
|
|
|
|
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
|
|
|
}
|