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"
|
2022-04-16 12:39:47 +02:00
|
|
|
|
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"
|
2022-10-15 21:54:11 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/get"
|
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"
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
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) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourcePhotos, acl.ActionUpdate)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2019-11-11 21:10:41 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
conf := get.Config()
|
2022-04-16 12:39:47 +02:00
|
|
|
settings := conf.Settings()
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-04-16 12:39:47 +02:00
|
|
|
if !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
|
|
|
|
}
|
|
|
|
|
2022-04-16 12:39:47 +02:00
|
|
|
// Configure index options.
|
2019-06-15 20:03:54 +02:00
|
|
|
path := conf.OriginalsPath()
|
2022-04-16 12:39:47 +02:00
|
|
|
convert := settings.Index.Convert && conf.SidecarWritable()
|
|
|
|
skipArchived := settings.Index.SkipArchived
|
2019-06-15 20:03:54 +02:00
|
|
|
|
2022-04-16 12:39:47 +02:00
|
|
|
indOpt := photoprism.NewIndexOptions(filepath.Clean(f.Path), f.Rescan, convert, true, false, skipArchived)
|
2020-05-06 15:53:47 +02:00
|
|
|
|
2020-05-07 21:46:00 +02:00
|
|
|
if len(indOpt.Path) > 1 {
|
2022-04-15 09:42:07 +02:00
|
|
|
event.InfoMsg(i18n.MsgIndexingFiles, clean.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
|
|
|
|
2022-04-16 12:39:47 +02:00
|
|
|
// Start indexing.
|
2022-10-15 21:54:11 +02:00
|
|
|
ind := get.Index()
|
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
|
|
|
|
2022-04-16 12:39:47 +02:00
|
|
|
// Configure purge options.
|
2020-05-07 21:46:00 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-04-16 12:39:47 +02:00
|
|
|
// Start purging.
|
2022-10-15 21:54:11 +02:00
|
|
|
prg := get.Purge()
|
2022-04-16 12:39:47 +02:00
|
|
|
|
2020-05-07 21:46:00 +02:00
|
|
|
if files, photos, err := prg.Start(prgOpt); err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": txt.UpperFirst(err.Error())})
|
2020-05-07 21:46:00 +02:00
|
|
|
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",
|
|
|
|
})
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
moments := get.Moments()
|
2020-05-30 01:41:47 +02:00
|
|
|
|
|
|
|
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) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourcePhotos, acl.ActionUpdate)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2020-01-02 02:58:26 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
conf := get.Config()
|
2020-06-25 14:54:04 +02:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
ind := get.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
|
|
|
})
|
|
|
|
}
|