💥 Simplified api of multipart uploading

This commit is contained in:
2024-11-05 20:56:53 +08:00
parent 4223eb6ecd
commit 2f99ac7b6e
4 changed files with 49 additions and 151 deletions

View File

@ -74,10 +74,10 @@ func uploadAttachmentMultipart(c *fiber.Ctx) error {
rid := c.Params("file")
cid := c.Params("chunk")
file, err := c.FormFile("file")
if err != nil {
return err
} else if file.Size > viper.GetInt64("performance.file_chunk_size") {
fileData := c.Body()
if len(fileData) == 0 {
return fiber.NewError(fiber.StatusBadRequest, "no file data")
} else if len(fileData) > viper.GetInt("performance.file_chunk_size") {
return fiber.NewError(fiber.StatusBadRequest, "file is too large for one chunk")
}
@ -94,7 +94,7 @@ func uploadAttachmentMultipart(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("chunk %s was uploaded", cid))
}
if err := services.UploadChunkToTemporary(c, cid, file, meta); err != nil {
if err := services.UploadChunkToTemporaryWithRaw(c, cid, fileData, meta); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}

View File

@ -57,6 +57,28 @@ func UploadChunkToTemporary(ctx *fiber.Ctx, cid string, file *multipart.FileHead
}
}
func UploadChunkToTemporaryWithRaw(ctx *fiber.Ctx, cid string, raw []byte, meta models.Attachment) error {
destMap := viper.GetStringMap("destinations.temporary")
var dest models.BaseDestination
rawDest, _ := jsoniter.Marshal(destMap)
_ = jsoniter.Unmarshal(rawDest, &dest)
switch dest.Type {
case models.DestinationTypeLocal:
var destConfigured models.LocalDestination
_ = jsoniter.Unmarshal(rawDest, &destConfigured)
tempPath := filepath.Join(destConfigured.Path, fmt.Sprintf("%s.part%s.partial", meta.Uuid, cid))
destPath := filepath.Join(destConfigured.Path, fmt.Sprintf("%s.part%s", meta.Uuid, cid))
if err := os.WriteFile(tempPath, raw, 0644); err != nil {
return err
}
return os.Rename(tempPath, destPath)
default:
return fmt.Errorf("invalid destination: unsupported protocol %s", dest.Type)
}
}
func CheckChunkExistsInTemporary(meta models.Attachment, cid string) bool {
destMap := viper.GetStringMap("destinations.temporary")