2024-11-23 08:52:06 +00:00
|
|
|
package filesengine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-12-14 07:42:59 +00:00
|
|
|
"fmt"
|
2024-11-23 08:52:06 +00:00
|
|
|
|
|
|
|
"git.optclblast.xyz/draincloud/draincloud-core/internal/storage"
|
2024-12-14 07:42:59 +00:00
|
|
|
"git.optclblast.xyz/draincloud/draincloud-core/internal/storage/models/files"
|
2024-12-15 16:56:03 +00:00
|
|
|
"github.com/google/uuid"
|
2024-11-23 08:52:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2024-12-15 16:56:03 +00:00
|
|
|
UserID uuid.UUID
|
2024-12-14 07:42:59 +00:00
|
|
|
Ext string
|
2024-11-23 08:52:06 +00:00
|
|
|
Type string
|
|
|
|
Size int64
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
2024-12-14 07:42:59 +00:00
|
|
|
// TODO save file
|
2024-11-23 08:52:06 +00:00
|
|
|
func (e *FilesEngine) SaveFile(
|
|
|
|
ctx context.Context,
|
|
|
|
file File,
|
2024-12-15 16:56:03 +00:00
|
|
|
) (uuid.UUID, error) {
|
2024-12-14 07:42:59 +00:00
|
|
|
fileID, err := e.metaStorage.SaveMetadata(ctx, files.FileMetadata{})
|
|
|
|
if err != nil {
|
2024-12-15 16:56:03 +00:00
|
|
|
return uuid.Nil, fmt.Errorf("failed to create new file metadata: %w", err)
|
2024-12-14 07:42:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = e.blobStorage.SaveBlob(ctx, fileID, file.Data); err != nil {
|
2024-12-15 16:56:03 +00:00
|
|
|
return uuid.Nil, fmt.Errorf("failed to save file data: %w", err)
|
2024-12-14 07:42:59 +00:00
|
|
|
}
|
2024-11-23 08:52:06 +00:00
|
|
|
|
2024-12-14 07:42:59 +00:00
|
|
|
return fileID, nil
|
2024-11-23 08:52:06 +00:00
|
|
|
}
|