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"
|
2020-02-02 00:31:09 +01:00
|
|
|
"io/ioutil"
|
2019-06-17 21:45:06 +02:00
|
|
|
"net/http"
|
2019-12-06 16:47:30 +01:00
|
|
|
"os"
|
|
|
|
"path"
|
2019-06-17 21:45:06 +02:00
|
|
|
"strconv"
|
2019-12-06 16:47:30 +01:00
|
|
|
"strings"
|
|
|
|
"time"
|
2019-06-17 21:45:06 +02:00
|
|
|
|
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-04-30 20:07:03 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
2020-01-06 14:32:15 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/thumb"
|
2020-01-13 11:07:09 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
2019-06-18 06:37:10 +02:00
|
|
|
|
2019-06-17 21:45:06 +02:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/gin-gonic/gin/binding"
|
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2020-01-12 14:00:56 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2019-06-17 21:45:06 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// GET /api/v1/albums
|
|
|
|
func GetAlbums(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.GET("/albums", func(c *gin.Context) {
|
2020-01-22 13:43:07 +01:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-05 19:21:35 +01:00
|
|
|
var f form.AlbumSearch
|
2019-06-17 21:45:06 +02:00
|
|
|
|
2020-04-30 20:07:03 +02:00
|
|
|
q := service.Query()
|
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-01-07 17:36:49 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": txt.UcFirst(err.Error())})
|
2019-06-17 21:45:06 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-05 14:18:40 +01:00
|
|
|
result, err := q.Albums(f)
|
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
|
|
|
|
}
|
|
|
|
|
2020-04-20 20:07:58 +02:00
|
|
|
// TODO c.Header("X-Count", strconv.Itoa(count))
|
|
|
|
c.Header("X-Limit", strconv.Itoa(f.Count))
|
|
|
|
c.Header("X-Offset", strconv.Itoa(f.Offset))
|
2019-06-17 21:45:06 +02:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, result)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-05 14:11:45 +01:00
|
|
|
// GET /api/v1/albums/:uuid
|
|
|
|
func GetAlbum(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.GET("/albums/:uuid", func(c *gin.Context) {
|
|
|
|
id := c.Param("uuid")
|
2020-04-30 20:07:03 +02:00
|
|
|
q := service.Query()
|
2020-03-28 15:29:17 +01:00
|
|
|
m, err := q.AlbumByUUID(id)
|
2019-12-05 14:11:45 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-01-23 10:16:18 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrAlbumNotFound)
|
2019-12-05 14:11:45 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, m)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-17 21:45:06 +02:00
|
|
|
// POST /api/v1/albums
|
|
|
|
func CreateAlbum(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.POST("/albums", func(c *gin.Context) {
|
2019-11-12 05:49:10 +01:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
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-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-17 21:45:06 +02:00
|
|
|
}
|
|
|
|
|
2020-04-30 20:07:03 +02:00
|
|
|
q := service.Query()
|
2019-12-11 16:55:18 +01:00
|
|
|
m := entity.NewAlbum(f.AlbumName)
|
2019-12-17 04:39:23 +01:00
|
|
|
m.AlbumFavorite = f.AlbumFavorite
|
|
|
|
|
|
|
|
log.Debugf("create album: %+v %+v", f, m)
|
2019-06-17 21:45:06 +02:00
|
|
|
|
2019-12-04 12:11:11 +01:00
|
|
|
if res := conf.Db().Create(m); res.Error != nil {
|
2019-06-18 06:37:10 +02:00
|
|
|
log.Error(res.Error.Error())
|
2020-05-03 18:00:50 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("%s already exists", txt.Quote(m.AlbumName))})
|
2019-06-18 06:37:10 +02:00
|
|
|
return
|
2019-06-17 21:45:06 +02:00
|
|
|
}
|
|
|
|
|
2020-02-04 01:39:53 +01:00
|
|
|
event.Success("album created")
|
2019-12-04 12:11:11 +01:00
|
|
|
|
2019-12-21 17:18:47 +01:00
|
|
|
event.Publish("config.updated", event.Data(conf.ClientConfig()))
|
2019-12-17 04:39:23 +01:00
|
|
|
|
2020-01-30 18:19:26 +01:00
|
|
|
PublishAlbumEvent(EntityCreated, m.AlbumUUID, c, q)
|
|
|
|
|
2019-12-04 12:11:11 +01:00
|
|
|
c.JSON(http.StatusOK, m)
|
2019-06-17 21:45:06 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-03 21:25:40 +01:00
|
|
|
// PUT /api/v1/albums/:uuid
|
|
|
|
func UpdateAlbum(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.PUT("/albums/:uuid", func(c *gin.Context) {
|
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-20 10:38:01 +02:00
|
|
|
uuid := c.Param("uuid")
|
2020-04-30 20:07:03 +02:00
|
|
|
q := service.Query()
|
2019-12-03 21:25:40 +01:00
|
|
|
|
2020-04-20 10:38:01 +02:00
|
|
|
m, err := q.AlbumByUUID(uuid)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrAlbumNotFound)
|
2019-12-03 21:25:40 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-20 10:38:01 +02:00
|
|
|
f, err := form.NewAlbum(m)
|
2019-12-03 21:25:40 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-04-20 10:38:01 +02:00
|
|
|
log.Error(err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, ErrSaveFailed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, ErrFormInvalid)
|
2019-12-03 21:25:40 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:07:03 +02:00
|
|
|
if err := m.Save(f); err != nil {
|
2020-04-20 10:38:01 +02:00
|
|
|
log.Error(err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, ErrSaveFailed)
|
|
|
|
return
|
|
|
|
}
|
2019-12-03 21:25:40 +01:00
|
|
|
|
2019-12-03 23:17:55 +01:00
|
|
|
event.Publish("config.updated", event.Data(conf.ClientConfig()))
|
2020-02-02 02:00:47 +01:00
|
|
|
event.Success("album saved")
|
2019-12-06 11:56:24 +01:00
|
|
|
|
2020-04-20 10:38:01 +02:00
|
|
|
PublishAlbumEvent(EntityUpdated, uuid, c, q)
|
2020-01-30 18:19:26 +01:00
|
|
|
|
2019-12-06 11:56:24 +01:00
|
|
|
c.JSON(http.StatusOK, m)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// DELETE /api/v1/albums/:uuid
|
|
|
|
func DeleteAlbum(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.DELETE("/albums/:uuid", func(c *gin.Context) {
|
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
id := c.Param("uuid")
|
2020-04-30 20:07:03 +02:00
|
|
|
q := service.Query()
|
2019-12-06 11:56:24 +01:00
|
|
|
|
2020-03-28 15:29:17 +01:00
|
|
|
m, err := q.AlbumByUUID(id)
|
2019-12-06 11:56:24 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-01-23 10:16:18 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrAlbumNotFound)
|
2019-12-06 11:56:24 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-30 18:19:26 +01:00
|
|
|
PublishAlbumEvent(EntityDeleted, id, c, q)
|
|
|
|
|
2019-12-06 11:56:24 +01:00
|
|
|
conf.Db().Delete(&m)
|
|
|
|
|
|
|
|
event.Publish("config.updated", event.Data(conf.ClientConfig()))
|
2020-05-03 18:00:50 +02:00
|
|
|
event.Success(fmt.Sprintf("album %s deleted", txt.Quote(m.AlbumName)))
|
2019-12-03 23:17:55 +01:00
|
|
|
|
2019-12-03 21:25:40 +01:00
|
|
|
c.JSON(http.StatusOK, m)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-17 21:45:06 +02:00
|
|
|
// POST /api/v1/albums/:uuid/like
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// uuid: string Album UUID
|
|
|
|
func LikeAlbum(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.POST("/albums/:uuid/like", func(c *gin.Context) {
|
2019-11-12 05:49:10 +01:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-30 18:19:26 +01:00
|
|
|
id := c.Param("uuid")
|
2020-04-30 20:07:03 +02:00
|
|
|
q := service.Query()
|
2019-06-17 21:45:06 +02:00
|
|
|
|
2020-03-28 15:29:17 +01:00
|
|
|
album, err := q.AlbumByUUID(id)
|
2019-06-17 21:45:06 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2020-01-23 10:10:32 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrAlbumNotFound)
|
2019-06-17 21:45:06 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
album.AlbumFavorite = true
|
|
|
|
conf.Db().Save(&album)
|
|
|
|
|
2019-12-03 23:17:55 +01:00
|
|
|
event.Publish("config.updated", event.Data(conf.ClientConfig()))
|
2020-01-30 18:19:26 +01:00
|
|
|
PublishAlbumEvent(EntityUpdated, id, c, q)
|
2019-12-03 23:17:55 +01:00
|
|
|
|
2019-06-17 21:45:06 +02:00
|
|
|
c.JSON(http.StatusOK, http.Response{})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// DELETE /api/v1/albums/:uuid/like
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// uuid: string Album UUID
|
|
|
|
func DislikeAlbum(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.DELETE("/albums/:uuid/like", func(c *gin.Context) {
|
2019-11-12 05:49:10 +01:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-30 18:19:26 +01:00
|
|
|
id := c.Param("uuid")
|
2020-04-30 20:07:03 +02:00
|
|
|
q := service.Query()
|
2020-03-28 15:29:17 +01:00
|
|
|
album, err := q.AlbumByUUID(id)
|
2019-06-17 21:45:06 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2020-01-23 10:10:32 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrAlbumNotFound)
|
2019-06-17 21:45:06 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
album.AlbumFavorite = false
|
|
|
|
conf.Db().Save(&album)
|
|
|
|
|
2019-12-03 23:17:55 +01:00
|
|
|
event.Publish("config.updated", event.Data(conf.ClientConfig()))
|
2020-01-30 18:19:26 +01:00
|
|
|
PublishAlbumEvent(EntityUpdated, id, c, q)
|
2019-12-03 23:17:55 +01:00
|
|
|
|
2019-06-17 21:45:06 +02:00
|
|
|
c.JSON(http.StatusOK, http.Response{})
|
|
|
|
})
|
|
|
|
}
|
2019-12-05 12:10:30 +01:00
|
|
|
|
|
|
|
// POST /api/v1/albums/:uuid/photos
|
|
|
|
func AddPhotosToAlbum(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.POST("/albums/:uuid/photos", 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-05 12:10:30 +01: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-12-05 12:10:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-04 05:18:22 +01:00
|
|
|
uuid := c.Param("uuid")
|
2020-04-30 20:07:03 +02:00
|
|
|
q := service.Query()
|
2020-03-28 15:29:17 +01:00
|
|
|
a, err := q.AlbumByUUID(uuid)
|
2020-02-04 05:18:22 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrAlbumNotFound)
|
2019-12-05 12:10:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-04 05:18:22 +01:00
|
|
|
photos, err := q.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)
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": txt.UcFirst(err.Error())})
|
2019-12-05 12:10:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-11 16:55:18 +01:00
|
|
|
var added []*entity.PhotoAlbum
|
2020-02-04 05:18:22 +01:00
|
|
|
|
|
|
|
for _, p := range photos {
|
2020-04-30 20:07:03 +02:00
|
|
|
added = append(added, entity.NewPhotoAlbum(p.PhotoUUID, a.AlbumUUID).FirstOrCreate())
|
2019-12-05 12:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(added) == 1 {
|
|
|
|
event.Success(fmt.Sprintf("one photo added to %s", a.AlbumName))
|
|
|
|
} else {
|
|
|
|
event.Success(fmt.Sprintf("%d photos added to %s", len(added), a.AlbumName))
|
|
|
|
}
|
|
|
|
|
2020-02-04 14:51:48 +01:00
|
|
|
PublishAlbumEvent(EntityUpdated, a.AlbumUUID, c, q)
|
|
|
|
|
2020-02-04 05:18:22 +01:00
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "photos added to album", "album": a, "added": added})
|
2019-12-05 12:10:30 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// DELETE /api/v1/albums/:uuid/photos
|
|
|
|
func RemovePhotosFromAlbum(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.DELETE("/albums/:uuid/photos", 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-05 12:10:30 +01: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-12-05 12:10:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-05 19:21:35 +01:00
|
|
|
if len(f.Photos) == 0 {
|
2019-12-05 12:10:30 +01: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-12-05 12:10:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:07:03 +02:00
|
|
|
q := service.Query()
|
2020-03-28 15:29:17 +01:00
|
|
|
a, err := q.AlbumByUUID(c.Param("uuid"))
|
2019-12-05 12:10:30 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-01-23 10:10:32 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrAlbumNotFound)
|
2019-12-05 12:10:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
db := conf.Db()
|
|
|
|
|
2019-12-11 16:55:18 +01:00
|
|
|
db.Where("album_uuid = ? AND photo_uuid IN (?)", a.AlbumUUID, f.Photos).Delete(&entity.PhotoAlbum{})
|
2019-12-05 12:10:30 +01:00
|
|
|
|
|
|
|
event.Success(fmt.Sprintf("photos removed from %s", a.AlbumName))
|
|
|
|
|
2020-02-04 14:51:48 +01:00
|
|
|
PublishAlbumEvent(EntityUpdated, a.AlbumUUID, c, q)
|
|
|
|
|
2019-12-05 19:21:35 +01:00
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "photos removed from album", "album": a, "photos": f.Photos})
|
2019-12-05 12:10:30 +01:00
|
|
|
})
|
|
|
|
}
|
2019-12-06 16:47:30 +01:00
|
|
|
|
|
|
|
// GET /albums/:uuid/download
|
|
|
|
func DownloadAlbum(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.GET("/albums/:uuid/download", func(c *gin.Context) {
|
|
|
|
start := time.Now()
|
|
|
|
|
2020-04-30 20:07:03 +02:00
|
|
|
q := service.Query()
|
2020-03-28 15:29:17 +01:00
|
|
|
a, err := q.AlbumByUUID(c.Param("uuid"))
|
2019-12-06 16:47:30 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-01-23 10:10:32 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrAlbumNotFound)
|
2019-12-06 16:47:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-20 20:07:58 +02:00
|
|
|
p, _, err := q.Photos(form.PhotoSearch{
|
2019-12-06 16:47:30 +01:00
|
|
|
Album: a.AlbumUUID,
|
|
|
|
Count: 10000,
|
|
|
|
Offset: 0,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2020-01-23 10:10:32 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": txt.UcFirst(err.Error())})
|
2019-12-06 16:47:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-06 22:50:04 +02:00
|
|
|
zipPath := path.Join(conf.TempPath(), "album")
|
2020-01-06 14:32:15 +01:00
|
|
|
zipToken := rnd.Token(3)
|
2019-12-06 16:47:30 +01:00
|
|
|
zipBaseName := fmt.Sprintf("%s-%s.zip", strings.Title(a.AlbumSlug), zipToken)
|
2019-12-13 16:25:47 +01:00
|
|
|
zipFileName := path.Join(zipPath, zipBaseName)
|
2019-12-06 16:47:30 +01:00
|
|
|
|
|
|
|
if err := os.MkdirAll(zipPath, 0700); err != nil {
|
|
|
|
log.Error(err)
|
2020-05-07 12:33:09 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": txt.UcFirst("failed to create zip folder")})
|
2019-12-06 16:47:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
newZipFile, err := os.Create(zipFileName)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
2020-01-07 17:36:49 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": txt.UcFirst(err.Error())})
|
2019-12-06 16:47:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer newZipFile.Close()
|
|
|
|
|
|
|
|
zipWriter := zip.NewWriter(newZipFile)
|
|
|
|
defer zipWriter.Close()
|
|
|
|
|
2020-01-06 14:32:15 +01:00
|
|
|
for _, f := range p {
|
|
|
|
fileName := path.Join(conf.OriginalsPath(), f.FileName)
|
2020-04-20 20:07:58 +02:00
|
|
|
fileAlias := f.ShareFileName()
|
2019-12-06 16:47:30 +01:00
|
|
|
|
2020-01-12 14:00:56 +01:00
|
|
|
if fs.FileExists(fileName) {
|
2019-12-06 16:47:30 +01:00
|
|
|
if err := addFileToZip(zipWriter, fileName, fileAlias); err != nil {
|
|
|
|
log.Error(err)
|
2020-01-07 17:36:49 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": txt.UcFirst("failed to create zip file")})
|
2019-12-06 16:47:30 +01:00
|
|
|
return
|
|
|
|
}
|
2020-05-03 18:00:50 +02:00
|
|
|
log.Infof("album: added %s as %s", txt.Quote(f.FileName), txt.Quote(fileAlias))
|
2019-12-06 16:47:30 +01:00
|
|
|
} else {
|
2020-05-03 18:00:50 +02:00
|
|
|
log.Warnf("album: %s is missing", txt.Quote(f.FileName))
|
2020-01-06 14:32:15 +01:00
|
|
|
f.FileMissing = true
|
|
|
|
conf.Db().Save(&f)
|
2019-12-06 16:47:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-03 18:00:50 +02:00
|
|
|
log.Infof("album: archive %s created in %s", txt.Quote(zipBaseName), time.Since(start))
|
2019-12-06 16:47:30 +01:00
|
|
|
zipWriter.Close()
|
|
|
|
newZipFile.Close()
|
|
|
|
|
2020-01-12 14:00:56 +01:00
|
|
|
if !fs.FileExists(zipFileName) {
|
2019-12-06 16:47:30 +01:00
|
|
|
log.Errorf("could not find zip file: %s", zipFileName)
|
2020-01-23 10:10:32 +01:00
|
|
|
c.Data(http.StatusNotFound, "image/svg+xml", photoIconSvg)
|
2019-12-06 16:47:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", zipBaseName))
|
|
|
|
|
|
|
|
c.File(zipFileName)
|
|
|
|
|
|
|
|
if err := os.Remove(zipFileName); err != nil {
|
2020-05-03 18:00:50 +02:00
|
|
|
log.Errorf("album: could not remove %s (%s)", txt.Quote(zipFileName), err.Error())
|
2019-12-06 16:47:30 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-12-16 11:04:49 +01:00
|
|
|
|
2020-01-22 13:43:07 +01:00
|
|
|
// GET /api/v1/albums/:uuid/thumbnail/:type
|
2019-12-16 11:04:49 +01:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// uuid: string Album UUID
|
|
|
|
// type: string Thumbnail type, see photoprism.ThumbnailTypes
|
|
|
|
func AlbumThumbnail(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.GET("/albums/:uuid/thumbnail/:type", func(c *gin.Context) {
|
|
|
|
typeName := c.Param("type")
|
|
|
|
uuid := c.Param("uuid")
|
2020-02-02 00:31:09 +01:00
|
|
|
start := time.Now()
|
2019-12-16 11:04:49 +01:00
|
|
|
|
2020-01-06 14:32:15 +01:00
|
|
|
thumbType, ok := thumb.Types[typeName]
|
2019-12-16 11:04:49 +01:00
|
|
|
|
|
|
|
if !ok {
|
2020-02-02 00:31:09 +01:00
|
|
|
log.Errorf("album: invalid thumb type %s", typeName)
|
2020-05-05 15:42:54 +02:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", photoIconSvg)
|
2019-12-16 11:04:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:07:03 +02:00
|
|
|
q := service.Query()
|
2019-12-16 11:04:49 +01:00
|
|
|
|
2020-02-02 00:31:09 +01:00
|
|
|
gc := conf.Cache()
|
|
|
|
cacheKey := fmt.Sprintf("album-thumbnail:%s:%s", uuid, typeName)
|
|
|
|
|
|
|
|
if cacheData, ok := gc.Get(cacheKey); ok {
|
|
|
|
log.Debugf("album: %s cache hit [%s]", cacheKey, time.Since(start))
|
|
|
|
c.Data(http.StatusOK, "image/jpeg", cacheData.([]byte))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-28 15:29:17 +01:00
|
|
|
f, err := q.AlbumThumbByUUID(uuid)
|
2019-12-16 11:04:49 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-02-02 00:31:09 +01:00
|
|
|
log.Debugf("album: no photos yet, using generic image for %s", uuid)
|
2019-12-16 11:04:49 +01:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", albumIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-06 14:32:15 +01:00
|
|
|
fileName := path.Join(conf.OriginalsPath(), f.FileName)
|
2019-12-16 11:04:49 +01:00
|
|
|
|
2020-01-12 14:00:56 +01:00
|
|
|
if !fs.FileExists(fileName) {
|
2020-02-02 00:31:09 +01:00
|
|
|
log.Errorf("album: could not find original for %s", fileName)
|
2020-05-05 15:42:54 +02:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", photoIconSvg)
|
2019-12-16 11:04:49 +01:00
|
|
|
|
|
|
|
// Set missing flag so that the file doesn't show up in search results anymore
|
2020-01-06 14:32:15 +01:00
|
|
|
f.FileMissing = true
|
|
|
|
conf.Db().Save(&f)
|
2019-12-16 11:04:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-08 19:51:21 +01:00
|
|
|
// Use original file if thumb size exceeds limit, see https://github.com/photoprism/photoprism/issues/157
|
2020-01-13 11:07:09 +01:00
|
|
|
if thumbType.ExceedsLimit() && c.Query("download") == "" {
|
2020-01-08 19:51:21 +01:00
|
|
|
log.Debugf("album: using original, thumbnail size exceeds limit (width %d, height %d)", thumbType.Width, thumbType.Height)
|
|
|
|
c.File(fileName)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-05 15:42:54 +02:00
|
|
|
var thumbnail string
|
2019-12-16 11:04:49 +01:00
|
|
|
|
2020-05-05 17:04:13 +02:00
|
|
|
if conf.ThumbUncached() || thumbType.OnDemand() {
|
|
|
|
thumbnail, err = thumb.FromFile(fileName, f.FileHash, conf.ThumbPath(), thumbType.Width, thumbType.Height, thumbType.Options...)
|
2020-05-05 15:42:54 +02:00
|
|
|
} else {
|
2020-05-05 17:04:13 +02:00
|
|
|
thumbnail, err = thumb.FromCache(fileName, f.FileHash, conf.ThumbPath(), thumbType.Width, thumbType.Height, thumbType.Options...)
|
2020-05-05 15:42:54 +02:00
|
|
|
}
|
2020-02-02 00:31:09 +01:00
|
|
|
|
2020-05-05 15:42:54 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("album: %s", err)
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", photoIconSvg)
|
|
|
|
return
|
|
|
|
}
|
2020-02-02 00:31:09 +01:00
|
|
|
|
2020-05-05 15:42:54 +02:00
|
|
|
if c.Query("download") != "" {
|
|
|
|
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", f.ShareFileName()))
|
|
|
|
}
|
2020-02-02 00:31:09 +01:00
|
|
|
|
2020-05-05 15:42:54 +02:00
|
|
|
thumbData, err := ioutil.ReadFile(thumbnail)
|
2020-02-02 00:31:09 +01:00
|
|
|
|
2020-05-05 15:42:54 +02:00
|
|
|
if err != nil {
|
2020-02-02 00:31:09 +01:00
|
|
|
log.Errorf("album: %s", err)
|
2020-05-05 15:42:54 +02:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", albumIconSvg)
|
|
|
|
return
|
2019-12-16 11:04:49 +01:00
|
|
|
}
|
2020-05-05 15:42:54 +02:00
|
|
|
|
|
|
|
gc.Set(cacheKey, thumbData, time.Hour)
|
|
|
|
|
|
|
|
log.Debugf("album: %s cached [%s]", cacheKey, time.Since(start))
|
|
|
|
|
|
|
|
c.Data(http.StatusOK, "image/jpeg", thumbData)
|
2019-12-16 11:04:49 +01:00
|
|
|
})
|
|
|
|
}
|