2019-06-17 21:45:06 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2019-12-06 16:47:30 +01:00
|
|
|
"archive/zip"
|
2019-06-18 06:37:10 +02:00
|
|
|
"fmt"
|
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"
|
|
|
|
"github.com/gin-gonic/gin/binding"
|
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"
|
2020-05-22 20:00:33 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
2020-01-13 11:07:09 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2020-01-12 14:00:56 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
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 {
|
|
|
|
log.Debugf("album: updated yaml file %s", txt.Quote(filepath.Base(fileName)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-17 21:45:06 +02:00
|
|
|
// GET /api/v1/albums
|
2020-06-25 14:54:04 +02:00
|
|
|
func GetAlbums(router *gin.RouterGroup) {
|
2019-06-17 21:45:06 +02:00
|
|
|
router.GET("/albums", func(c *gin.Context) {
|
2020-06-25 14:54:04 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourceAlbums, acl.ActionSearch)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2020-01-22 13:43:07 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-05 19:21:35 +01:00
|
|
|
var f form.AlbumSearch
|
2019-06-17 21:45:06 +02:00
|
|
|
|
2019-12-05 19:21:35 +01:00
|
|
|
err := c.MustBindWith(&f, binding.Form)
|
2019-06-17 21:45:06 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortBadRequest(c)
|
2019-06-17 21:45:06 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
// Guest permissions are limited to shared albums.
|
|
|
|
if s.Guest() {
|
2020-12-31 12:34:06 +01:00
|
|
|
f.ID = s.Shares.Join(query.Or)
|
2020-06-25 14:54:04 +02:00
|
|
|
}
|
|
|
|
|
2020-05-25 19:10:44 +02:00
|
|
|
result, err := query.AlbumSearch(f)
|
2020-06-23 07:55:33 +02:00
|
|
|
|
2019-06-17 21:45:06 +02:00
|
|
|
if err != nil {
|
2020-01-07 17:36:49 +01:00
|
|
|
c.AbortWithStatusJSON(400, gin.H{"error": txt.UcFirst(err.Error())})
|
2019-06-17 21:45:06 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-08 09:02:30 +01:00
|
|
|
AddCountHeader(c, len(result))
|
|
|
|
AddLimitHeader(c, f.Count)
|
|
|
|
AddOffsetHeader(c, f.Offset)
|
|
|
|
AddTokenHeaders(c)
|
2019-06-17 21:45:06 +02:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, result)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
id := 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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
log.Debugf("album: creating %+v %+v", f, a)
|
2019-06-17 21:45:06 +02:00
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
if res := entity.Db().Create(a); res.Error != nil {
|
|
|
|
AbortAlreadyExists(c, txt.Quote(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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
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 {
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
conf := service.Config()
|
2020-05-23 20:58:58 +02:00
|
|
|
id := 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
|
|
|
|
}
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
PublishAlbumEvent(EntityDeleted, id, c)
|
2020-01-30 18:19:26 +01:00
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
conf.Db().Delete(&a)
|
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
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
event.SuccessMsg(i18n.MsgAlbumDeleted, txt.Quote(a.AlbumTitle))
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, a)
|
2019-12-03 21:25:40 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
id := 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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
id := 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
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
a, err := query.AlbumByUID(c.Param("uid"))
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
photos, err := query.AlbumPhotos(cloneAlbum, 10000)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("album: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
added = append(added, a.AddPhotos(photos.UIDs())...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(added) > 0 {
|
2020-07-04 12:54:35 +02:00
|
|
|
event.SuccessMsg(i18n.MsgSelectionAddedTo, txt.Quote(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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
uid := c.Param("uid")
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
photos, err := query.PhotoSelection(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 {
|
2020-07-04 12:54:35 +02:00
|
|
|
event.SuccessMsg(i18n.MsgEntryAddedTo, txt.Quote(a.Title()))
|
2020-05-30 01:41:47 +02:00
|
|
|
} else {
|
2020-07-04 12:54:35 +02:00
|
|
|
event.SuccessMsg(i18n.MsgEntriesAddedTo, len(added), txt.Quote(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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
a, err := query.AlbumByUID(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 {
|
2020-07-04 12:54:35 +02:00
|
|
|
event.SuccessMsg(i18n.MsgEntryRemovedFrom, txt.Quote(a.Title()))
|
2020-06-01 09:45:24 +02:00
|
|
|
} else {
|
2020-07-04 12:54:35 +02:00
|
|
|
event.SuccessMsg(i18n.MsgEntriesRemovedFrom, len(removed), txt.Quote(txt.Quote(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
|
|
|
|
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()
|
2020-05-23 20:58:58 +02:00
|
|
|
a, err := query.AlbumByUID(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
|
|
|
|
}
|
|
|
|
|
2021-01-27 21:30:10 +01:00
|
|
|
files, err := query.AlbumPhotos(a, 10000)
|
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-01-27 21:30:10 +01:00
|
|
|
albumName := strings.Title(a.AlbumSlug)
|
|
|
|
|
|
|
|
if len(albumName) < 2 {
|
|
|
|
albumName = fmt.Sprintf("photoprism-album-%s", a.AlbumUID)
|
|
|
|
}
|
|
|
|
|
|
|
|
zipFileName := fmt.Sprintf("%s.zip", albumName)
|
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 == "" {
|
|
|
|
log.Warnf("download: empty file hash, skipped %s", txt.Quote(file.FileName))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if file.FileSidecar {
|
|
|
|
log.Debugf("download: skipped sidecar %s", txt.Quote(file.FileName))
|
|
|
|
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-01-27 21:30:10 +01:00
|
|
|
log.Infof("download: added %s as %s", txt.Quote(file.FileName), txt.Quote(alias))
|
2019-12-06 16:47:30 +01:00
|
|
|
} else {
|
2021-01-27 21:30:10 +01:00
|
|
|
log.Errorf("download: file %s is missing", txt.Quote(file.FileName))
|
2019-12-06 16:47:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-27 21:30:10 +01:00
|
|
|
log.Infof("download: album zip %s created in %s", txt.Quote(zipFileName), time.Since(start))
|
2019-12-06 16:47:30 +01:00
|
|
|
})
|
|
|
|
}
|