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"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2019-11-16 16:06:34 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2019-12-15 17:43:17 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/util"
|
2019-06-14 21:16:59 +02:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
|
|
|
)
|
|
|
|
|
|
|
|
var importer *photoprism.Importer
|
|
|
|
|
|
|
|
func initImporter(conf *config.Config) {
|
|
|
|
if importer != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-15 20:03:54 +02:00
|
|
|
initIndexer(conf)
|
2019-06-14 21:16:59 +02:00
|
|
|
|
|
|
|
converter := photoprism.NewConverter(conf)
|
|
|
|
|
|
|
|
importer = photoprism.NewImporter(conf, indexer, converter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// POST /api/v1/import
|
|
|
|
func Import(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
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()
|
|
|
|
|
2019-12-15 17:43:17 +01:00
|
|
|
if subPath = c.Param("path"); subPath != "" {
|
|
|
|
subPath = strings.Replace(subPath, ".", "", -1)
|
2019-06-14 21:16:59 +02:00
|
|
|
log.Debugf("import sub path: %s", subPath)
|
|
|
|
path = path + subPath
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
initImporter(conf)
|
|
|
|
|
|
|
|
importer.ImportPhotosFromDirectory(path)
|
|
|
|
|
2019-12-15 17:43:17 +01:00
|
|
|
if subPath != "" && util.DirectoryIsEmpty(path) {
|
|
|
|
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})
|
2019-11-17 00:55:40 +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
|
|
|
})
|
|
|
|
}
|