init
This commit is contained in:
57
infrastructure/storage/local_storage.go
Normal file
57
infrastructure/storage/local_storage.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
type LocalStorage struct {
|
||||
basePath string
|
||||
baseURL string
|
||||
}
|
||||
|
||||
func NewLocalStorage(basePath, baseURL string) (*LocalStorage, error) {
|
||||
if err := os.MkdirAll(basePath, 0755); err != nil {
|
||||
return nil, fmt.Errorf("failed to create storage directory: %w", err)
|
||||
}
|
||||
|
||||
return &LocalStorage{
|
||||
basePath: basePath,
|
||||
baseURL: baseURL,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *LocalStorage) Upload(file io.Reader, filename string, category string) (string, error) {
|
||||
dir := filepath.Join(s.basePath, category)
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return "", fmt.Errorf("failed to create category directory: %w", err)
|
||||
}
|
||||
|
||||
timestamp := time.Now().Format("20060102_150405")
|
||||
newFilename := fmt.Sprintf("%s_%s", timestamp, filename)
|
||||
filePath := filepath.Join(dir, newFilename)
|
||||
|
||||
dst, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create file: %w", err)
|
||||
}
|
||||
defer dst.Close()
|
||||
|
||||
if _, err := io.Copy(dst, file); err != nil {
|
||||
return "", fmt.Errorf("failed to save file: %w", err)
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("%s/%s/%s", s.baseURL, category, newFilename)
|
||||
return url, nil
|
||||
}
|
||||
|
||||
func (s *LocalStorage) Delete(url string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *LocalStorage) GetURL(path string) string {
|
||||
return fmt.Sprintf("%s/%s", s.baseURL, path)
|
||||
}
|
||||
Reference in New Issue
Block a user