2019-06-15 20:03:54 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2019-11-16 16:06:34 +01:00
|
|
|
"path/filepath"
|
2019-06-15 20:03:54 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2020-01-09 01:21:09 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/classify"
|
2019-11-11 21:10:41 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2019-11-16 16:06:34 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2019-12-09 08:04:41 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2019-12-14 20:35:14 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/nsfw"
|
2019-06-15 20:03:54 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
2020-01-12 14:00:56 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2019-06-15 20:03:54 +02:00
|
|
|
)
|
|
|
|
|
2020-01-02 05:03:47 +01:00
|
|
|
var ind *photoprism.Index
|
|
|
|
var nd *nsfw.Detector
|
2019-06-15 20:03:54 +02:00
|
|
|
|
2020-01-02 05:03:47 +01:00
|
|
|
func initIndex(conf *config.Config) {
|
|
|
|
if ind != nil {
|
2019-06-15 20:03:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-15 17:19:16 +01:00
|
|
|
initNsfwDetector(conf)
|
|
|
|
|
2020-01-09 01:21:09 +01:00
|
|
|
tf := classify.New(conf.ResourcesPath(), conf.TensorFlowDisabled())
|
2019-06-15 20:03:54 +02:00
|
|
|
|
2020-01-02 05:03:47 +01:00
|
|
|
ind = photoprism.NewIndex(conf, tf, nd)
|
2019-06-15 20:03:54 +02:00
|
|
|
}
|
|
|
|
|
2019-12-15 17:19:16 +01:00
|
|
|
func initNsfwDetector(conf *config.Config) {
|
2020-01-02 05:03:47 +01:00
|
|
|
if nd != nil {
|
2019-12-15 17:19:16 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-09 01:21:09 +01:00
|
|
|
nd = nsfw.New(conf.NSFWModelPath())
|
2019-12-15 17:19:16 +01:00
|
|
|
}
|
|
|
|
|
2020-01-02 05:28:40 +01:00
|
|
|
// POST /api/v1/index
|
2020-01-02 02:58:26 +01:00
|
|
|
func StartIndexing(router *gin.RouterGroup, conf *config.Config) {
|
2020-01-02 05:28:40 +01:00
|
|
|
router.POST("/index", func(c *gin.Context) {
|
2019-11-11 21:10:41 +01:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-15 20:03:54 +02:00
|
|
|
start := time.Now()
|
2019-12-09 08:04:41 +01:00
|
|
|
|
2020-01-02 05:03:47 +01:00
|
|
|
var f form.IndexOptions
|
2019-12-09 08:04:41 +01:00
|
|
|
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
2020-01-07 17:36:49 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": txt.UcFirst(err.Error())})
|
2019-12-09 08:04:41 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-15 20:03:54 +02:00
|
|
|
path := conf.OriginalsPath()
|
|
|
|
|
2019-11-16 16:06:34 +01:00
|
|
|
event.Info(fmt.Sprintf("indexing photos in \"%s\"", filepath.Base(path)))
|
2019-06-15 20:03:54 +02:00
|
|
|
|
2020-01-08 22:29:40 +01:00
|
|
|
cancel := func(err error) {
|
|
|
|
log.Error(err.Error())
|
|
|
|
event.Publish("index.completed", event.Data{"path": path, "seconds": int(time.Since(start).Seconds())})
|
|
|
|
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
|
|
|
|
}
|
|
|
|
|
2019-12-11 05:04:41 +01:00
|
|
|
if f.ConvertRaw && !conf.ReadOnly() {
|
2020-01-02 05:03:47 +01:00
|
|
|
convert := photoprism.NewConvert(conf)
|
2020-01-08 22:29:40 +01:00
|
|
|
|
|
|
|
if err := convert.Start(conf.OriginalsPath()); err != nil {
|
|
|
|
cancel(err)
|
|
|
|
return
|
|
|
|
}
|
2019-12-11 04:12:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if f.CreateThumbs {
|
2020-01-08 22:29:40 +01:00
|
|
|
thumbnails := photoprism.NewThumbnails(conf)
|
|
|
|
|
|
|
|
if err := thumbnails.Start(false); err != nil {
|
|
|
|
cancel(err)
|
|
|
|
return
|
2019-12-11 04:12:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-02 05:03:47 +01:00
|
|
|
initIndex(conf)
|
2019-06-15 20:03:54 +02:00
|
|
|
|
2019-12-11 04:12:54 +01:00
|
|
|
if f.SkipUnchanged {
|
2020-01-02 05:03:47 +01:00
|
|
|
ind.Start(photoprism.IndexOptionsNone())
|
2019-12-09 08:04:41 +01:00
|
|
|
} else {
|
2020-01-02 05:03:47 +01:00
|
|
|
ind.Start(photoprism.IndexOptionsAll())
|
2019-12-09 08:04:41 +01:00
|
|
|
}
|
2019-06-15 20:03:54 +02:00
|
|
|
|
2019-11-16 16:06:34 +01:00
|
|
|
elapsed := int(time.Since(start).Seconds())
|
2019-06-15 20:03:54 +02:00
|
|
|
|
2019-11-16 16:06:34 +01:00
|
|
|
event.Success(fmt.Sprintf("indexing completed in %d s", 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
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("indexing completed in %d s", elapsed)})
|
2019-06-15 20:03:54 +02:00
|
|
|
})
|
|
|
|
}
|
2020-01-02 02:58:26 +01:00
|
|
|
|
2020-01-02 05:28:40 +01:00
|
|
|
// DELETE /api/v1/index
|
2020-01-02 02:58:26 +01:00
|
|
|
func CancelIndexing(router *gin.RouterGroup, conf *config.Config) {
|
2020-01-02 05:28:40 +01:00
|
|
|
router.DELETE("/index", func(c *gin.Context) {
|
2020-01-02 02:58:26 +01:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-02 05:03:47 +01:00
|
|
|
initIndex(conf)
|
2020-01-02 02:58:26 +01:00
|
|
|
|
2020-01-02 05:03:47 +01:00
|
|
|
ind.Cancel()
|
2020-01-02 02:58:26 +01:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "indexing canceled"})
|
|
|
|
})
|
|
|
|
}
|