Initial commit: Open sourcing all of the Maple Open Technologies code.
This commit is contained in:
commit
755d54a99d
2010 changed files with 448675 additions and 0 deletions
|
|
@ -0,0 +1,24 @@
|
|||
// monorepo/cloud/backend/internal/maplefile/repo/fileobjectstorage/delete.go
|
||||
package fileobjectstorage
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// DeleteEncryptedData removes encrypted file data from S3
|
||||
func (impl *fileObjectStorageRepositoryImpl) DeleteEncryptedData(storagePath string) error {
|
||||
ctx := context.Background()
|
||||
|
||||
// Delete the encrypted data
|
||||
err := impl.Storage.DeleteByKeys(ctx, []string{storagePath})
|
||||
if err != nil {
|
||||
impl.Logger.Error("Failed to delete encrypted data",
|
||||
zap.String("storagePath", storagePath),
|
||||
zap.Error(err))
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
// monorepo/cloud/backend/internal/maplefile/repo/fileobjectstorage/get_encrypted_data.go
|
||||
package fileobjectstorage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// GetEncryptedData retrieves encrypted file data from S3
|
||||
func (impl *fileObjectStorageRepositoryImpl) GetEncryptedData(storagePath string) ([]byte, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
// Get the encrypted data
|
||||
reader, err := impl.Storage.GetBinaryData(ctx, storagePath)
|
||||
if err != nil {
|
||||
impl.Logger.Error("Failed to get encrypted data",
|
||||
zap.String("storagePath", storagePath),
|
||||
zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
defer reader.Close()
|
||||
|
||||
// Read all data into memory
|
||||
data, err := io.ReadAll(reader)
|
||||
if err != nil {
|
||||
impl.Logger.Error("Failed to read encrypted data",
|
||||
zap.String("storagePath", storagePath),
|
||||
zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
// monorepo/cloud/backend/internal/maplefile/repo/fileobjectstorage/get_object_size.go
|
||||
package fileobjectstorage
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// GetObjectSize returns the size in bytes of an object at the given storage path
|
||||
func (impl *fileObjectStorageRepositoryImpl) GetObjectSize(storagePath string) (int64, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
// Get object size from storage
|
||||
size, err := impl.Storage.GetObjectSize(ctx, storagePath)
|
||||
if err != nil {
|
||||
impl.Logger.Error("Failed to get object size",
|
||||
zap.String("storagePath", storagePath),
|
||||
zap.Error(err))
|
||||
return 0, err
|
||||
}
|
||||
|
||||
impl.Logger.Debug("Retrieved object size",
|
||||
zap.String("storagePath", storagePath),
|
||||
zap.Int64("size", size))
|
||||
|
||||
return size, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
// monorepo/cloud/backend/internal/maplefile/repo/fileobjectstorage/impl.go
|
||||
package fileobjectstorage
|
||||
|
||||
import (
|
||||
"go.uber.org/zap"
|
||||
|
||||
"codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/config"
|
||||
dom_file "codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/internal/domain/file"
|
||||
s3storage "codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/pkg/storage/object/s3"
|
||||
)
|
||||
|
||||
type fileObjectStorageRepositoryImpl struct {
|
||||
Config *config.Configuration
|
||||
Logger *zap.Logger
|
||||
Storage s3storage.S3ObjectStorage
|
||||
}
|
||||
|
||||
func NewRepository(cfg *config.Configuration, logger *zap.Logger, s3 s3storage.S3ObjectStorage) dom_file.FileObjectStorageRepository {
|
||||
logger = logger.Named("FileObjectStorageRepository")
|
||||
return &fileObjectStorageRepositoryImpl{
|
||||
Config: cfg,
|
||||
Logger: logger.With(zap.String("repository", "file_storage")),
|
||||
Storage: s3,
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
// monorepo/cloud/backend/internal/maplefile/repo/fileobjectstorage/presigned_download_url.go
|
||||
package fileobjectstorage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// GeneratePresignedDownloadURL creates a time-limited URL that allows direct download
|
||||
// of the file data located at the given storage path, with proper content disposition headers.
|
||||
func (impl *fileObjectStorageRepositoryImpl) GeneratePresignedDownloadURL(storagePath string, duration time.Duration) (string, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
// Generate presigned download URL with content disposition
|
||||
presignedURL, err := impl.Storage.GetDownloadablePresignedURL(ctx, storagePath, duration)
|
||||
if err != nil {
|
||||
impl.Logger.Error("Failed to generate presigned download URL",
|
||||
zap.String("storagePath", storagePath),
|
||||
zap.Duration("duration", duration),
|
||||
zap.Error(err))
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Replace the hostname in the presigned URL with the public endpoint if configured
|
||||
if impl.Config.S3.PublicEndpoint != "" && impl.Config.S3.PublicEndpoint != impl.Config.S3.Endpoint {
|
||||
parsedURL, err := url.Parse(presignedURL)
|
||||
if err == nil {
|
||||
// Parse the public endpoint to get the host
|
||||
publicEndpoint, err := url.Parse(impl.Config.S3.PublicEndpoint)
|
||||
if err == nil {
|
||||
// Replace the host in the presigned URL
|
||||
parsedURL.Scheme = publicEndpoint.Scheme
|
||||
parsedURL.Host = publicEndpoint.Host
|
||||
presignedURL = parsedURL.String()
|
||||
|
||||
impl.Logger.Debug("Replaced presigned URL hostname with public endpoint",
|
||||
zap.String("original_endpoint", impl.Config.S3.Endpoint),
|
||||
zap.String("public_endpoint", impl.Config.S3.PublicEndpoint))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl.Logger.Debug("Generated presigned download URL",
|
||||
zap.String("storagePath", storagePath),
|
||||
zap.Duration("duration", duration),
|
||||
zap.String("url", presignedURL))
|
||||
|
||||
return presignedURL, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
// monorepo/cloud/backend/internal/maplefile/repo/fileobjectstorage/presigned_upload_url.go
|
||||
package fileobjectstorage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// GeneratePresignedUploadURL creates a temporary, time-limited URL that allows clients to upload
|
||||
// encrypted file data directly to the storage system at the specified storage path.
|
||||
func (impl *fileObjectStorageRepositoryImpl) GeneratePresignedUploadURL(storagePath string, duration time.Duration) (string, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
// Generate presigned upload URL
|
||||
url, err := impl.Storage.GeneratePresignedUploadURL(ctx, storagePath, duration)
|
||||
if err != nil {
|
||||
impl.Logger.Error("Failed to generate presigned upload URL",
|
||||
zap.String("storagePath", storagePath),
|
||||
zap.Duration("duration", duration),
|
||||
zap.Error(err))
|
||||
return "", err
|
||||
}
|
||||
|
||||
impl.Logger.Debug("Generated presigned upload URL",
|
||||
zap.String("storagePath", storagePath),
|
||||
zap.Duration("duration", duration))
|
||||
|
||||
return url, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
package fileobjectstorage
|
||||
|
||||
import (
|
||||
"go.uber.org/zap"
|
||||
|
||||
"codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/config"
|
||||
dom_file "codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/internal/domain/file"
|
||||
s3storage "codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/pkg/storage/object/s3"
|
||||
)
|
||||
|
||||
// ProvideRepository provides a file object storage repository for Wire DI
|
||||
func ProvideRepository(cfg *config.Config, logger *zap.Logger, s3 s3storage.S3ObjectStorage) dom_file.FileObjectStorageRepository {
|
||||
return NewRepository(cfg, logger, s3)
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
// monorepo/cloud/backend/internal/maplefile/repo/fileobjectstorage/upload.go
|
||||
package fileobjectstorage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// StoreEncryptedData uploads encrypted file data to S3 and returns the storage path
|
||||
func (impl *fileObjectStorageRepositoryImpl) StoreEncryptedData(ownerID string, fileID string, encryptedData []byte) (string, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
// Generate a storage path using a deterministic pattern
|
||||
storagePath := fmt.Sprintf("users/%s/files/%s", ownerID, fileID)
|
||||
|
||||
// Always store encrypted data as private
|
||||
err := impl.Storage.UploadContentWithVisibility(ctx, storagePath, encryptedData, false)
|
||||
if err != nil {
|
||||
impl.Logger.Error("Failed to store encrypted data",
|
||||
zap.String("fileID", fileID),
|
||||
zap.String("ownerID", ownerID),
|
||||
zap.Error(err))
|
||||
return "", err
|
||||
}
|
||||
|
||||
return storagePath, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
// monorepo/cloud/backend/internal/maplefile/repo/fileobjectstorage/verify_object_exists.go
|
||||
package fileobjectstorage
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// VerifyObjectExists checks if an object exists at the given storage path.
|
||||
func (impl *fileObjectStorageRepositoryImpl) VerifyObjectExists(storagePath string) (bool, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
// Check if object exists in storage
|
||||
exists, err := impl.Storage.ObjectExists(ctx, storagePath)
|
||||
if err != nil {
|
||||
impl.Logger.Error("Failed to verify if object exists",
|
||||
zap.String("storagePath", storagePath),
|
||||
zap.Error(err))
|
||||
return false, err
|
||||
}
|
||||
|
||||
impl.Logger.Debug("Verified object existence",
|
||||
zap.String("storagePath", storagePath),
|
||||
zap.Bool("exists", exists))
|
||||
|
||||
return exists, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue