2019-06-17 21:45:06 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2020-12-17 18:24:55 +01:00
|
|
|
"path/filepath"
|
2022-04-10 14:38:51 +02:00
|
|
|
"sync"
|
2019-06-17 21:45:06 +02:00
|
|
|
|
2021-01-27 21:30:10 +01:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-12-14 18:34:52 +01:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
2019-12-11 16:55:18 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2019-12-03 23:17:55 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2019-12-05 19:21:35 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2022-10-15 21:54:11 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/get"
|
2020-07-04 12:54:35 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
2020-05-08 15:41:01 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2021-09-18 15:32:39 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/search"
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2019-06-17 21:45:06 +02:00
|
|
|
)
|
|
|
|
|
2022-04-10 14:38:51 +02:00
|
|
|
var albumMutex = sync.Mutex{}
|
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
// SaveAlbumAsYaml saves album data as YAML file.
|
|
|
|
func SaveAlbumAsYaml(a entity.Album) {
|
2022-10-15 21:54:11 +02:00
|
|
|
c := get.Config()
|
2020-12-17 18:24:55 +01:00
|
|
|
|
|
|
|
// Write YAML sidecar file (optional).
|
2020-12-18 09:11:42 +01:00
|
|
|
if !c.BackupYaml() {
|
2020-12-17 18:24:55 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fileName := a.YamlFileName(c.AlbumsPath())
|
|
|
|
|
|
|
|
if err := a.SaveAsYaml(fileName); err != nil {
|
|
|
|
log.Errorf("album: %s (update yaml)", err)
|
|
|
|
} else {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Debugf("album: updated yaml file %s", clean.Log(filepath.Base(fileName)))
|
2020-12-17 18:24:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-18 00:46:34 +01:00
|
|
|
// GetAlbum returns album details as JSON.
|
|
|
|
//
|
2020-05-23 20:58:58 +02:00
|
|
|
// GET /api/v1/albums/:uid
|
2020-06-25 14:54:04 +02:00
|
|
|
func GetAlbum(router *gin.RouterGroup) {
|
2020-05-23 20:58:58 +02:00
|
|
|
router.GET("/albums/:uid", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourceAlbums, acl.ActionView)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2020-06-25 14:54:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
id := clean.UID(c.Param("uid"))
|
2020-12-17 18:24:55 +01:00
|
|
|
a, err := query.AlbumByUID(id)
|
2019-12-05 14:11:45 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2022-08-01 15:57:19 +02:00
|
|
|
AbortAlbumNotFound(c)
|
2019-12-05 14:11:45 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
c.JSON(http.StatusOK, a)
|
2019-12-05 14:11:45 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-18 00:46:34 +01:00
|
|
|
// CreateAlbum adds a new album.
|
|
|
|
//
|
2019-06-17 21:45:06 +02:00
|
|
|
// POST /api/v1/albums
|
2020-06-25 14:54:04 +02:00
|
|
|
func CreateAlbum(router *gin.RouterGroup) {
|
2019-06-17 21:45:06 +02:00
|
|
|
router.POST("/albums", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourceAlbums, acl.ActionCreate)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2019-11-12 05:49:10 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-05 19:21:35 +01:00
|
|
|
var f form.Album
|
2019-06-17 21:45:06 +02:00
|
|
|
|
2019-12-05 19:21:35 +01:00
|
|
|
if err := c.BindJSON(&f); err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortBadRequest(c)
|
2019-06-18 06:37:10 +02:00
|
|
|
return
|
2019-06-17 21:45:06 +02:00
|
|
|
}
|
|
|
|
|
2022-04-10 14:38:51 +02:00
|
|
|
albumMutex.Lock()
|
|
|
|
defer albumMutex.Unlock()
|
|
|
|
|
2022-09-30 19:15:10 +02:00
|
|
|
a := entity.NewUserAlbum(f.AlbumTitle, entity.AlbumDefault, s.UserUID)
|
2020-12-17 18:24:55 +01:00
|
|
|
a.AlbumFavorite = f.AlbumFavorite
|
2019-12-17 04:39:23 +01:00
|
|
|
|
2022-06-16 18:59:03 +02:00
|
|
|
// Existing album?
|
2022-09-28 09:01:17 +02:00
|
|
|
if found := a.Find(); found == nil {
|
2022-06-16 18:59:03 +02:00
|
|
|
// Not found, create new album.
|
2022-09-28 09:01:17 +02:00
|
|
|
if err := a.Create(); err != nil {
|
2022-06-16 18:59:03 +02:00
|
|
|
// Report unexpected error.
|
|
|
|
log.Errorf("album: %s (create)", err)
|
|
|
|
AbortUnexpected(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
event.SuccessMsg(i18n.MsgAlbumCreated)
|
|
|
|
} else {
|
|
|
|
// Exists, restore if necessary.
|
2022-09-28 09:01:17 +02:00
|
|
|
a = found
|
2022-06-16 18:59:03 +02:00
|
|
|
if !a.Deleted() {
|
|
|
|
event.InfoMsg(i18n.ErrAlreadyExists, a.Title())
|
|
|
|
c.JSON(http.StatusOK, a)
|
|
|
|
return
|
2022-09-28 09:01:17 +02:00
|
|
|
} else if err := a.Restore(); err == nil {
|
2022-06-16 18:59:03 +02:00
|
|
|
event.SuccessMsg(i18n.MsgRestored, a.Title())
|
|
|
|
} else {
|
|
|
|
// Report unexpected error.
|
|
|
|
log.Errorf("album: %s (restore)", err)
|
|
|
|
AbortUnexpected(c)
|
|
|
|
return
|
|
|
|
}
|
2019-06-17 21:45:06 +02:00
|
|
|
}
|
|
|
|
|
2022-06-16 18:59:03 +02:00
|
|
|
// Publish event and create/update YAML backup.
|
2020-06-25 14:54:04 +02:00
|
|
|
UpdateClientConfig()
|
2022-10-02 11:38:30 +02:00
|
|
|
// PublishAlbumEvent(EntityCreated, a.AlbumUID, c)
|
2020-12-17 18:24:55 +01:00
|
|
|
SaveAlbumAsYaml(*a)
|
2020-01-30 18:19:26 +01:00
|
|
|
|
2022-06-16 18:59:03 +02:00
|
|
|
// Return as JSON.
|
2020-12-17 18:24:55 +01:00
|
|
|
c.JSON(http.StatusOK, a)
|
2019-06-17 21:45:06 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-18 00:46:34 +01:00
|
|
|
// UpdateAlbum updates album metadata like title and description.
|
|
|
|
//
|
2020-05-23 20:58:58 +02:00
|
|
|
// PUT /api/v1/albums/:uid
|
2020-06-25 14:54:04 +02:00
|
|
|
func UpdateAlbum(router *gin.RouterGroup) {
|
2020-05-23 20:58:58 +02:00
|
|
|
router.PUT("/albums/:uid", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourceAlbums, acl.ActionUpdate)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2019-12-03 21:25:40 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
uid := clean.UID(c.Param("uid"))
|
2020-12-17 18:24:55 +01:00
|
|
|
a, err := query.AlbumByUID(uid)
|
2020-04-20 10:38:01 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2022-08-01 15:57:19 +02:00
|
|
|
AbortAlbumNotFound(c)
|
2019-12-03 21:25:40 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
f, err := form.NewAlbum(a)
|
2019-12-03 21:25:40 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-04-20 10:38:01 +02:00
|
|
|
log.Error(err)
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortSaveFailed(c)
|
2020-04-20 10:38:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
|
|
|
log.Error(err)
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortBadRequest(c)
|
2019-12-03 21:25:40 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-10 14:38:51 +02:00
|
|
|
albumMutex.Lock()
|
|
|
|
defer albumMutex.Unlock()
|
|
|
|
|
|
|
|
if err = a.SaveForm(f); err != nil {
|
2020-04-20 10:38:01 +02:00
|
|
|
log.Error(err)
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortSaveFailed(c)
|
2020-04-20 10:38:01 +02:00
|
|
|
return
|
|
|
|
}
|
2019-12-03 21:25:40 +01:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
UpdateClientConfig()
|
2020-05-27 19:38:40 +02:00
|
|
|
|
2020-07-04 12:54:35 +02:00
|
|
|
event.SuccessMsg(i18n.MsgAlbumSaved)
|
2019-12-06 11:56:24 +01:00
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
// PublishAlbumEvent(EntityUpdated, uid, c)
|
2020-01-30 18:19:26 +01:00
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
SaveAlbumAsYaml(a)
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, a)
|
2019-12-06 11:56:24 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-18 00:46:34 +01:00
|
|
|
// DeleteAlbum deletes an existing album.
|
|
|
|
//
|
2020-05-23 20:58:58 +02:00
|
|
|
// DELETE /api/v1/albums/:uid
|
2020-06-25 14:54:04 +02:00
|
|
|
func DeleteAlbum(router *gin.RouterGroup) {
|
2020-05-23 20:58:58 +02:00
|
|
|
router.DELETE("/albums/:uid", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourceAlbums, acl.ActionDelete)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2019-12-06 11:56:24 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
id := clean.UID(c.Param("uid"))
|
2019-12-06 11:56:24 +01:00
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
a, err := query.AlbumByUID(id)
|
2019-12-06 11:56:24 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2022-08-01 15:57:19 +02:00
|
|
|
AbortAlbumNotFound(c)
|
2019-12-06 11:56:24 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-10 14:38:51 +02:00
|
|
|
albumMutex.Lock()
|
|
|
|
defer albumMutex.Unlock()
|
|
|
|
|
2021-11-23 19:23:10 +01:00
|
|
|
// Regular, manually created album?
|
|
|
|
if a.IsDefault() {
|
|
|
|
// Soft delete manually created albums.
|
|
|
|
err = a.Delete()
|
|
|
|
} else {
|
|
|
|
// Permanently delete automatically created albums.
|
|
|
|
err = a.DeletePermanently()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2021-09-06 11:19:18 +02:00
|
|
|
log.Errorf("album: %s (delete)", err)
|
|
|
|
AbortDeleteFailed(c)
|
|
|
|
return
|
|
|
|
}
|
2020-01-30 18:19:26 +01:00
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
// PublishAlbumEvent(EntityDeleted, id, c)
|
2019-12-06 11:56:24 +01:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
UpdateClientConfig()
|
2020-07-04 12:54:35 +02:00
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
SaveAlbumAsYaml(a)
|
2019-12-03 23:17:55 +01:00
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
event.SuccessMsg(i18n.MsgAlbumDeleted, clean.Log(a.AlbumTitle))
|
2020-12-17 18:24:55 +01:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, a)
|
2019-12-03 21:25:40 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-18 00:46:34 +01:00
|
|
|
// LikeAlbum sets the favorite flag for an album.
|
|
|
|
//
|
2020-05-23 20:58:58 +02:00
|
|
|
// POST /api/v1/albums/:uid/like
|
2019-06-17 21:45:06 +02:00
|
|
|
//
|
|
|
|
// Parameters:
|
2022-08-10 16:09:21 +02:00
|
|
|
//
|
|
|
|
// uid: string Album UID
|
2020-06-25 14:54:04 +02:00
|
|
|
func LikeAlbum(router *gin.RouterGroup) {
|
2020-05-23 20:58:58 +02:00
|
|
|
router.POST("/albums/:uid/like", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourceAlbums, acl.ActionUpdate)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2019-11-12 05:49:10 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
id := clean.UID(c.Param("uid"))
|
2020-12-17 18:24:55 +01:00
|
|
|
a, err := query.AlbumByUID(id)
|
2019-06-17 21:45:06 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2022-08-01 15:57:19 +02:00
|
|
|
AbortAlbumNotFound(c)
|
2019-06-17 21:45:06 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
if err := a.Update("AlbumFavorite", true); err != nil {
|
2020-12-08 22:40:13 +01:00
|
|
|
Abort(c, http.StatusInternalServerError, i18n.ErrSaveFailed)
|
|
|
|
return
|
|
|
|
}
|
2019-06-17 21:45:06 +02:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
UpdateClientConfig()
|
2020-12-17 18:24:55 +01:00
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
PublishAlbumEvent(EntityUpdated, id, c)
|
2019-12-03 23:17:55 +01:00
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
SaveAlbumAsYaml(a)
|
|
|
|
|
2020-07-04 12:54:35 +02:00
|
|
|
c.JSON(http.StatusOK, i18n.NewResponse(http.StatusOK, i18n.MsgChangesSaved))
|
2019-06-17 21:45:06 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-18 00:46:34 +01:00
|
|
|
// DislikeAlbum removes the favorite flag from an album.
|
|
|
|
//
|
2020-05-23 20:58:58 +02:00
|
|
|
// DELETE /api/v1/albums/:uid/like
|
2019-06-17 21:45:06 +02:00
|
|
|
//
|
|
|
|
// Parameters:
|
2022-08-10 16:09:21 +02:00
|
|
|
//
|
|
|
|
// uid: string Album UID
|
2020-06-25 14:54:04 +02:00
|
|
|
func DislikeAlbum(router *gin.RouterGroup) {
|
2020-05-23 20:58:58 +02:00
|
|
|
router.DELETE("/albums/:uid/like", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourceAlbums, acl.ActionUpdate)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2019-11-12 05:49:10 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
id := clean.UID(c.Param("uid"))
|
2020-12-17 18:24:55 +01:00
|
|
|
a, err := query.AlbumByUID(id)
|
2019-06-17 21:45:06 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2022-08-01 15:57:19 +02:00
|
|
|
AbortAlbumNotFound(c)
|
2019-06-17 21:45:06 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
if err := a.Update("AlbumFavorite", false); err != nil {
|
2020-12-08 22:40:13 +01:00
|
|
|
Abort(c, http.StatusInternalServerError, i18n.ErrSaveFailed)
|
|
|
|
return
|
|
|
|
}
|
2019-06-17 21:45:06 +02:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
UpdateClientConfig()
|
2020-12-17 18:24:55 +01:00
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
PublishAlbumEvent(EntityUpdated, id, c)
|
2019-12-03 23:17:55 +01:00
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
SaveAlbumAsYaml(a)
|
|
|
|
|
2020-07-04 12:54:35 +02:00
|
|
|
c.JSON(http.StatusOK, i18n.NewResponse(http.StatusOK, i18n.MsgChangesSaved))
|
2019-06-17 21:45:06 +02:00
|
|
|
})
|
|
|
|
}
|
2019-12-05 12:10:30 +01:00
|
|
|
|
2021-11-18 00:46:34 +01:00
|
|
|
// CloneAlbums creates a new album containing pictures from other albums.
|
|
|
|
//
|
2020-06-14 11:39:53 +02:00
|
|
|
// POST /api/v1/albums/:uid/clone
|
2020-06-25 14:54:04 +02:00
|
|
|
func CloneAlbums(router *gin.RouterGroup) {
|
2020-06-14 11:39:53 +02:00
|
|
|
router.POST("/albums/:uid/clone", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourceAlbums, acl.ActionCreate)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2020-06-14 11:39:53 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
a, err := query.AlbumByUID(clean.UID(c.Param("uid")))
|
2020-06-14 11:39:53 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2022-08-01 15:57:19 +02:00
|
|
|
AbortAlbumNotFound(c)
|
2020-06-14 11:39:53 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var f form.Selection
|
|
|
|
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortBadRequest(c)
|
2020-06-14 11:39:53 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var added []entity.PhotoAlbum
|
|
|
|
|
|
|
|
for _, uid := range f.Albums {
|
|
|
|
cloneAlbum, err := query.AlbumByUID(uid)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("album: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-01-03 12:51:59 +01:00
|
|
|
photos, err := search.AlbumPhotos(cloneAlbum, 10000, false)
|
2020-06-14 11:39:53 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("album: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
added = append(added, a.AddPhotos(photos.UIDs())...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(added) > 0 {
|
2022-04-15 09:42:07 +02:00
|
|
|
event.SuccessMsg(i18n.MsgSelectionAddedTo, clean.Log(a.Title()))
|
2020-06-14 11:39:53 +02:00
|
|
|
|
|
|
|
PublishAlbumEvent(EntityUpdated, a.AlbumUID, c)
|
2020-12-17 18:24:55 +01:00
|
|
|
|
|
|
|
SaveAlbumAsYaml(a)
|
2020-06-14 11:39:53 +02:00
|
|
|
}
|
|
|
|
|
2020-07-07 10:51:55 +02:00
|
|
|
c.JSON(http.StatusOK, gin.H{"code": http.StatusOK, "message": i18n.Msg(i18n.MsgAlbumCloned), "album": a, "added": added})
|
2020-06-14 11:39:53 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-18 00:46:34 +01:00
|
|
|
// AddPhotosToAlbum adds photos to an album.
|
|
|
|
//
|
2020-05-23 20:58:58 +02:00
|
|
|
// POST /api/v1/albums/:uid/photos
|
2020-06-25 14:54:04 +02:00
|
|
|
func AddPhotosToAlbum(router *gin.RouterGroup) {
|
2020-05-23 20:58:58 +02:00
|
|
|
router.POST("/albums/:uid/photos", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourceAlbums, acl.ActionUpdate)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2019-12-05 12:10:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-04 05:18:22 +01:00
|
|
|
var f form.Selection
|
2019-12-05 12:10:30 +01:00
|
|
|
|
2019-12-05 19:21:35 +01:00
|
|
|
if err := c.BindJSON(&f); err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortBadRequest(c)
|
2019-12-05 12:10:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
uid := clean.UID(c.Param("uid"))
|
2020-05-23 20:58:58 +02:00
|
|
|
a, err := query.AlbumByUID(uid)
|
2020-02-04 05:18:22 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2022-08-01 15:57:19 +02:00
|
|
|
AbortAlbumNotFound(c)
|
2019-12-05 12:10:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-27 17:32:54 +01:00
|
|
|
// Fetch selection from index.
|
|
|
|
photos, err := query.SelectedPhotos(f)
|
2019-12-05 12:10:30 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-02-04 05:18:22 +01:00
|
|
|
log.Errorf("album: %s", err)
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortBadRequest(c)
|
2019-12-05 12:10:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-01 09:45:24 +02:00
|
|
|
added := a.AddPhotos(photos.UIDs())
|
2020-02-04 05:18:22 +01:00
|
|
|
|
2020-06-01 09:45:24 +02:00
|
|
|
if len(added) > 0 {
|
|
|
|
if len(added) == 1 {
|
2022-04-15 09:42:07 +02:00
|
|
|
event.SuccessMsg(i18n.MsgEntryAddedTo, clean.Log(a.Title()))
|
2020-05-30 01:41:47 +02:00
|
|
|
} else {
|
2022-04-15 09:42:07 +02:00
|
|
|
event.SuccessMsg(i18n.MsgEntriesAddedTo, len(added), clean.Log(a.Title()))
|
2020-05-26 11:00:39 +02:00
|
|
|
}
|
2019-12-05 12:10:30 +01:00
|
|
|
|
2021-01-08 12:20:41 +01:00
|
|
|
RemoveFromAlbumCoverCache(a.AlbumUID)
|
2020-12-17 18:24:55 +01:00
|
|
|
|
2020-06-01 09:45:24 +02:00
|
|
|
PublishAlbumEvent(EntityUpdated, a.AlbumUID, c)
|
2020-12-17 18:24:55 +01:00
|
|
|
|
|
|
|
SaveAlbumAsYaml(a)
|
2019-12-05 12:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-07-07 10:51:55 +02:00
|
|
|
c.JSON(http.StatusOK, gin.H{"code": http.StatusOK, "message": i18n.Msg(i18n.MsgChangesSaved), "album": a, "photos": photos.UIDs(), "added": added})
|
2019-12-05 12:10:30 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-18 00:46:34 +01:00
|
|
|
// RemovePhotosFromAlbum removes photos from an album.
|
|
|
|
//
|
2020-05-23 20:58:58 +02:00
|
|
|
// DELETE /api/v1/albums/:uid/photos
|
2020-06-25 14:54:04 +02:00
|
|
|
func RemovePhotosFromAlbum(router *gin.RouterGroup) {
|
2020-05-23 20:58:58 +02:00
|
|
|
router.DELETE("/albums/:uid/photos", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourceAlbums, acl.ActionUpdate)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2019-12-05 12:10:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-04 05:18:22 +01:00
|
|
|
var f form.Selection
|
2019-12-05 12:10:30 +01:00
|
|
|
|
2019-12-05 19:21:35 +01:00
|
|
|
if err := c.BindJSON(&f); err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortBadRequest(c)
|
2019-12-05 12:10:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-05 19:21:35 +01:00
|
|
|
if len(f.Photos) == 0 {
|
2020-07-04 12:54:35 +02:00
|
|
|
Abort(c, http.StatusBadRequest, i18n.ErrNoItemsSelected)
|
2019-12-05 12:10:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
a, err := query.AlbumByUID(clean.UID(c.Param("uid")))
|
2019-12-05 12:10:30 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2022-08-01 15:57:19 +02:00
|
|
|
AbortAlbumNotFound(c)
|
2019-12-05 12:10:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-01 09:45:24 +02:00
|
|
|
removed := a.RemovePhotos(f.Photos)
|
2019-12-05 12:10:30 +01:00
|
|
|
|
2020-06-01 09:45:24 +02:00
|
|
|
if len(removed) > 0 {
|
|
|
|
if len(removed) == 1 {
|
2022-04-15 09:42:07 +02:00
|
|
|
event.SuccessMsg(i18n.MsgEntryRemovedFrom, clean.Log(a.Title()))
|
2020-06-01 09:45:24 +02:00
|
|
|
} else {
|
2022-09-28 09:01:17 +02:00
|
|
|
event.SuccessMsg(i18n.MsgEntriesRemovedFrom, len(removed), clean.Log(a.Title()))
|
2020-06-01 09:45:24 +02:00
|
|
|
}
|
2019-12-05 12:10:30 +01:00
|
|
|
|
2021-01-08 12:20:41 +01:00
|
|
|
RemoveFromAlbumCoverCache(a.AlbumUID)
|
2020-12-17 18:24:55 +01:00
|
|
|
|
2020-06-01 09:45:24 +02:00
|
|
|
PublishAlbumEvent(EntityUpdated, a.AlbumUID, c)
|
2020-12-17 18:24:55 +01:00
|
|
|
|
|
|
|
SaveAlbumAsYaml(a)
|
2020-06-01 09:45:24 +02:00
|
|
|
}
|
2020-02-04 14:51:48 +01:00
|
|
|
|
2020-07-07 10:51:55 +02:00
|
|
|
c.JSON(http.StatusOK, gin.H{"code": http.StatusOK, "message": i18n.Msg(i18n.MsgChangesSaved), "album": a, "photos": f.Photos, "removed": removed})
|
2019-12-05 12:10:30 +01:00
|
|
|
})
|
|
|
|
}
|