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