|
@@ -0,0 +1,99 @@
|
|
|
|
+package com.qmth.teachcloud.common.service.impl;
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
+import com.qmth.teachcloud.common.bean.vo.FilePathVo;
|
|
|
|
+import com.qmth.teachcloud.common.entity.SysTools;
|
|
|
|
+import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
|
|
|
|
+import com.qmth.teachcloud.common.enums.ToolsTypeEnum;
|
|
|
|
+import com.qmth.teachcloud.common.enums.UploadFileEnum;
|
|
|
|
+import com.qmth.teachcloud.common.mapper.SysToolsMapper;
|
|
|
|
+import com.qmth.teachcloud.common.service.FileUploadService;
|
|
|
|
+import com.qmth.teachcloud.common.service.SysToolsService;
|
|
|
|
+import com.qmth.teachcloud.common.util.FileUtil;
|
|
|
|
+import org.apache.commons.codec.digest.DigestUtils;
|
|
|
|
+import org.apache.commons.io.FilenameUtils;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
+
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.util.Objects;
|
|
|
|
+import java.util.StringJoiner;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * <p>
|
|
|
|
+ * 工具管理 服务实现类
|
|
|
|
+ * </p>
|
|
|
|
+ *
|
|
|
|
+ * @author xf
|
|
|
|
+ * @since 2024-08-19
|
|
|
|
+ */
|
|
|
|
+@Service
|
|
|
|
+public class SysToolsServiceImpl extends ServiceImpl<SysToolsMapper, SysTools> implements SysToolsService {
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ private FileUploadService fileUploadService;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void upload(MultipartFile file, String md5, ToolsTypeEnum type) {
|
|
|
|
+ File parentDirVar = null;
|
|
|
|
+ try {
|
|
|
|
+ String fileName = FilenameUtils.getName(file.getOriginalFilename());
|
|
|
|
+
|
|
|
|
+// if (!fileName.endsWith(".zip")) {
|
|
|
|
+// throw ExceptionResultEnum.ERROR.exception("只能上传后缀为[.zip]的文件");
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+ String fileMd5 = DigestUtils.md5Hex(file.getBytes());
|
|
|
|
+ if (!Objects.equals(fileMd5, md5)) {
|
|
|
|
+ throw ExceptionResultEnum.MD5_EQUALS_FALSE.exception();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ SysTools sysTools = this.getById(type);
|
|
|
|
+ if (sysTools == null) {
|
|
|
|
+ sysTools = new SysTools(type);
|
|
|
|
+ }
|
|
|
|
+ // 上传
|
|
|
|
+ String filePathName = buildPath(true, UploadFileEnum.FILE.getTitle(), type.name()) + fileName;
|
|
|
|
+ FilePathVo filePathVo = fileUploadService.uploadFile(file, UploadFileEnum.FILE, filePathName, fileMd5);
|
|
|
|
+ sysTools.setPath(JSON.toJSONString(filePathVo));
|
|
|
|
+ // 保存或更新
|
|
|
|
+ this.saveOrUpdate(sysTools);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw ExceptionResultEnum.ERROR.exception(type.getName() + "上传失败:" + e.getMessage());
|
|
|
|
+ } finally {
|
|
|
|
+ if (parentDirVar != null) {
|
|
|
|
+ FileUtil.deleteFile(parentDirVar);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public String getExternalByType(ToolsTypeEnum type) {
|
|
|
|
+ SysTools sysTools = this.getById(type);
|
|
|
|
+ if (sysTools != null) {
|
|
|
|
+ return fileUploadService.filePreview(sysTools.getPath());
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 路径拼接
|
|
|
|
+ *
|
|
|
|
+ * @param endSeparator 结尾是否带分隔符
|
|
|
|
+ * @param paths 路径数组
|
|
|
|
+ */
|
|
|
|
+ private String buildPath(boolean endSeparator, String... paths) {
|
|
|
|
+ StringJoiner stringJoiner = new StringJoiner(File.separator);
|
|
|
|
+ for (String path : paths) {
|
|
|
|
+ stringJoiner.add(path);
|
|
|
|
+ }
|
|
|
|
+ String path = stringJoiner.toString();
|
|
|
|
+ if (endSeparator) {
|
|
|
|
+ path = path + File.separator;
|
|
|
|
+ }
|
|
|
|
+ return path;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|