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"
|
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"
|
2019-12-09 08:04:41 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/util"
|
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-02 05:03:47 +01:00
|
|
|
tf := photoprism.NewTensorFlow(conf)
|
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-02 05:03:47 +01:00
|
|
|
nd = nsfw.NewDetector(conf.NSFWModelPath())
|
2019-12-15 17:19:16 +01:00
|
|
|
}
|
|
|
|
|
2020-01-02 05:03:47 +01:00
|
|
|
// POST /api/v1/ind
|
2020-01-02 02:58:26 +01:00
|
|
|
func StartIndexing(router *gin.RouterGroup, conf *config.Config) {
|
2020-01-02 05:03:47 +01:00
|
|
|
router.POST("/ind", 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 {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": util.UcFirst(err.Error())})
|
|
|
|
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
|
|
|
|
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)
|
|
|
|
convert.Path(conf.OriginalsPath())
|
2019-12-11 04:12:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if f.CreateThumbs {
|
|
|
|
if err := photoprism.CreateThumbnailsFromOriginals(conf.OriginalsPath(), conf.ThumbnailsPath(), false); err != nil {
|
|
|
|
event.Error(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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:03:47 +01:00
|
|
|
event.Publish("ind.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:03:47 +01:00
|
|
|
// DELETE /api/v1/ind
|
2020-01-02 02:58:26 +01:00
|
|
|
func CancelIndexing(router *gin.RouterGroup, conf *config.Config) {
|
2020-01-02 05:03:47 +01:00
|
|
|
router.DELETE("/ind", 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"})
|
|
|
|
})
|
|
|
|
}
|