Disable import, upload and raw converter in read-only mode #56

This commit is contained in:
Michael Mayer 2019-07-02 22:03:23 +02:00
parent b976c095f3
commit bc65ad57d5
7 changed files with 43 additions and 1 deletions

11
internal/api/errors.go Normal file
View file

@ -0,0 +1,11 @@
package api
import (
"github.com/gin-gonic/gin"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/util"
)
var (
ErrReadOnly = gin.H{"error": util.UcFirst(config.ErrReadOnly.Error())}
)

View file

@ -29,6 +29,11 @@ func initImporter(conf *config.Config) {
// POST /api/v1/import
func Import(router *gin.RouterGroup, conf *config.Config) {
router.POST("/import/*path", func(c *gin.Context) {
if conf.ReadOnly() {
c.AbortWithStatusJSON(http.StatusForbidden, ErrReadOnly)
return
}
start := time.Now()
path := conf.ImportPath()

View file

@ -17,6 +17,11 @@ import (
// POST /api/v1/upload/:path
func Upload(router *gin.RouterGroup, conf *config.Config) {
router.POST("/upload/:path", func(c *gin.Context) {
if conf.ReadOnly() {
c.AbortWithStatusJSON(http.StatusForbidden, ErrReadOnly)
return
}
start := time.Now()
subPath := c.Param("path")

View file

@ -20,6 +20,10 @@ func convertAction(ctx *cli.Context) error {
conf := config.NewConfig(ctx)
if conf.ReadOnly() {
return config.ErrReadOnly
}
if err := conf.CreateDirectories(); err != nil {
return err
}

View file

@ -21,6 +21,10 @@ func importAction(ctx *cli.Context) error {
conf := config.NewConfig(ctx)
if conf.ReadOnly() {
return config.ErrReadOnly
}
if err := conf.CreateDirectories(); err != nil {
return err
}

View file

@ -0,0 +1,9 @@
package config
import (
"errors"
)
var (
ErrReadOnly = errors.New("not available in read only mode")
)

View file

@ -75,7 +75,7 @@ func (c *Converter) ConvertCommand(image *MediaFile, jpegFilename string, xmpFil
// ConvertToJpeg converts a single image the JPEG format.
func (c *Converter) ConvertToJpeg(image *MediaFile) (*MediaFile, error) {
if !image.Exists() {
return nil, fmt.Errorf("can not convert, file does not exist: %s", image.Filename())
return nil, fmt.Errorf("can not convert to jpeg, file does not exist: %s", image.Filename())
}
if image.IsJpeg() {
@ -92,6 +92,10 @@ func (c *Converter) ConvertToJpeg(image *MediaFile) (*MediaFile, error) {
return mediaFile, nil
}
if c.conf.ReadOnly() {
return nil, fmt.Errorf("can not convert to jpeg in read only mode: %s", image.Filename())
}
log.Infof("converting \"%s\" to \"%s\"", image.filename, jpegFilename)
xmpFilename := baseFilename + ".xmp"