mirror of
https://codeberg.org/shroff/phylum.git
synced 2026-05-06 20:29:22 -05:00
[server][storage] Accept strings instead of UUID in storage ops
This commit is contained in:
@@ -21,7 +21,7 @@ func (f filesystem) OpenWrite(r Resource, versionID uuid.UUID) (io.WriteCloser,
|
||||
if versionID == uuid.Nil {
|
||||
versionID, _ = uuid.NewV7()
|
||||
}
|
||||
if dest, err := storage.DefaultBackend().OpenWrite(versionID); err != nil {
|
||||
if dest, err := storage.DefaultBackend().OpenWrite(versionID.String()); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return computeProps(dest, func(len int, hash hash.Hash, mimeType string) error {
|
||||
|
||||
@@ -31,6 +31,6 @@ func (v Version) OpenRead(start, length int) (io.ReadCloser, error) {
|
||||
if b := storage.GetBackend(v.Storage); b == nil {
|
||||
return nil, errors.New("storage backend not found: " + v.Storage)
|
||||
} else {
|
||||
return b.OpenRead(v.ID, start, length)
|
||||
return b.OpenRead(v.ID.String(), start, length)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,8 +35,8 @@ func createLocalBackend(params map[string]string) (LocalBackend, error) {
|
||||
return l, nil
|
||||
}
|
||||
|
||||
func (l localStorage) OpenRead(id uuid.UUID, start, length int) (io.ReadCloser, error) {
|
||||
file, err := os.OpenFile(l.path(id), os.O_RDONLY, 0640)
|
||||
func (l localStorage) OpenRead(name string, start, length int) (io.ReadCloser, error) {
|
||||
file, err := os.OpenFile(filepath.Join(l.root, name), os.O_RDONLY, 0640)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -47,7 +47,7 @@ func (l localStorage) OpenRead(id uuid.UUID, start, length int) (io.ReadCloser,
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func (l localStorage) OpenWrite(id uuid.UUID) (io.WriteCloser, error) {
|
||||
func (l localStorage) OpenWrite(name string) (io.WriteCloser, error) {
|
||||
f, err := os.CreateTemp(l.tmp, "upload-*")
|
||||
tmpFilePath := filepath.Join(l.tmp, f.Name())
|
||||
if err != nil {
|
||||
@@ -58,18 +58,18 @@ func (l localStorage) OpenWrite(id uuid.UUID) (io.WriteCloser, error) {
|
||||
os.Remove(tmpFilePath)
|
||||
return err
|
||||
}
|
||||
return os.Rename(tmpFilePath, l.path(id))
|
||||
return os.Rename(tmpFilePath, filepath.Join(l.root, name))
|
||||
}}, nil
|
||||
}
|
||||
|
||||
func (l localStorage) Delete(id uuid.UUID) error {
|
||||
return os.Remove(l.path(id))
|
||||
func (l localStorage) Delete(name string) error {
|
||||
return os.Remove(filepath.Join(l.root, name))
|
||||
}
|
||||
|
||||
func (l localStorage) DeleteAll(ids uuid.UUIDs) []error {
|
||||
func (l localStorage) DeleteAll(names []string) []error {
|
||||
errs := make([]error, 0)
|
||||
for _, id := range ids {
|
||||
err := l.Delete(id)
|
||||
for _, name := range names {
|
||||
err := l.Delete(name)
|
||||
if err != nil && !errors.Is(err, fs.ErrNotExist) {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
@@ -78,7 +78,7 @@ func (l localStorage) DeleteAll(ids uuid.UUIDs) []error {
|
||||
}
|
||||
|
||||
func (l localStorage) path(id uuid.UUID) string {
|
||||
return filepath.Join(string(l.root), id.String())
|
||||
return filepath.Join(l.root, id.String())
|
||||
}
|
||||
|
||||
func (l localStorage) String() string {
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
@@ -52,9 +51,9 @@ func createMinioBackend(params map[string]string) (Backend, error) {
|
||||
return minioStorage{client: client, tmp: params["tmp"], bucketName: params["bucket_name"], prefix: params["prefix"]}, nil
|
||||
}
|
||||
|
||||
func (s minioStorage) OpenRead(id uuid.UUID, start, length int) (io.ReadCloser, error) {
|
||||
func (s minioStorage) OpenRead(name string, start, length int) (io.ReadCloser, error) {
|
||||
// TODO: Range
|
||||
if r, err := s.client.GetObject(context.Background(), s.bucketName, s.prefix+id.String(), minio.GetObjectOptions{}); err != nil {
|
||||
if r, err := s.client.GetObject(context.Background(), s.bucketName, s.prefix+name, minio.GetObjectOptions{}); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return r, ErrRangeNotSupported
|
||||
@@ -79,17 +78,17 @@ func (s minioStorage) OpenRead(id uuid.UUID, start, length int) (io.ReadCloser,
|
||||
// }}, nil
|
||||
// }
|
||||
|
||||
func (s minioStorage) Delete(id uuid.UUID) error {
|
||||
return s.client.RemoveObject(context.Background(), s.bucketName, s.prefix+id.String(), minio.RemoveObjectOptions{})
|
||||
func (s minioStorage) Delete(name string) error {
|
||||
return s.client.RemoveObject(context.Background(), s.bucketName, s.prefix+name, minio.RemoveObjectOptions{})
|
||||
}
|
||||
|
||||
func (s minioStorage) DeleteAll(ids uuid.UUIDs) []error {
|
||||
func (s minioStorage) DeleteAll(names []string) []error {
|
||||
objs := make(chan minio.ObjectInfo)
|
||||
go func() {
|
||||
defer close(objs)
|
||||
for _, id := range ids {
|
||||
for _, name := range names {
|
||||
objs <- minio.ObjectInfo{
|
||||
Key: s.prefix + id.String(),
|
||||
Key: s.prefix + name,
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -2,18 +2,16 @@ package storage
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Backend interface {
|
||||
OpenRead(id uuid.UUID, start, length int) (io.ReadCloser, error)
|
||||
Delete(id uuid.UUID) error
|
||||
DeleteAll(ids uuid.UUIDs) []error
|
||||
OpenRead(name string, start, length int) (io.ReadCloser, error)
|
||||
Delete(name string) error
|
||||
DeleteAll(names []string) []error
|
||||
String() string
|
||||
}
|
||||
|
||||
type LocalBackend interface {
|
||||
Backend
|
||||
OpenWrite(id uuid.UUID) (io.WriteCloser, error)
|
||||
OpenWrite(name string) (io.WriteCloser, error)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user