Umstellung auf uuid
All checks were successful
release-tag / release-image (push) Successful in 1m32s
All checks were successful
release-tag / release-image (push) Successful in 1m32s
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime"
|
||||
"net/http"
|
||||
@@ -23,22 +22,35 @@ type Meta struct {
|
||||
}
|
||||
|
||||
type Store interface {
|
||||
Save(ctx context.Context, id int64, filename string, r io.Reader) (Meta, error)
|
||||
Open(ctx context.Context, id int64) (io.ReadSeekCloser, Meta, error)
|
||||
Stat(ctx context.Context, id int64) (Meta, bool, error)
|
||||
Delete(ctx context.Context, id int64) error
|
||||
Save(ctx context.Context, id string, filename string, r io.Reader) (Meta, error)
|
||||
Open(ctx context.Context, id string) (io.ReadSeekCloser, Meta, error)
|
||||
Stat(ctx context.Context, id string) (Meta, bool, error)
|
||||
Delete(ctx context.Context, id string) error
|
||||
}
|
||||
|
||||
type FS struct{ root string }
|
||||
|
||||
func New(root string) *FS { return &FS{root: root} }
|
||||
|
||||
func (fs *FS) dir(id int64) string { return filepath.Join(fs.root, "files", fmt.Sprintf("%d", id)) }
|
||||
func (fs *FS) metaPath(id int64) string { return filepath.Join(fs.dir(id), "meta.json") }
|
||||
func (fs *FS) blobPath(id int64, name string) string {
|
||||
func (fs *FS) dir(id string) string { return filepath.Join(fs.root, "files", sanitizeID(id)) }
|
||||
func (fs *FS) metaPath(id string) string { return filepath.Join(fs.dir(id), "meta.json") }
|
||||
func (fs *FS) blobPath(id string, name string) string {
|
||||
return filepath.Join(fs.dir(id), "blob"+safeExt(name))
|
||||
}
|
||||
|
||||
func sanitizeID(id string) string {
|
||||
// nur 0-9a-zA-Z- zulassen; Rest mit '_' ersetzen
|
||||
b := make([]rune, 0, len(id))
|
||||
for _, r := range id {
|
||||
if (r >= '0' && r <= '9') || (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || r == '-' {
|
||||
b = append(b, r)
|
||||
} else {
|
||||
b = append(b, '_')
|
||||
}
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func safeExt(name string) string {
|
||||
ext := filepath.Ext(name)
|
||||
if len(ext) > 16 { // unrealistisch lange Exts beschneiden
|
||||
@@ -47,7 +59,7 @@ func safeExt(name string) string {
|
||||
return ext
|
||||
}
|
||||
|
||||
func (fs *FS) Save(_ context.Context, id int64, filename string, r io.Reader) (Meta, error) {
|
||||
func (fs *FS) Save(_ context.Context, id string, filename string, r io.Reader) (Meta, error) {
|
||||
if strings.TrimSpace(filename) == "" {
|
||||
return Meta{}, errors.New("filename required")
|
||||
}
|
||||
@@ -107,7 +119,7 @@ func (fs *FS) Save(_ context.Context, id int64, filename string, r io.Reader) (M
|
||||
return meta, nil
|
||||
}
|
||||
|
||||
func (fs *FS) Open(_ context.Context, id int64) (io.ReadSeekCloser, Meta, error) {
|
||||
func (fs *FS) Open(_ context.Context, id string) (io.ReadSeekCloser, Meta, error) {
|
||||
meta, ok, err := fs.Stat(context.Background(), id)
|
||||
if err != nil {
|
||||
return nil, Meta{}, err
|
||||
@@ -119,7 +131,7 @@ func (fs *FS) Open(_ context.Context, id int64) (io.ReadSeekCloser, Meta, error)
|
||||
return f, meta, err
|
||||
}
|
||||
|
||||
func (fs *FS) Stat(_ context.Context, id int64) (Meta, bool, error) {
|
||||
func (fs *FS) Stat(_ context.Context, id string) (Meta, bool, error) {
|
||||
b, err := os.ReadFile(fs.metaPath(id))
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
@@ -139,7 +151,7 @@ func (fs *FS) Stat(_ context.Context, id int64) (Meta, bool, error) {
|
||||
return m, true, nil
|
||||
}
|
||||
|
||||
func (fs *FS) Delete(_ context.Context, id int64) error {
|
||||
func (fs *FS) Delete(_ context.Context, id string) error {
|
||||
return os.RemoveAll(fs.dir(id))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user