Block writes operations in read-only mode on the server
This commit is contained in:
+11
-6
@@ -73,6 +73,17 @@ func buildConfig(bundledFiles fs.FS, args []string) *server.ServerConfig {
|
||||
var spacePrimitives server.SpacePrimitives
|
||||
spacePrimitives, err := server.NewDiskSpacePrimitives(rootSpaceConfig.SpaceFolderPath, rootSpaceConfig.GitIgnore)
|
||||
|
||||
rootSpaceConfig.ReadOnlyMode = os.Getenv("SB_READ_ONLY") != ""
|
||||
|
||||
if rootSpaceConfig.ReadOnlyMode {
|
||||
log.Println("Starting in read-only mode.")
|
||||
}
|
||||
|
||||
if rootSpaceConfig.ReadOnlyMode {
|
||||
// Wrap the space primitives in read only mode
|
||||
spacePrimitives = server.NewReadOnlySpacePrimitives(spacePrimitives)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@@ -109,8 +120,6 @@ func buildConfig(bundledFiles fs.FS, args []string) *server.ServerConfig {
|
||||
pieces[0], rootSpaceConfig.Auth.LockoutLimit, rootSpaceConfig.Auth.LockoutTime)
|
||||
}
|
||||
|
||||
rootSpaceConfig.ReadOnlyMode = os.Getenv("SB_READ_ONLY") != ""
|
||||
|
||||
if os.Getenv("SB_NAME") != "" {
|
||||
rootSpaceConfig.SpaceName = os.Getenv("SB_NAME")
|
||||
}
|
||||
@@ -119,10 +128,6 @@ func buildConfig(bundledFiles fs.FS, args []string) *server.ServerConfig {
|
||||
rootSpaceConfig.SpaceDescription = os.Getenv("SB_DESCRIPTION")
|
||||
}
|
||||
|
||||
if rootSpaceConfig.ReadOnlyMode {
|
||||
log.Println("Starting in read-only mode.")
|
||||
}
|
||||
|
||||
if os.Getenv("SB_URL_PREFIX") != "" {
|
||||
hostUrlPrefix := os.Getenv("SB_URL_PREFIX")
|
||||
// Normalize the host URL prefix (if set)
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
type ReadOnlySpacePrimitives struct {
|
||||
wrapped SpacePrimitives
|
||||
}
|
||||
|
||||
var _ SpacePrimitives = &ReadOnlySpacePrimitives{}
|
||||
var NotAllowedError = errors.New("Not allowed")
|
||||
|
||||
func NewReadOnlySpacePrimitives(wrapped SpacePrimitives) *ReadOnlySpacePrimitives {
|
||||
return &ReadOnlySpacePrimitives{wrapped: wrapped}
|
||||
}
|
||||
|
||||
// FetchFileList retrieves a list of all files in the space
|
||||
func (ro *ReadOnlySpacePrimitives) FetchFileList() ([]FileMeta, error) {
|
||||
return ro.wrapped.FetchFileList()
|
||||
}
|
||||
|
||||
// GetFileMeta retrieves metadata for a specific file
|
||||
func (ro *ReadOnlySpacePrimitives) GetFileMeta(path string) (FileMeta, error) {
|
||||
return ro.wrapped.GetFileMeta(path)
|
||||
}
|
||||
|
||||
// ReadFile reads a file and returns its data and metadata
|
||||
func (ro *ReadOnlySpacePrimitives) ReadFile(path string) ([]byte, FileMeta, error) {
|
||||
return ro.wrapped.ReadFile(path)
|
||||
}
|
||||
|
||||
// WriteFile returns an error since this is a read-only implementation
|
||||
func (ro *ReadOnlySpacePrimitives) WriteFile(path string, data []byte, meta *FileMeta) (FileMeta, error) {
|
||||
return FileMeta{}, NotAllowedError
|
||||
}
|
||||
|
||||
// DeleteFile returns an error since this is a read-only implementation
|
||||
func (ro *ReadOnlySpacePrimitives) DeleteFile(path string) error {
|
||||
return NotAllowedError
|
||||
}
|
||||
Reference in New Issue
Block a user