2019-06-14 23:35:18 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2019-06-18 05:28:30 +00:00
|
|
|
"github.com/jinzhu/gorm"
|
2019-06-14 23:35:18 +00:00
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2019-12-11 15:55:18 +00:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2019-12-06 15:47:30 +00:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2019-12-05 18:21:35 +00:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2020-04-24 16:19:18 +00:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2020-01-12 13:00:56 +00:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2019-06-14 23:35:18 +00:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2020-01-09 01:09:54 +00:00
|
|
|
// POST /api/v1/batch/photos/archive
|
|
|
|
func BatchPhotosArchive(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.POST("/batch/photos/archive", func(c *gin.Context) {
|
2019-11-11 20:10:41 +00:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-14 23:35:18 +00:00
|
|
|
start := time.Now()
|
|
|
|
|
2020-02-04 04:18:22 +00:00
|
|
|
var f form.Selection
|
2019-06-14 23:35:18 +00:00
|
|
|
|
2019-12-05 18:21:35 +00:00
|
|
|
if err := c.BindJSON(&f); err != nil {
|
2020-01-07 16:36:49 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": txt.UcFirst(err.Error())})
|
2019-06-18 04:37:10 +00:00
|
|
|
return
|
2019-06-14 23:35:18 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 18:21:35 +00:00
|
|
|
if len(f.Photos) == 0 {
|
2019-06-14 23:35:18 +00:00
|
|
|
log.Error("no photos selected")
|
2020-01-07 16:36:49 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": txt.UcFirst("no photos selected")})
|
2019-06-18 04:37:10 +00:00
|
|
|
return
|
2019-06-14 23:35:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 08:42:42 +00:00
|
|
|
log.Infof("photos: archiving %#v", f.Photos)
|
2019-06-14 23:35:18 +00:00
|
|
|
|
2020-05-09 09:00:22 +00:00
|
|
|
err := entity.Db().Where("photo_uuid IN (?)", f.Photos).Delete(&entity.Photo{}).Error
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, ErrSaveFailed)
|
|
|
|
return
|
|
|
|
}
|
2019-06-14 23:35:18 +00:00
|
|
|
|
2020-05-11 09:01:28 +00:00
|
|
|
if err := entity.UpdatePhotoCounts(); err != nil {
|
2020-05-08 13:41:01 +00:00
|
|
|
log.Errorf("photos: %s", err)
|
|
|
|
}
|
2019-06-14 23:35:18 +00:00
|
|
|
|
2019-12-06 15:47:30 +00:00
|
|
|
elapsed := int(time.Since(start).Seconds())
|
|
|
|
|
2020-01-06 04:45:03 +00:00
|
|
|
event.Publish("config.updated", event.Data(conf.ClientConfig()))
|
2019-12-11 03:12:54 +00:00
|
|
|
|
2020-02-04 10:38:43 +00:00
|
|
|
event.EntitiesArchived("photos", f.Photos)
|
2020-01-30 17:19:26 +00:00
|
|
|
|
2020-01-09 01:09:54 +00:00
|
|
|
c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("photos archived in %d s", elapsed)})
|
2019-12-06 15:47:30 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-06 04:45:03 +00:00
|
|
|
// POST /api/v1/batch/photos/restore
|
|
|
|
func BatchPhotosRestore(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.POST("/batch/photos/restore", func(c *gin.Context) {
|
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
|
2020-02-04 04:18:22 +00:00
|
|
|
var f form.Selection
|
2020-01-06 04:45:03 +00:00
|
|
|
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
2020-01-07 16:36:49 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": txt.UcFirst(err.Error())})
|
2020-01-06 04:45:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(f.Photos) == 0 {
|
|
|
|
log.Error("no photos selected")
|
2020-01-07 16:36:49 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": txt.UcFirst("no photos selected")})
|
2020-01-06 04:45:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("restoring photos: %#v", f.Photos)
|
|
|
|
|
2020-05-09 09:00:22 +00:00
|
|
|
err := entity.Db().Unscoped().Model(&entity.Photo{}).Where("photo_uuid IN (?)", f.Photos).
|
|
|
|
UpdateColumn("deleted_at", gorm.Expr("NULL")).Error
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, ErrSaveFailed)
|
|
|
|
return
|
|
|
|
}
|
2020-01-06 04:45:03 +00:00
|
|
|
|
2020-05-11 09:01:28 +00:00
|
|
|
if err := entity.UpdatePhotoCounts(); err != nil {
|
2020-05-08 14:01:34 +00:00
|
|
|
log.Errorf("photos: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-01-06 04:45:03 +00:00
|
|
|
elapsed := int(time.Since(start).Seconds())
|
|
|
|
|
|
|
|
event.Publish("config.updated", event.Data(conf.ClientConfig()))
|
|
|
|
|
2020-02-04 10:38:43 +00:00
|
|
|
event.EntitiesRestored("photos", f.Photos)
|
2020-01-30 17:19:26 +00:00
|
|
|
|
2020-01-06 04:45:03 +00:00
|
|
|
c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("photos restored in %d s", elapsed)})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-06 15:47:30 +00:00
|
|
|
// POST /api/v1/batch/albums/delete
|
|
|
|
func BatchAlbumsDelete(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.POST("/batch/albums/delete", func(c *gin.Context) {
|
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-04 04:18:22 +00:00
|
|
|
var f form.Selection
|
2019-12-06 15:47:30 +00:00
|
|
|
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
2020-01-07 16:36:49 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": txt.UcFirst(err.Error())})
|
2019-12-06 15:47:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(f.Albums) == 0 {
|
|
|
|
log.Error("no albums selected")
|
2020-01-07 16:36:49 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": txt.UcFirst("no albums selected")})
|
2019-12-06 15:47:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-07 08:42:42 +00:00
|
|
|
log.Infof("albums: deleting %#v", f.Albums)
|
2019-12-06 15:47:30 +00:00
|
|
|
|
2020-05-08 13:41:01 +00:00
|
|
|
entity.Db().Where("album_uuid IN (?)", f.Albums).Delete(&entity.Album{})
|
|
|
|
entity.Db().Where("album_uuid IN (?)", f.Albums).Delete(&entity.PhotoAlbum{})
|
2019-12-06 15:47:30 +00:00
|
|
|
|
|
|
|
event.Publish("config.updated", event.Data(conf.ClientConfig()))
|
2019-06-14 23:35:18 +00:00
|
|
|
|
2020-02-04 10:38:43 +00:00
|
|
|
event.EntitiesDeleted("albums", f.Albums)
|
2020-01-30 17:19:26 +00:00
|
|
|
|
2019-12-06 15:47:30 +00:00
|
|
|
c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("albums deleted")})
|
2019-06-14 23:35:18 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-15 01:44:10 +00:00
|
|
|
// POST /api/v1/batch/photos/private
|
|
|
|
func BatchPhotosPrivate(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.POST("/batch/photos/private", func(c *gin.Context) {
|
2019-11-11 20:10:41 +00:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-14 23:35:18 +00:00
|
|
|
start := time.Now()
|
|
|
|
|
2020-02-04 04:18:22 +00:00
|
|
|
var f form.Selection
|
2019-06-14 23:35:18 +00:00
|
|
|
|
2019-12-05 18:21:35 +00:00
|
|
|
if err := c.BindJSON(&f); err != nil {
|
2020-01-07 16:36:49 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": txt.UcFirst(err.Error())})
|
2019-06-18 04:37:10 +00:00
|
|
|
return
|
2019-06-14 23:35:18 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 18:21:35 +00:00
|
|
|
if len(f.Photos) == 0 {
|
2019-06-14 23:35:18 +00:00
|
|
|
log.Error("no photos selected")
|
2020-01-07 16:36:49 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": txt.UcFirst("no photos selected")})
|
2019-06-18 04:37:10 +00:00
|
|
|
return
|
2019-06-14 23:35:18 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 18:21:35 +00:00
|
|
|
log.Infof("marking photos as private: %#v", f.Photos)
|
2019-06-14 23:35:18 +00:00
|
|
|
|
2020-05-08 13:41:01 +00:00
|
|
|
err := entity.Db().Model(entity.Photo{}).Where("photo_uuid IN (?)", f.Photos).UpdateColumn("photo_private", gorm.Expr("IF (`photo_private`, 0, 1)")).Error
|
2020-04-24 16:19:18 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, ErrSaveFailed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-11 09:01:28 +00:00
|
|
|
if err := entity.UpdatePhotoCounts(); err != nil {
|
2020-05-09 09:00:22 +00:00
|
|
|
log.Errorf("photos: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-05-08 13:41:01 +00:00
|
|
|
if entities, err := query.PhotoSelection(f); err == nil {
|
2020-04-24 16:19:18 +00:00
|
|
|
event.EntitiesUpdated("photos", entities)
|
|
|
|
}
|
2019-06-14 23:35:18 +00:00
|
|
|
|
2020-04-29 10:42:52 +00:00
|
|
|
event.Publish("config.updated", event.Data(conf.ClientConfig()))
|
|
|
|
|
2019-06-14 23:35:18 +00:00
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
2019-06-15 01:44:10 +00:00
|
|
|
c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("photos marked as private in %s", elapsed)})
|
2019-06-14 23:35:18 +00:00
|
|
|
})
|
|
|
|
}
|
2019-06-18 05:28:30 +00:00
|
|
|
|
2020-02-04 04:18:22 +00:00
|
|
|
// POST /api/v1/batch/labels/delete
|
|
|
|
func BatchLabelsDelete(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.POST("/batch/labels/delete", func(c *gin.Context) {
|
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var f form.Selection
|
|
|
|
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": txt.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(f.Labels) == 0 {
|
|
|
|
log.Error("no labels selected")
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": txt.UcFirst("no labels selected")})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-07 08:42:42 +00:00
|
|
|
log.Infof("labels: deleting %#v", f.Labels)
|
2020-02-04 04:18:22 +00:00
|
|
|
|
2020-05-08 13:41:01 +00:00
|
|
|
entity.Db().Where("label_uuid IN (?)", f.Labels).Delete(&entity.Label{})
|
2020-02-04 04:18:22 +00:00
|
|
|
|
|
|
|
event.Publish("config.updated", event.Data(conf.ClientConfig()))
|
|
|
|
|
2020-02-04 10:38:43 +00:00
|
|
|
event.EntitiesDeleted("labels", f.Labels)
|
2020-02-04 04:18:22 +00:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("labels deleted")})
|
|
|
|
})
|
|
|
|
}
|