feat(editor,storage): storage library to abstract writabble file system

This commit is contained in:
Guz
2025-11-20 15:07:53 -03:00
parent ff0ab4c2c9
commit b0dce3c29f
5 changed files with 169 additions and 0 deletions

View File

@@ -1,3 +1,8 @@
module code.capytal.cc/capytal/comicverse/editor
go 1.25.2
require (
code.capytal.cc/loreddev/smalltrip v0.0.0-20251113171745-e3813daa807e
code.capytal.cc/loreddev/x v0.0.0-20251113171626-2ce5d71249c1
)

View File

@@ -0,0 +1,4 @@
code.capytal.cc/loreddev/smalltrip v0.0.0-20251113171745-e3813daa807e h1:LdkirHDzhkcnhOBnDN0po84DjHAAkGztjHu/4mfWpSI=
code.capytal.cc/loreddev/smalltrip v0.0.0-20251113171745-e3813daa807e/go.mod h1:jMvSPUj295pTk/ixyxZfwZJE/RQ7DZzvQ3cVoAklkPA=
code.capytal.cc/loreddev/x v0.0.0-20251113171626-2ce5d71249c1 h1:BE0QdvwVVTG/t7nwNO5rrLf1vdAc5axv/1mWd/oAWhw=
code.capytal.cc/loreddev/x v0.0.0-20251113171626-2ce5d71249c1/go.mod h1:p5ZPHzutdbUDfpvNBCjv5ls6rM4YNl2k4ipD5b0aRho=

98
editor/storage/local.go Normal file
View File

@@ -0,0 +1,98 @@
package storage
import (
"errors"
"fmt"
"io"
"io/fs"
"log/slog"
"os"
"path"
)
func Newlocal(
root *os.Root,
logger *slog.Logger,
) Storage {
return &local{
log: logger,
root: root,
}
}
type local struct {
log *slog.Logger
root *os.Root
}
var _ Storage = (*local)(nil)
func (files *local) Exists(p string) bool {
if _, err := files.root.Stat(p); err != nil {
return false
}
return true
}
func (files *local) Open(p string) (fs.File, error) {
log := files.log.With(
slog.String("path", p),
slog.String("root", files.root.Name()))
log.Debug("Opening file")
defer log.Debug("File opened")
f, err := files.root.Open(p)
if errors.Is(err, os.ErrNotExist) {
return nil, ErrNotExists
} else if err != nil {
return nil, err
}
return f, nil
}
func (files *local) Write(p string, d []byte) (int, error) {
log := files.log.With(
slog.String("path", p),
slog.String("root", files.root.Name()))
log.Debug("Writing file")
defer log.Debug("File wrote")
if err := files.root.MkdirAll(path.Dir(p), os.ModePerm); err != nil {
return 0, fmt.Errorf("file.local: failed to create parent directories %q: %w", path.Dir(p), err)
}
err := files.root.WriteFile(p, d, os.ModePerm)
if err != nil {
return 0, fmt.Errorf("file.local: failed to write file %q: %w", p, err)
}
return len(d), nil
}
func (files *local) WriteFrom(p string, r io.Reader) (int64, error) {
log := files.log.With(
slog.String("path", p),
slog.String("root", files.root.Name()))
log.Debug("Writing file")
defer log.Debug("File wrote")
if err := files.root.MkdirAll(path.Dir(p), os.ModePerm); err != nil {
return 0, fmt.Errorf("file.local: failed to create parent directories %q: %w", path.Dir(p), err)
}
f, err := files.root.Create(p)
if err != nil {
return 0, fmt.Errorf("file.local: failed to create file %q: %w", p, err)
}
n, err := f.ReadFrom(r)
if err != nil {
return 0, fmt.Errorf("file.local: failed to write file %q: %w", p, err)
}
return n, nil
}

42
editor/storage/storage.go Normal file
View File

@@ -0,0 +1,42 @@
package storage
import (
"io"
"io/fs"
"os"
"path"
)
type Storage interface {
Exists(p string) bool
Open(p string) (fs.File, error)
Write(p string, b []byte) (int, error)
WriteFrom(p string, r io.Reader) (int64, error)
}
type withRoot struct {
root string
Storage
}
func WithRoot(rootDir string, s Storage) Storage {
return &withRoot{root: rootDir, Storage: s}
}
func (f *withRoot) Exists(p string) bool {
return f.Storage.Exists(path.Join(f.root, p))
}
func (f *withRoot) Open(p string) (fs.File, error) {
return f.Storage.Open(path.Join(f.root, p))
}
func (f *withRoot) Write(p string, b []byte) (int, error) {
return f.Storage.Write(path.Join(f.root, p), b)
}
func (f *withRoot) WriteFrom(p string, r io.Reader) (int64, error) {
return f.Storage.WriteFrom(path.Join(f.root, p), r)
}
var ErrNotExists = os.ErrNotExist

View File

@@ -30,6 +30,26 @@ dev:
dev/debug:
$(MAKE) -j2 debug dev/assets
editor/dev/server:
cd ./editor; go run github.com/joho/godotenv/cmd/godotenv@v1.5.1 \
go run github.com/air-verse/air@v1.52.2 \
--build.cmd "go build -o tmp/bin/main ./cmd" \
--build.bin "tmp/bin/main" \
--build.exclude_dir "node_modules" \
--build.include_ext "go" \
--build.stop_on_error "false" \
--misc.clean_on_exit true \
-- -dev -port $(PORT) -hostname 0.0.0.0
editor/dev/assets:
cd ./editor; tailwindcss \
-i ./assets/css/tailwind.css \
-o ./assets/css/style.css \
--watch
editor/dev:
$(MAKE) -j2 editor/dev/assets editor/dev/server
debug:
dlv debug -l 127.0.0.1:38697 \
--continue \