Initial commit

This commit is contained in:
2026-01-28 20:44:34 +08:00
commit 500e8b74a7
236 changed files with 29886 additions and 0 deletions

59
internal/storage/local.go Normal file
View File

@@ -0,0 +1,59 @@
package storage
import (
"context"
"io"
"os"
"path/filepath"
)
type LocalStorage struct {
RootPath string
}
func NewLocalStorage(rootPath string) *LocalStorage {
// 确保根目录存在
if _, err := os.Stat(rootPath); os.IsNotExist(err) {
os.MkdirAll(rootPath, 0755)
}
return &LocalStorage{RootPath: rootPath}
}
func (s *LocalStorage) Save(ctx context.Context, path string, reader io.Reader) error {
fullPath := filepath.Join(s.RootPath, path)
dir := filepath.Dir(fullPath)
if _, err := os.Stat(dir); os.IsNotExist(err) {
os.MkdirAll(dir, 0755)
}
file, err := os.Create(fullPath)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(file, reader)
return err
}
func (s *LocalStorage) Open(ctx context.Context, path string) (io.ReadCloser, error) {
fullPath := filepath.Join(s.RootPath, path)
return os.Open(fullPath)
}
func (s *LocalStorage) Delete(ctx context.Context, path string) error {
fullPath := filepath.Join(s.RootPath, path)
return os.Remove(fullPath)
}
func (s *LocalStorage) Exists(ctx context.Context, path string) (bool, error) {
fullPath := filepath.Join(s.RootPath, path)
_, err := os.Stat(fullPath)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}

88
internal/storage/s3.go Normal file
View File

@@ -0,0 +1,88 @@
package storage
import (
"context"
"io"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
type S3Storage struct {
client *s3.Client
bucket string
}
func NewS3Storage(ctx context.Context, endpoint, region, accessKey, secretKey, bucket string, useSSL bool) (*S3Storage, error) {
customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
if endpoint != "" {
return aws.Endpoint{
URL: endpoint,
SigningRegion: region,
HostnameImmutable: true,
}, nil
}
return aws.Endpoint{}, &aws.EndpointNotFoundError{}
})
cfg, err := config.LoadDefaultConfig(ctx,
config.WithRegion(region),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(accessKey, secretKey, "")),
config.WithEndpointResolverWithOptions(customResolver),
)
if err != nil {
return nil, err
}
client := s3.NewFromConfig(cfg)
return &S3Storage{
client: client,
bucket: bucket,
}, nil
}
func (s *S3Storage) Save(ctx context.Context, path string, reader io.Reader) error {
_, err := s.client.PutObject(ctx, &s3.PutObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(path),
Body: reader,
})
return err
}
func (s *S3Storage) Open(ctx context.Context, path string) (io.ReadCloser, error) {
output, err := s.client.GetObject(ctx, &s3.GetObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(path),
})
if err != nil {
return nil, err
}
return output.Body, nil
}
func (s *S3Storage) Delete(ctx context.Context, path string) error {
_, err := s.client.DeleteObject(ctx, &s3.DeleteObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(path),
})
return err
}
func (s *S3Storage) Exists(ctx context.Context, path string) (bool, error) {
_, err := s.client.HeadObject(ctx, &s3.HeadObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(path),
})
if err != nil {
// In AWS SDK v2, we check if the error is 404
if strings.Contains(err.Error(), "NotFound") || strings.Contains(err.Error(), "404") {
return false, nil
}
return false, err
}
return true, nil
}

View File

@@ -0,0 +1,15 @@
package storage
import (
"context"
"io"
)
type Storage interface {
Save(ctx context.Context, path string, reader io.Reader) error
Open(ctx context.Context, path string) (io.ReadCloser, error)
Delete(ctx context.Context, path string) error
Exists(ctx context.Context, path string) (bool, error)
}
var GlobalStorage Storage

View File

@@ -0,0 +1,69 @@
package storage
import (
"context"
"io"
"path/filepath"
"strings"
"github.com/studio-b12/gowebdav"
)
type WebDAVStorage struct {
client *gowebdav.Client
root string
}
func NewWebDAVStorage(url, username, password, root string) *WebDAVStorage {
client := gowebdav.NewClient(url, username, password)
return &WebDAVStorage{
client: client,
root: root,
}
}
func (s *WebDAVStorage) getFullPath(path string) string {
return filepath.ToSlash(filepath.Join(s.root, path))
}
func (s *WebDAVStorage) Save(ctx context.Context, path string, reader io.Reader) error {
fullPath := s.getFullPath(path)
// Ensure directory exists
dir := filepath.Dir(fullPath)
if dir != "." && dir != "/" {
parts := strings.Split(strings.Trim(dir, "/"), "/")
current := ""
for _, part := range parts {
current += "/" + part
_ = s.client.Mkdir(current, 0755)
}
}
return s.client.WriteStream(fullPath, reader, 0644)
}
func (s *WebDAVStorage) Open(ctx context.Context, path string) (io.ReadCloser, error) {
fullPath := s.getFullPath(path)
return s.client.ReadStream(fullPath)
}
func (s *WebDAVStorage) Delete(ctx context.Context, path string) error {
fullPath := s.getFullPath(path)
return s.client.Remove(fullPath)
}
func (s *WebDAVStorage) Exists(ctx context.Context, path string) (bool, error) {
fullPath := s.getFullPath(path)
_, err := s.client.Stat(fullPath)
if err != nil {
// gowebdav's Stat returns error if not found
// We could check for 404 but gowebdav doesn't export error types easily
// Usually we check if it's a 404
if strings.Contains(err.Error(), "404") || strings.Contains(err.Error(), "not found") {
return false, nil
}
return false, err
}
return true, nil
}