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-06-15 20:03:54 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
2020-04-05 22:26:53 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
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: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()
|
|
|
|
|
2020-04-05 22:26:53 +02:00
|
|
|
ind := service.Index()
|
2019-06-15 20:03:54 +02:00
|
|
|
|
2020-05-07 21:46:00 +02:00
|
|
|
indOpt := photoprism.IndexOptions{
|
2020-05-06 15:53:47 +02:00
|
|
|
Rescan: f.Rescan,
|
2020-05-06 10:35:41 +02:00
|
|
|
Convert: f.Convert && !conf.ReadOnly(),
|
2020-05-06 15:53:47 +02:00
|
|
|
Path: filepath.Clean(f.Path),
|
|
|
|
}
|
|
|
|
|
2020-05-07 21:46:00 +02:00
|
|
|
if len(indOpt.Path) > 1 {
|
|
|
|
event.Info(fmt.Sprintf("indexing files in %s", txt.Quote(indOpt.Path)))
|
2020-05-06 15:53:47 +02:00
|
|
|
} else {
|
|
|
|
event.Info("indexing originals...")
|
2019-12-09 08:04:41 +01:00
|
|
|
}
|
2019-06-15 20:03:54 +02:00
|
|
|
|
2020-05-08 07:43:14 +02:00
|
|
|
indexed := ind.Start(indOpt)
|
2020-05-07 21:46:00 +02:00
|
|
|
|
|
|
|
prg := service.Purge()
|
|
|
|
|
|
|
|
prgOpt := photoprism.PurgeOptions{
|
2020-05-08 07:43:14 +02:00
|
|
|
Path: filepath.Clean(f.Path),
|
|
|
|
Ignore: indexed,
|
2020-05-07 21:46:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if files, photos, err := prg.Start(prgOpt); err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": txt.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
} else if len(files) > 0 || len(photos) > 0 {
|
|
|
|
event.Info(fmt.Sprintf("removed %d files and %d photos", len(files), len(photos)))
|
|
|
|
}
|
2020-05-06 10:35:41 +02:00
|
|
|
|
2020-05-30 01:41:47 +02:00
|
|
|
moments := service.Moments()
|
|
|
|
|
|
|
|
if err := moments.Start(); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
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})
|
2020-05-27 19:38:40 +02:00
|
|
|
|
|
|
|
UpdateClientConfig(conf)
|
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-04-05 22:26:53 +02:00
|
|
|
ind := service.Index()
|
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"})
|
|
|
|
})
|
|
|
|
}
|