mirror of https://github.com/halo-dev/halo
Complete multi files upload api
parent
0324947f36
commit
943d8f0865
|
@ -1,14 +1,19 @@
|
||||||
package cc.ryanc.halo.web.controller.admin.api;
|
package cc.ryanc.halo.web.controller.admin.api;
|
||||||
|
|
||||||
import cc.ryanc.halo.model.dto.AttachmentOutputDTO;
|
import cc.ryanc.halo.model.dto.AttachmentOutputDTO;
|
||||||
|
import cc.ryanc.halo.model.entity.Attachment;
|
||||||
import cc.ryanc.halo.service.AttachmentService;
|
import cc.ryanc.halo.service.AttachmentService;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.data.web.PageableDefault;
|
import org.springframework.data.web.PageableDefault;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import static org.springframework.data.domain.Sort.Direction.DESC;
|
import static org.springframework.data.domain.Sort.Direction.DESC;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -23,7 +28,6 @@ public class AttachmentController {
|
||||||
|
|
||||||
private final AttachmentService attachmentService;
|
private final AttachmentService attachmentService;
|
||||||
|
|
||||||
|
|
||||||
public AttachmentController(AttachmentService attachmentService) {
|
public AttachmentController(AttachmentService attachmentService) {
|
||||||
this.attachmentService = attachmentService;
|
this.attachmentService = attachmentService;
|
||||||
}
|
}
|
||||||
|
@ -63,7 +67,23 @@ public class AttachmentController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("upload")
|
@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));
|
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<AttachmentOutputDTO> uploadAttachments(@RequestPart("files") MultipartFile[] files) {
|
||||||
|
List<AttachmentOutputDTO> 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue