|
@@ -0,0 +1,343 @@
|
|
|
+package cn.com.ctop.shiwan.modules.service;
|
|
|
+
|
|
|
+import cn.com.ctop.common.module.utils.Check;
|
|
|
+import cn.com.ctop.common.module.utils.CosUtils;
|
|
|
+import cn.com.ctop.common.module.utils.LoadFileUtil;
|
|
|
+import cn.com.ctop.common.module.vo.ResFileDTO;
|
|
|
+import cn.com.ctop.shiwan.modules.FileUtil;
|
|
|
+import cn.com.ctop.shiwan.modules.entity.ShiwanFileUpload;
|
|
|
+import cn.com.ctop.shiwan.modules.mapper.ShiwanFileUploadMapper;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.xxl.job.core.enums.NoEn;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.jeecg.common.util.DateUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.nio.charset.Charset;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.Enumeration;
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.List;
|
|
|
+import java.util.UUID;
|
|
|
+import java.util.zip.ZipEntry;
|
|
|
+import java.util.zip.ZipFile;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 试玩平台,文件信息
|
|
|
+ * try-play
|
|
|
+ *
|
|
|
+ * @author jeecg-boot
|
|
|
+ * @version V1.0
|
|
|
+ * @date 2020-10-21
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMapper, ShiwanFileUpload> implements cn.com.ctop.shiwan.modules.service.IShiwanFileUploadService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ShiwanFileUploadMapper shiwanFileUploadMapper;
|
|
|
+ @Value("${zip.local.download-path}")
|
|
|
+ private String downloadPath;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ShiwanFileUpload saveOrUpdateFile(MultipartFile file, String userId, Long accountId, String fileType, Long parentId, String uuid) {
|
|
|
+ ShiwanFileUpload existFiles = getExistFiles(userId, accountId, file.getOriginalFilename(), fileType, parentId);
|
|
|
+ StringBuffer path = new StringBuffer();
|
|
|
+ String uuids = UUID.randomUUID().toString();
|
|
|
+ path.append(downloadPath).append(DateUtils.getNowDate(DateUtils.WEB_FORMAT)).append("/").append(uuids).append("/");
|
|
|
+ log.info("----------服务器下载地址:{}", path.toString());
|
|
|
+ ShiwanFileUpload shiwanFileUpload = null;
|
|
|
+ String newUrl = FileUtil.approvalFile(file, path.toString());
|
|
|
+ String md5 = null;
|
|
|
+ try {
|
|
|
+ md5 = LoadFileUtil.getMD5(newUrl);
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("获取MD5签名失败", e);
|
|
|
+ }
|
|
|
+ if (!Check.isNull(existFiles)) {
|
|
|
+ existFiles.setSignature(md5);
|
|
|
+ shiwanFileUpload = updateFile(file, existFiles);
|
|
|
+ if (!Check.isNull(shiwanFileUpload)) {
|
|
|
+ List<String> urlList = unzipFiles(shiwanFileUpload, path.toString());
|
|
|
+ saveInnerFiles(shiwanFileUpload, urlList);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ ShiwanFileUpload fileUpload = new ShiwanFileUpload();
|
|
|
+ fileUpload.setSignature(md5);
|
|
|
+ fileUpload.setUserId(userId);
|
|
|
+ fileUpload.setAccountId(accountId);
|
|
|
+ fileUpload.setFileType(fileType);
|
|
|
+ fileUpload.setParentId(parentId);
|
|
|
+ shiwanFileUpload = saveFile(file, fileUpload, uuid);
|
|
|
+ if (!Check.isNull(shiwanFileUpload)) {
|
|
|
+ List<String> urlList = unzipFiles(shiwanFileUpload, path.toString());
|
|
|
+ saveInnerFiles(shiwanFileUpload, urlList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return shiwanFileUpload;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新数据并上传文件到COS
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return cn.com.ctop.shiwan.modules.entity.ShiwanFileUpload
|
|
|
+ * @throws
|
|
|
+ * @author ZHAOXA
|
|
|
+ */
|
|
|
+ private ShiwanFileUpload updateFile(MultipartFile file, ShiwanFileUpload existFiles) {
|
|
|
+ ResFileDTO resultFile = null;
|
|
|
+ String url = "";
|
|
|
+ ShiwanFileUpload fileUpload = new ShiwanFileUpload();
|
|
|
+ if (existFiles.getParentId() != 0) {
|
|
|
+ url = "try-play/inner-file/";
|
|
|
+ } else {
|
|
|
+ url = "try-play/zip/";
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ resultFile = CosUtils.uploadDetailInputStreamV2(file.getInputStream(), existFiles.getCfileName(), existFiles.getFileSuffix(), file.getSize(), url);
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("更新文件,上传COS失败", e);
|
|
|
+ }
|
|
|
+ QueryWrapper<ShiwanFileUpload> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("id", existFiles.getId());
|
|
|
+ existFiles.setFileSize(resultFile.getFileSize() + "b");
|
|
|
+ existFiles.setUploadTime(new Date());
|
|
|
+ shiwanFileUploadMapper.update(existFiles, queryWrapper);
|
|
|
+ shiwanFileUploadMapper.deleteByParentId(existFiles.getId());
|
|
|
+ return existFiles;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 存储数据并上传文件到COS
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return cn.com.ctop.shiwan.modules.entity.ShiwanFileUpload
|
|
|
+ * @throws
|
|
|
+ * @author ZHAOXA
|
|
|
+ */
|
|
|
+ private ShiwanFileUpload saveFile(MultipartFile file, ShiwanFileUpload fileUpload, String uuid) {
|
|
|
+ String filename = file.getOriginalFilename();
|
|
|
+ ResFileDTO resultFile = null;
|
|
|
+ String url = "";
|
|
|
+ if (Check.isNull(fileUpload.getParentId())) {
|
|
|
+ url = "try-play/zip/";
|
|
|
+ fileUpload.setParentId((long) NoEn.NO0.valueInt());
|
|
|
+ } else {
|
|
|
+ url = "try-play/inner-file/".concat(uuid).concat("/");
|
|
|
+ }
|
|
|
+ String fileSuffix = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
|
|
|
+ try {
|
|
|
+ resultFile = CosUtils.uploadDetailInputStream(file.getInputStream(), filename, fileSuffix, file.getSize(), url);
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("上传COS异常", e);
|
|
|
+ }
|
|
|
+ if (!Check.isNull(resultFile)) {
|
|
|
+ fileUpload.setCfileName(resultFile.getCFileName());
|
|
|
+ fileUpload.setFileName(filename);
|
|
|
+ fileUpload.setFileSuffix(fileSuffix);
|
|
|
+ if ("zip".equals(fileSuffix)) {
|
|
|
+ fileUpload.setFileLevel(NoEn.NO0.valueInt());
|
|
|
+ } else {
|
|
|
+ fileUpload.setFileLevel(NoEn.NO1.valueInt());
|
|
|
+ }
|
|
|
+ fileUpload.setFileStatus(NoEn.NO1.valueStr());
|
|
|
+ fileUpload.setFileSize(resultFile.getFileSize() + "b");
|
|
|
+ fileUpload.setFileUrl(resultFile.getFileUrl());
|
|
|
+ fileUpload.setUploadTime(new Date());
|
|
|
+ shiwanFileUploadMapper.insert(fileUpload);
|
|
|
+ }
|
|
|
+ return shiwanFileUploadMapper.selectById(fileUpload.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ShiwanFileUpload createNewZipFile(MultipartFile file, String id, String updateName, String userId, Long accountId, String fileType) throws IOException {
|
|
|
+ ShiwanFileUpload fileUpload = shiwanFileUploadMapper.selectById(id);
|
|
|
+ StringBuffer path = new StringBuffer();
|
|
|
+ String uuid = UUID.randomUUID().toString();
|
|
|
+ path.append(downloadPath).append(DateUtils.getNowDate(DateUtils.WEB_FORMAT)).append("/").append(uuid).append("/");
|
|
|
+ log.info("----------服务器下载地址:{}", path.toString());
|
|
|
+ //解压zip文件
|
|
|
+ List<String> urlList = unzipFiles(fileUpload, path.toString());
|
|
|
+ if (null != urlList && urlList.size() > 0) {
|
|
|
+ Iterator it = urlList.iterator();
|
|
|
+ while (it.hasNext()) {
|
|
|
+ String url = it.next().toString();
|
|
|
+ String fileName = url.substring(url.lastIndexOf("/") + 1);
|
|
|
+ if (updateName.equals(fileName)) {
|
|
|
+ //移除被替换的对象
|
|
|
+ it.remove();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //添加上新的对象
|
|
|
+ String newUrl = FileUtil.approvalFile(file, path.toString());
|
|
|
+ urlList.add(newUrl);
|
|
|
+ //压缩zip文件
|
|
|
+ String newFileName = DateUtils.getNowDate(DateUtils.LONG_FORMAT).concat("_COPYFROM_").concat(fileUpload.getFileName());
|
|
|
+ return createNewZipFiles(urlList, path.toString(), newFileName, updateName, userId, accountId, fileType);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<ShiwanFileUpload> selectListByParentId(String id) {
|
|
|
+ return shiwanFileUploadMapper.selectListByParentId(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解压文件
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return void
|
|
|
+ * @throws
|
|
|
+ * @author ZHAOXA
|
|
|
+ */
|
|
|
+ private List<String> unzipFiles(ShiwanFileUpload fileUpload, String path) {
|
|
|
+ List<String> urlList = new ArrayList<>();
|
|
|
+ try {
|
|
|
+ //下载到本地服务器
|
|
|
+ String innerZipPath = FileUtil.writeFiles(path.concat("oldZip/"), fileUpload.getFileUrl(), fileUpload.getCfileName());
|
|
|
+ log.info("----------zip包解压地址:{}", innerZipPath);
|
|
|
+ ZipFile zipFile = new ZipFile(innerZipPath, Charset.forName("GBK"));
|
|
|
+ Enumeration<?> enumeration = zipFile.entries();
|
|
|
+ ZipEntry zipEntry = null;
|
|
|
+ path.concat("innerFile/");
|
|
|
+ while (enumeration.hasMoreElements()) {
|
|
|
+ zipEntry = (ZipEntry) enumeration.nextElement();
|
|
|
+ // 如果是文件夹,就创建个文件夹
|
|
|
+ if (zipEntry.isDirectory()) {
|
|
|
+ String dirPath = path + zipEntry.getName();
|
|
|
+ File dir = new File(dirPath);
|
|
|
+ dir.mkdirs();
|
|
|
+ } else {
|
|
|
+ String url = path + zipEntry.getName();
|
|
|
+ // 如果是文件,就先创建一个文件,然后用io流把内容copy过去
|
|
|
+ File targetFile = new File(url);
|
|
|
+ // 保证这个文件的父文件夹必须要存在
|
|
|
+ if (!targetFile.getParentFile().exists()) {
|
|
|
+ targetFile.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
+ targetFile.createNewFile();
|
|
|
+ // 将压缩文件内容写入到这个文件中
|
|
|
+ InputStream is = zipFile.getInputStream(zipEntry);
|
|
|
+ FileOutputStream fos = new FileOutputStream(targetFile);
|
|
|
+ int len;
|
|
|
+ byte[] buf = new byte[8192];
|
|
|
+ while ((len = is.read(buf)) != -1) {
|
|
|
+ fos.write(buf, 0, len);
|
|
|
+ }
|
|
|
+ urlList.add(url);
|
|
|
+ // 关流顺序,先打开的后关闭
|
|
|
+ fos.close();
|
|
|
+ is.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ deletelocalFiles(path.concat("oldZip/").concat(fileUpload.getCfileName()));
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("解压原zip失败 ", e);
|
|
|
+ }
|
|
|
+ return urlList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 压缩ZIP文件,并存储数据到本地
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return void
|
|
|
+ * @throws
|
|
|
+ * @author ZHAOXA
|
|
|
+ */
|
|
|
+ private ShiwanFileUpload createNewZipFiles(List<String> list, String innerFilePath, String zipfilename, String updateName, String userId, Long accountId, String fileType) throws IOException {
|
|
|
+ String zipUrl = FileUtil.listToZip(list, innerFilePath, zipfilename);
|
|
|
+ ShiwanFileUpload shiwanFileUpload = new ShiwanFileUpload();
|
|
|
+ shiwanFileUpload.setUserId(userId);
|
|
|
+ shiwanFileUpload.setAccountId(accountId);
|
|
|
+ shiwanFileUpload.setFileType(fileType);
|
|
|
+ try {
|
|
|
+ shiwanFileUpload.setSignature(LoadFileUtil.getMD5(zipUrl));
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("获取MD5签名失败", e);
|
|
|
+ }
|
|
|
+ //存储新ZIP包
|
|
|
+ ShiwanFileUpload newZipEntity = saveFile(FileUtil.getMulFileByPath(zipUrl), shiwanFileUpload, null);
|
|
|
+ deletelocalFiles(zipUrl);
|
|
|
+ if (!Check.isNull(newZipEntity)) {
|
|
|
+ saveInnerFiles(newZipEntity, list);
|
|
|
+ }
|
|
|
+ return newZipEntity;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 存储zip内部文件
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return void
|
|
|
+ * @throws
|
|
|
+ * @author ZHAOXA
|
|
|
+ */
|
|
|
+ private void saveInnerFiles(ShiwanFileUpload newZipEntity, List<String> list) {
|
|
|
+ MultipartFile mulFileByPath = null;
|
|
|
+ for (String url : list) {
|
|
|
+ ShiwanFileUpload shiwanFileUpload = new ShiwanFileUpload();
|
|
|
+ shiwanFileUpload.setUserId(newZipEntity.getUserId());
|
|
|
+ shiwanFileUpload.setAccountId(newZipEntity.getAccountId());
|
|
|
+ shiwanFileUpload.setFileType(newZipEntity.getFileType());
|
|
|
+ shiwanFileUpload.setParentId(newZipEntity.getId());
|
|
|
+ try {
|
|
|
+ shiwanFileUpload.setSignature(LoadFileUtil.getMD5(url));
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("获取MD5签名失败", e);
|
|
|
+ }
|
|
|
+ mulFileByPath = FileUtil.getMulFileByPath(url);
|
|
|
+ saveFile(mulFileByPath, shiwanFileUpload, newZipEntity.getCfileName());
|
|
|
+ deletelocalFiles(url);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除本地文件
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return void
|
|
|
+ * @throws
|
|
|
+ * @author ZHAOXA
|
|
|
+ */
|
|
|
+ private void deletelocalFiles(String filePath) {
|
|
|
+ File file = new File(filePath);
|
|
|
+ if (file.exists()) {
|
|
|
+ file.delete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询现有的文件
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return cn.com.ctop.shiwan.modules.entity.ShiwanFileUpload
|
|
|
+ * @throws
|
|
|
+ * @author ZHAOXA
|
|
|
+ */
|
|
|
+ private ShiwanFileUpload getExistFiles(String userId, Long accountId, String fileName, String fileType, Long parentId) {
|
|
|
+ JSONObject job = new JSONObject();
|
|
|
+ job.put("useId", userId);
|
|
|
+ job.put("accountId", accountId);
|
|
|
+ job.put("fileName", fileName);
|
|
|
+ job.put("fileType", fileType);
|
|
|
+ job.put("parentId", parentId);
|
|
|
+ ShiwanFileUpload fileEntity = shiwanFileUploadMapper.selectEntity(job);
|
|
|
+ if (Check.isNull(fileEntity)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return fileEntity;
|
|
|
+ }
|
|
|
+}
|