diff --git a/src/main/java/run/halo/app/controller/admin/api/AttachmentController.java b/src/main/java/run/halo/app/controller/admin/api/AttachmentController.java index cbf4e5199..aa3eccf56 100644 --- a/src/main/java/run/halo/app/controller/admin/api/AttachmentController.java +++ b/src/main/java/run/halo/app/controller/admin/api/AttachmentController.java @@ -7,6 +7,7 @@ import org.springframework.data.web.PageableDefault; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import run.halo.app.cache.lock.CacheLock; import run.halo.app.model.dto.AttachmentDTO; import run.halo.app.model.entity.Attachment; import run.halo.app.model.params.AttachmentParam; @@ -75,11 +76,17 @@ public class AttachmentController { * @param id id */ @DeleteMapping("{id:\\d+}") - @ApiOperation("Delete attachment by id") + @ApiOperation("Delete attachment permanently by id") public AttachmentDTO deletePermanently(@PathVariable("id") Integer id) { return attachmentService.convertToDto(attachmentService.removePermanently(id)); } + @DeleteMapping + @ApiOperation("Delete attachments permanently in batch by id array") + public List deletePermanentlyInBatch(@RequestBody List ids) { + return attachmentService.removePermanently(ids); + } + @PostMapping("upload") @ApiOperation("Uploads single file") public AttachmentDTO uploadAttachment(@RequestPart("file") MultipartFile file) { diff --git a/src/main/java/run/halo/app/service/AttachmentService.java b/src/main/java/run/halo/app/service/AttachmentService.java index 3d8360348..095abf3d3 100644 --- a/src/main/java/run/halo/app/service/AttachmentService.java +++ b/src/main/java/run/halo/app/service/AttachmentService.java @@ -10,6 +10,7 @@ import run.halo.app.model.entity.Attachment; import run.halo.app.model.params.AttachmentQuery; import run.halo.app.service.base.CrudService; +import java.util.Collection; import java.util.List; @@ -49,6 +50,15 @@ public interface AttachmentService extends CrudService { @NonNull Attachment removePermanently(@NonNull Integer id); + /** + * Removes attachment permanently in batch. + * + * @param ids attachment ids must not be null + * @return attachment detail list deleted + */ + @NonNull + List removePermanently(@NonNull Collection ids); + /** * Converts to attachment output dto. * diff --git a/src/main/java/run/halo/app/service/impl/AttachmentServiceImpl.java b/src/main/java/run/halo/app/service/impl/AttachmentServiceImpl.java index feb5c7ddb..e26c47734 100644 --- a/src/main/java/run/halo/app/service/impl/AttachmentServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/AttachmentServiceImpl.java @@ -6,8 +6,10 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.domain.Specification; import org.springframework.lang.NonNull; +import org.springframework.lang.Nullable; import org.springframework.stereotype.Service; import org.springframework.util.Assert; +import org.springframework.util.CollectionUtils; import org.springframework.web.multipart.MultipartFile; import run.halo.app.exception.AlreadyExistsException; import run.halo.app.handler.file.FileHandlers; @@ -22,11 +24,11 @@ import run.halo.app.service.AttachmentService; import run.halo.app.service.OptionService; import run.halo.app.service.base.AbstractCrudService; import run.halo.app.utils.HaloUtils; +import run.halo.app.utils.ServiceUtils; import javax.persistence.criteria.Predicate; -import java.util.LinkedList; -import java.util.List; -import java.util.Objects; +import java.util.*; +import java.util.stream.Collectors; /** * AttachmentService implementation @@ -140,6 +142,15 @@ public class AttachmentServiceImpl extends AbstractCrudService removePermanently(@Nullable Collection ids) { + if (CollectionUtils.isEmpty(ids)) { + return Collections.emptyList(); + } + + return ids.stream().map(this::removePermanently).collect(Collectors.toList()); + } + @Override public AttachmentDTO convertToDto(Attachment attachment) { Assert.notNull(attachment, "Attachment must not be null");