|
@@ -1,8 +1,8 @@
|
|
|
package com.controller.common;
|
|
|
|
|
|
import cn.com.ty.lift.common.utils.ValuePool;
|
|
|
-import cn.com.ty.lift.common.verify.Verify;
|
|
|
-import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.com.ty.lift.common.verify.Validate;
|
|
|
+import cn.hutool.core.util.ArrayUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
|
import lombok.AllArgsConstructor;
|
|
@@ -13,17 +13,21 @@ import org.springframework.web.bind.annotation.RequestParam;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
|
- * 文件上传接口
|
|
|
+ * 图片上传接口
|
|
|
*/
|
|
|
@Slf4j
|
|
|
@AllArgsConstructor
|
|
|
@RestController
|
|
|
@RequestMapping("common")
|
|
|
public class CommonController {
|
|
|
- private static final String DateInPath = "yyyy/MM/dd";
|
|
|
+
|
|
|
+ private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy/MM/dd");
|
|
|
+
|
|
|
private SystemConfiguration systemConfiguration;
|
|
|
|
|
|
/**
|
|
@@ -34,29 +38,26 @@ public class CommonController {
|
|
|
*/
|
|
|
@PostMapping("uploads")
|
|
|
public RestResponse uploads(@RequestParam("files") MultipartFile[] files) {
|
|
|
- Verify.notTrue(Objects.isNull(files) || files.length == 0, Verify.Upload.fileDataMissing);
|
|
|
+ Validate.notTrue(ArrayUtil.isEmpty(files), ValuePool.UPLOAD_DATA_MISSING);
|
|
|
+ Map<String, MultipartFile> fileMap = new HashMap<>();
|
|
|
+ //1 先解析文件格式
|
|
|
+ for (MultipartFile file : files) {
|
|
|
+ String fileName = handleFile(file);
|
|
|
+ fileMap.put(fileName, file);
|
|
|
+ }
|
|
|
+ //2 批量上传
|
|
|
try {
|
|
|
- Map<String, MultipartFile> fileMap = new HashMap<>();
|
|
|
- //1 先解析文件格式
|
|
|
- for (MultipartFile file : files) {
|
|
|
- String fileName = handleFile(file);
|
|
|
- if (StrUtil.isEmpty(fileName)) {
|
|
|
- return RestResponse.fail(Verify.Upload.fileFormatNotSupport);
|
|
|
- }
|
|
|
- fileMap.put(fileName, file);
|
|
|
- }
|
|
|
- //2 批量上传
|
|
|
List<String> urls = new ArrayList<>();
|
|
|
for (Map.Entry<String, MultipartFile> entry : fileMap.entrySet()) {
|
|
|
String url = systemConfiguration.build().putObject(entry.getKey(), entry.getValue().getBytes());
|
|
|
- log.info("上传文件,文件URL: {}", url);
|
|
|
- Verify.notNull(url, Verify.Upload.fileUploadFail);
|
|
|
+ log.info("upload file complete, file URL: {}", url);
|
|
|
+ Validate.notNull(url, ValuePool.UPLOAD_FAIL);
|
|
|
urls.add(url);
|
|
|
}
|
|
|
return RestResponse.success(urls);
|
|
|
} catch (Exception e) {
|
|
|
- log.error("上传文件异常", e);
|
|
|
- return RestResponse.fail(Verify.Upload.fileUploadFail);
|
|
|
+ log.error("upload file occur exception", e);
|
|
|
+ return RestResponse.fail(ValuePool.UPLOAD_FAIL);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -68,18 +69,15 @@ public class CommonController {
|
|
|
*/
|
|
|
@PostMapping("upload")
|
|
|
public RestResponse upload(@RequestParam("file") MultipartFile file) {
|
|
|
- Verify.notTrue(Objects.isNull(file) || file.isEmpty(), Verify.Upload.fileDataMissing);
|
|
|
+ Validate.notTrue(Objects.isNull(file) || file.isEmpty(), ValuePool.UPLOAD_DATA_MISSING);
|
|
|
+ String fileName = handleFile(file);
|
|
|
try {
|
|
|
- String fileName = handleFile(file);
|
|
|
- if (StrUtil.isEmpty(fileName)) {
|
|
|
- return RestResponse.fail(Verify.Upload.fileFormatNotSupport);
|
|
|
- }
|
|
|
String url = systemConfiguration.build().putObject(fileName, file.getBytes());
|
|
|
- log.info("上传文件,文件URL: {}", url);
|
|
|
+ log.info("upload file complete, file URL: {}", url);
|
|
|
return RestResponse.success(url);
|
|
|
} catch (Exception e) {
|
|
|
- log.error("上传文件异常", e);
|
|
|
- return RestResponse.fail(Verify.Upload.fileUploadFail);
|
|
|
+ log.error("upload file occur exception", e);
|
|
|
+ return RestResponse.fail(ValuePool.UPLOAD_FAIL);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -90,27 +88,29 @@ public class CommonController {
|
|
|
* @return the name string of the file.
|
|
|
*/
|
|
|
private String handleFile(MultipartFile file) {
|
|
|
- Verify.notTrue(Objects.isNull(file) || file.isEmpty(), Verify.Upload.fileDataMissing);
|
|
|
+ Validate.notTrue(Objects.isNull(file) || file.isEmpty(), ValuePool.UPLOAD_DATA_MISSING);
|
|
|
// 获取文件名,带后缀
|
|
|
String originalFilename = file.getOriginalFilename();
|
|
|
- log.info("上传文件,原文件名:{}", originalFilename);
|
|
|
+ log.info("the original file name:{}", originalFilename);
|
|
|
// 获取文件的后缀格式
|
|
|
+ Validate.notNull(originalFilename, ValuePool.UPLOAD_ORIGINAL_NAME_MISSING);
|
|
|
int lastDotIndex = originalFilename.lastIndexOf(ValuePool.DOT);
|
|
|
- Verify.notTrue(-1 == lastDotIndex, "文件名解析不到文件格式");
|
|
|
+ Validate.notTrue(-1 == lastDotIndex, ValuePool.UPLOAD_FORMAT_MISSING);
|
|
|
String fileSuffix = originalFilename.substring(lastDotIndex).toLowerCase();
|
|
|
- Verify.notNull(fileSuffix, Verify.Upload.fileFormatIllegal);
|
|
|
+ Validate.notNull(fileSuffix, ValuePool.UPLOAD_FORMAT_ILLEGAL);
|
|
|
long fileSize = file.getSize();
|
|
|
- if (StrUtil.equalsAny(fileSuffix, Verify.Upload.pics)) {
|
|
|
- Verify.notTrue(fileSize > Verify.Upload.maxSizePic, "文件大小不超过" + Verify.Upload.maxSizePicDesc);
|
|
|
- } else if (StrUtil.equalsAny(fileSuffix, Verify.Upload.files)) {
|
|
|
- Verify.notTrue(fileSize > Verify.Upload.maxSizeFile, "文件大小不超过" + Verify.Upload.maxSizeFileDesc);
|
|
|
- } else if (StrUtil.equalsAny(fileSuffix, Verify.Upload.videos)) {
|
|
|
- Verify.notTrue(fileSize > Verify.Upload.maxSizeVideo, "文件大小不超过" + Verify.Upload.maxSizeVideoDesc);
|
|
|
+ log.info("the size of file: {}", fileSize);
|
|
|
+ if (StrUtil.equalsAny(fileSuffix, ValuePool.UPLOAD_PICS)) {
|
|
|
+ Validate.notTrue(fileSize > ValuePool.UPLOAD_MAX_SIZE_PIC, ValuePool.UPLOAD_MAX_SIZE_PIC_DESC);
|
|
|
+ } else if (StrUtil.equalsAny(fileSuffix, ValuePool.UPLOAD_FILES)) {
|
|
|
+ Validate.notTrue(fileSize > ValuePool.UPLOAD_MAX_SIZE_FILE, ValuePool.UPLOAD_MAX_SIZE_FILE_DESC);
|
|
|
+ } else if (StrUtil.equalsAny(fileSuffix, ValuePool.UPLOAD_VIDEOS)) {
|
|
|
+ Validate.notTrue(fileSize > ValuePool.UPLOAD_MAX_SIZE_VIDEO, ValuePool.UPLOAD_MAX_SIZE_VIDEO_DESC);
|
|
|
} else {
|
|
|
- return null;
|
|
|
+ throw Validate.validateException(ValuePool.UPLOAD_FORMAT_NOT_SUPPORT);
|
|
|
}
|
|
|
- String fileName = StrUtil.format("{}/{}{}", DateUtil.format(DateUtil.date(), DateInPath), IdWorker.getIdStr(), fileSuffix);
|
|
|
- log.info("上传文件,新文件名:{}", fileName);
|
|
|
+ String fileName = StrUtil.format("{}/{}{}", DATE_TIME_FORMATTER.format(LocalDate.now()), IdWorker.getIdStr(), fileSuffix);
|
|
|
+ log.info("the new file name:{}", fileName);
|
|
|
return fileName;
|
|
|
}
|
|
|
}
|