2022-04-11 11:53:21 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/get"
|
2022-04-11 11:53:21 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
|
|
|
"github.com/photoprism/photoprism/internal/search"
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2022-04-11 11:53:21 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DownloadAlbum streams the album contents as zip archive.
|
|
|
|
//
|
|
|
|
// GET /api/v1/albums/:uid/dl
|
|
|
|
func DownloadAlbum(router *gin.RouterGroup) {
|
|
|
|
router.GET("/albums/:uid/dl", func(c *gin.Context) {
|
|
|
|
if InvalidDownloadToken(c) {
|
2022-09-28 09:01:17 +02:00
|
|
|
AbortForbidden(c)
|
2022-04-11 11:53:21 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
conf := get.Config()
|
2022-04-11 11:53:21 +02:00
|
|
|
|
|
|
|
if !conf.Settings().Features.Download {
|
|
|
|
AbortFeatureDisabled(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
start := time.Now()
|
2022-09-28 09:01:17 +02:00
|
|
|
a, err := query.AlbumByUID(clean.UID(c.Param("uid")))
|
2022-04-11 11:53:21 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2022-08-01 15:57:19 +02:00
|
|
|
AbortAlbumNotFound(c)
|
2022-04-11 11:53:21 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
files, err := search.AlbumPhotos(a, 10000, true)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
AbortEntityNotFound(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
zipFileName := a.ZipName()
|
|
|
|
|
|
|
|
AddDownloadHeader(c, zipFileName)
|
|
|
|
|
|
|
|
zipWriter := zip.NewWriter(c.Writer)
|
|
|
|
defer zipWriter.Close()
|
|
|
|
|
|
|
|
var aliases = make(map[string]int)
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
if file.FileHash == "" {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Warnf("download: empty file hash, skipped %s", clean.Log(file.FileName))
|
2022-04-11 11:53:21 +02:00
|
|
|
continue
|
2022-04-13 01:59:32 +02:00
|
|
|
} else if file.FileName == "" {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Warnf("download: empty file name, skipped %s", clean.Log(file.FileUID))
|
2022-04-13 01:59:32 +02:00
|
|
|
continue
|
2022-04-11 11:53:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if file.FileSidecar {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Debugf("download: skipped sidecar %s", clean.Log(file.FileName))
|
2022-04-11 11:53:21 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fileName := photoprism.FileName(file.FileRoot, file.FileName)
|
|
|
|
alias := file.ShareBase(0)
|
|
|
|
key := strings.ToLower(alias)
|
|
|
|
|
|
|
|
if seq := aliases[key]; seq > 0 {
|
|
|
|
alias = file.ShareBase(seq)
|
|
|
|
}
|
|
|
|
|
|
|
|
aliases[key] += 1
|
|
|
|
|
|
|
|
if fs.FileExists(fileName) {
|
|
|
|
if err := addFileToZip(zipWriter, fileName, alias); err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Errorf("download: failed adding %s to album zip (%s)", clean.Log(file.FileName), err)
|
2022-04-11 11:53:21 +02:00
|
|
|
Abort(c, http.StatusInternalServerError, i18n.ErrZipFailed)
|
|
|
|
return
|
|
|
|
}
|
2022-04-13 01:59:32 +02:00
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Infof("download: added %s as %s", clean.Log(file.FileName), clean.Log(alias))
|
2022-04-11 11:53:21 +02:00
|
|
|
} else {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Warnf("download: album file %s is missing", clean.Log(file.FileName))
|
2022-04-11 11:53:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Infof("download: created %s [%s]", clean.Log(zipFileName), time.Since(start))
|
2022-04-11 11:53:21 +02:00
|
|
|
})
|
|
|
|
}
|