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-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-22 20:00:33 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
2020-05-22 16:29:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetFolders is a reusable request handler for directory listings (GET /api/v1/folders/*).
|
2020-05-23 20:58:58 +02:00
|
|
|
func GetFolders(router *gin.RouterGroup, conf *config.Config, root, pathName string) {
|
|
|
|
router.GET("/folders/"+root, 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-22 20:00:33 +02:00
|
|
|
cacheKey := fmt.Sprintf("folders:%s:%t", pathName, recursive)
|
|
|
|
|
|
|
|
if cacheData, ok := gc.Get(cacheKey); ok {
|
2020-05-23 20:58:58 +02:00
|
|
|
log.Debugf("cache hit for %s [%s]", cacheKey, time.Since(start))
|
2020-05-22 20:00:33 +02:00
|
|
|
c.JSON(http.StatusOK, cacheData.([]entity.Folder))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-22 16:29:12 +02:00
|
|
|
folders, err := entity.Folders(root, pathName, recursive)
|
|
|
|
|
|
|
|
if err != nil {
|
2020-05-22 20:00:33 +02:00
|
|
|
log.Errorf("folders: %s", err)
|
|
|
|
} else {
|
|
|
|
gc.Set(cacheKey, folders, time.Minute*5)
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
log.Debugf("cached %s [%s]", cacheKey, time.Since(start))
|
2020-05-22 16:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, folders)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GET /api/v1/folders/originals
|
|
|
|
func GetFoldersOriginals(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
GetFolders(router, conf, entity.FolderRootOriginals, conf.OriginalsPath())
|
|
|
|
}
|
|
|
|
|
|
|
|
// GET /api/v1/folders/import
|
|
|
|
func GetFoldersImport(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
GetFolders(router, conf, entity.FolderRootImport, conf.ImportPath())
|
|
|
|
}
|