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-01-27 21:30:10 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
|
|
|
|
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"
|
2020-01-13 11:07:09 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2020-01-12 14:00:56 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2019-12-05 19:21:35 +01:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
// POST /api/v1/zip
|
2020-06-25 14:54:04 +02:00
|
|
|
func CreateZip(router *gin.RouterGroup) {
|
2019-12-05 19:21:35 +01:00
|
|
|
router.POST("/zip", func(c *gin.Context) {
|
2020-06-25 14:54:04 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourcePhotos, acl.ActionDownload)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(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
|
|
|
|
}
|
|
|
|
|
2020-05-25 19:10:44 +02:00
|
|
|
files, err := query.FileSelection(f)
|
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
|
|
|
|
}
|
|
|
|
|
2020-04-06 22:50:04 +02:00
|
|
|
zipPath := path.Join(conf.TempPath(), "zip")
|
2021-01-27 21:30:10 +01:00
|
|
|
zipToken := rnd.Token(8)
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
newZipFile, err := os.Create(zipFileName)
|
|
|
|
|
|
|
|
if 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
|
|
|
|
}
|
|
|
|
|
|
|
|
defer newZipFile.Close()
|
|
|
|
|
|
|
|
zipWriter := zip.NewWriter(newZipFile)
|
|
|
|
defer zipWriter.Close()
|
|
|
|
|
2021-01-27 21:30:10 +01:00
|
|
|
dlName := DownloadName(c)
|
|
|
|
|
|
|
|
var aliases = make(map[string]int)
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
if file.FileHash == "" {
|
|
|
|
log.Warnf("download: empty file hash, skipped %s", txt.Quote(file.FileName))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if file.FileSidecar {
|
|
|
|
log.Debugf("download: skipped sidecar %s", txt.Quote(file.FileName))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-07-07 10:51:55 +02:00
|
|
|
Error(c, http.StatusInternalServerError, err, i18n.ErrZipFailed)
|
2019-12-05 19:21:35 +01:00
|
|
|
return
|
|
|
|
}
|
2021-01-27 21:30:10 +01:00
|
|
|
log.Infof("download: added %s as %s", txt.Quote(file.FileName), txt.Quote(alias))
|
2019-12-05 19:21:35 +01:00
|
|
|
} else {
|
2021-01-27 21:30:10 +01:00
|
|
|
log.Warnf("download: file %s is missing", txt.Quote(file.FileName))
|
|
|
|
logError("download", file.Update("FileMissing", true))
|
2019-12-05 19:21:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
elapsed := int(time.Since(start).Seconds())
|
|
|
|
|
2021-01-27 21:30:10 +01:00
|
|
|
log.Infof("download: zip %s created in %s", txt.Quote(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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GET /api/v1/zip/:filename
|
2020-06-25 14:54:04 +02:00
|
|
|
func DownloadZip(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) {
|
2020-05-27 19:38:40 +02:00
|
|
|
c.Data(http.StatusForbidden, "image/svg+xml", brokenIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
conf := service.Config()
|
2019-12-05 19:21:35 +01:00
|
|
|
zipBaseName := 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) {
|
2019-12-05 19:21:35 +01:00
|
|
|
log.Errorf("could not find zip file: %s", zipFileName)
|
|
|
|
c.Data(404, "image/svg+xml", photoIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-08 09:02:30 +01:00
|
|
|
c.FileAttachment(zipFileName, zipBaseName)
|
2019-12-05 19:21:35 +01:00
|
|
|
|
2019-12-05 19:49:05 +01:00
|
|
|
if err := os.Remove(zipFileName); err != nil {
|
2021-01-27 21:30:10 +01:00
|
|
|
log.Errorf("download: failed removing %s (%s)", txt.Quote(zipFileName), err.Error())
|
2019-12-05 19:49:05 +01:00
|
|
|
}
|
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
|
|
|
|
}
|