improve storage
This commit is contained in:
parent
a07f826510
commit
3e21d30f62
@ -20,13 +20,14 @@ var (
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
Gin *gin.Engine
|
||||
DB *gorm.DB
|
||||
Config *ServiceConfig
|
||||
Store *photosstore.Store
|
||||
Storage *Storage
|
||||
LogOk *Logger
|
||||
LogErr *Logger
|
||||
Gin *gin.Engine
|
||||
DB *gorm.DB
|
||||
Config *ServiceConfig
|
||||
Store *photosstore.Store
|
||||
StorageTmp *Storage
|
||||
StorageUpload *Storage
|
||||
LogOk *Logger
|
||||
LogErr *Logger
|
||||
}
|
||||
|
||||
type ServiceConfig struct {
|
||||
@ -37,12 +38,13 @@ type ServiceConfig struct {
|
||||
|
||||
func New(config *ServiceConfig) *Service {
|
||||
return &Service{
|
||||
Gin: gin.New(),
|
||||
Config: config,
|
||||
Store: &photosstore.Store{Path: config.StorePath},
|
||||
Storage: &Storage{Path: config.StorePath},
|
||||
LogOk: &Logger{os.Stdout, "Photos"},
|
||||
LogErr: &Logger{os.Stderr, "Photos"},
|
||||
Gin: gin.New(),
|
||||
Config: config,
|
||||
Store: &photosstore.Store{Path: config.StorePath},
|
||||
StorageTmp: NewStorage(config.StorePath, "tmp"),
|
||||
StorageUpload: NewStorage(config.StorePath, "upload"),
|
||||
LogOk: &Logger{os.Stdout, "Photos"},
|
||||
LogErr: &Logger{os.Stderr, "Photos"},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,16 +7,16 @@ import (
|
||||
)
|
||||
|
||||
type Storage struct {
|
||||
Path string
|
||||
BasePath string
|
||||
Path string
|
||||
}
|
||||
|
||||
const (
|
||||
StorageTmp = "tmp"
|
||||
StorageUpload = "upload"
|
||||
)
|
||||
func NewStorage(basePath, path string) *Storage {
|
||||
return &Storage{basePath, path}
|
||||
}
|
||||
|
||||
func (s *Storage) Join(paths ...string) string {
|
||||
return filepath.Join(s.Path, filepath.Join(paths...))
|
||||
return filepath.Join(s.BasePath, s.Path, filepath.Join(paths...))
|
||||
}
|
||||
|
||||
func (s *Storage) Create(paths ...string) error {
|
||||
|
@ -13,8 +13,13 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const (
|
||||
MaxUploadPartSize = 4 << 20 // 4MB
|
||||
)
|
||||
|
||||
var (
|
||||
ErrUploadNotExists = errors.New("upload id doesn't exists")
|
||||
ErrUploadTooLarge = fmt.Errorf("upload part too large ( > %d B)", MaxUploadPartSize)
|
||||
)
|
||||
|
||||
func (s *Service) UploadCreate(c *gin.Context) {
|
||||
@ -24,7 +29,7 @@ func (s *Service) UploadCreate(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := s.Storage.Create(StorageTmp, sha.String()); err != nil {
|
||||
if err := s.StorageTmp.Create(sha.String()); err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
@ -54,13 +59,18 @@ func (s *Service) UploadPart(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if !s.Storage.Exists(StorageTmp, upload.Id) {
|
||||
if !s.StorageTmp.Exists(upload.Id) {
|
||||
c.AbortWithError(http.StatusNotFound, ErrUploadNotExists)
|
||||
return
|
||||
}
|
||||
|
||||
if c.Request.ContentLength > MaxUploadPartSize {
|
||||
c.AbortWithError(http.StatusRequestEntityTooLarge, ErrUploadTooLarge)
|
||||
return
|
||||
}
|
||||
|
||||
f, err := os.Create(
|
||||
s.Storage.Join(StorageTmp, upload.Id, fmt.Sprint(uploadPart.Part)),
|
||||
s.StorageTmp.Join(upload.Id, fmt.Sprint(uploadPart.Part)),
|
||||
)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
@ -93,7 +103,7 @@ func (s *Service) UploadCancel(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := s.Storage.Delete(StorageTmp, upload.Id); err != nil {
|
||||
if err := s.StorageTmp.Delete(upload.Id); err != nil {
|
||||
c.AbortWithError(http.StatusNotFound, ErrUploadNotExists)
|
||||
return
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user