mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 12:17:24 +00:00
Initial commit
This commit is contained in:
61
server/storage/local.go
Normal file
61
server/storage/local.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type LocalStorage struct {
|
||||
Path string
|
||||
}
|
||||
|
||||
func (s *LocalStorage) Upload(filePath string) (string, error) {
|
||||
id := uuid.New().String()
|
||||
inputPath := s.Path + "/" + id
|
||||
input, err := os.OpenFile(inputPath, os.O_RDWR|os.O_CREATE, 0755)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer input.Close()
|
||||
output, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer output.Close()
|
||||
_, err = io.Copy(input, output)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (s *LocalStorage) Download(storageKey string) (string, error) {
|
||||
path := s.Path + "/" + storageKey
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
return "", ErrFileUnavailable
|
||||
}
|
||||
return path, nil
|
||||
}
|
||||
|
||||
func (s *LocalStorage) Delete(storageKey string) error {
|
||||
path := s.Path + "/" + storageKey
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
return os.Remove(path)
|
||||
}
|
||||
|
||||
func (s *LocalStorage) ToString() string {
|
||||
return s.Path
|
||||
}
|
||||
|
||||
func (s *LocalStorage) FromString(config string) error {
|
||||
s.Path = config
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *LocalStorage) Type() string {
|
||||
return "local"
|
||||
}
|
52
server/storage/s3.go
Normal file
52
server/storage/s3.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type S3Storage struct {
|
||||
EndPoint string
|
||||
AccessKeyID string
|
||||
SecretAccessKey string
|
||||
BucketName string
|
||||
}
|
||||
|
||||
func (s *S3Storage) Upload(filePath string) (string, error) {
|
||||
// TODO: Implement S3 upload logic here
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func (s *S3Storage) Download(storageKey string) (string, error) {
|
||||
// TODO: Implement S3 download logic here
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func (s *S3Storage) Delete(storageKey string) error {
|
||||
// TODO: Implement S3 delete logic here
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *S3Storage) ToString() string {
|
||||
data, _ := json.Marshal(s)
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func (s *S3Storage) FromString(config string) error {
|
||||
var s3Config S3Storage
|
||||
if err := json.Unmarshal([]byte(config), &s3Config); err != nil {
|
||||
return err
|
||||
}
|
||||
s.EndPoint = s3Config.EndPoint
|
||||
s.AccessKeyID = s3Config.AccessKeyID
|
||||
s.SecretAccessKey = s3Config.SecretAccessKey
|
||||
s.BucketName = s3Config.BucketName
|
||||
if s.EndPoint == "" || s.AccessKeyID == "" || s.SecretAccessKey == "" || s.BucketName == "" {
|
||||
return errors.New("invalid S3 configuration")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *S3Storage) Type() string {
|
||||
return "s3"
|
||||
}
|
48
server/storage/storage.go
Normal file
48
server/storage/storage.go
Normal 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
|
||||
}
|
Reference in New Issue
Block a user