2019-06-15 20:03:54 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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-06-25 14:54:04 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
2020-11-22 01:30:48 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
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"
|
2020-07-07 10:51:55 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
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"
|
2021-12-14 20:01:39 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/sanitize"
|
2020-01-12 14:00:56 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2019-06-15 20:03:54 +02:00
|
|
|
)
|
|
|
|
|
2021-08-31 13:33:27 +02:00
|
|
|
// StartIndexing indexes media files in the "originals" folder.
|
|
|
|
//
|
2020-01-02 05:28:40 +01:00
|
|
|
// POST /api/v1/index
|
2020-06-25 14:54:04 +02:00
|
|
|
func StartIndexing(router *gin.RouterGroup) {
|
2020-01-02 05:28:40 +01:00
|
|
|
router.POST("/index", func(c *gin.Context) {
|
2020-06-25 14:54:04 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourcePhotos, acl.ActionUpdate)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2019-11-11 21:10:41 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
conf := service.Config()
|
|
|
|
|
2020-06-09 08:37:43 +02:00
|
|
|
if !conf.Settings().Features.Library {
|
2020-07-07 10:51:55 +02:00
|
|
|
AbortFeatureDisabled(c)
|
2020-06-09 08:37:43 +02:00
|
|
|
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-07-04 12:54:35 +02:00
|
|
|
AbortBadRequest(c)
|
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
|
|
|
|
2022-04-02 18:04:02 +02:00
|
|
|
convert := conf.Settings().Index.Convert && conf.SidecarWritable()
|
|
|
|
indOpt := photoprism.NewIndexOptions(filepath.Clean(f.Path), f.Rescan, convert, true, false)
|
2020-05-06 15:53:47 +02:00
|
|
|
|
2020-05-07 21:46:00 +02:00
|
|
|
if len(indOpt.Path) > 1 {
|
2021-12-14 20:01:39 +01:00
|
|
|
event.InfoMsg(i18n.MsgIndexingFiles, sanitize.Log(indOpt.Path))
|
2020-05-06 15:53:47 +02:00
|
|
|
} else {
|
2020-07-07 10:51:55 +02:00
|
|
|
event.InfoMsg(i18n.MsgIndexingOriginals)
|
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
|
|
|
|
2021-01-08 13:29:01 +01:00
|
|
|
RemoveFromFolderCache(entity.RootOriginals)
|
2020-11-22 01:30:48 +01:00
|
|
|
|
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 {
|
2020-07-07 10:51:55 +02:00
|
|
|
event.InfoMsg(i18n.MsgRemovedFilesAndPhotos, len(files), len(photos))
|
2020-05-07 21:46:00 +02:00
|
|
|
}
|
2020-05-06 10:35:41 +02:00
|
|
|
|
2020-12-09 21:44:04 +01:00
|
|
|
event.Publish("index.updating", event.Data{
|
|
|
|
"step": "moments",
|
|
|
|
})
|
|
|
|
|
2020-05-30 01:41:47 +02:00
|
|
|
moments := service.Moments()
|
|
|
|
|
|
|
|
if err := moments.Start(); err != nil {
|
2020-07-18 13:21:43 +02:00
|
|
|
log.Warnf("moments: %s", err)
|
2020-05-30 01:41:47 +02:00
|
|
|
}
|
|
|
|
|
2019-11-16 16:06:34 +01:00
|
|
|
elapsed := int(time.Since(start).Seconds())
|
2019-06-15 20:03:54 +02:00
|
|
|
|
2020-07-07 10:51:55 +02:00
|
|
|
msg := i18n.Msg(i18n.MsgIndexingCompletedIn, elapsed)
|
|
|
|
|
|
|
|
event.Success(msg)
|
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
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
UpdateClientConfig()
|
2019-11-16 16:06:34 +01:00
|
|
|
|
2020-07-07 10:51:55 +02:00
|
|
|
c.JSON(http.StatusOK, i18n.Response{Code: http.StatusOK, Msg: msg})
|
2019-06-15 20:03:54 +02:00
|
|
|
})
|
|
|
|
}
|
2020-01-02 02:58:26 +01:00
|
|
|
|
2021-08-31 13:33:27 +02:00
|
|
|
// CancelIndexing stops indexing media files in the "originals" folder.
|
|
|
|
//
|
2020-01-02 05:28:40 +01:00
|
|
|
// DELETE /api/v1/index
|
2020-06-25 14:54:04 +02:00
|
|
|
func CancelIndexing(router *gin.RouterGroup) {
|
2020-01-02 05:28:40 +01:00
|
|
|
router.DELETE("/index", func(c *gin.Context) {
|
2020-06-25 14:54:04 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourcePhotos, acl.ActionUpdate)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2020-01-02 02:58:26 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
conf := service.Config()
|
|
|
|
|
2020-06-09 08:37:43 +02:00
|
|
|
if !conf.Settings().Features.Library {
|
2020-07-07 10:51:55 +02:00
|
|
|
AbortFeatureDisabled(c)
|
2020-06-09 08:37:43 +02:00
|
|
|
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
|
|
|
|
2020-07-07 10:51:55 +02:00
|
|
|
c.JSON(http.StatusOK, i18n.NewResponse(http.StatusOK, i18n.MsgIndexingCanceled))
|
2020-01-02 02:58:26 +01:00
|
|
|
})
|
|
|
|
}
|