|
@@ -21,6 +21,8 @@ import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.util.*;
|
|
@@ -42,6 +44,49 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
|
|
|
@Resource
|
|
|
FileStoreUtil fileStoreUtil;
|
|
|
|
|
|
+ /**
|
|
|
+ * 验证附件
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public AttachmentInfoDto validateAttachment(File file) throws IOException {
|
|
|
+ String fileName = FilenameUtils.getBaseName(file.getName());
|
|
|
+ String format = "." + FilenameUtils.getExtension(file.getName());
|
|
|
+
|
|
|
+ SysConfig sysConfig = commonCacheService.addSysConfigCache(SystemConstant.ATTACHMENT_TYPE);
|
|
|
+ Optional.ofNullable(sysConfig).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("未配置附件类型"));
|
|
|
+ List<String> attachmentTypeList = Arrays.asList(sysConfig.getConfigValue().replaceAll("\\[", "").replaceAll("\\]", "").split(","));
|
|
|
+ if (Objects.nonNull(format)) {
|
|
|
+ long count = attachmentTypeList.stream().filter(s -> format.equalsIgnoreCase(s)).count();
|
|
|
+ if (count == 0) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("文件格式只能为" + attachmentTypeList.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ SysConfig sysConfigLength = commonCacheService.addSysConfigCache(SystemConstant.ATTACHMENT_LENGTH);
|
|
|
+ Optional.ofNullable(sysConfigLength).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("未配置附件名称长度"));
|
|
|
+ int attachmentLength = Integer.parseInt(sysConfigLength.getConfigValue());
|
|
|
+ if (Objects.nonNull(fileName) && fileName.length() > attachmentLength) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("文件名长度不能超过" + attachmentLength + "个字符");
|
|
|
+ }
|
|
|
+ long size = file.length();
|
|
|
+ BigDecimal b = new BigDecimal(size);
|
|
|
+ BigDecimal num = new BigDecimal(1024);
|
|
|
+ b = b.divide(num, 2, BigDecimal.ROUND_HALF_UP).divide(num, 2, BigDecimal.ROUND_HALF_UP)
|
|
|
+ .setScale(2, BigDecimal.ROUND_HALF_UP);
|
|
|
+
|
|
|
+ SysConfig sysConfigSize = commonCacheService.addSysConfigCache(SystemConstant.ATTACHMENT_SIZE);
|
|
|
+ Optional.ofNullable(sysConfigSize).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("未配置附件大小"));
|
|
|
+ double attachmentSize = Double.valueOf(sysConfigSize.getConfigValue());
|
|
|
+ if (b.doubleValue() > attachmentSize) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("文件大小不能超过" + attachmentSize + "MB");
|
|
|
+ }
|
|
|
+ return new AttachmentInfoDto(fileName, format, b, DigestUtils.md5Hex(new FileInputStream(file)));
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 验证附件
|
|
|
*
|
|
@@ -90,6 +135,34 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
|
|
|
return new AttachmentInfoDto(fileName, format, b, fileMd5);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 上传文件接口
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ * @param type
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public BasicAttachment saveAttachment(File file, UploadFileEnum type) throws Exception {
|
|
|
+ BasicAttachment basicAttachment = null;
|
|
|
+ AttachmentInfoDto attachmentInfoDto = this.validateAttachment(file);
|
|
|
+ if (Objects.nonNull(attachmentInfoDto)) {
|
|
|
+ String fileName = SystemConstant.getNanoId();
|
|
|
+ StringJoiner stringJoiner = SystemConstant.getDirName(type, true).add(fileName).add(attachmentInfoDto.getFormat());
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put(SystemConstant.TYPE, fileStoreUtil.uploadFileEnumIsOss(type) ? SystemConstant.OSS : SystemConstant.LOCAL);
|
|
|
+ fileStoreUtil.ossUpload(stringJoiner.toString(), new FileInputStream(file), attachmentInfoDto.getMd5(), type.getFssType());
|
|
|
+ jsonObject.put(SystemConstant.UPLOAD_TYPE, type);
|
|
|
+ jsonObject.put(SystemConstant.PATH, stringJoiner.toString());
|
|
|
+ attachmentInfoDto.setFileName(fileName);
|
|
|
+ basicAttachment = new BasicAttachment(jsonObject.toJSONString(), attachmentInfoDto, null);
|
|
|
+ this.save(basicAttachment);
|
|
|
+ }
|
|
|
+ Optional.ofNullable(basicAttachment).orElseThrow(() -> ExceptionResultEnum.ATTACHMENT_ERROR.exception());
|
|
|
+ return basicAttachment;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 上传文件接口
|
|
|
*
|