2019-06-14 21:16:59 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2019-12-15 17:43:17 +01:00
|
|
|
"os"
|
2022-09-30 00:42:19 +02:00
|
|
|
"path"
|
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"
|
|
|
|
|
2022-09-30 00:42:19 +02:00
|
|
|
"github.com/dustin/go-humanize/english"
|
2020-01-06 14:32:15 +01:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-12-14 20:01:39 +01:00
|
|
|
|
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"
|
2022-10-15 21:54:11 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/get"
|
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"
|
2021-12-14 20:01:39 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2020-01-13 11:07:09 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2019-06-14 21:16:59 +02:00
|
|
|
)
|
|
|
|
|
2022-09-30 00:42:19 +02:00
|
|
|
const (
|
|
|
|
UploadPath = "/upload"
|
|
|
|
)
|
|
|
|
|
2021-08-31 13:33:27 +02:00
|
|
|
// StartImport imports media files from a directory and converts/indexes them as needed.
|
|
|
|
//
|
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) {
|
2022-09-30 00:42:19 +02:00
|
|
|
s := AuthAny(c, acl.ResourceFiles, acl.Permissions{acl.ActionManage, acl.ActionUpload})
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2019-07-02 22:03:23 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
conf := get.Config()
|
2020-06-25 14:54:04 +02:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-09-30 00:42:19 +02:00
|
|
|
srcFolder := ""
|
|
|
|
importPath := conf.ImportPath()
|
2019-06-14 21:16:59 +02:00
|
|
|
|
2023-02-14 11:37:22 +01:00
|
|
|
// Import from subfolder?
|
2022-10-27 15:47:32 +02:00
|
|
|
if srcFolder = c.Param("path"); srcFolder != "" && srcFolder != "/" {
|
|
|
|
srcFolder = clean.UserPath(srcFolder)
|
2020-05-06 15:53:47 +02:00
|
|
|
} else if f.Path != "" {
|
2022-10-27 15:47:32 +02:00
|
|
|
srcFolder = clean.UserPath(f.Path)
|
2022-09-30 00:42:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// To avoid conflicts, uploads are imported from "import_path/upload/session_ref/timestamp".
|
|
|
|
if token := path.Base(srcFolder); token != "" && path.Dir(srcFolder) == UploadPath {
|
|
|
|
srcFolder = path.Join(UploadPath, s.RefID+token)
|
|
|
|
event.AuditInfo([]string{ClientIP(c), "session %s", "import uploads from %s as %s", "granted"}, s.RefID, clean.Log(srcFolder), s.User().AclRole().String())
|
|
|
|
} else if acl.Resources.Deny(acl.ResourceFiles, s.User().AclRole(), acl.ActionManage) {
|
|
|
|
event.AuditErr([]string{ClientIP(c), "session %s", "import files from %s as %s", "denied"}, s.RefID, clean.Log(srcFolder), s.User().AclRole().String())
|
|
|
|
AbortForbidden(c)
|
|
|
|
return
|
2019-06-14 21:16:59 +02:00
|
|
|
}
|
|
|
|
|
2022-09-30 00:42:19 +02:00
|
|
|
importPath = path.Join(importPath, srcFolder)
|
2020-01-04 07:56:56 +01:00
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
imp := get.Import()
|
2019-06-14 21:16:59 +02:00
|
|
|
|
2021-01-08 13:29:01 +01:00
|
|
|
RemoveFromFolderCache(entity.RootImport)
|
2020-11-22 01:30:48 +01:00
|
|
|
|
2023-03-14 21:47:14 +01:00
|
|
|
// Get destination folder.
|
2022-09-30 00:42:19 +02:00
|
|
|
var destFolder string
|
2023-02-20 15:54:33 +01:00
|
|
|
if destFolder = s.User().GetUploadPath(); destFolder == "" {
|
2022-09-30 00:42:19 +02:00
|
|
|
destFolder = conf.ImportDest()
|
|
|
|
}
|
|
|
|
|
2020-01-31 15:29:06 +01:00
|
|
|
var opt photoprism.ImportOptions
|
|
|
|
|
2022-09-30 00:42:19 +02:00
|
|
|
// Copy or move files to the destination folder?
|
2020-01-31 15:29:06 +01:00
|
|
|
if f.Move {
|
2022-09-30 00:42:19 +02:00
|
|
|
event.InfoMsg(i18n.MsgMovingFilesFrom, clean.Log(filepath.Base(importPath)))
|
|
|
|
opt = photoprism.ImportOptionsMove(importPath, destFolder)
|
2020-01-31 15:29:06 +01:00
|
|
|
} else {
|
2022-09-30 00:42:19 +02:00
|
|
|
event.InfoMsg(i18n.MsgCopyingFilesFrom, clean.Log(filepath.Base(importPath)))
|
|
|
|
opt = photoprism.ImportOptionsCopy(importPath, destFolder)
|
2020-01-31 15:29:06 +01:00
|
|
|
}
|
|
|
|
|
2022-09-30 00:42:19 +02:00
|
|
|
// Add imported files to albums if allowed.
|
|
|
|
if len(f.Albums) > 0 &&
|
|
|
|
acl.Resources.AllowAny(acl.ResourceAlbums, s.User().AclRole(), acl.Permissions{acl.ActionCreate, acl.ActionUpload}) {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Debugf("import: adding files to album %s", clean.Log(strings.Join(f.Albums, " and ")))
|
2020-06-01 09:45:24 +02:00
|
|
|
opt.Albums = f.Albums
|
|
|
|
}
|
|
|
|
|
2022-09-30 19:15:10 +02:00
|
|
|
// Set user UID if known.
|
|
|
|
if s.UserUID != "" {
|
2023-03-11 14:09:00 +01:00
|
|
|
opt.UID = s.UserUID
|
2022-09-30 19:15:10 +02:00
|
|
|
}
|
|
|
|
|
2022-09-30 00:42:19 +02:00
|
|
|
// Start import.
|
|
|
|
imported := imp.Start(opt)
|
2019-06-14 21:16:59 +02:00
|
|
|
|
2022-09-30 00:42:19 +02:00
|
|
|
// Delete empty import directory.
|
|
|
|
if srcFolder != "" && importPath != conf.ImportPath() && fs.DirIsEmpty(importPath) {
|
|
|
|
if err := os.Remove(importPath); err != nil {
|
|
|
|
log.Errorf("import: failed deleting empty folder %s: %s", clean.Log(importPath), err)
|
2019-12-15 17:43:17 +01:00
|
|
|
} else {
|
2022-09-30 00:42:19 +02:00
|
|
|
log.Infof("import: deleted empty folder %s", clean.Log(importPath))
|
2019-12-15 17:43:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 00:42:19 +02:00
|
|
|
// Update moments if files have been imported.
|
|
|
|
if n := len(imported); n == 0 {
|
|
|
|
log.Infof("import: no new files found to import", clean.Log(importPath))
|
|
|
|
} else {
|
|
|
|
log.Infof("import: imported %s", english.Plural(n, "file", "files"))
|
2022-10-15 21:54:11 +02:00
|
|
|
if moments := get.Moments(); moments == nil {
|
2022-09-30 00:42:19 +02:00
|
|
|
log.Warnf("import: moments service not set - possible bug")
|
|
|
|
} else if err := moments.Start(); err != nil {
|
|
|
|
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
|
|
|
|
2022-09-30 00:42:19 +02:00
|
|
|
// Show success message.
|
2020-07-07 10:51:55 +02:00
|
|
|
msg := i18n.Msg(i18n.MsgImportCompletedIn, elapsed)
|
|
|
|
|
|
|
|
event.Success(msg)
|
2023-03-11 14:09:00 +01:00
|
|
|
|
|
|
|
eventData := event.Data{
|
|
|
|
"uid": opt.UID,
|
|
|
|
"action": opt.Action,
|
|
|
|
"path": importPath,
|
|
|
|
"seconds": elapsed,
|
|
|
|
}
|
|
|
|
|
|
|
|
event.Publish("import.completed", eventData)
|
|
|
|
event.Publish("index.completed", eventData)
|
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)
|
|
|
|
}
|
|
|
|
|
2022-09-30 00:42:19 +02:00
|
|
|
// Update the user interface.
|
2020-06-25 14:54:04 +02:00
|
|
|
UpdateClientConfig()
|
2019-11-16 16:06:34 +01:00
|
|
|
|
2021-10-01 16:44:50 +02:00
|
|
|
// Update album, label, and subject cover thumbs.
|
|
|
|
if err := query.UpdateCovers(); err != nil {
|
2021-10-02 14:24:44 +02:00
|
|
|
log.Warnf("index: %s (update covers)", err)
|
2021-08-30 18:58:27 +02: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
|
|
|
|
2021-08-31 13:33:27 +02:00
|
|
|
// CancelImport stops the current import operation.
|
|
|
|
//
|
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) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourceFiles, acl.ActionManage)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2020-01-02 03:57:28 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
conf := get.Config()
|
2020-06-25 14:54:04 +02:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
imp := get.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
|
|
|
})
|
|
|
|
}
|