working commit

This commit is contained in:
2026-02-01 21:03:43 +02:00
parent 9c18f62997
commit 18e2a61c8a
10 changed files with 410 additions and 111 deletions
+41 -9
View File
@@ -25,11 +25,34 @@ func NewStorage(basepath string) *Storage {
return res
}
const filesubdir = "files"
const tmpsubdir = "tmps"
func (store *Storage) makeCollecionpath(collection string) string {
return filepath.Join(store.basepath, filesubdir, collection)
}
func (store *Storage) makeFilepath(collection, filename string) string {
return filepath.Join(store.basepath, filesubdir, collection, filename)
}
func (store *Storage) makeTmppath(tmpname string) string {
return filepath.Join(store.basepath, tmpsubdir, tmpname)
}
func (store *Storage) makeTmpsubdir() string {
return filepath.Join(store.basepath, tmpsubdir)
}
func (store *Storage) makeFilesubdir(collection, filename string) string {
return filepath.Join(store.basepath, filesubdir)
}
func (store *Storage) GetFileReader(collection, filename string) (io.ReadCloser, error) {
var err error
var res io.ReadCloser
filename = filepath.Join(store.basepath, collection, filename)
filename = store.makeFilepath(collection, filename)
file, err := os.OpenFile(filename, os.O_RDONLY, 0)
if err != nil {
return res, err
@@ -40,15 +63,16 @@ func (store *Storage) GetFileReader(collection, filename string) (io.ReadCloser,
func (store *Storage) HardlinkFile(tmpname, collection, filename string) error {
var err error
dirname := filepath.Join(store.basepath, collection)
dirname := store.makeCollecionpath(collection)
err = os.MkdirAll(dirname, 0750)
if err != nil {
return err
}
filename = filepath.Join(store.basepath, collection, filename)
filename = store.makeFilepath(collection, filename)
os.Remove(filename) // TODO
tmpname = store.makeTmppath(tmpname)
err = os.Link(tmpname, filename)
if err != nil {
return err
@@ -67,11 +91,17 @@ func (store *Storage) WriteTempFile(source io.Reader) (string, int64, string, er
tmpname := auxuuid.NewUUID()
tmpname = fmt.Sprintf("file-%s.tmp", tmpname)
tmppath := filepath.Join(store.basepath, tmpname)
tmppath := store.makeTmppath(tmpname)
tmpdirpath := store.makeTmpsubdir()
err = os.MkdirAll(tmpdirpath, 0750)
if err != nil {
return tmpname, size, digest, err
}
file, err := os.OpenFile(tmppath, os.O_WRONLY|os.O_CREATE, 0640)
if err != nil {
return tmppath, size, digest, err
return tmpname, size, digest, err
}
defer file.Close()
@@ -80,23 +110,25 @@ func (store *Storage) WriteTempFile(source io.Reader) (string, int64, string, er
size, err = io.Copy(writer, source)
if err != nil {
return tmppath, size, digest, err
return tmpname, size, digest, err
}
digest = hex.EncodeToString(hasher.Sum(nil))
digest = fmt.Sprintf("sha256:%s", digest)
return tmppath, size, digest, err
return tmpname, size, digest, err
}
func (store *Storage) DeleteFile(collection, filename string) error {
var err error
filename = filepath.Join(store.basepath, collection, filename)
filename = store.makeFilepath(collection, filename)
err = os.Remove(filename)
if err != nil {
return err
}
dirname := filepath.Join(store.basepath, collection)
// TODO: more safe removing
dirname := store.makeCollecionpath(collection)
err = os.RemoveAll(dirname)
if err != nil {
return err