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"
|
|
|
|
"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-24 22:16:06 +02:00
|
|
|
func GetFolders(router *gin.RouterGroup, conf *config.Config, root, rootPath string) {
|
|
|
|
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()
|
|
|
|
gc := 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-26 09:02:19 +02:00
|
|
|
cached := !listFiles && c.Query("uncached") == ""
|
|
|
|
resp := FoldersResponse{Root: root, Recursive: recursive, Cached: cached}
|
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-25 19:10:44 +02:00
|
|
|
if cached {
|
|
|
|
if cacheData, ok := gc.Get(cacheKey); ok {
|
|
|
|
log.Debugf("cache hit for %s [%s]", cacheKey, time.Since(start))
|
|
|
|
c.JSON(http.StatusOK, cacheData.(FoldersResponse))
|
|
|
|
return
|
|
|
|
}
|
2020-05-22 20:00:33 +02:00
|
|
|
}
|
|
|
|
|
2020-05-24 22:16:06 +02:00
|
|
|
if folders, err := query.FoldersByPath(root, 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 {
|
|
|
|
if files, err := query.FilesByPath(root, path); err != nil {
|
|
|
|
log.Errorf("folders: %s", err)
|
|
|
|
} else {
|
|
|
|
resp.Files = files
|
|
|
|
}
|
2020-05-22 16:29:12 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 09:02:19 +02:00
|
|
|
if cached {
|
|
|
|
gc.Set(cacheKey, resp, time.Minute*5)
|
|
|
|
log.Debugf("cached %s [%s]", cacheKey, time.Since(start))
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
router.GET("/folders/"+root, handler)
|
|
|
|
router.GET("/folders/"+root+"/*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-24 22:16:06 +02:00
|
|
|
GetFolders(router, conf, entity.RootOriginals, 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-24 22:16:06 +02:00
|
|
|
GetFolders(router, conf, entity.RootImport, conf.ImportPath())
|
2020-05-22 16:29:12 +02:00
|
|
|
}
|