48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
)
|
|
|
|
type Storage interface {
|
|
Save(ctx context.Context, file *UploadedFile) (*UploadedFile, error)
|
|
}
|
|
|
|
type Local struct {
|
|
Host string
|
|
Path string
|
|
}
|
|
|
|
func (p *Local) Save(ctx context.Context, file *UploadedFile) (*UploadedFile, error) {
|
|
saveToPath := filepath.Join(file.Path, time.Now().Format("2006/01/02"))
|
|
if err := os.MkdirAll(saveToPath, 0o755); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
filename := file.Hash + filepath.Ext(file.Name)
|
|
finalPath := filepath.Join(os.TempDir(), filename)
|
|
saveTo := filepath.Join(saveToPath, file.Name)
|
|
if err := os.Rename(finalPath, saveTo); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
file.Path = filepath.Join(time.Now().Format("2006/01/02"), filename)
|
|
file.Preview = filepath.Join(p.Host, time.Now().Format("2006/01/02"), filename)
|
|
return file, nil
|
|
}
|
|
|
|
type S3 struct {
|
|
Endpoint string
|
|
AccessKeyID string
|
|
AccessKeySecret string
|
|
BucketName string
|
|
Path string
|
|
}
|
|
|
|
func (s *S3) Save(ctx context.Context, file *UploadedFile) (*UploadedFile, error) {
|
|
return nil, nil
|
|
}
|