photoprism/internal/api/folder.go
Michael Mayer 65212f393f API: Shorten cache debug messages #260
Signed-off-by: Michael Mayer <michael@liquidbytes.net>
2020-05-22 20:08:28 +02:00

57 lines
1.5 KiB
Go

package api
import (
"fmt"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/service"
)
// GetFolders is a reusable request handler for directory listings (GET /api/v1/folders/*).
func GetFolders(router *gin.RouterGroup, conf *config.Config, root, pathName string) {
router.GET("/folders/" + root, func(c *gin.Context) {
if Unauthorized(c, conf) {
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
return
}
start := time.Now()
gc := service.Cache()
recursive := c.Query("recursive") != ""
cacheKey := fmt.Sprintf("folders:%s:%t", pathName, recursive)
if cacheData, ok := gc.Get(cacheKey); ok {
log.Debugf("%s cache hit [%s]", cacheKey, time.Since(start))
c.JSON(http.StatusOK, cacheData.([]entity.Folder))
return
}
folders, err := entity.Folders(root, pathName, recursive)
if err != nil {
log.Errorf("folders: %s", err)
} else {
gc.Set(cacheKey, folders, time.Minute*5)
log.Debugf("%s cached [%s]", cacheKey, time.Since(start))
}
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())
}