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"
|
2019-06-14 21:16:59 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
2020-01-13 11:07:09 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2019-06-14 21:16:59 +02:00
|
|
|
)
|
|
|
|
|
2020-01-02 05:03:47 +01:00
|
|
|
var imp *photoprism.Import
|
2019-06-14 21:16:59 +02:00
|
|
|
|
2020-01-02 05:03:47 +01:00
|
|
|
func initImport(conf *config.Config) {
|
|
|
|
if imp != nil {
|
2019-06-14 21:16:59 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-02 05:03:47 +01:00
|
|
|
initIndex(conf)
|
2019-06-14 21:16:59 +02:00
|
|
|
|
2020-01-02 05:03:47 +01:00
|
|
|
convert := photoprism.NewConvert(conf)
|
2019-06-14 21:16:59 +02:00
|
|
|
|
2020-01-02 05:03:47 +01:00
|
|
|
imp = photoprism.NewImport(conf, ind, convert)
|
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) {
|
2019-07-02 22:03:23 +02:00
|
|
|
if conf.ReadOnly() {
|
|
|
|
c.AbortWithStatusJSON(http.StatusForbidden, ErrReadOnly)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-12 05:49:10 +01:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-15 17:43:17 +01:00
|
|
|
subPath := ""
|
2019-06-14 21:16:59 +02:00
|
|
|
start := time.Now()
|
|
|
|
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)
|
2019-06-14 21:16:59 +02:00
|
|
|
log.Debugf("import sub path: %s", subPath)
|
|
|
|
path = path + subPath
|
|
|
|
}
|
|
|
|
|
2020-01-04 07:56:56 +01:00
|
|
|
path = filepath.Clean(path)
|
|
|
|
|
2019-11-16 16:06:34 +01:00
|
|
|
event.Info(fmt.Sprintf("importing photos from \"%s\"", filepath.Base(path)))
|
2019-06-14 21:16:59 +02:00
|
|
|
|
2020-01-02 05:03:47 +01:00
|
|
|
initImport(conf)
|
2019-06-14 21:16:59 +02:00
|
|
|
|
2020-01-02 05:03:47 +01:00
|
|
|
imp.Start(path)
|
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 {
|
|
|
|
log.Errorf("import: could not deleted empty directory \"%s\": %s", path, err)
|
|
|
|
} else {
|
|
|
|
log.Infof("import: deleted empty directory \"%s\"", path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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})
|
2019-11-16 23:22:50 +01:00
|
|
|
event.Publish("config.updated", event.Data(conf.ClientConfig()))
|
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-01-02 05:03:47 +01:00
|
|
|
initImport(conf)
|
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"})
|
|
|
|
})
|
|
|
|
}
|