|
@@ -57,82 +57,6 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
|
|
|
@Autowired
|
|
|
private DictionaryConfig dictionaryConfig;
|
|
|
|
|
|
- @Override
|
|
|
- public BasicAttachment save(MultipartFile file, String md5, Long userId) {
|
|
|
- Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
|
|
|
- try {
|
|
|
- if (file == null) {
|
|
|
- throw ExceptionResultEnum.ERROR.exception("文件为空");
|
|
|
- }
|
|
|
- if (StringUtils.isBlank(md5)) {
|
|
|
- throw ExceptionResultEnum.ERROR.exception("md5为空");
|
|
|
- }
|
|
|
- // 文件名
|
|
|
- String fileName = file.getOriginalFilename();
|
|
|
- int lastIndex = fileName.lastIndexOf(".");
|
|
|
- // 文件类型
|
|
|
- String type = fileName.substring(lastIndex);
|
|
|
- List<String> attachmentType = dictionaryConfig.sysDomain().getAttachmentType();
|
|
|
- if (attachmentType != null && attachmentType.size() > 0) {
|
|
|
- if (!attachmentType.contains(type)) {
|
|
|
- throw ExceptionResultEnum.ERROR.exception("文件格式不支持");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 文件大小
|
|
|
- long size = file.getSize();
|
|
|
- if (size > 200 * 1024 * 1024) {
|
|
|
- throw ExceptionResultEnum.ERROR.exception("文件大小不能超过200M");
|
|
|
- }
|
|
|
-
|
|
|
- // md5
|
|
|
- String fileMd5 = DigestUtils.md5Hex(file.getBytes());
|
|
|
- if (!md5.equals(fileMd5)) {
|
|
|
- throw ExceptionResultEnum.ERROR.exception("md5不一致,文件可能损坏");
|
|
|
- }
|
|
|
-
|
|
|
- // 保存
|
|
|
- BasicAttachment attachment = new BasicAttachment();
|
|
|
-
|
|
|
- // 保存文件
|
|
|
- boolean isOss = dictionaryConfig.sysDomain().isOss();
|
|
|
- // 文件保存路径
|
|
|
- LocalDateTime localDateTime = LocalDateTime.now();
|
|
|
- String serverUpload = dictionaryConfig.sysDomain().getServerUpload();
|
|
|
- StringJoiner sj = new StringJoiner(File.separator)
|
|
|
- .add(serverUpload)
|
|
|
- .add(String.valueOf(schoolId))
|
|
|
- .add(String.valueOf(localDateTime.getYear()))
|
|
|
- .add(String.valueOf(localDateTime.getMonthValue()))
|
|
|
- .add(String.valueOf(localDateTime.getDayOfMonth()));
|
|
|
- if (isOss) {
|
|
|
- // todo
|
|
|
- } else {
|
|
|
- File destFolder = new File(sj.toString());
|
|
|
- if (!destFolder.exists()) {
|
|
|
- destFolder.mkdirs();
|
|
|
- }
|
|
|
- File newFile = new File(sj.toString(), UUID.fastUUID() + type);
|
|
|
- FileUtils.copyInputStreamToFile(file.getInputStream(), newFile);
|
|
|
- attachment.setPath(newFile.getPath());
|
|
|
- }
|
|
|
-
|
|
|
- attachment.setName(file.getOriginalFilename());
|
|
|
- attachment.setType(type);
|
|
|
- attachment.setSize(BigDecimal.valueOf(size));
|
|
|
- attachment.setMd5(md5);
|
|
|
- attachment.setCreateId(userId);
|
|
|
- attachment.setCreateTime(System.currentTimeMillis());
|
|
|
-
|
|
|
- this.save(attachment);
|
|
|
- return attachment;
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 保存附件
|
|
|
*
|