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
|
|
|
)
|
|
|
|
|
|
|
|
var indexer *photoprism.Indexer
|
2019-12-15 17:19:16 +01:00
|
|
|
var nsfwDetector *nsfw.Detector
|
2019-06-15 20:03:54 +02:00
|
|
|
|
|
|
|
func initIndexer(conf *config.Config) {
|
|
|
|
if indexer != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-15 17:19:16 +01:00
|
|
|
initNsfwDetector(conf)
|
|
|
|
|
2019-06-15 20:03:54 +02:00
|
|
|
tensorFlow := photoprism.NewTensorFlow(conf)
|
|
|
|
|
2019-12-14 20:35:14 +01:00
|
|
|
indexer = photoprism.NewIndexer(conf, tensorFlow, nsfwDetector)
|
2019-06-15 20:03:54 +02:00
|
|
|
}
|
|
|
|
|
2019-12-15 17:19:16 +01:00
|
|
|
func initNsfwDetector(conf *config.Config) {
|
|
|
|
if nsfwDetector != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
nsfwDetector = nsfw.NewDetector(conf.NSFWModelPath())
|
|
|
|
}
|
|
|
|
|
2019-06-15 20:03:54 +02:00
|
|
|
// POST /api/v1/index
|
|
|
|
func Index(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
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
|
|
|
|
|
|
|
var f form.IndexerOptions
|
|
|
|
|
|
|
|
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() {
|
2019-12-11 04:12:54 +01:00
|
|
|
converter := photoprism.NewConverter(conf)
|
|
|
|
converter.ConvertAll(conf.OriginalsPath())
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.CreateThumbs {
|
|
|
|
if err := photoprism.CreateThumbnailsFromOriginals(conf.OriginalsPath(), conf.ThumbnailsPath(), false); err != nil {
|
|
|
|
event.Error(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-15 20:03:54 +02:00
|
|
|
initIndexer(conf)
|
|
|
|
|
2019-12-11 04:12:54 +01:00
|
|
|
if f.SkipUnchanged {
|
2019-12-09 08:04:41 +01:00
|
|
|
indexer.IndexOriginals(photoprism.IndexerOptionsNone())
|
|
|
|
} else {
|
|
|
|
indexer.IndexOriginals(photoprism.IndexerOptionsAll())
|
|
|
|
}
|
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))
|
|
|
|
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
|
|
|
})
|
|
|
|
}
|