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