2019-06-17 21:45:06 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2019-12-06 16:47:30 +01:00
|
|
|
"archive/zip"
|
2019-06-17 21:45:06 +02:00
|
|
|
"net/http"
|
2020-12-17 18:24:55 +01:00
|
|
|
"path/filepath"
|
2019-12-06 16:47:30 +01:00
|
|
|
"strings"
|
|
|
|
"time"
|
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"
|
2020-07-04 12:54:35 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
2020-06-07 10:09:35 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
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"
|
2020-05-22 20:00:33 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
2021-12-14 18:34:52 +01:00
|
|
|
|
2020-01-13 11:07:09 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2021-12-14 18:34:52 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/sanitize"
|
2019-06-17 21:45:06 +02:00
|
|
|
)
|
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
// SaveAlbumAsYaml saves album data as YAML file.
|
|
|
|
func SaveAlbumAsYaml(a entity.Album) {
|
|
|
|
c := service.Config()
|
|
|
|
|
|
|
|
// 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 {
|
2021-12-14 20:01:39 +01:00
|
|
|
log.Debugf("album: updated yaml file %s", sanitize.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) {
|
2020-06-25 14:54:04 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourceAlbums, acl.ActionRead)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2020-06-25 14:54:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-14 18:34:52 +01:00
|
|
|
id := sanitize.IdString(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 {
|
2020-07-04 12:54:35 +02:00
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrAlbumNotFound)
|
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) {
|
2020-06-25 14:54:04 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourceAlbums, acl.ActionCreate)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(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
|
|
|
}
|
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
a := entity.NewAlbum(f.AlbumTitle, entity.AlbumDefault)
|
|
|
|
a.AlbumFavorite = f.AlbumFavorite
|
2019-12-17 04:39:23 +01:00
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
if res := entity.Db().Create(a); res.Error != nil {
|
2021-12-14 20:01:39 +01:00
|
|
|
AbortAlreadyExists(c, sanitize.Log(a.AlbumTitle))
|
2019-06-18 06:37:10 +02:00
|
|
|
return
|
2019-06-17 21:45:06 +02:00
|
|
|
}
|
|
|
|
|
2020-07-04 12:54:35 +02:00
|
|
|
event.SuccessMsg(i18n.MsgAlbumCreated)
|
2019-12-04 12:11:11 +01:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
UpdateClientConfig()
|
2019-12-17 04:39:23 +01:00
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
PublishAlbumEvent(EntityCreated, a.AlbumUID, c)
|
|
|
|
|
|
|
|
SaveAlbumAsYaml(*a)
|
2020-01-30 18:19:26 +01:00
|
|
|
|
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) {
|
2020-06-25 14:54:04 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourceAlbums, acl.ActionUpdate)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2019-12-03 21:25:40 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-14 18:34:52 +01:00
|
|
|
uid := sanitize.IdString(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 {
|
2020-07-04 12:54:35 +02:00
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrAlbumNotFound)
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
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
|
|
|
|
2020-05-23 20:58:58 +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) {
|
2020-06-25 14:54:04 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourceAlbums, acl.ActionDelete)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2019-12-06 11:56:24 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-14 18:34:52 +01:00
|
|
|
id := sanitize.IdString(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 {
|
2020-07-04 12:54:35 +02:00
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrAlbumNotFound)
|
2019-12-06 11:56:24 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-09-06 11:19:18 +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
|
|
|
|
2021-12-14 20:01:39 +01:00
|
|
|
event.SuccessMsg(i18n.MsgAlbumDeleted, sanitize.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:
|
2020-05-23 20:58:58 +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) {
|
2020-06-25 14:54:04 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourceAlbums, acl.ActionLike)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2019-11-12 05:49:10 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-14 18:34:52 +01:00
|
|
|
id := sanitize.IdString(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 {
|
2020-07-04 12:54:35 +02:00
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrAlbumNotFound)
|
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:
|
2020-05-23 20:58:58 +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) {
|
2020-06-25 14:54:04 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourceAlbums, acl.ActionLike)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2019-11-12 05:49:10 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-14 18:34:52 +01:00
|
|
|
id := sanitize.IdString(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 {
|
2020-07-04 12:54:35 +02:00
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrAlbumNotFound)
|
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) {
|
2020-06-25 14:54:04 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourceAlbums, acl.ActionUpdate)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2020-06-14 11:39:53 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-14 18:34:52 +01:00
|
|
|
a, err := query.AlbumByUID(sanitize.IdString(c.Param("uid")))
|
2020-06-14 11:39:53 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrAlbumNotFound)
|
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 {
|
2021-12-14 20:01:39 +01:00
|
|
|
event.SuccessMsg(i18n.MsgSelectionAddedTo, sanitize.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) {
|
2020-06-25 14:54:04 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourceAlbums, acl.ActionUpdate)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(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
|
|
|
|
}
|
|
|
|
|
2021-12-14 18:34:52 +01:00
|
|
|
uid := sanitize.IdString(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 {
|
2020-07-04 12:54:35 +02:00
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrAlbumNotFound)
|
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 {
|
2021-12-14 20:01:39 +01:00
|
|
|
event.SuccessMsg(i18n.MsgEntryAddedTo, sanitize.Log(a.Title()))
|
2020-05-30 01:41:47 +02:00
|
|
|
} else {
|
2021-12-14 20:01:39 +01:00
|
|
|
event.SuccessMsg(i18n.MsgEntriesAddedTo, len(added), sanitize.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) {
|
2020-06-25 14:54:04 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourceAlbums, acl.ActionUpdate)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(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
|
|
|
|
}
|
|
|
|
|
2021-12-14 18:34:52 +01:00
|
|
|
a, err := query.AlbumByUID(sanitize.IdString(c.Param("uid")))
|
2019-12-05 12:10:30 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrAlbumNotFound)
|
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 {
|
2021-12-14 20:01:39 +01:00
|
|
|
event.SuccessMsg(i18n.MsgEntryRemovedFrom, sanitize.Log(a.Title()))
|
2020-06-01 09:45:24 +02:00
|
|
|
} else {
|
2021-12-14 20:01:39 +01:00
|
|
|
event.SuccessMsg(i18n.MsgEntriesRemovedFrom, len(removed), sanitize.Log(sanitize.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
|
|
|
})
|
|
|
|
}
|
2019-12-06 16:47:30 +01:00
|
|
|
|
2021-11-18 00:46:34 +01:00
|
|
|
// DownloadAlbum streams the album contents as zip archive.
|
|
|
|
//
|
2020-06-14 11:39:53 +02:00
|
|
|
// GET /api/v1/albums/:uid/dl
|
2020-06-25 14:54:04 +02:00
|
|
|
func DownloadAlbum(router *gin.RouterGroup) {
|
2020-05-27 19:38:40 +02:00
|
|
|
router.GET("/albums/:uid/dl", func(c *gin.Context) {
|
2020-06-25 14:54:04 +02:00
|
|
|
if InvalidDownloadToken(c) {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2020-05-27 19:38:40 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-06 16:47:30 +01:00
|
|
|
start := time.Now()
|
2021-12-14 18:34:52 +01:00
|
|
|
a, err := query.AlbumByUID(sanitize.IdString(c.Param("uid")))
|
2019-12-06 16:47:30 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrAlbumNotFound)
|
2019-12-06 16:47:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-03 12:51:59 +01:00
|
|
|
files, err := search.AlbumPhotos(a, 10000, true)
|
2019-12-06 16:47:30 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortEntityNotFound(c)
|
2019-12-06 16:47:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-18 00:46:34 +01:00
|
|
|
zipFileName := a.ZipName()
|
2019-12-06 16:47:30 +01:00
|
|
|
|
2021-01-08 09:02:30 +01:00
|
|
|
AddDownloadHeader(c, zipFileName)
|
2019-12-06 16:47:30 +01:00
|
|
|
|
2020-09-06 14:58:08 +02:00
|
|
|
zipWriter := zip.NewWriter(c.Writer)
|
2020-05-08 15:41:01 +02:00
|
|
|
defer func() { _ = zipWriter.Close() }()
|
2019-12-06 16:47:30 +01:00
|
|
|
|
2021-01-27 21:30:10 +01:00
|
|
|
var aliases = make(map[string]int)
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
if file.FileHash == "" {
|
2021-12-14 20:01:39 +01:00
|
|
|
log.Warnf("download: empty file hash, skipped %s", sanitize.Log(file.FileName))
|
2021-01-27 21:30:10 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if file.FileSidecar {
|
2021-12-14 20:01:39 +01:00
|
|
|
log.Debugf("download: skipped sidecar %s", sanitize.Log(file.FileName))
|
2021-01-27 21:30:10 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fileName := photoprism.FileName(file.FileRoot, file.FileName)
|
|
|
|
alias := file.ShareBase(0)
|
|
|
|
key := strings.ToLower(alias)
|
|
|
|
|
|
|
|
if seq := aliases[key]; seq > 0 {
|
|
|
|
alias = file.ShareBase(seq)
|
|
|
|
}
|
|
|
|
|
|
|
|
aliases[key] += 1
|
2019-12-06 16:47:30 +01:00
|
|
|
|
2020-01-12 14:00:56 +01:00
|
|
|
if fs.FileExists(fileName) {
|
2021-01-27 21:30:10 +01:00
|
|
|
if err := addFileToZip(zipWriter, fileName, alias); err != nil {
|
2019-12-06 16:47:30 +01:00
|
|
|
log.Error(err)
|
2020-09-20 19:14:50 +02:00
|
|
|
Abort(c, http.StatusInternalServerError, i18n.ErrZipFailed)
|
2019-12-06 16:47:30 +01:00
|
|
|
return
|
|
|
|
}
|
2021-12-14 20:01:39 +01:00
|
|
|
log.Infof("download: added %s as %s", sanitize.Log(file.FileName), sanitize.Log(alias))
|
2019-12-06 16:47:30 +01:00
|
|
|
} else {
|
2021-12-14 20:01:39 +01:00
|
|
|
log.Errorf("download: failed finding %s", sanitize.Log(file.FileName))
|
2019-12-06 16:47:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 20:01:39 +01:00
|
|
|
log.Infof("download: created %s [%s]", sanitize.Log(zipFileName), time.Since(start))
|
2019-12-06 16:47:30 +01:00
|
|
|
})
|
|
|
|
}
|