infoalloggi-monorepo/apps/backend/storage_handlers/fs.go
Marco Pedone 1ac2d4b3ab Refactor storage handlers to use centralized configuration
- Updated `fs.go` and `minio.go` to replace hardcoded storage paths with values from the new config package.
- Introduced a new `config` package to manage application configuration, including database and storage settings.
- Removed the `models.go` file from `typedefs` as it was no longer needed.
- Added a new `config.go` file to handle loading environment variables and application settings.
- Created a new `types.go` file in `typedefs` to define storage strategies.
- Updated `storage_handler.go` to utilize the new configuration structure.
2025-08-22 12:57:33 +02:00

75 lines
1.5 KiB
Go

package storagehandlers
import (
"backend/config"
"io"
"log"
"mime/multipart"
"os"
"path/filepath"
)
type Fs_Handler struct {
}
func NewFsHandler() *Fs_Handler {
return &Fs_Handler{}
}
func (f *Fs_Handler) Init() error {
if _, err := os.Stat(config.Cfg.Storage.Path); os.IsNotExist(err) {
return err
}
return nil
}
func (f *Fs_Handler) Add(file *multipart.FileHeader, fileId string) error {
src, err := file.Open()
if err != nil {
log.Printf("Error opening file: %v\n", err)
return err
}
defer src.Close()
extension := filepath.Ext(file.Filename)
destPath := filepath.Join(config.Cfg.Storage.Path, fileId+extension)
dst, err := os.Create(destPath)
if err != nil {
log.Println("Error creating file")
return err
}
defer dst.Close()
if _, err = io.Copy(dst, src); err != nil {
log.Println("Error copying file")
return err
}
return nil
}
func (f *Fs_Handler) Delete(filename string) error {
filePath := filepath.Join(config.Cfg.Storage.Path, filename)
if _, err := os.Stat(filePath); os.IsNotExist(err) {
return err
}
return os.Remove(filePath)
}
func (f *Fs_Handler) Get(filename string) (string, error) {
path := filepath.Join(config.Cfg.Storage.Path, filename)
if _, err := os.Stat(path); os.IsNotExist(err) {
return "", err
}
return path, nil
}
func (f *Fs_Handler) List() ([]string, error) {
files, err := os.ReadDir(config.Cfg.Storage.Path)
if err != nil {
return nil, err
}
var filenames []string
for _, file := range files {
filenames = append(filenames, file.Name())
}
return filenames, nil
}