Files
huobao-drama/infrastructure/storage/local_storage.go
Connor 9600fc542c init
2026-01-12 13:17:11 +08:00

58 lines
1.3 KiB
Go

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)
}