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"
|
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
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-05-27 13:40:21 +02:00
|
|
|
func GetFolders(router *gin.RouterGroup, conf *config.Config, urlPath, rootName, rootPath string) {
|
2020-05-24 22:16:06 +02:00
|
|
|
handler := func(c *gin.Context) {
|
2020-05-22 16:29:12 +02:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-22 20:00:33 +02:00
|
|
|
start := time.Now()
|
2020-05-30 01:41:47 +02:00
|
|
|
cache := service.Cache()
|
2020-05-22 16:29:12 +02:00
|
|
|
recursive := c.Query("recursive") != ""
|
2020-05-24 22:16:06 +02:00
|
|
|
listFiles := c.Query("files") != ""
|
2020-05-30 01:41:47 +02:00
|
|
|
uncached := listFiles || c.Query("uncached") != ""
|
|
|
|
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-05-27 13:40:21 +02:00
|
|
|
if files, err := query.FilesByPath(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-05-25 19:10:44 +02:00
|
|
|
c.Header("X-Count", strconv.Itoa(len(resp.Files)+len(resp.Folders)))
|
2020-05-24 22:16:06 +02:00
|
|
|
c.Header("X-Offset", "0")
|
|
|
|
|
|
|
|
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
|
|
|
|
func GetFoldersOriginals(router *gin.RouterGroup, conf *config.Config) {
|
2020-05-27 13:40:21 +02:00
|
|
|
GetFolders(router, conf, "originals", entity.RootDefault, conf.OriginalsPath())
|
2020-05-22 16:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GET /api/v1/folders/import
|
|
|
|
func GetFoldersImport(router *gin.RouterGroup, conf *config.Config) {
|
2020-05-27 13:40:21 +02:00
|
|
|
GetFolders(router, conf, "import", entity.RootImport, conf.ImportPath())
|
2020-05-22 16:29:12 +02:00
|
|
|
}
|