infoalloggi-monorepo/apps/backend/storage_handlers/storage_handler.go

46 lines
843 B
Go
Raw Normal View History

2025-08-04 17:45:44 +02:00
package storagehandlers
import (
"backend/config"
"backend/typesdefs"
2025-08-04 17:45:44 +02:00
"errors"
"mime/multipart"
)
type StorageHandler interface {
Init() error
Add(file *multipart.FileHeader, fileId string) error
Delete(filename string) error
Get(filename string) (string, error)
List() ([]string, error)
}
func NewStorageHandler(strategy typesdefs.StorageStrategy) (StorageHandler, error) {
if config.Cfg.Storage.Path == "" {
2025-08-04 17:45:44 +02:00
return nil, errors.New("storage path not set")
}
switch strategy {
case typesdefs.FS:
2025-08-04 17:45:44 +02:00
h := NewFsHandler()
err := h.Init()
if err != nil {
return nil, err
}
return h, nil
case typesdefs.S3:
2025-08-04 17:45:44 +02:00
h, err := NewS3Handler()
if err != nil {
return nil, err
}
err = h.Init()
if err != nil {
return nil, err
}
return h, nil
default:
return nil, errors.New("invalid storage strategy")
}
}