2021-11-26 14:28:50 +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-28 09:01:17 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2021-11-26 14:28:50 +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 14:28:50 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/search"
|
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SearchAlbums finds albums and returns them as JSON.
|
|
|
|
//
|
|
|
|
// GET /api/v1/albums
|
|
|
|
func SearchAlbums(router *gin.RouterGroup) {
|
|
|
|
router.GET("/albums", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := AuthAny(c, acl.ResourceAlbums, acl.Permissions{acl.ActionSearch, acl.ActionView, acl.AccessShared})
|
2021-11-26 14:28:50 +01:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Abort if permission was not granted.
|
|
|
|
if s.Abort(c) {
|
2021-11-26 14:28:50 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-30 19:15:10 +02:00
|
|
|
var err error
|
2021-11-26 14:28:50 +01:00
|
|
|
var f form.SearchAlbums
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Abort if request params are invalid.
|
2022-09-30 19:15:10 +02:00
|
|
|
if err = c.MustBindWith(&f, binding.Form); err != nil {
|
2022-09-28 09:01:17 +02:00
|
|
|
event.AuditWarn([]string{ClientIP(c), "session %s", "albums", "search", "form invalid", "%s"}, s.RefID, err)
|
2021-11-26 14:28:50 +01:00
|
|
|
AbortBadRequest(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
conf := get.Config()
|
2022-08-31 17:42:57 +02:00
|
|
|
|
2022-09-30 19:15:10 +02:00
|
|
|
// Ignore private flag if feature is disabled.
|
|
|
|
if !conf.Settings().Features.Private {
|
2022-09-28 09:01:17 +02:00
|
|
|
f.Public = false
|
2021-11-26 14:28:50 +01:00
|
|
|
}
|
|
|
|
|
2022-09-30 19:15:10 +02:00
|
|
|
// Find matching albums.
|
|
|
|
result, err := search.UserAlbums(f, s)
|
2021-11-26 14:28:50 +01:00
|
|
|
|
2022-09-30 19:15:10 +02:00
|
|
|
// Ok?
|
2021-11-26 14:28:50 +01:00
|
|
|
if err != nil {
|
2022-09-28 09:01:17 +02:00
|
|
|
event.AuditWarn([]string{ClientIP(c), "session %s", "albums", "search", "%s"}, s.RefID, err)
|
2022-04-15 09:42:07 +02:00
|
|
|
c.AbortWithStatusJSON(400, gin.H{"error": txt.UpperFirst(err.Error())})
|
2021-11-26 14:28:50 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
AddCountHeader(c, len(result))
|
|
|
|
AddLimitHeader(c, f.Count)
|
|
|
|
AddOffsetHeader(c, f.Offset)
|
2022-10-13 22:11:02 +02:00
|
|
|
AddTokenHeaders(c, s)
|
2021-11-26 14:28:50 +01:00
|
|
|
|
2022-09-30 19:15:10 +02:00
|
|
|
// Return as JSON.
|
2021-11-26 14:28:50 +01:00
|
|
|
c.JSON(http.StatusOK, result)
|
|
|
|
})
|
|
|
|
}
|