2019-12-05 19:21:35 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2021-01-27 21:30:10 +01:00
|
|
|
"strings"
|
2019-12-05 19:21:35 +01:00
|
|
|
"time"
|
|
|
|
|
2021-12-14 18:34:52 +01:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-01-27 21:30:10 +01:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
2019-12-05 19:21:35 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2020-07-07 10:51:55 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
2020-06-07 10:09:35 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
2020-05-08 15:41:01 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2020-06-25 14:54:04 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2020-01-13 11:07:09 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2021-12-14 18:34:52 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
2019-12-05 19:21:35 +01:00
|
|
|
)
|
|
|
|
|
2022-07-21 15:16:20 +02:00
|
|
|
// ZipCreate creates a zip file archive for download.
|
2022-02-27 17:32:54 +01:00
|
|
|
//
|
2019-12-05 19:21:35 +01:00
|
|
|
// POST /api/v1/zip
|
2022-07-21 15:16:20 +02:00
|
|
|
func ZipCreate(router *gin.RouterGroup) {
|
2019-12-05 19:21:35 +01:00
|
|
|
router.POST("/zip", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourcePhotos, acl.ActionDownload)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2020-01-22 13:43:07 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
conf := service.Config()
|
|
|
|
|
2020-04-20 10:38:01 +02:00
|
|
|
if !conf.Settings().Features.Download {
|
2020-07-07 10:51:55 +02:00
|
|
|
AbortFeatureDisabled(c)
|
2020-04-20 10:38:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-04 05:18:22 +01:00
|
|
|
var f form.Selection
|
2019-12-05 19:21:35 +01:00
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortBadRequest(c)
|
2019-12-05 19:21:35 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-25 19:10:44 +02:00
|
|
|
if f.Empty() {
|
2020-07-07 10:51:55 +02:00
|
|
|
Abort(c, http.StatusBadRequest, i18n.ErrNoItemsSelected)
|
2019-12-05 19:21:35 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
// Configure file selection based on user settings.
|
|
|
|
var selection query.FileSelection
|
|
|
|
if dl := conf.Settings().Download; dl.Disabled {
|
|
|
|
AbortFeatureDisabled(c)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
selection = query.DownloadSelection(dl.MediaRaw, dl.MediaSidecar, dl.Originals)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find files to download.
|
|
|
|
files, err := query.SelectedFiles(f, selection)
|
2019-12-05 19:21:35 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-07-07 10:51:55 +02:00
|
|
|
Error(c, http.StatusBadRequest, err, i18n.ErrZipFailed)
|
2020-05-25 19:10:44 +02:00
|
|
|
return
|
|
|
|
} else if len(files) == 0 {
|
2020-07-07 10:51:55 +02:00
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrNoFilesForDownload)
|
2019-12-05 19:21:35 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
// Configure file names.
|
|
|
|
dlName := DownloadName(c)
|
2020-04-06 22:50:04 +02:00
|
|
|
zipPath := path.Join(conf.TempPath(), "zip")
|
2022-04-15 09:42:07 +02:00
|
|
|
zipToken := rnd.GenerateToken(8)
|
2021-01-27 21:30:10 +01:00
|
|
|
zipBaseName := fmt.Sprintf("photoprism-download-%s-%s.zip", time.Now().Format("20060102-150405"), zipToken)
|
2019-12-13 16:25:47 +01:00
|
|
|
zipFileName := path.Join(zipPath, zipBaseName)
|
2019-12-05 19:21:35 +01:00
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
// Create temp directory.
|
2022-07-19 20:41:36 +02:00
|
|
|
if err = os.MkdirAll(zipPath, 0700); err != nil {
|
2020-07-07 10:51:55 +02:00
|
|
|
Error(c, http.StatusInternalServerError, err, i18n.ErrZipFailed)
|
2019-12-05 19:21:35 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
// Create new zip file.
|
|
|
|
var newZipFile *os.File
|
|
|
|
if newZipFile, err = os.Create(zipFileName); err != nil {
|
2020-07-07 10:51:55 +02:00
|
|
|
Error(c, http.StatusInternalServerError, err, i18n.ErrZipFailed)
|
2019-12-05 19:21:35 +01:00
|
|
|
return
|
2022-04-15 09:42:07 +02:00
|
|
|
} else {
|
|
|
|
defer newZipFile.Close()
|
2019-12-05 19:21:35 +01:00
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
// Create zip writer.
|
2019-12-05 19:21:35 +01:00
|
|
|
zipWriter := zip.NewWriter(newZipFile)
|
2022-07-19 20:41:36 +02:00
|
|
|
defer func(w *zip.Writer) {
|
|
|
|
logError("zip", w.Close())
|
|
|
|
}(zipWriter)
|
2019-12-05 19:21:35 +01:00
|
|
|
|
2021-01-27 21:30:10 +01:00
|
|
|
var aliases = make(map[string]int)
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
// Add files to zip.
|
2021-01-27 21:30:10 +01:00
|
|
|
for _, file := range files {
|
|
|
|
fileName := photoprism.FileName(file.FileRoot, file.FileName)
|
|
|
|
alias := file.DownloadName(dlName, 0)
|
|
|
|
key := strings.ToLower(alias)
|
|
|
|
|
|
|
|
if seq := aliases[key]; seq > 0 {
|
|
|
|
alias = file.DownloadName(dlName, seq)
|
|
|
|
}
|
|
|
|
|
|
|
|
aliases[key] += 1
|
2019-12-05 19:21:35 +01:00
|
|
|
|
2020-01-12 14:00:56 +01:00
|
|
|
if fs.FileExists(fileName) {
|
2021-01-27 21:30:10 +01:00
|
|
|
if err := addFileToZip(zipWriter, fileName, alias); err != nil {
|
2022-07-21 15:16:20 +02:00
|
|
|
log.Errorf("zip: failed adding %s to zip (%s)", clean.Log(file.FileName), err)
|
2022-04-13 01:59:32 +02:00
|
|
|
Abort(c, http.StatusInternalServerError, i18n.ErrZipFailed)
|
2019-12-05 19:21:35 +01:00
|
|
|
return
|
|
|
|
}
|
2022-04-13 01:59:32 +02:00
|
|
|
|
2022-07-21 15:16:20 +02:00
|
|
|
log.Infof("zip: added %s as %s", clean.Log(file.FileName), clean.Log(alias))
|
2019-12-05 19:21:35 +01:00
|
|
|
} else {
|
2022-07-21 15:16:20 +02:00
|
|
|
log.Warnf("zip: media file %s is missing", clean.Log(file.FileName))
|
|
|
|
logError("zip", file.Update("FileMissing", true))
|
2019-12-05 19:21:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
elapsed := int(time.Since(start).Seconds())
|
|
|
|
|
2022-07-21 15:16:20 +02:00
|
|
|
log.Infof("zip: created %s [%s]", clean.Log(zipBaseName), time.Since(start))
|
2019-12-05 19:21:35 +01:00
|
|
|
|
2020-07-07 10:51:55 +02:00
|
|
|
c.JSON(http.StatusOK, gin.H{"code": http.StatusOK, "message": i18n.Msg(i18n.MsgZipCreatedIn, elapsed), "filename": zipBaseName})
|
2019-12-05 19:21:35 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-21 15:16:20 +02:00
|
|
|
// ZipDownload downloads a zip file archive.
|
2022-02-27 17:32:54 +01:00
|
|
|
//
|
2019-12-05 19:21:35 +01:00
|
|
|
// GET /api/v1/zip/:filename
|
2022-07-21 15:16:20 +02:00
|
|
|
func ZipDownload(router *gin.RouterGroup) {
|
2019-12-05 19:21:35 +01:00
|
|
|
router.GET("/zip/:filename", func(c *gin.Context) {
|
2020-06-25 14:54:04 +02:00
|
|
|
if InvalidDownloadToken(c) {
|
2022-07-21 15:16:20 +02:00
|
|
|
log.Errorf("zip: %s", c.AbortWithError(http.StatusForbidden, fmt.Errorf("invalid download token")))
|
2020-05-27 19:38:40 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
conf := service.Config()
|
2022-04-15 09:42:07 +02:00
|
|
|
zipBaseName := clean.FileName(filepath.Base(c.Param("filename")))
|
2020-04-06 22:50:04 +02:00
|
|
|
zipPath := path.Join(conf.TempPath(), "zip")
|
2019-12-13 16:25:47 +01:00
|
|
|
zipFileName := path.Join(zipPath, zipBaseName)
|
2019-12-05 19:21:35 +01:00
|
|
|
|
2020-01-12 14:00:56 +01:00
|
|
|
if !fs.FileExists(zipFileName) {
|
2022-07-21 15:16:20 +02:00
|
|
|
log.Errorf("zip: %s", c.AbortWithError(http.StatusNotFound, fmt.Errorf("%s not found", clean.Log(zipFileName))))
|
2019-12-05 19:21:35 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-21 15:16:20 +02:00
|
|
|
defer func(fileName, baseName string) {
|
|
|
|
log.Debugf("zip: %s has been downloaded", clean.Log(baseName))
|
|
|
|
|
|
|
|
// Wait a moment before deleting the zip file, just to be sure:
|
|
|
|
// https://github.com/photoprism/photoprism/issues/2532
|
|
|
|
time.Sleep(time.Second)
|
2019-12-05 19:21:35 +01:00
|
|
|
|
2022-07-21 15:16:20 +02:00
|
|
|
// Remove the zip file to free up disk space.
|
|
|
|
if err := os.Remove(fileName); err != nil {
|
|
|
|
log.Warnf("zip: failed deleting %s (%s)", clean.Log(fileName), err)
|
|
|
|
} else {
|
|
|
|
log.Debugf("zip: deleted %s", clean.Log(baseName))
|
|
|
|
}
|
|
|
|
}(zipFileName, zipBaseName)
|
|
|
|
|
|
|
|
log.Debugf("zip: submitting %s", clean.Log(zipBaseName))
|
|
|
|
|
|
|
|
c.FileAttachment(zipFileName, zipBaseName)
|
2019-12-05 19:21:35 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-27 17:32:54 +01:00
|
|
|
// addFileToZip adds a file to a zip archive.
|
2019-12-05 19:21:35 +01:00
|
|
|
func addFileToZip(zipWriter *zip.Writer, fileName, fileAlias string) error {
|
|
|
|
fileToZip, err := os.Open(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fileToZip.Close()
|
|
|
|
|
|
|
|
// Get the file information
|
|
|
|
info, err := fileToZip.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
header, err := zip.FileInfoHeader(info)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
header.Name = fileAlias
|
|
|
|
|
|
|
|
// Change to deflate to gain better compression
|
|
|
|
// see http://golang.org/pkg/archive/zip/#pkg-constants
|
|
|
|
header.Method = zip.Deflate
|
|
|
|
|
|
|
|
writer, err := zipWriter.CreateHeader(header)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = io.Copy(writer, fileToZip)
|
|
|
|
return err
|
|
|
|
}
|