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"
|
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"
|
2019-12-11 07:37:39 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/repo"
|
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"
|
|
|
|
"github.com/photoprism/photoprism/internal/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GET /api/v1/albums
|
|
|
|
func GetAlbums(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.GET("/albums", func(c *gin.Context) {
|
2019-12-05 19:21:35 +01:00
|
|
|
var f form.AlbumSearch
|
2019-06-17 21:45:06 +02:00
|
|
|
|
2019-12-11 07:37:39 +01:00
|
|
|
r := repo.New(conf.OriginalsPath(), conf.Db())
|
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 {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": util.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-11 07:37:39 +01:00
|
|
|
result, err := r.Albums(f)
|
2019-06-17 21:45:06 +02:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(400, gin.H{"error": util.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-05 19:21:35 +01:00
|
|
|
c.Header("X-Result-Count", strconv.Itoa(f.Count))
|
|
|
|
c.Header("X-Result-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")
|
2019-12-11 07:37:39 +01:00
|
|
|
r := repo.New(conf.OriginalsPath(), conf.Db())
|
|
|
|
m, err := r.FindAlbumByUUID(id)
|
2019-12-05 14:11:45 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(404, gin.H{"error": util.UcFirst(err.Error())})
|
|
|
|
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 {
|
2019-06-17 21:45:06 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": util.UcFirst(err.Error())})
|
2019-06-18 06:37:10 +02:00
|
|
|
return
|
2019-06-17 21:45:06 +02:00
|
|
|
}
|
|
|
|
|
2019-12-11 16:55:18 +01:00
|
|
|
m := entity.NewAlbum(f.AlbumName)
|
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())
|
2019-12-04 12:11:11 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("\"%s\" already exists", m.AlbumName)})
|
2019-06-18 06:37:10 +02:00
|
|
|
return
|
2019-06-17 21:45:06 +02:00
|
|
|
}
|
|
|
|
|
2019-12-11 04:12:54 +01:00
|
|
|
event.Publish("count.albums", event.Data{
|
|
|
|
"count": 1,
|
|
|
|
})
|
|
|
|
|
2019-12-06 11:56:24 +01:00
|
|
|
event.Success(fmt.Sprintf("album \"%s\" created", m.AlbumName))
|
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
|
|
|
|
}
|
|
|
|
|
2019-12-05 19:21:35 +01:00
|
|
|
var f form.Album
|
2019-12-03 21:25:40 +01:00
|
|
|
|
2019-12-05 19:21:35 +01:00
|
|
|
if err := c.BindJSON(&f); err != nil {
|
2019-12-03 21:25:40 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": util.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
id := c.Param("uuid")
|
2019-12-11 07:37:39 +01:00
|
|
|
r := repo.New(conf.OriginalsPath(), conf.Db())
|
2019-12-03 21:25:40 +01:00
|
|
|
|
2019-12-11 07:37:39 +01:00
|
|
|
m, err := r.FindAlbumByUUID(id)
|
2019-12-03 21:25:40 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(404, gin.H{"error": util.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-05 19:21:35 +01:00
|
|
|
m.Rename(f.AlbumName)
|
2019-12-03 21:25:40 +01:00
|
|
|
conf.Db().Save(&m)
|
|
|
|
|
2019-12-03 23:17:55 +01:00
|
|
|
event.Publish("config.updated", event.Data(conf.ClientConfig()))
|
2019-12-06 11:56:24 +01:00
|
|
|
event.Success(fmt.Sprintf("album \"%s\" updated", m.AlbumName))
|
|
|
|
|
|
|
|
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")
|
2019-12-11 07:37:39 +01:00
|
|
|
r := repo.New(conf.OriginalsPath(), conf.Db())
|
2019-12-06 11:56:24 +01:00
|
|
|
|
2019-12-11 07:37:39 +01:00
|
|
|
m, err := r.FindAlbumByUUID(id)
|
2019-12-06 11:56:24 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(404, gin.H{"error": util.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.Db().Delete(&m)
|
|
|
|
|
|
|
|
event.Publish("config.updated", event.Data(conf.ClientConfig()))
|
|
|
|
event.Success(fmt.Sprintf("album \"%s\" deleted", 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
|
|
|
|
}
|
|
|
|
|
2019-12-11 07:37:39 +01:00
|
|
|
r := repo.New(conf.OriginalsPath(), conf.Db())
|
2019-06-17 21:45:06 +02:00
|
|
|
|
2019-12-11 07:37:39 +01:00
|
|
|
album, err := r.FindAlbumByUUID(c.Param("uuid"))
|
2019-06-17 21:45:06 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(404, gin.H{"error": util.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
album.AlbumFavorite = true
|
|
|
|
conf.Db().Save(&album)
|
|
|
|
|
2019-12-03 23:17:55 +01:00
|
|
|
event.Publish("config.updated", event.Data(conf.ClientConfig()))
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-12-11 07:37:39 +01:00
|
|
|
r := repo.New(conf.OriginalsPath(), conf.Db())
|
|
|
|
album, err := r.FindAlbumByUUID(c.Param("uuid"))
|
2019-06-17 21:45:06 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(404, gin.H{"error": util.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
album.AlbumFavorite = false
|
|
|
|
conf.Db().Save(&album)
|
|
|
|
|
2019-12-03 23:17:55 +01:00
|
|
|
event.Publish("config.updated", event.Data(conf.ClientConfig()))
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-12-05 19:21:35 +01:00
|
|
|
var f form.PhotoUUIDs
|
2019-12-05 12:10:30 +01:00
|
|
|
|
2019-12-05 19:21:35 +01:00
|
|
|
if err := c.BindJSON(&f); err != nil {
|
2019-12-05 12:10:30 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": util.UcFirst(err.Error())})
|
|
|
|
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")
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": util.UcFirst("no photos selected")})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-11 07:37:39 +01:00
|
|
|
r := repo.New(conf.OriginalsPath(), conf.Db())
|
|
|
|
a, err := r.FindAlbumByUUID(c.Param("uuid"))
|
2019-12-05 12:10:30 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(404, gin.H{"error": util.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
db := conf.Db()
|
2019-12-11 16:55:18 +01:00
|
|
|
var added []*entity.PhotoAlbum
|
2019-12-05 12:10:30 +01:00
|
|
|
var failed []string
|
|
|
|
|
2019-12-05 19:21:35 +01:00
|
|
|
for _, photoUUID := range f.Photos {
|
2019-12-11 07:37:39 +01:00
|
|
|
if p, err := r.FindPhotoByUUID(photoUUID); err != nil {
|
2019-12-05 12:10:30 +01:00
|
|
|
failed = append(failed, photoUUID)
|
|
|
|
} else {
|
2019-12-11 16:55:18 +01:00
|
|
|
added = append(added, entity.NewPhotoAlbum(p.PhotoUUID, a.AlbumUUID).FirstOrCreate(db))
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "photos added to album", "album": a, "added": added, "failed": failed})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2019-12-05 19:21:35 +01:00
|
|
|
var f form.PhotoUUIDs
|
2019-12-05 12:10:30 +01:00
|
|
|
|
2019-12-05 19:21:35 +01:00
|
|
|
if err := c.BindJSON(&f); err != nil {
|
2019-12-05 12:10:30 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": util.UcFirst(err.Error())})
|
|
|
|
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")
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": util.UcFirst("no photos selected")})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-11 07:37:39 +01:00
|
|
|
r := repo.New(conf.OriginalsPath(), conf.Db())
|
|
|
|
a, err := r.FindAlbumByUUID(c.Param("uuid"))
|
2019-12-05 12:10:30 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(404, gin.H{"error": util.UcFirst(err.Error())})
|
|
|
|
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))
|
|
|
|
|
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()
|
|
|
|
|
2019-12-11 07:37:39 +01:00
|
|
|
r := repo.New(conf.OriginalsPath(), conf.Db())
|
|
|
|
a, err := r.FindAlbumByUUID(c.Param("uuid"))
|
2019-12-06 16:47:30 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(404, gin.H{"error": util.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-11 07:37:39 +01:00
|
|
|
p, err := r.Photos(form.PhotoSearch{
|
2019-12-06 16:47:30 +01:00
|
|
|
Album: a.AlbumUUID,
|
|
|
|
Count: 10000,
|
|
|
|
Offset: 0,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(404, gin.H{"error": util.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
zipPath := path.Join(conf.ExportPath(), "album")
|
|
|
|
zipToken := util.RandomToken(3)
|
|
|
|
zipBaseName := fmt.Sprintf("%s-%s.zip", strings.Title(a.AlbumSlug), zipToken)
|
|
|
|
zipFileName := fmt.Sprintf("%s/%s", zipPath, zipBaseName)
|
|
|
|
|
|
|
|
if err := os.MkdirAll(zipPath, 0700); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": util.UcFirst("failed to create zip directory")})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
newZipFile, err := os.Create(zipFileName)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": util.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer newZipFile.Close()
|
|
|
|
|
|
|
|
zipWriter := zip.NewWriter(newZipFile)
|
|
|
|
defer zipWriter.Close()
|
|
|
|
|
|
|
|
for _, file := range p {
|
|
|
|
fileName := fmt.Sprintf("%s/%s", conf.OriginalsPath(), file.FileName)
|
|
|
|
fileAlias := file.DownloadFileName()
|
|
|
|
|
|
|
|
if util.Exists(fileName) {
|
|
|
|
if err := addFileToZip(zipWriter, fileName, fileAlias); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": util.UcFirst("failed to create zip file")})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Infof("album: added \"%s\" as \"%s\"", file.FileName, fileAlias)
|
|
|
|
} else {
|
|
|
|
log.Warnf("album: \"%s\" is missing", file.FileName)
|
|
|
|
file.FileMissing = true
|
|
|
|
conf.Db().Save(&file)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("album: archive \"%s\" created in %s", zipBaseName, time.Since(start))
|
|
|
|
|
|
|
|
zipWriter.Close()
|
|
|
|
newZipFile.Close()
|
|
|
|
|
|
|
|
if !util.Exists(zipFileName) {
|
|
|
|
log.Errorf("could not find zip file: %s", zipFileName)
|
|
|
|
c.Data(404, "image/svg+xml", photoIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", zipBaseName))
|
|
|
|
|
|
|
|
c.File(zipFileName)
|
|
|
|
|
|
|
|
if err := os.Remove(zipFileName); err != nil {
|
|
|
|
log.Errorf("album: could not remove \"%s\" %s", zipFileName, err.Error())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|