2020-05-22 16:29:12 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-05-22 20:00:33 +02:00
|
|
|
"fmt"
|
2020-05-22 16:29:12 +02:00
|
|
|
"net/http"
|
2020-05-24 22:16:06 +02:00
|
|
|
"path/filepath"
|
2020-05-22 20:00:33 +02:00
|
|
|
"time"
|
2020-05-22 16:29:12 +02:00
|
|
|
|
2021-12-14 20:01:39 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/sanitize"
|
|
|
|
|
2020-05-22 16:29:12 +02:00
|
|
|
"github.com/gin-gonic/gin"
|
2020-06-22 21:21:02 +02:00
|
|
|
"github.com/gin-gonic/gin/binding"
|
2021-11-26 14:28:50 +01:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
2020-05-22 16:29:12 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2020-06-22 21:21:02 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2020-05-24 22:16:06 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2020-05-22 20:00:33 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
2020-05-22 16:29:12 +02:00
|
|
|
)
|
|
|
|
|
2021-10-09 13:57:28 +02:00
|
|
|
// FoldersResponse represents the folders API response.
|
2020-05-24 22:16:06 +02:00
|
|
|
type FoldersResponse struct {
|
|
|
|
Root string `json:"root,omitempty"`
|
|
|
|
Folders []entity.Folder `json:"folders"`
|
|
|
|
Files []entity.File `json:"files,omitempty"`
|
|
|
|
Recursive bool `json:"recursive,omitempty"`
|
2020-05-26 09:02:19 +02:00
|
|
|
Cached bool `json:"cached,omitempty"`
|
2020-05-24 22:16:06 +02:00
|
|
|
}
|
|
|
|
|
2021-11-26 14:28:50 +01:00
|
|
|
// SearchFoldersOriginals returns folders in originals as JSON.
|
|
|
|
//
|
|
|
|
// GET /api/v1/folders/originals
|
|
|
|
func SearchFoldersOriginals(router *gin.RouterGroup) {
|
|
|
|
conf := service.Config()
|
|
|
|
SearchFolders(router, "originals", entity.RootOriginals, conf.OriginalsPath())
|
|
|
|
}
|
|
|
|
|
|
|
|
// SearchFoldersImport returns import folders as JSON.
|
|
|
|
//
|
|
|
|
// GET /api/v1/folders/import
|
|
|
|
func SearchFoldersImport(router *gin.RouterGroup) {
|
|
|
|
conf := service.Config()
|
|
|
|
SearchFolders(router, "import", entity.RootImport, conf.ImportPath())
|
|
|
|
}
|
|
|
|
|
|
|
|
// SearchFolders is a reusable request handler for directory listings (GET /api/v1/folders/*).
|
|
|
|
func SearchFolders(router *gin.RouterGroup, urlPath, rootName, rootPath string) {
|
2020-05-24 22:16:06 +02:00
|
|
|
handler := func(c *gin.Context) {
|
2020-06-25 14:54:04 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourceFolders, acl.ActionSearch)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2020-05-22 16:29:12 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-26 14:28:50 +01:00
|
|
|
var f form.SearchFolders
|
2020-06-22 21:21:02 +02:00
|
|
|
|
2020-05-22 20:00:33 +02:00
|
|
|
start := time.Now()
|
2020-06-22 21:21:02 +02:00
|
|
|
err := c.MustBindWith(&f, binding.Form)
|
|
|
|
|
|
|
|
if err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortBadRequest(c)
|
2020-06-22 21:21:02 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-08 13:29:01 +01:00
|
|
|
cache := service.FolderCache()
|
2020-06-22 21:21:02 +02:00
|
|
|
recursive := f.Recursive
|
|
|
|
listFiles := f.Files
|
|
|
|
uncached := listFiles || f.Uncached
|
2020-05-30 01:41:47 +02:00
|
|
|
resp := FoldersResponse{Root: rootName, Recursive: recursive, Cached: !uncached}
|
2021-12-14 20:01:39 +01:00
|
|
|
path := sanitize.Path(c.Param("path"))
|
2020-05-22 16:29:12 +02:00
|
|
|
|
2021-01-08 13:29:01 +01:00
|
|
|
cacheKey := fmt.Sprintf("folder:%s:%t:%t", filepath.Join(rootName, path), recursive, listFiles)
|
2020-05-22 20:00:33 +02:00
|
|
|
|
2020-05-30 01:41:47 +02:00
|
|
|
if !uncached {
|
2021-01-08 13:29:01 +01:00
|
|
|
if cacheData, ok := cache.Get(cacheKey); ok {
|
|
|
|
cached := cacheData.(FoldersResponse)
|
|
|
|
|
2021-11-27 18:41:10 +01:00
|
|
|
log.Tracef("api: cache hit for %s [%s]", cacheKey, time.Since(start))
|
2021-01-08 13:29:01 +01:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, cached)
|
|
|
|
return
|
2020-05-25 19:10:44 +02:00
|
|
|
}
|
2020-05-22 20:00:33 +02:00
|
|
|
}
|
|
|
|
|
2020-05-27 13:40:21 +02:00
|
|
|
if folders, err := query.FoldersByPath(rootName, rootPath, path, recursive); err != nil {
|
2021-01-08 13:29:01 +01:00
|
|
|
log.Errorf("folder: %s", err)
|
2020-05-24 22:16:06 +02:00
|
|
|
c.JSON(http.StatusOK, resp)
|
|
|
|
return
|
2020-05-22 20:00:33 +02:00
|
|
|
} else {
|
2020-05-24 22:16:06 +02:00
|
|
|
resp.Folders = folders
|
|
|
|
}
|
2020-05-22 20:00:33 +02:00
|
|
|
|
2020-05-24 22:16:06 +02:00
|
|
|
if listFiles {
|
2020-06-22 21:21:02 +02:00
|
|
|
if files, err := query.FilesByPath(f.Count, f.Offset, rootName, path); err != nil {
|
2021-01-08 13:29:01 +01:00
|
|
|
log.Errorf("folder: %s", err)
|
2020-05-24 22:16:06 +02:00
|
|
|
} else {
|
|
|
|
resp.Files = files
|
|
|
|
}
|
2020-05-22 16:29:12 +02:00
|
|
|
}
|
|
|
|
|
2020-05-30 01:41:47 +02:00
|
|
|
if !uncached {
|
2021-01-08 13:29:01 +01:00
|
|
|
cache.SetDefault(cacheKey, resp)
|
|
|
|
log.Debugf("cached %s [%s]", cacheKey, time.Since(start))
|
2020-05-26 09:02:19 +02:00
|
|
|
}
|
2020-05-24 22:16:06 +02:00
|
|
|
|
2021-01-08 09:02:30 +01:00
|
|
|
AddFileCountHeaders(c, len(resp.Files), len(resp.Folders))
|
|
|
|
AddCountHeader(c, len(resp.Files)+len(resp.Folders))
|
|
|
|
AddLimitHeader(c, f.Count)
|
|
|
|
AddOffsetHeader(c, f.Offset)
|
|
|
|
AddTokenHeaders(c)
|
2020-05-24 22:16:06 +02:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, resp)
|
|
|
|
}
|
|
|
|
|
2020-05-27 13:40:21 +02:00
|
|
|
router.GET("/folders/"+urlPath, handler)
|
|
|
|
router.GET("/folders/"+urlPath+"/*path", handler)
|
2020-05-22 16:29:12 +02:00
|
|
|
}
|