draincloud-core/internal/files_engine/engine.go
2024-12-15 11:56:03 -05:00

52 lines
1.0 KiB
Go

package filesengine
import (
"context"
"fmt"
"git.optclblast.xyz/draincloud/draincloud-core/internal/storage"
"git.optclblast.xyz/draincloud/draincloud-core/internal/storage/models/files"
"github.com/google/uuid"
)
type FilesEngine struct {
blobStorage storage.BlobStorage
metaStorage storage.MetaStorage
}
func NewFilesEngine(
blobStorage storage.BlobStorage,
metaStorage storage.MetaStorage,
) *FilesEngine {
return &FilesEngine{
blobStorage: blobStorage,
metaStorage: metaStorage,
}
}
type File struct {
Name string
UserID uuid.UUID
Ext string
Type string
Size int64
Data []byte
}
// TODO save file
func (e *FilesEngine) SaveFile(
ctx context.Context,
file File,
) (uuid.UUID, error) {
fileID, err := e.metaStorage.SaveMetadata(ctx, files.FileMetadata{})
if err != nil {
return uuid.Nil, fmt.Errorf("failed to create new file metadata: %w", err)
}
if err = e.blobStorage.SaveBlob(ctx, fileID, file.Data); err != nil {
return uuid.Nil, fmt.Errorf("failed to save file data: %w", err)
}
return fileID, nil
}