2019-06-14 21:16:59 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
2020-06-25 14:54:04 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
2020-11-22 01:30:48 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
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"
|
2020-07-07 10:51:55 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
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*
|
2020-06-25 14:54:04 +02:00
|
|
|
func StartImport(router *gin.RouterGroup) {
|
2019-06-14 21:16:59 +02:00
|
|
|
router.POST("/import/*path", func(c *gin.Context) {
|
2020-06-25 14:54:04 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourcePhotos, acl.ActionImport)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2019-07-02 22:03:23 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
conf := service.Config()
|
|
|
|
|
2020-04-20 10:38:01 +02:00
|
|
|
if conf.ReadOnly() || !conf.Settings().Features.Import {
|
2020-07-07 10:51:55 +02:00
|
|
|
AbortFeatureDisabled(c)
|
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 {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortBadRequest(c)
|
2020-01-31 15:29:06 +01:00
|
|
|
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-11-22 01:30:48 +01:00
|
|
|
ClearFoldersCache(entity.RootImport)
|
|
|
|
|
2020-01-31 15:29:06 +01:00
|
|
|
var opt photoprism.ImportOptions
|
|
|
|
|
|
|
|
if f.Move {
|
2020-07-07 10:51:55 +02:00
|
|
|
event.InfoMsg(i18n.MsgMovingFilesFrom, txt.Quote(filepath.Base(path)))
|
2020-01-31 15:29:06 +01:00
|
|
|
opt = photoprism.ImportOptionsMove(path)
|
|
|
|
} else {
|
2020-07-07 10:51:55 +02:00
|
|
|
event.InfoMsg(i18n.MsgCopyingFilesFrom, txt.Quote(filepath.Base(path)))
|
2020-01-31 15:29:06 +01:00
|
|
|
opt = photoprism.ImportOptionsCopy(path)
|
|
|
|
}
|
|
|
|
|
2020-06-01 09:45:24 +02:00
|
|
|
if len(f.Albums) > 0 {
|
|
|
|
log.Debugf("import: files will be added to album %s", strings.Join(f.Albums, " and "))
|
|
|
|
opt.Albums = f.Albums
|
|
|
|
}
|
|
|
|
|
2020-01-31 15:29:06 +01:00
|
|
|
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-07-15 11:28:54 +02:00
|
|
|
log.Errorf("import: failed deleting 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 10:26:29 +02:00
|
|
|
moments := service.Moments()
|
|
|
|
|
|
|
|
if err := moments.Start(); err != nil {
|
2020-07-18 13:21:43 +02:00
|
|
|
log.Warnf("moments: %s", err)
|
2020-06-03 10:26:29 +02:00
|
|
|
}
|
|
|
|
|
2019-11-16 16:06:34 +01:00
|
|
|
elapsed := int(time.Since(start).Seconds())
|
2019-06-14 21:16:59 +02:00
|
|
|
|
2020-07-07 10:51:55 +02:00
|
|
|
msg := i18n.Msg(i18n.MsgImportCompletedIn, elapsed)
|
|
|
|
|
|
|
|
event.Success(msg)
|
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
|
|
|
|
2020-06-01 09:45:24 +02:00
|
|
|
for _, uid := range f.Albums {
|
|
|
|
PublishAlbumEvent(EntityUpdated, uid, c)
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
UpdateClientConfig()
|
2019-11-16 16:06:34 +01:00
|
|
|
|
2020-07-07 10:51:55 +02:00
|
|
|
c.JSON(http.StatusOK, i18n.Response{Code: http.StatusOK, Msg: msg})
|
2019-06-14 21:16:59 +02:00
|
|
|
})
|
|
|
|
}
|
2020-01-02 03:57:28 +01:00
|
|
|
|
|
|
|
// DELETE /api/v1/import
|
2020-06-25 14:54:04 +02:00
|
|
|
func CancelImport(router *gin.RouterGroup) {
|
2020-01-02 03:57:28 +01:00
|
|
|
router.DELETE("/import", func(c *gin.Context) {
|
2020-06-25 14:54:04 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourcePhotos, acl.ActionImport)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2020-01-02 03:57:28 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
conf := service.Config()
|
|
|
|
|
2020-06-09 08:37:43 +02:00
|
|
|
if conf.ReadOnly() || !conf.Settings().Features.Import {
|
2020-07-07 10:51:55 +02:00
|
|
|
AbortFeatureDisabled(c)
|
2020-06-09 08:37:43 +02:00
|
|
|
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
|
|
|
|
2020-07-07 10:51:55 +02:00
|
|
|
c.JSON(http.StatusOK, i18n.NewResponse(http.StatusOK, i18n.MsgImportCanceled))
|
2020-01-02 03:57:28 +01:00
|
|
|
})
|
|
|
|
}
|