From 943d8f08650e3b074f69293092475b290518c512 Mon Sep 17 00:00:00 2001 From: johnniang Date: Sat, 30 Mar 2019 00:20:49 +0800 Subject: [PATCH] Complete multi files upload api --- .../admin/api/AttachmentController.java | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/main/java/cc/ryanc/halo/web/controller/admin/api/AttachmentController.java b/src/main/java/cc/ryanc/halo/web/controller/admin/api/AttachmentController.java index 5eb8df2b4..5814596ae 100644 --- a/src/main/java/cc/ryanc/halo/web/controller/admin/api/AttachmentController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/admin/api/AttachmentController.java @@ -1,14 +1,19 @@ package cc.ryanc.halo.web.controller.admin.api; import cc.ryanc.halo.model.dto.AttachmentOutputDTO; +import cc.ryanc.halo.model.entity.Attachment; import cc.ryanc.halo.service.AttachmentService; import io.swagger.annotations.ApiOperation; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.web.PageableDefault; +import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import java.util.LinkedList; +import java.util.List; + import static org.springframework.data.domain.Sort.Direction.DESC; /** @@ -23,7 +28,6 @@ public class AttachmentController { private final AttachmentService attachmentService; - public AttachmentController(AttachmentService attachmentService) { this.attachmentService = attachmentService; } @@ -63,7 +67,23 @@ public class AttachmentController { } @PostMapping("upload") - public AttachmentOutputDTO uploadAttachment(@RequestParam("file") MultipartFile file) { + @ApiOperation("Uploads single file") + public AttachmentOutputDTO uploadAttachment(@RequestPart("file") MultipartFile file) { return new AttachmentOutputDTO().convertFrom(attachmentService.upload(file)); } + + @PostMapping(value = "uploads", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + @ApiOperation("Uploads multi files (Invalid in Swagger UI)") + public List uploadAttachments(@RequestPart("files") MultipartFile[] files) { + List result = new LinkedList<>(); + + for (MultipartFile file : files) { + // Upload single file + Attachment attachment = attachmentService.upload(file); + // Convert and add + result.add(new AttachmentOutputDTO().convertFrom(attachment)); + } + + return result; + } }