|
@@ -3,8 +3,6 @@ package com.diagbot.service.impl;
|
|
|
import com.diagbot.client.fastdfs.FastDFSClient;
|
|
|
import com.diagbot.client.fastdfs.FastDFSFile;
|
|
|
import com.diagbot.dto.FileDTO;
|
|
|
-import com.diagbot.exception.CommonException;
|
|
|
-import com.diagbot.exception.ServiceErrorCode;
|
|
|
import com.diagbot.service.UploadService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -23,22 +21,20 @@ import java.io.InputStream;
|
|
|
public class UploadServiceImpl implements UploadService {
|
|
|
@Override
|
|
|
public FileDTO singleFileUpload(MultipartFile file) {
|
|
|
- FileDTO fileDTO = new FileDTO();
|
|
|
-
|
|
|
if (file.isEmpty()) {
|
|
|
- throw new CommonException(ServiceErrorCode.FILE_UPLOAD_ERROE, "文件不能为空");
|
|
|
+ return new FileDTO("FAILURE", "文件不能为空");
|
|
|
}
|
|
|
//文件大小上限1M
|
|
|
if (file.getSize() > 1024 * 1024) {
|
|
|
- throw new CommonException(ServiceErrorCode.FILE_MAX_SIZE_LIMIT, "文件上传失败,超出大小限制1MB");
|
|
|
+ return new FileDTO("FAILURE", "文件上传失败,超出大小限制1MB");
|
|
|
}
|
|
|
try {
|
|
|
- fileDTO = saveFile(file);
|
|
|
+ FileDTO fileDTO = saveFile(file);
|
|
|
+ return fileDTO;
|
|
|
} catch (Exception e) {
|
|
|
log.error("文件上传失败", e);
|
|
|
- throw new CommonException(ServiceErrorCode.FILE_UPLOAD_ERROE, "文件上传失败,请重新上传");
|
|
|
+ return new FileDTO("FAILURE", "文件上传失败,请重新上传");
|
|
|
}
|
|
|
- return fileDTO;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -48,7 +44,7 @@ public class UploadServiceImpl implements UploadService {
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
public FileDTO saveFile(MultipartFile multipartFile) throws IOException {
|
|
|
- FileDTO fileDTO = new FileDTO();
|
|
|
+
|
|
|
String[] fileAbsolutePath = {};
|
|
|
String fileName = multipartFile.getOriginalFilename();
|
|
|
String ext = fileName.substring(fileName.lastIndexOf(".") + 1);
|
|
@@ -65,19 +61,18 @@ public class UploadServiceImpl implements UploadService {
|
|
|
fileAbsolutePath = FastDFSClient.upload(file); //upload to fastdfs
|
|
|
} catch (Exception e) {
|
|
|
log.error("文件上传异常", e);
|
|
|
- throw new CommonException(ServiceErrorCode.FILE_UPLOAD_ERROE, "文件上传异常");
|
|
|
+ return new FileDTO("FAILURE", "文件上传异常");
|
|
|
}
|
|
|
if (fileAbsolutePath == null) {
|
|
|
log.error("文件上传失败,请重新上传");
|
|
|
- throw new CommonException(ServiceErrorCode.FILE_UPLOAD_ERROE, "文件上传失败,请重新上传");
|
|
|
+ return new FileDTO("FAILURE", "文件上传失败,请重新上传");
|
|
|
}
|
|
|
String path = "/" + fileAbsolutePath[0] + "/" + fileAbsolutePath[1];
|
|
|
- fileDTO.setState("SUCCESS");
|
|
|
+ FileDTO fileDTO = new FileDTO("SUCCESS", "文件上传成功");
|
|
|
fileDTO.setUrl(path);
|
|
|
fileDTO.setOriginal(multipartFile.getOriginalFilename());
|
|
|
fileDTO.setTitle(multipartFile.getOriginalFilename());
|
|
|
return fileDTO;
|
|
|
-
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -86,7 +81,7 @@ public class UploadServiceImpl implements UploadService {
|
|
|
* @param path
|
|
|
* @return
|
|
|
*/
|
|
|
- public Boolean deleteRemoteFile(String path) {
|
|
|
+ public FileDTO deleteRemoteFile(String path) {
|
|
|
if (path.startsWith("/")) {
|
|
|
path = path.substring(1);
|
|
|
}
|
|
@@ -96,8 +91,8 @@ public class UploadServiceImpl implements UploadService {
|
|
|
FastDFSClient.deleteFile(groupName, fileName);
|
|
|
} catch (Exception e) {
|
|
|
log.error("", e);
|
|
|
- throw new CommonException(ServiceErrorCode.FILE_DELETE_ERROR, "文件删除失败");
|
|
|
+ return new FileDTO("FAILURE", "文件删除失败");
|
|
|
}
|
|
|
- return true;
|
|
|
+ return new FileDTO("SUCCESS", "文件删除成功");
|
|
|
}
|
|
|
}
|