Initial commit

This commit is contained in:
2025-05-11 20:32:14 +08:00
commit d97247159f
80 changed files with 13013 additions and 0 deletions

48
server/storage/storage.go Normal file
View File

@@ -0,0 +1,48 @@
package storage
import (
"errors"
"nysoure/server/model"
)
var (
// ErrFileUnavailable is returned when the file is unavailable.
// When this error is returned, it is required to delete the file info from the database.
ErrFileUnavailable = errors.New("file unavailable")
)
type IStorage interface {
// Upload uploads a file to the storage and returns the storage key.
Upload(filePath string) (string, error)
// Download return the download url of the file with the given storage key.
Download(storageKey string) (string, error)
// Delete deletes the file with the given storage key.
Delete(storageKey string) error
// ToString returns the storage configuration as a string.
ToString() string
// FromString initializes the storage configuration from a string.
FromString(config string) error
// Type returns the type of the storage.
Type() string
}
func NewStorage(s model.Storage) IStorage {
switch s.Type {
case "s3":
r := S3Storage{}
err := r.FromString(s.Config)
if err != nil {
return nil
}
return &r
case "local":
r := LocalStorage{}
err := r.FromString(s.Config)
if err != nil {
return nil
}
return &r
}
return nil
}