|
@@ -0,0 +1,176 @@
|
|
|
|
+package com.qmth.distributed.print.business.service.impl;
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
+import com.qmth.distributed.print.business.bean.upgrade.Mainfest;
|
|
|
|
+import com.qmth.distributed.print.business.entity.ClientUpgrade;
|
|
|
|
+import com.qmth.distributed.print.business.mapper.ClientUpgradeMapper;
|
|
|
|
+import com.qmth.distributed.print.business.service.ClientUpgradeService;
|
|
|
|
+import com.qmth.teachcloud.common.bean.vo.FilePathVo;
|
|
|
|
+import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
|
|
|
|
+import com.qmth.teachcloud.common.enums.UploadFileEnum;
|
|
|
|
+import com.qmth.teachcloud.common.enums.clientpackage.ClientPackageEnum;
|
|
|
|
+import com.qmth.teachcloud.common.service.FileUploadService;
|
|
|
|
+import org.apache.commons.codec.digest.DigestUtils;
|
|
|
|
+import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
|
|
|
|
+import org.apache.commons.io.FilenameUtils;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
+
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.InputStream;
|
|
|
|
+import java.util.Objects;
|
|
|
|
+import java.util.StringJoiner;
|
|
|
|
+import java.util.zip.ZipEntry;
|
|
|
|
+import java.util.zip.ZipInputStream;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * <p>
|
|
|
|
+ * 服务实现类
|
|
|
|
+ * </p>
|
|
|
|
+ *
|
|
|
|
+ * @author xf
|
|
|
|
+ * @since 2024-03-20
|
|
|
|
+ */
|
|
|
|
+@Service
|
|
|
|
+public class ClientUpgradeServiceImpl extends ServiceImpl<ClientUpgradeMapper, ClientUpgrade> implements ClientUpgradeService {
|
|
|
|
+
|
|
|
|
+ private static String platform = "WINDOWS";
|
|
|
|
+ private static String MAIN_FEST_JSON = "mainfest.json";
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ private FileUploadService fileUploadService;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void upload(MultipartFile file, String md5, ClientPackageEnum type) {
|
|
|
|
+ try {
|
|
|
|
+ String fileName = FilenameUtils.getBaseName(file.getOriginalFilename());
|
|
|
|
+
|
|
|
|
+ if (ClientPackageEnum.INSTALL.equals(type) && fileName.endsWith(".exe")) {
|
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("安装包只能上传后缀为[.exe]的文件");
|
|
|
|
+ }
|
|
|
|
+ if (ClientPackageEnum.UPGRADE.equals(type) && 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();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ ClientUpgrade clientUpgrade = this.getByPlatform(platform);
|
|
|
|
+ //安装包
|
|
|
|
+ if (ClientPackageEnum.INSTALL.equals(type)) {
|
|
|
|
+ // 上传
|
|
|
|
+ String filePathName = buildPath(true, UploadFileEnum.UPGRADE.getTitle(), "release", platform) + file.getName();
|
|
|
|
+ FilePathVo filePathVo = fileUploadService.uploadFile(file.getInputStream(), filePathName, UploadFileEnum.UPGRADE);
|
|
|
|
+ clientUpgrade.setInstallPath(JSON.toJSONString(filePathVo));
|
|
|
|
+ } else if (ClientPackageEnum.UPGRADE.equals(type)) {
|
|
|
|
+ // 读取指定文件内容mainfest.josn
|
|
|
|
+ String mainfestContent = readMainfest(file.getInputStream());
|
|
|
|
+ if (StringUtils.isBlank(mainfestContent)) {
|
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("读取mainfest.json文件内容失败");
|
|
|
|
+ }
|
|
|
|
+ // 解析mainfest.josn内容
|
|
|
|
+ Mainfest mainfest = JSON.parseObject(mainfestContent, Mainfest.class);
|
|
|
|
+ String version = mainfest.getVersion();
|
|
|
|
+ if (StringUtils.isBlank(version)) {
|
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("version值必填");
|
|
|
|
+ }
|
|
|
|
+ Integer build = mainfest.getBuild();
|
|
|
|
+ if (build == null) {
|
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("build值必填");
|
|
|
|
+ }
|
|
|
|
+ Integer supportMin = mainfest.getSupportMin();
|
|
|
|
+ if (supportMin == null) {
|
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("supportMin值必填");
|
|
|
|
+ }
|
|
|
|
+ // 上传
|
|
|
|
+ String filePathName = buildPath(true, UploadFileEnum.UPGRADE.getTitle(), "update", platform, version, String.valueOf(build)) + file.getName();
|
|
|
|
+ FilePathVo filePathVo = fileUploadService.uploadFile(file.getInputStream(), filePathName, UploadFileEnum.UPGRADE);
|
|
|
|
+ clientUpgrade.setVersion(version);
|
|
|
|
+ clientUpgrade.setBuild(build);
|
|
|
|
+ clientUpgrade.setSupportMin(supportMin);
|
|
|
|
+ clientUpgrade.setUpgradePath(JSON.toJSONString(filePathVo));
|
|
|
|
+ } else {
|
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("请选择正确的包类型");
|
|
|
|
+ }
|
|
|
|
+ // 保存或更新
|
|
|
|
+ this.saveOrUpdate(clientUpgrade);
|
|
|
|
+ // 升级包
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ } finally {
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 升级数据查询
|
|
|
|
+ *
|
|
|
|
+ * @param platform 平台类型
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public ClientUpgrade getByPlatform(String platform) {
|
|
|
|
+ QueryWrapper<ClientUpgrade> queryWrapper = new QueryWrapper<>();
|
|
|
|
+ queryWrapper.lambda().eq(ClientUpgrade::getPlatform, platform);
|
|
|
|
+ ClientUpgrade clientUpgrade = this.getOne(queryWrapper);
|
|
|
|
+ if (clientUpgrade == null) {
|
|
|
|
+ clientUpgrade = new ClientUpgrade(platform);
|
|
|
|
+ }
|
|
|
|
+ return clientUpgrade;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 读取mainfest.json文件
|
|
|
|
+ *
|
|
|
|
+ * @param inputStream 文件流
|
|
|
|
+ */
|
|
|
|
+ private static String readMainfest(InputStream inputStream) {
|
|
|
|
+ String jsonContent = null;
|
|
|
|
+ try (ZipInputStream zipInputStream = new ZipInputStream(inputStream)) {
|
|
|
|
+ ZipEntry zipEntry;
|
|
|
|
+
|
|
|
|
+ while ((zipEntry = zipInputStream.getNextEntry()) != null) {
|
|
|
|
+ String entryName = zipEntry.getName();
|
|
|
|
+ if (entryName.equals(MAIN_FEST_JSON)) {
|
|
|
|
+ // 读取目标文件的内容
|
|
|
|
+ StringBuilder content = new StringBuilder();
|
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
|
+ int length;
|
|
|
|
+ while ((length = zipInputStream.read(buffer)) > 0) {
|
|
|
|
+ content.append(new String(buffer, 0, length));
|
|
|
|
+ }
|
|
|
|
+ jsonContent = content.toString();
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ zipInputStream.closeEntry();
|
|
|
|
+ }
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return jsonContent;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 路径拼接
|
|
|
|
+ *
|
|
|
|
+ * @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;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|