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
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2020-06-22 21:21:02 +02:00
|
|
|
"github.com/gin-gonic/gin/binding"
|
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
|
|
|
}
|
|
|
|
|
2020-05-22 16:29:12 +02:00
|
|
|
// GetFolders is a reusable request handler for directory listings (GET /api/v1/folders/*).
|
2020-06-25 14:54:04 +02:00
|
|
|
func GetFolders(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
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:21:02 +02:00
|
|
|
var f form.FolderSearch
|
|
|
|
|
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}
|
2020-05-24 22:16:06 +02:00
|
|
|
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-02-06 16:30:30 +01:00
|
|
|
log.Debugf("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
|
|
|
}
|
|
|
|
|
2021-10-09 13:57:28 +02:00
|
|
|
// GetFoldersOriginals returns folders in originals as JSON.
|
|
|
|
//
|
2020-05-22 16:29:12 +02:00
|
|
|
// GET /api/v1/folders/originals
|
2020-06-25 14:54:04 +02:00
|
|
|
func GetFoldersOriginals(router *gin.RouterGroup) {
|
|
|
|
conf := service.Config()
|
|
|
|
GetFolders(router, "originals", entity.RootOriginals, conf.OriginalsPath())
|
2020-05-22 16:29:12 +02:00
|
|
|
}
|
|
|
|
|
2021-10-09 13:57:28 +02:00
|
|
|
// GetFoldersImport returns import folders as JSON.
|
|
|
|
//
|
2020-05-22 16:29:12 +02:00
|
|
|
// GET /api/v1/folders/import
|
2020-06-25 14:54:04 +02:00
|
|
|
func GetFoldersImport(router *gin.RouterGroup) {
|
|
|
|
conf := service.Config()
|
|
|
|
GetFolders(router, "import", entity.RootImport, conf.ImportPath())
|
2020-05-22 16:29:12 +02:00
|
|
|
}
|