Complete attachment deletion permanently service

pull/137/head
johnniang 2019-03-27 19:37:22 +08:00
parent b6f1a29951
commit c37b6dd4f8
5 changed files with 59 additions and 12 deletions

View File

@ -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);
}
/**

View File

@ -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
}

View File

@ -23,7 +23,8 @@ public interface AttachmentService extends CrudService<Attachment, Integer> {
* @param pageable page info must not be null
* @return a page of attachment output dto
*/
Page<AttachmentOutputDTO> pageDtosBy(Pageable pageable);
@NonNull
Page<AttachmentOutputDTO> pageDtosBy(@NonNull Pageable pageable);
/**
* Uploads file.
@ -34,4 +35,13 @@ public interface AttachmentService extends CrudService<Attachment, Integer> {
*/
@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);
}

View File

@ -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<Attachment, Integ
return create(attachment);
}
@Override
public Attachment removePermanently(Integer id) {
// Remove it from database
Attachment deletedAttachment = removeById(id);
// Remove the file
fileHandlers.delete(deletedAttachment);
log.debug("Deleted attachment: [{}]", deletedAttachment);
return deletedAttachment;
}
/**
* Get attachment type from options.
*

View File

@ -58,8 +58,8 @@ public class AttachmentController {
*/
@DeleteMapping("{id:\\d+}")
@ApiOperation("Delete attachment by id")
public void deletePermanently(@PathVariable("id") Integer id) {
attachmentService.removeById(id);
public AttachmentOutputDTO deletePermanently(@PathVariable("id") Integer id) {
return new AttachmentOutputDTO().convertFrom(attachmentService.removePermanently(id));
}
@PostMapping("upload")