2019-06-14 21:16:59 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2019-12-15 17:43:17 +01:00
|
|
|
"os"
|
2019-11-16 16:06:34 +01:00
|
|
|
"path/filepath"
|
2019-12-15 17:43:17 +01:00
|
|
|
"strings"
|
2019-06-14 21:16:59 +02:00
|
|
|
"time"
|
|
|
|
|
2020-01-06 14:32:15 +01:00
|
|
|
"github.com/gin-gonic/gin"
|
2019-06-14 21:16:59 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2019-11-16 16:06:34 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2020-01-31 15:29:06 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2019-06-14 21:16:59 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
2020-04-05 22:26:53 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
2020-01-13 11:07:09 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2020-01-31 15:29:06 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2019-06-14 21:16:59 +02:00
|
|
|
)
|
|
|
|
|
2020-01-02 03:57:28 +01:00
|
|
|
// POST /api/v1/import*
|
|
|
|
func StartImport(router *gin.RouterGroup, conf *config.Config) {
|
2019-06-14 21:16:59 +02:00
|
|
|
router.POST("/import/*path", func(c *gin.Context) {
|
2020-04-20 10:38:01 +02:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
2019-07-02 22:03:23 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-20 10:38:01 +02:00
|
|
|
if conf.ReadOnly() || !conf.Settings().Features.Import {
|
|
|
|
c.AbortWithStatusJSON(http.StatusForbidden, ErrFeatureDisabled)
|
2019-11-12 05:49:10 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-14 21:16:59 +02:00
|
|
|
start := time.Now()
|
2020-01-31 15:29:06 +01:00
|
|
|
|
|
|
|
var f form.ImportOptions
|
|
|
|
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": txt.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
subPath := ""
|
2019-06-14 21:16:59 +02:00
|
|
|
path := conf.ImportPath()
|
|
|
|
|
2020-01-04 07:56:56 +01:00
|
|
|
if subPath = c.Param("path"); subPath != "" && subPath != "/" {
|
2019-12-15 17:43:17 +01:00
|
|
|
subPath = strings.Replace(subPath, ".", "", -1)
|
2020-05-22 16:29:12 +02:00
|
|
|
path = filepath.Join(path, subPath)
|
2020-05-06 15:53:47 +02:00
|
|
|
} else if f.Path != "" {
|
|
|
|
subPath = strings.Replace(f.Path, ".", "", -1)
|
2020-05-22 16:29:12 +02:00
|
|
|
path = filepath.Join(path, subPath)
|
2019-06-14 21:16:59 +02:00
|
|
|
}
|
|
|
|
|
2020-01-04 07:56:56 +01:00
|
|
|
path = filepath.Clean(path)
|
|
|
|
|
2020-04-05 22:26:53 +02:00
|
|
|
imp := service.Import()
|
2019-06-14 21:16:59 +02:00
|
|
|
|
2020-01-31 15:29:06 +01:00
|
|
|
var opt photoprism.ImportOptions
|
|
|
|
|
|
|
|
if f.Move {
|
2020-05-03 18:00:50 +02:00
|
|
|
event.Info(fmt.Sprintf("moving files from %s", txt.Quote(filepath.Base(path))))
|
2020-01-31 15:29:06 +01:00
|
|
|
opt = photoprism.ImportOptionsMove(path)
|
|
|
|
} else {
|
2020-05-03 18:00:50 +02:00
|
|
|
event.Info(fmt.Sprintf("copying files from %s", txt.Quote(filepath.Base(path))))
|
2020-01-31 15:29:06 +01:00
|
|
|
opt = photoprism.ImportOptionsCopy(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
imp.Start(opt)
|
2019-06-14 21:16:59 +02:00
|
|
|
|
2020-01-12 14:00:56 +01:00
|
|
|
if subPath != "" && path != conf.ImportPath() && fs.IsEmpty(path) {
|
2019-12-15 17:43:17 +01:00
|
|
|
if err := os.Remove(path); err != nil {
|
2020-05-07 12:49:06 +02:00
|
|
|
log.Errorf("import: could not delete empty folder %s: %s", txt.Quote(path), err)
|
2019-12-15 17:43:17 +01:00
|
|
|
} else {
|
2020-05-07 12:33:09 +02:00
|
|
|
log.Infof("import: deleted empty folder %s", txt.Quote(path))
|
2019-12-15 17:43:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-16 16:06:34 +01:00
|
|
|
elapsed := int(time.Since(start).Seconds())
|
2019-06-14 21:16:59 +02:00
|
|
|
|
2019-11-16 17:48:51 +01:00
|
|
|
event.Success(fmt.Sprintf("import completed in %d s", elapsed))
|
2019-11-16 16:06:34 +01:00
|
|
|
event.Publish("import.completed", event.Data{"path": path, "seconds": elapsed})
|
2020-01-02 05:38:21 +01:00
|
|
|
event.Publish("index.completed", event.Data{"path": path, "seconds": elapsed})
|
2020-05-27 19:38:40 +02:00
|
|
|
|
|
|
|
UpdateClientConfig(conf)
|
2019-11-16 16:06:34 +01:00
|
|
|
|
2019-11-16 17:48:51 +01:00
|
|
|
c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("import completed in %d s", elapsed)})
|
2019-06-14 21:16:59 +02:00
|
|
|
})
|
|
|
|
}
|
2020-01-02 03:57:28 +01:00
|
|
|
|
|
|
|
// DELETE /api/v1/import
|
|
|
|
func CancelImport(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.DELETE("/import", func(c *gin.Context) {
|
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-05 22:26:53 +02:00
|
|
|
imp := service.Import()
|
2020-01-02 03:57:28 +01:00
|
|
|
|
2020-01-02 05:03:47 +01:00
|
|
|
imp.Cancel()
|
2020-01-02 03:57:28 +01:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "import canceled"})
|
|
|
|
})
|
|
|
|
}
|