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"
|
2019-12-11 07:37:39 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/repo"
|
2019-12-05 19:21:35 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/util"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
// POST /api/v1/zip
|
|
|
|
func CreateZip(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.POST("/zip", func(c *gin.Context) {
|
|
|
|
var f form.PhotoUUIDs
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": util.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(f.Photos) == 0 {
|
|
|
|
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())
|
|
|
|
files, err := r.FindFilesByUUID(f.Photos, 1000, 0)
|
2019-12-05 19:21:35 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(404, gin.H{"error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
zipPath := path.Join(conf.ExportPath(), "zip")
|
2019-12-05 19:49:05 +01:00
|
|
|
zipToken := util.RandomToken(3)
|
|
|
|
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)
|
|
|
|
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()
|
|
|
|
|
2019-12-05 21:06:53 +01:00
|
|
|
for _, file := range files {
|
2019-12-13 16:25:47 +01:00
|
|
|
fileName := path.Join(conf.OriginalsPath(), file.FileName)
|
2019-12-05 21:06:53 +01:00
|
|
|
fileAlias := file.DownloadFileName()
|
2019-12-05 19:21:35 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2019-12-05 21:06:53 +01:00
|
|
|
log.Infof("zip: added \"%s\" as \"%s\"", file.FileName, fileAlias)
|
2019-12-05 19:21:35 +01:00
|
|
|
} else {
|
2019-12-05 21:06:53 +01:00
|
|
|
log.Warnf("zip: \"%s\" is missing", file.FileName)
|
2019-12-05 19:21:35 +01:00
|
|
|
file.FileMissing = true
|
|
|
|
conf.Db().Save(&file)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
elapsed := int(time.Since(start).Seconds())
|
|
|
|
|
2019-12-05 21:06:53 +01:00
|
|
|
log.Infof("zip: archive \"%s\" created in %s", 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"))
|
|
|
|
zipPath := path.Join(conf.ExportPath(), "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))
|
|
|
|
|
|
|
|
if !util.Exists(zipFileName) {
|
|
|
|
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 {
|
|
|
|
log.Errorf("zip: could not remove \"%s\" %s", zipFileName, err.Error())
|
|
|
|
}
|
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
|
|
|
|
}
|