2019-12-05 19:21:35 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2020-04-30 20:07:03 +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/rnd"
|
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2019-12-05 19:21:35 +01:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
// POST /api/v1/zip
|
|
|
|
func CreateZip(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.POST("/zip", func(c *gin.Context) {
|
2020-01-22 13:43:07 +01:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-20 10:38:01 +02:00
|
|
|
if !conf.Settings().Features.Download {
|
|
|
|
c.AbortWithStatusJSON(http.StatusForbidden, ErrFeatureDisabled)
|
|
|
|
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-01-07 17:36:49 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": txt.UcFirst(err.Error())})
|
2019-12-05 19:21:35 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(f.Photos) == 0 {
|
|
|
|
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 19:21:35 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:07:03 +02:00
|
|
|
q := service.Query()
|
2020-03-28 15:29:17 +01:00
|
|
|
files, err := q.FilesByUUID(f.Photos, 1000, 0)
|
2019-12-05 19:21:35 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(404, gin.H{"error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-06 22:50:04 +02:00
|
|
|
zipPath := path.Join(conf.TempPath(), "zip")
|
2020-01-06 14:32:15 +01:00
|
|
|
zipToken := rnd.Token(3)
|
2019-12-05 19:49:05 +01:00
|
|
|
zipYear := time.Now().Format("January-2006")
|
|
|
|
zipBaseName := fmt.Sprintf("Photos-%s-%s.zip", zipYear, 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 {
|
|
|
|
log.Error(err)
|
2020-01-07 17:36:49 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": txt.UcFirst("failed to create zip directory")})
|
2019-12-05 19:21:35 +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-05 19:21:35 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer newZipFile.Close()
|
|
|
|
|
|
|
|
zipWriter := zip.NewWriter(newZipFile)
|
|
|
|
defer zipWriter.Close()
|
|
|
|
|
2020-01-06 14:32:15 +01:00
|
|
|
for _, f := range files {
|
|
|
|
fileName := path.Join(conf.OriginalsPath(), f.FileName)
|
2020-04-01 12:00:45 +02:00
|
|
|
fileAlias := f.ShareFileName()
|
2019-12-05 19:21:35 +01:00
|
|
|
|
2020-01-12 14:00:56 +01:00
|
|
|
if fs.FileExists(fileName) {
|
2019-12-05 19:21:35 +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-05 19:21:35 +01:00
|
|
|
return
|
|
|
|
}
|
2020-05-03 18:00:50 +02:00
|
|
|
log.Infof("zip: added %s as %s", txt.Quote(f.FileName), txt.Quote(fileAlias))
|
2019-12-05 19:21:35 +01:00
|
|
|
} else {
|
2020-05-03 18:00:50 +02:00
|
|
|
log.Warnf("zip: %s is missing", txt.Quote(f.FileName))
|
2020-01-06 14:32:15 +01:00
|
|
|
f.FileMissing = true
|
|
|
|
conf.Db().Save(&f)
|
2019-12-05 19:21:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
elapsed := int(time.Since(start).Seconds())
|
|
|
|
|
2020-05-03 18:00:50 +02:00
|
|
|
log.Infof("zip: archive %s created in %s", txt.Quote(zipBaseName), time.Since(start))
|
2019-12-05 19:21:35 +01:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("zip created in %d s", elapsed), "filename": zipBaseName})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GET /api/v1/zip/:filename
|
|
|
|
func DownloadZip(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.GET("/zip/:filename", func(c *gin.Context) {
|
|
|
|
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
|
|
|
|
|
|
|
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", zipBaseName))
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
c.File(zipFileName)
|
|
|
|
|
2019-12-05 19:49:05 +01:00
|
|
|
if err := os.Remove(zipFileName); err != nil {
|
2020-05-03 18:00:50 +02:00
|
|
|
log.Errorf("zip: could not remove %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
|
|
|
|
}
|