2019-06-14 21:16:59 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
|
|
|
|
|
|
|
"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-06-14 21:16:59 +02:00
|
|
|
start := time.Now()
|
|
|
|
path := conf.ImportPath()
|
|
|
|
|
|
|
|
if subPath := c.Param("path"); subPath != "" {
|
|
|
|
log.Debugf("import sub path: %s", subPath)
|
|
|
|
path = path + subPath
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("importing photos from %s", path)
|
|
|
|
|
|
|
|
initImporter(conf)
|
|
|
|
|
|
|
|
importer.ImportPhotosFromDirectory(path)
|
|
|
|
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
2019-06-14 22:01:58 +02:00
|
|
|
c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("completed import in %s", elapsed)})
|
2019-06-14 21:16:59 +02:00
|
|
|
})
|
|
|
|
}
|