From c37b6dd4f81e7865663e887096abb056e8ea816e Mon Sep 17 00:00:00 2001 From: johnniang Date: Wed, 27 Mar 2019 19:37:22 +0800 Subject: [PATCH] Complete attachment deletion permanently service --- .../ryanc/halo/filehandler/FileHandlers.java | 37 +++++++++++++++---- .../halo/filehandler/QnYunFileHandler.java | 3 ++ .../ryanc/halo/service/AttachmentService.java | 12 +++++- .../service/impl/AttachmentServiceImpl.java | 15 +++++++- .../admin/api/AttachmentController.java | 4 +- 5 files changed, 59 insertions(+), 12 deletions(-) diff --git a/src/main/java/cc/ryanc/halo/filehandler/FileHandlers.java b/src/main/java/cc/ryanc/halo/filehandler/FileHandlers.java index 7633d249f..89a98e5e2 100644 --- a/src/main/java/cc/ryanc/halo/filehandler/FileHandlers.java +++ b/src/main/java/cc/ryanc/halo/filehandler/FileHandlers.java @@ -1,6 +1,7 @@ package cc.ryanc.halo.filehandler; import cc.ryanc.halo.exception.FileOperationException; +import cc.ryanc.halo.model.entity.Attachment; import cc.ryanc.halo.model.enums.AttachmentType; import cc.ryanc.halo.model.support.UploadResult; import lombok.extern.slf4j.Slf4j; @@ -8,6 +9,7 @@ import org.springframework.context.ApplicationContext; import org.springframework.lang.NonNull; import org.springframework.lang.Nullable; import org.springframework.stereotype.Component; +import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; import org.springframework.web.multipart.MultipartFile; @@ -34,27 +36,46 @@ public class FileHandlers { addFileHandlers(applicationContext.getBeansOfType(FileHandler.class).values()); } - public UploadResult upload(MultipartFile file, AttachmentType attachmentType) { + /** + * Uploads files. + * + * @param file multipart file must not be null + * @param attachmentType attachment type must not be null + * @return upload result + * @throws FileOperationException throws when fail to delete attachment or no available file handler to upload it + */ + @NonNull + public UploadResult upload(@NonNull MultipartFile file, @NonNull AttachmentType attachmentType) { + Assert.notNull(file, "Multipart file must not be null"); + Assert.notNull(attachmentType, "Attachment type must not be null"); + for (FileHandler fileHandler : fileHandlers) { if (fileHandler.supportType(attachmentType)) { return fileHandler.upload(file); } } - log.error("There is no available file handle for attachment type: [{}]", attachmentType); - throw new FileOperationException("No available file handler to filehandler the file").setErrorData(attachmentType); + throw new FileOperationException("No available file handler to upload the file").setErrorData(attachmentType); } - public void delete(String key, AttachmentType attachmentType) { + /** + * Deletes attachment. + * + * @param attachment attachment detail must not be null + * @throws FileOperationException throws when fail to delete attachment or no available file handler to delete it + */ + public void delete(@NonNull Attachment attachment) { + Assert.notNull(attachment, "Attachment must not be null"); + for (FileHandler fileHandler : fileHandlers) { - if (fileHandler.supportType(attachmentType)) { - fileHandler.delete(key); + if (fileHandler.supportType(attachment.getType())) { + // Delete the file + fileHandler.delete(attachment.getFileKey()); return; } } - log.error("There is no available file handle for attachment type: [{}]", attachmentType); - throw new FileOperationException("No available file handler to delete the file").setErrorData(attachmentType); + throw new FileOperationException("No available file handler to delete the file").setErrorData(attachment); } /** diff --git a/src/main/java/cc/ryanc/halo/filehandler/QnYunFileHandler.java b/src/main/java/cc/ryanc/halo/filehandler/QnYunFileHandler.java index 7fc54f76d..192da8b7a 100644 --- a/src/main/java/cc/ryanc/halo/filehandler/QnYunFileHandler.java +++ b/src/main/java/cc/ryanc/halo/filehandler/QnYunFileHandler.java @@ -124,6 +124,9 @@ public class QnYunFileHandler implements FileHandler { @Override public void delete(String key) { + Assert.notNull(key, "File key must not be blank"); + + // TODO Handle file deletion } diff --git a/src/main/java/cc/ryanc/halo/service/AttachmentService.java b/src/main/java/cc/ryanc/halo/service/AttachmentService.java index b548faeae..97f9e23db 100644 --- a/src/main/java/cc/ryanc/halo/service/AttachmentService.java +++ b/src/main/java/cc/ryanc/halo/service/AttachmentService.java @@ -23,7 +23,8 @@ public interface AttachmentService extends CrudService { * @param pageable page info must not be null * @return a page of attachment output dto */ - Page pageDtosBy(Pageable pageable); + @NonNull + Page pageDtosBy(@NonNull Pageable pageable); /** * Uploads file. @@ -34,4 +35,13 @@ public interface AttachmentService extends CrudService { */ @NonNull Attachment upload(@NonNull MultipartFile file); + + /** + * Removes attachment permanently. + * + * @param id attachment id must not be null + * @return attachment detail deleted + */ + @NonNull + Attachment removePermanently(@NonNull Integer id); } diff --git a/src/main/java/cc/ryanc/halo/service/impl/AttachmentServiceImpl.java b/src/main/java/cc/ryanc/halo/service/impl/AttachmentServiceImpl.java index e1e64e774..0098e5bd5 100644 --- a/src/main/java/cc/ryanc/halo/service/impl/AttachmentServiceImpl.java +++ b/src/main/java/cc/ryanc/halo/service/impl/AttachmentServiceImpl.java @@ -1,5 +1,6 @@ package cc.ryanc.halo.service.impl; +import cc.ryanc.halo.filehandler.FileHandlers; import cc.ryanc.halo.model.dto.AttachmentOutputDTO; import cc.ryanc.halo.model.entity.Attachment; import cc.ryanc.halo.model.enums.AttachmentType; @@ -9,7 +10,6 @@ import cc.ryanc.halo.repository.AttachmentRepository; import cc.ryanc.halo.service.AttachmentService; import cc.ryanc.halo.service.OptionService; import cc.ryanc.halo.service.base.AbstractCrudService; -import cc.ryanc.halo.filehandler.FileHandlers; import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -83,6 +83,19 @@ public class AttachmentServiceImpl extends AbstractCrudService