From bc65ad57d50bbc3f59f4dd529571fa29b3ae0726 Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Tue, 2 Jul 2019 22:03:23 +0200 Subject: [PATCH] Disable import, upload and raw converter in read-only mode #56 --- internal/api/errors.go | 11 +++++++++++ internal/api/import.go | 5 +++++ internal/api/upload.go | 5 +++++ internal/commands/convert.go | 4 ++++ internal/commands/import.go | 4 ++++ internal/config/errors.go | 9 +++++++++ internal/photoprism/converter.go | 6 +++++- 7 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 internal/api/errors.go create mode 100644 internal/config/errors.go diff --git a/internal/api/errors.go b/internal/api/errors.go new file mode 100644 index 000000000..c5f8201ee --- /dev/null +++ b/internal/api/errors.go @@ -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())} +) diff --git a/internal/api/import.go b/internal/api/import.go index 1b9dd6642..fc25453d8 100644 --- a/internal/api/import.go +++ b/internal/api/import.go @@ -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() diff --git a/internal/api/upload.go b/internal/api/upload.go index 177a0c512..7e2553dea 100644 --- a/internal/api/upload.go +++ b/internal/api/upload.go @@ -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") diff --git a/internal/commands/convert.go b/internal/commands/convert.go index 3ba17061e..706298fae 100644 --- a/internal/commands/convert.go +++ b/internal/commands/convert.go @@ -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 } diff --git a/internal/commands/import.go b/internal/commands/import.go index 70420a9e5..10338da88 100644 --- a/internal/commands/import.go +++ b/internal/commands/import.go @@ -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 } diff --git a/internal/config/errors.go b/internal/config/errors.go new file mode 100644 index 000000000..a4bf25a45 --- /dev/null +++ b/internal/config/errors.go @@ -0,0 +1,9 @@ +package config + +import ( + "errors" +) + +var ( + ErrReadOnly = errors.New("not available in read only mode") +) diff --git a/internal/photoprism/converter.go b/internal/photoprism/converter.go index a05b5968d..b8b669aef 100644 --- a/internal/photoprism/converter.go +++ b/internal/photoprism/converter.go @@ -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"