|
@@ -1,26 +1,39 @@
|
|
|
package com.qmth.distributed.print.business.service.impl;
|
|
|
|
|
|
import cn.hutool.core.lang.UUID;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.aliyun.oss.common.utils.BinaryUtil;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.qmth.boot.api.exception.ApiException;
|
|
|
import com.qmth.distributed.print.business.config.DictionaryConfig;
|
|
|
import com.qmth.distributed.print.business.entity.BasicAttachment;
|
|
|
+import com.qmth.distributed.print.business.entity.SysUser;
|
|
|
+import com.qmth.distributed.print.business.enums.UploadFileEnum;
|
|
|
import com.qmth.distributed.print.business.mapper.BasicAttachmentMapper;
|
|
|
import com.qmth.distributed.print.business.service.BasicAttachmentService;
|
|
|
+import com.qmth.distributed.print.business.util.OssUtil;
|
|
|
+import com.qmth.distributed.print.business.util.ServletUtil;
|
|
|
+import com.qmth.distributed.print.common.contant.SystemConstant;
|
|
|
import com.qmth.distributed.print.common.enums.ExceptionResultEnum;
|
|
|
-import com.qmth.distributed.print.business.enums.StorageLevelEnum;
|
|
|
-import com.qmth.distributed.print.business.enums.StorageTypeEnum;
|
|
|
+import com.qmth.distributed.print.common.util.HexUtils;
|
|
|
+import com.qmth.distributed.print.common.util.ResultUtil;
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
import java.util.StringJoiner;
|
|
|
|
|
|
/**
|
|
@@ -33,6 +46,10 @@ import java.util.StringJoiner;
|
|
|
*/
|
|
|
@Service
|
|
|
public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMapper, BasicAttachment> implements BasicAttachmentService {
|
|
|
+ private final static Logger log = LoggerFactory.getLogger(BasicAttachmentServiceImpl.class);
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ OssUtil ossUtil;
|
|
|
|
|
|
@Autowired
|
|
|
private DictionaryConfig dictionaryConfig;
|
|
@@ -111,4 +128,148 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存附件
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ * @param md5
|
|
|
+ * @param type
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public BasicAttachment saveAttachment(MultipartFile file, String md5, UploadFileEnum type) {
|
|
|
+ return saveAttachmentCommon(file, md5, type, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存附件
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ * @param md5
|
|
|
+ * @param type
|
|
|
+ * @param objId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public BasicAttachment saveAttachment(MultipartFile file, String md5, UploadFileEnum type, Long objId) {
|
|
|
+ return saveAttachmentCommon(file, md5, type, objId);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除附件
|
|
|
+ *
|
|
|
+ * @param tbAttachment
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void deleteAttachment(BasicAttachment tbAttachment) {
|
|
|
+ if (Objects.nonNull(tbAttachment) && Objects.nonNull(tbAttachment.getPath())) {
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(tbAttachment.getPath());
|
|
|
+ String type = String.valueOf(jsonObject.get(SystemConstant.TYPE));
|
|
|
+ if (Objects.nonNull(type) && Objects.equals(type, SystemConstant.OSS)) {//删除阿里云附件
|
|
|
+ ossUtil.ossDelete(jsonObject.get(SystemConstant.PATH).toString());
|
|
|
+ } else {//删除服务器附件
|
|
|
+ File file = new File(jsonObject.get(SystemConstant.PATH).toString());
|
|
|
+ file.delete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存附件公用
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ * @param md5
|
|
|
+ * @param type
|
|
|
+ * @param objId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public BasicAttachment saveAttachmentCommon(MultipartFile file, String md5, UploadFileEnum type, Long objId) {
|
|
|
+ if (Objects.isNull(type)) {
|
|
|
+ throw ExceptionResultEnum.ATTACHMENT_TYPE_EMPTY.exception();
|
|
|
+ }
|
|
|
+ BasicAttachment basicAttachment = null;
|
|
|
+ try {
|
|
|
+ SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
|
|
|
+ int temp = file.getOriginalFilename().indexOf(".");
|
|
|
+ String fileName = file.getOriginalFilename().substring(0, temp);
|
|
|
+ String format = file.getOriginalFilename().substring(temp, file.getOriginalFilename().length());
|
|
|
+ List<String> attachmentTypeList = dictionaryConfig.sysDomain().getAttachmentType();
|
|
|
+ if (Objects.nonNull(format)) {
|
|
|
+ long count = attachmentTypeList.stream().filter(s -> format.equalsIgnoreCase(s)).count();
|
|
|
+ if (count == 0) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("文件格式只能为" + attachmentTypeList.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ int attachmentLength = dictionaryConfig.sysDomain().getAttachmentLength().intValue();
|
|
|
+ if (Objects.nonNull(fileName) && fileName.length() > attachmentLength) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("文件名长度不能超过" + attachmentLength + "个字符");
|
|
|
+ }
|
|
|
+ long size = file.getSize();
|
|
|
+ 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);
|
|
|
+ double attachmentSize = dictionaryConfig.sysDomain().getAttachmentSize().doubleValue();
|
|
|
+ if (b.doubleValue() > attachmentSize) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("文件大小不能超过" + attachmentSize + "MB");
|
|
|
+ }
|
|
|
+ log.info("fileName:{}", fileName);
|
|
|
+ log.info("format:{}", format);
|
|
|
+ log.info("size:{}", b);
|
|
|
+ log.info("getOriginalFilename:{}", file.getOriginalFilename());
|
|
|
+ String fileMd5 = DigestUtils.md5Hex(file.getBytes());
|
|
|
+ log.info("fileMd5:{}", fileMd5);
|
|
|
+ log.info("md5:{}", md5);
|
|
|
+ if (!Objects.equals(fileMd5, md5)) {
|
|
|
+ throw ExceptionResultEnum.MD5_EQUALS_FALSE.exception();
|
|
|
+ }
|
|
|
+ boolean oss = dictionaryConfig.sysDomain().isOss();
|
|
|
+ LocalDateTime nowTime = LocalDateTime.now();
|
|
|
+ StringJoiner stringJoiner = new StringJoiner("");
|
|
|
+ if (!oss) {
|
|
|
+ stringJoiner.add(SystemConstant.TEMP_FILES_DIR).add(File.separator);
|
|
|
+ }
|
|
|
+ stringJoiner.add(type.getTitle()).add(File.separator);
|
|
|
+// else if (type == UploadFileEnum.PAPER) {//试卷需要单独
|
|
|
+// stringJoiner.add(type.getTitle()).add(File.separator);
|
|
|
+// } else if (type == UploadFileEnum.UPLOAD) {
|
|
|
+// stringJoiner.add(type.getTitle()).add(File.separator);
|
|
|
+// }
|
|
|
+ stringJoiner.add(String.valueOf(nowTime.getYear())).add(File.separator)
|
|
|
+ .add(String.format("%02d", nowTime.getMonthValue())).add(File.separator)
|
|
|
+ .add(String.format("%02d", nowTime.getDayOfMonth()));
|
|
|
+
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ stringJoiner.add(File.separator).add(SystemConstant.getUuid()).add(format);
|
|
|
+ if (oss) {//上传至oss
|
|
|
+ ossUtil.ossUpload(stringJoiner.toString(), file.getInputStream(), BinaryUtil.toBase64String(HexUtils.decodeHex(md5)));
|
|
|
+ jsonObject.put(SystemConstant.TYPE, SystemConstant.OSS);
|
|
|
+ } else {//上传至服务器
|
|
|
+ File finalFile = new File(stringJoiner.toString());
|
|
|
+ if (!finalFile.exists()) {
|
|
|
+ finalFile.getParentFile().mkdirs();
|
|
|
+ finalFile.createNewFile();
|
|
|
+ }
|
|
|
+ FileUtils.copyInputStreamToFile(file.getInputStream(), finalFile);
|
|
|
+ jsonObject.put(SystemConstant.TYPE, SystemConstant.LOCAL);
|
|
|
+ }
|
|
|
+ jsonObject.put(SystemConstant.PATH, stringJoiner.toString());
|
|
|
+ jsonObject.put(SystemConstant.UPLOAD_TYPE, type);
|
|
|
+
|
|
|
+ basicAttachment = new BasicAttachment(jsonObject.toJSONString(), fileName, format, b, fileMd5, requestUser.getId(), objId);
|
|
|
+ save(basicAttachment);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("请求出错", e);
|
|
|
+ deleteAttachment(basicAttachment);
|
|
|
+ if (e instanceof ApiException) {
|
|
|
+ ResultUtil.error((ApiException) e, e.getMessage());
|
|
|
+ } else {
|
|
|
+ ResultUtil.error(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return basicAttachment;
|
|
|
+ }
|
|
|
}
|