2020-05-22 16:29:12 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-05-30 01:41:47 +02:00
|
|
|
"encoding/json"
|
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"
|
|
|
|
"strconv"
|
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
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-30 01:41:47 +02:00
|
|
|
cache := service.Cache()
|
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
|
|
|
|
2020-05-24 22:16:06 +02:00
|
|
|
cacheKey := fmt.Sprintf("folders:%s:%t:%t", filepath.Join(rootPath, path), recursive, listFiles)
|
2020-05-22 20:00:33 +02:00
|
|
|
|
2020-05-30 01:41:47 +02:00
|
|
|
if !uncached {
|
|
|
|
if cacheData, err := cache.Get(cacheKey); err == nil {
|
|
|
|
var cached FoldersResponse
|
|
|
|
|
|
|
|
if err := json.Unmarshal(cacheData, &cached); err != nil {
|
|
|
|
log.Errorf("folders: %s", err)
|
|
|
|
} else {
|
|
|
|
log.Debugf("cache hit for %s [%s]", cacheKey, time.Since(start))
|
|
|
|
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 {
|
2020-05-22 20:00:33 +02:00
|
|
|
log.Errorf("folders: %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 {
|
2020-05-24 22:16:06 +02:00
|
|
|
log.Errorf("folders: %s", err)
|
|
|
|
} else {
|
|
|
|
resp.Files = files
|
|
|
|
}
|
2020-05-22 16:29:12 +02:00
|
|
|
}
|
|
|
|
|
2020-05-30 01:41:47 +02:00
|
|
|
if !uncached {
|
|
|
|
if c, err := json.Marshal(resp); err == nil {
|
|
|
|
logError("folders", cache.Set(cacheKey, c))
|
|
|
|
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
|
|
|
|
2020-06-22 21:21:02 +02:00
|
|
|
c.Header("X-Files", strconv.Itoa(len(resp.Files)))
|
|
|
|
c.Header("X-Folders", strconv.Itoa(len(resp.Folders)))
|
2020-05-25 19:10:44 +02:00
|
|
|
c.Header("X-Count", strconv.Itoa(len(resp.Files)+len(resp.Folders)))
|
2020-06-22 21:21:02 +02:00
|
|
|
c.Header("X-Limit", strconv.Itoa(f.Count))
|
|
|
|
c.Header("X-Offset", strconv.Itoa(f.Offset))
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|