2019-06-15 01:35:18 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-11-21 18:08:41 +01:00
|
|
|
"net/http"
|
2023-01-19 21:11:17 +01:00
|
|
|
"path"
|
2022-07-22 19:18:42 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/dustin/go-humanize/english"
|
2020-11-21 18:08:41 +01:00
|
|
|
|
2020-07-07 10:51:55 +02:00
|
|
|
"github.com/gin-gonic/gin"
|
2019-06-18 07:28:30 +02:00
|
|
|
"github.com/jinzhu/gorm"
|
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-06 16:47:30 +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-07 10:51:55 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
2021-12-14 18:34:52 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
2020-04-24 18:19:18 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2019-06-15 01:35:18 +02:00
|
|
|
)
|
|
|
|
|
2021-08-30 18:58:27 +02:00
|
|
|
// BatchPhotosArchive moves multiple photos to the archive.
|
|
|
|
//
|
2020-01-09 02:09:54 +01:00
|
|
|
// POST /api/v1/batch/photos/archive
|
2020-06-25 14:54:04 +02:00
|
|
|
func BatchPhotosArchive(router *gin.RouterGroup) {
|
2020-01-09 02:09:54 +01:00
|
|
|
router.POST("/batch/photos/archive", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourcePhotos, acl.ActionDelete)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2019-11-11 21:10:41 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-04 05:18:22 +01:00
|
|
|
var f form.Selection
|
2019-06-15 01:35:18 +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-15 01:35:18 +02:00
|
|
|
}
|
|
|
|
|
2019-12-05 19:21:35 +01:00
|
|
|
if len(f.Photos) == 0 {
|
2020-07-07 10:51:55 +02:00
|
|
|
Abort(c, http.StatusBadRequest, i18n.ErrNoItemsSelected)
|
2019-06-18 06:37:10 +02:00
|
|
|
return
|
2019-06-15 01:35:18 +02:00
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Infof("photos: archiving %s", clean.Log(f.String()))
|
2019-06-15 01:35:18 +02:00
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
if get.Config().BackupYaml() {
|
2022-02-27 17:32:54 +01:00
|
|
|
// Fetch selection from index.
|
|
|
|
photos, err := query.SelectedPhotos(f)
|
2020-05-09 11:00:22 +02:00
|
|
|
|
2021-02-05 16:32:08 +01:00
|
|
|
if err != nil {
|
|
|
|
AbortEntityNotFound(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range photos {
|
|
|
|
if err := p.Archive(); err != nil {
|
|
|
|
log.Errorf("archive: %s", err)
|
|
|
|
} else {
|
|
|
|
SavePhotoAsYaml(p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if err := entity.Db().Where("photo_uid IN (?)", f.Photos).Delete(&entity.Photo{}).Error; err != nil {
|
|
|
|
log.Errorf("archive: %s", err)
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortSaveFailed(c)
|
2020-05-09 11:00:22 +02:00
|
|
|
return
|
2022-04-04 08:54:03 +02:00
|
|
|
} else if err := entity.Db().Model(&entity.PhotoAlbum{}).Where("photo_uid IN (?)", f.Photos).UpdateColumn("hidden", true).Error; err != nil {
|
2021-02-05 16:32:08 +01:00
|
|
|
log.Errorf("archive: %s", err)
|
2020-05-09 11:00:22 +02:00
|
|
|
}
|
2019-06-15 01:35:18 +02:00
|
|
|
|
2021-09-23 16:06:59 +02:00
|
|
|
// Update precalculated photo and file counts.
|
2021-10-02 14:24:44 +02:00
|
|
|
logWarn("index", entity.UpdateCounts())
|
2021-09-23 16:06:59 +02:00
|
|
|
|
2021-10-01 16:44:50 +02:00
|
|
|
// Update album, subject, and label cover thumbs.
|
2021-10-02 14:24:44 +02:00
|
|
|
logWarn("index", query.UpdateCovers())
|
2019-06-15 01:35:18 +02:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
UpdateClientConfig()
|
2019-12-11 04:12:54 +01:00
|
|
|
|
2020-02-04 11:38:43 +01:00
|
|
|
event.EntitiesArchived("photos", f.Photos)
|
2020-01-30 18:19:26 +01:00
|
|
|
|
2020-07-07 10:51:55 +02:00
|
|
|
c.JSON(http.StatusOK, i18n.NewResponse(http.StatusOK, i18n.MsgSelectionArchived))
|
2019-12-06 16:47:30 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-30 18:58:27 +02:00
|
|
|
// BatchPhotosRestore restores multiple photos from the archive.
|
|
|
|
//
|
2021-02-05 16:32:08 +01:00
|
|
|
// POST /api/v1/batch/photos/restore
|
|
|
|
func BatchPhotosRestore(router *gin.RouterGroup) {
|
|
|
|
router.POST("/batch/photos/restore", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourcePhotos, acl.ActionDelete)
|
2020-11-21 17:36:41 +01:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2020-11-21 17:36:41 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var f form.Selection
|
|
|
|
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
|
|
|
AbortBadRequest(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(f.Photos) == 0 {
|
|
|
|
Abort(c, http.StatusBadRequest, i18n.ErrNoItemsSelected)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Infof("photos: restoring %s", clean.Log(f.String()))
|
2020-11-21 17:36:41 +01:00
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
if get.Config().BackupYaml() {
|
2022-02-27 17:32:54 +01:00
|
|
|
// Fetch selection from index.
|
|
|
|
photos, err := query.SelectedPhotos(f)
|
2020-11-21 17:36:41 +01:00
|
|
|
|
2021-02-05 16:32:08 +01:00
|
|
|
if err != nil {
|
|
|
|
AbortEntityNotFound(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range photos {
|
|
|
|
if err := p.Restore(); err != nil {
|
|
|
|
log.Errorf("restore: %s", err)
|
|
|
|
} else {
|
|
|
|
SavePhotoAsYaml(p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if err := entity.Db().Unscoped().Model(&entity.Photo{}).Where("photo_uid IN (?)", f.Photos).
|
2022-04-04 08:54:03 +02:00
|
|
|
UpdateColumn("deleted_at", gorm.Expr("NULL")).Error; err != nil {
|
2021-02-05 16:32:08 +01:00
|
|
|
log.Errorf("restore: %s", err)
|
|
|
|
AbortSaveFailed(c)
|
2020-11-21 17:36:41 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-23 16:06:59 +02:00
|
|
|
// Update precalculated photo and file counts.
|
2021-10-02 14:24:44 +02:00
|
|
|
logWarn("index", entity.UpdateCounts())
|
2021-09-23 16:06:59 +02:00
|
|
|
|
2021-10-01 16:44:50 +02:00
|
|
|
// Update album, subject, and label cover thumbs.
|
2021-10-02 14:24:44 +02:00
|
|
|
logWarn("index", query.UpdateCovers())
|
2020-11-21 17:36:41 +01:00
|
|
|
|
|
|
|
UpdateClientConfig()
|
|
|
|
|
2021-02-05 16:32:08 +01:00
|
|
|
event.EntitiesRestored("photos", f.Photos)
|
2020-11-21 17:36:41 +01:00
|
|
|
|
2021-02-05 16:32:08 +01:00
|
|
|
c.JSON(http.StatusOK, i18n.NewResponse(http.StatusOK, i18n.MsgSelectionRestored))
|
2020-11-21 17:36:41 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-30 18:58:27 +02:00
|
|
|
// BatchPhotosApprove approves multiple photos that are currently under review.
|
|
|
|
//
|
2021-02-05 16:32:08 +01:00
|
|
|
// POST /api/v1/batch/photos/approve
|
|
|
|
func BatchPhotosApprove(router *gin.RouterGroup) {
|
|
|
|
router.POST("batch/photos/approve", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourcePhotos, acl.ActionUpdate)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2020-01-06 05:45:03 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-04 05:18:22 +01:00
|
|
|
var f form.Selection
|
2020-01-06 05:45:03 +01:00
|
|
|
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortBadRequest(c)
|
2020-01-06 05:45:03 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(f.Photos) == 0 {
|
2020-07-07 10:51:55 +02:00
|
|
|
Abort(c, http.StatusBadRequest, i18n.ErrNoItemsSelected)
|
2020-01-06 05:45:03 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Infof("photos: approving %s", clean.Log(f.String()))
|
2020-01-06 05:45:03 +01:00
|
|
|
|
2022-02-27 17:32:54 +01:00
|
|
|
// Fetch selection from index.
|
|
|
|
photos, err := query.SelectedPhotos(f)
|
2020-05-09 11:00:22 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2021-02-05 16:32:08 +01:00
|
|
|
AbortEntityNotFound(c)
|
2020-05-09 11:00:22 +02:00
|
|
|
return
|
|
|
|
}
|
2020-01-06 05:45:03 +01:00
|
|
|
|
2021-02-05 16:32:08 +01:00
|
|
|
var approved entity.Photos
|
|
|
|
|
|
|
|
for _, p := range photos {
|
2023-02-03 08:23:46 +01:00
|
|
|
if err = p.Approve(); err != nil {
|
2021-02-05 16:32:08 +01:00
|
|
|
log.Errorf("approve: %s", err)
|
|
|
|
} else {
|
|
|
|
approved = append(approved, p)
|
|
|
|
SavePhotoAsYaml(p)
|
|
|
|
}
|
2020-05-08 16:01:34 +02:00
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
UpdateClientConfig()
|
2020-01-06 05:45:03 +01:00
|
|
|
|
2021-02-05 16:32:08 +01:00
|
|
|
event.EntitiesUpdated("photos", approved)
|
2020-01-30 18:19:26 +01:00
|
|
|
|
2021-02-05 16:32:08 +01:00
|
|
|
c.JSON(http.StatusOK, i18n.NewResponse(http.StatusOK, i18n.MsgSelectionApproved))
|
2020-01-06 05:45:03 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-05 22:33:29 +02:00
|
|
|
// BatchAlbumsDelete permanently removes multiple albums.
|
2021-08-30 18:58:27 +02:00
|
|
|
//
|
2019-12-06 16:47:30 +01:00
|
|
|
// POST /api/v1/batch/albums/delete
|
2020-06-25 14:54:04 +02:00
|
|
|
func BatchAlbumsDelete(router *gin.RouterGroup) {
|
2019-12-06 16:47:30 +01:00
|
|
|
router.POST("/batch/albums/delete", 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 16:47:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-04 05:18:22 +01:00
|
|
|
var f form.Selection
|
2019-12-06 16:47:30 +01:00
|
|
|
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortBadRequest(c)
|
2019-12-06 16:47:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(f.Albums) == 0 {
|
2020-07-07 10:51:55 +02:00
|
|
|
Abort(c, http.StatusBadRequest, i18n.ErrNoAlbumsSelected)
|
2019-12-06 16:47:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Infof("albums: deleting %s", clean.Log(f.String()))
|
2019-12-06 16:47:30 +01:00
|
|
|
|
2022-06-16 18:59:03 +02:00
|
|
|
// Soft delete albums, can be restored.
|
2020-05-23 20:58:58 +02:00
|
|
|
entity.Db().Where("album_uid IN (?)", f.Albums).Delete(&entity.Album{})
|
2022-06-16 18:59:03 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
KEEP ENTRIES AS ALBUMS MAY NOW BE RESTORED BY NAME
|
|
|
|
entity.Db().Where("album_uid IN (?)", f.Albums).Delete(&entity.PhotoAlbum{})
|
|
|
|
*/
|
2019-12-06 16:47:30 +01:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
UpdateClientConfig()
|
2019-06-15 01:35:18 +02:00
|
|
|
|
2020-02-04 11:38:43 +01:00
|
|
|
event.EntitiesDeleted("albums", f.Albums)
|
2020-01-30 18:19:26 +01:00
|
|
|
|
2020-07-07 10:51:55 +02:00
|
|
|
c.JSON(http.StatusOK, i18n.NewResponse(http.StatusOK, i18n.MsgAlbumsDeleted))
|
2019-06-15 01:35:18 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-30 18:58:27 +02:00
|
|
|
// BatchPhotosPrivate flags multiple photos as private.
|
|
|
|
//
|
2019-06-15 03:44:10 +02:00
|
|
|
// POST /api/v1/batch/photos/private
|
2020-06-25 14:54:04 +02:00
|
|
|
func BatchPhotosPrivate(router *gin.RouterGroup) {
|
2019-06-15 03:44:10 +02:00
|
|
|
router.POST("/batch/photos/private", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourcePhotos, acl.AccessPrivate)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2019-11-11 21:10:41 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-04 05:18:22 +01:00
|
|
|
var f form.Selection
|
2019-06-15 01:35:18 +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-15 01:35:18 +02:00
|
|
|
}
|
|
|
|
|
2019-12-05 19:21:35 +01:00
|
|
|
if len(f.Photos) == 0 {
|
2020-07-07 10:51:55 +02:00
|
|
|
Abort(c, http.StatusBadRequest, i18n.ErrNoItemsSelected)
|
2019-06-18 06:37:10 +02:00
|
|
|
return
|
2019-06-15 01:35:18 +02:00
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Infof("photos: updating private flag for %s", clean.Log(f.String()))
|
2020-04-24 18:19:18 +02:00
|
|
|
|
2022-04-04 08:54:03 +02:00
|
|
|
if err := entity.Db().Model(entity.Photo{}).Where("photo_uid IN (?)", f.Photos).UpdateColumn("photo_private",
|
2021-02-05 16:32:08 +01:00
|
|
|
gorm.Expr("CASE WHEN photo_private > 0 THEN 0 ELSE 1 END")).Error; err != nil {
|
|
|
|
log.Errorf("private: %s", err)
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortSaveFailed(c)
|
2020-04-24 18:19:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-23 16:06:59 +02:00
|
|
|
// Update precalculated photo and file counts.
|
2021-10-02 14:24:44 +02:00
|
|
|
logWarn("index", entity.UpdateCounts())
|
2020-05-09 11:00:22 +02:00
|
|
|
|
2022-02-27 17:32:54 +01:00
|
|
|
// Fetch selection from index.
|
|
|
|
if photos, err := query.SelectedPhotos(f); err == nil {
|
2021-02-05 16:32:08 +01:00
|
|
|
for _, p := range photos {
|
|
|
|
SavePhotoAsYaml(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
event.EntitiesUpdated("photos", photos)
|
2020-04-24 18:19:18 +02:00
|
|
|
}
|
2019-06-15 01:35:18 +02:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
UpdateClientConfig()
|
2020-04-29 12:42:52 +02:00
|
|
|
|
2021-01-08 12:20:41 +01:00
|
|
|
FlushCoverCache()
|
|
|
|
|
2020-07-07 10:51:55 +02:00
|
|
|
c.JSON(http.StatusOK, i18n.NewResponse(http.StatusOK, i18n.MsgSelectionProtected))
|
2019-06-15 01:35:18 +02:00
|
|
|
})
|
|
|
|
}
|
2019-06-18 07:28:30 +02:00
|
|
|
|
2021-08-30 18:58:27 +02:00
|
|
|
// BatchLabelsDelete deletes multiple labels.
|
|
|
|
//
|
2020-02-04 05:18:22 +01:00
|
|
|
// POST /api/v1/batch/labels/delete
|
2020-06-25 14:54:04 +02:00
|
|
|
func BatchLabelsDelete(router *gin.RouterGroup) {
|
2020-02-04 05:18:22 +01:00
|
|
|
router.POST("/batch/labels/delete", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourceLabels, acl.ActionDelete)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2020-02-04 05:18:22 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var f form.Selection
|
|
|
|
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortBadRequest(c)
|
2020-02-04 05:18:22 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(f.Labels) == 0 {
|
|
|
|
log.Error("no labels selected")
|
2020-07-07 10:51:55 +02:00
|
|
|
Abort(c, http.StatusBadRequest, i18n.ErrNoLabelsSelected)
|
2020-02-04 05:18:22 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Infof("labels: deleting %s", clean.Log(f.String()))
|
2020-02-04 05:18:22 +01:00
|
|
|
|
2020-06-02 17:57:12 +02:00
|
|
|
var labels entity.Labels
|
|
|
|
|
|
|
|
if err := entity.Db().Where("label_uid IN (?)", f.Labels).Find(&labels).Error; err != nil {
|
2020-07-07 10:51:55 +02:00
|
|
|
Error(c, http.StatusInternalServerError, err, i18n.ErrDeleteFailed)
|
2020-06-02 17:57:12 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, label := range labels {
|
|
|
|
logError("labels", label.Delete())
|
|
|
|
}
|
2020-02-04 05:18:22 +01:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
UpdateClientConfig()
|
2020-02-04 05:18:22 +01:00
|
|
|
|
2020-02-04 11:38:43 +01:00
|
|
|
event.EntitiesDeleted("labels", f.Labels)
|
2020-02-04 05:18:22 +01:00
|
|
|
|
2020-07-07 10:51:55 +02:00
|
|
|
c.JSON(http.StatusOK, i18n.NewResponse(http.StatusOK, i18n.MsgLabelsDeleted))
|
2020-02-04 05:18:22 +01:00
|
|
|
})
|
|
|
|
}
|
2021-01-24 15:28:59 +01:00
|
|
|
|
2021-10-05 22:33:29 +02:00
|
|
|
// BatchPhotosDelete permanently removes multiple photos from the archive.
|
2021-08-30 18:58:27 +02:00
|
|
|
//
|
2021-01-24 15:28:59 +01:00
|
|
|
// POST /api/v1/batch/photos/delete
|
|
|
|
func BatchPhotosDelete(router *gin.RouterGroup) {
|
|
|
|
router.POST("/batch/photos/delete", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourcePhotos, acl.ActionDelete)
|
2021-01-24 15:28:59 +01:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2021-01-24 15:28:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
conf := get.Config()
|
2021-01-24 15:28:59 +01:00
|
|
|
|
|
|
|
if conf.ReadOnly() || !conf.Settings().Features.Delete {
|
|
|
|
AbortFeatureDisabled(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var f form.Selection
|
|
|
|
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
|
|
|
AbortBadRequest(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(f.Photos) == 0 {
|
|
|
|
Abort(c, http.StatusBadRequest, i18n.ErrNoItemsSelected)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Infof("photos: deleting %s", clean.Log(f.String()))
|
2021-01-24 15:28:59 +01:00
|
|
|
|
2022-07-22 19:18:42 +02:00
|
|
|
// Fetch selection from index and record time.
|
|
|
|
deleteStart := time.Now()
|
2022-02-27 17:32:54 +01:00
|
|
|
photos, err := query.SelectedPhotos(f)
|
2021-01-24 15:28:59 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
AbortEntityNotFound(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var deleted entity.Photos
|
|
|
|
|
2022-07-22 19:18:42 +02:00
|
|
|
var numFiles = 0
|
|
|
|
|
2021-01-24 15:28:59 +01:00
|
|
|
// Delete photos.
|
|
|
|
for _, p := range photos {
|
2023-01-19 21:11:17 +01:00
|
|
|
// Report file deletion.
|
|
|
|
event.AuditWarn([]string{ClientIP(c), s.UserName, "delete", path.Join(p.PhotoPath, p.PhotoName+"*")})
|
|
|
|
|
|
|
|
// Remove all related files from storage.
|
2022-07-22 19:18:42 +02:00
|
|
|
n, err := photoprism.DeletePhoto(p, true, true)
|
|
|
|
|
|
|
|
numFiles += n
|
|
|
|
|
|
|
|
if err != nil {
|
2021-02-05 16:32:08 +01:00
|
|
|
log.Errorf("delete: %s", err)
|
2021-01-24 15:28:59 +01:00
|
|
|
} else {
|
|
|
|
deleted = append(deleted, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-22 19:18:42 +02:00
|
|
|
if numFiles > 0 {
|
|
|
|
log.Infof("delete: removed %s [%s]", english.Plural(numFiles, "file", "files"), time.Since(deleteStart))
|
|
|
|
}
|
|
|
|
|
2021-09-23 16:06:59 +02:00
|
|
|
// Any photos deleted?
|
2021-01-24 15:28:59 +01:00
|
|
|
if len(deleted) > 0 {
|
2021-09-23 16:06:59 +02:00
|
|
|
// Update precalculated photo and file counts.
|
2021-10-02 14:24:44 +02:00
|
|
|
logWarn("index", entity.UpdateCounts())
|
2021-01-24 15:28:59 +01:00
|
|
|
|
|
|
|
UpdateClientConfig()
|
|
|
|
|
|
|
|
event.EntitiesDeleted("photos", deleted.UIDs())
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, i18n.NewResponse(http.StatusOK, i18n.MsgPermanentlyDeleted))
|
|
|
|
})
|
|
|
|
}
|