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"
|
|
|
|
"github.com/photoprism/photoprism/internal/models"
|
|
|
|
"github.com/photoprism/photoprism/internal/util"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BatchParams struct {
|
|
|
|
Ids []int `json:"ids"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// POST /api/v1/batch/photos/delete
|
|
|
|
func BatchPhotosDelete(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.POST("/batch/photos/delete", func(c *gin.Context) {
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
var params BatchParams
|
|
|
|
|
|
|
|
if err := c.BindJSON(¶ms); err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": util.UcFirst(err.Error())})
|
2019-06-18 06:37:10 +02:00
|
|
|
return
|
2019-06-15 01:35:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(params.Ids) == 0 {
|
|
|
|
log.Error("no photos selected")
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": util.UcFirst("no photos selected")})
|
2019-06-18 06:37:10 +02:00
|
|
|
return
|
2019-06-15 01:35:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("deleting photos: %#v", params.Ids)
|
|
|
|
|
|
|
|
db := conf.Db()
|
|
|
|
|
|
|
|
db.Where("id IN (?)", params.Ids).Delete(&models.Photo{})
|
|
|
|
db.Where("photo_id IN (?)", params.Ids).Delete(&models.File{})
|
|
|
|
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("photos deleted in %s", elapsed)})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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-06-15 01:35:18 +02:00
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
var params BatchParams
|
|
|
|
|
|
|
|
if err := c.BindJSON(¶ms); err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": util.UcFirst(err.Error())})
|
2019-06-18 06:37:10 +02:00
|
|
|
return
|
2019-06-15 01:35:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(params.Ids) == 0 {
|
|
|
|
log.Error("no photos selected")
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": util.UcFirst("no photos selected")})
|
2019-06-18 06:37:10 +02:00
|
|
|
return
|
2019-06-15 01:35:18 +02:00
|
|
|
}
|
|
|
|
|
2019-06-15 03:44:10 +02:00
|
|
|
log.Infof("marking photos as private: %#v", params.Ids)
|
2019-06-15 01:35:18 +02:00
|
|
|
|
|
|
|
db := conf.Db()
|
|
|
|
|
2019-06-18 07:28:30 +02:00
|
|
|
db.Model(models.Photo{}).Where("id IN (?)", params.Ids).UpdateColumn("photo_private", gorm.Expr("IF (`photo_private`, 0, 1)"))
|
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) {
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
var params BatchParams
|
|
|
|
|
|
|
|
if err := c.BindJSON(¶ms); err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": util.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(params.Ids) == 0 {
|
|
|
|
log.Error("no photos selected")
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": util.UcFirst("no photos selected")})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("marking photos as story: %#v", params.Ids)
|
|
|
|
|
|
|
|
db := conf.Db()
|
|
|
|
|
|
|
|
db.Model(models.Photo{}).Where("id IN (?)", params.Ids).Updates(map[string]interface{}{
|
|
|
|
"photo_story": gorm.Expr("IF (`photo_story`, 0, 1)"),
|
|
|
|
"photo_private": "0",
|
|
|
|
})
|
|
|
|
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("photos marked as story in %s", elapsed)})
|
|
|
|
})
|
|
|
|
}
|