|
@@ -12,6 +12,7 @@ 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.api.vo.Result;
|
|
|
import org.jeecg.common.util.DateUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
@@ -51,38 +52,51 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
|
|
|
|
|
|
@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());
|
|
|
+ path.append(downloadPath).append(DateUtils.getNowDate(DateUtils.WEB_FORMAT)).append("/").append(UUID.randomUUID().toString()).append("/");
|
|
|
+ log.info("-------下载到服务器地址:{}", path.toString());
|
|
|
ShiwanFileUpload shiwanFileUpload = null;
|
|
|
- String newUrl = FileUtil.approvalFile(file, path.toString());
|
|
|
+ String newFileUrl = FileUtil.approvalFile(file, path.toString());
|
|
|
String md5 = null;
|
|
|
try {
|
|
|
- md5 = LoadFileUtil.getMD5(newUrl);
|
|
|
+ md5 = LoadFileUtil.getMD5(newFileUrl);
|
|
|
} catch (IOException e) {
|
|
|
log.error("获取MD5签名失败", e);
|
|
|
}
|
|
|
+ ShiwanFileUpload existFiles = getExistFiles(userId, accountId, file.getOriginalFilename(), fileType, parentId, md5);
|
|
|
if (!Check.isNull(existFiles)) {
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
existFiles.setSignature(md5);
|
|
|
- shiwanFileUpload = updateFile(file, existFiles);
|
|
|
+ try {
|
|
|
+ shiwanFileUpload = updateFile(file, existFiles);
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("更新文件,上传SOC异常", e);
|
|
|
+ }
|
|
|
if (!Check.isNull(shiwanFileUpload)) {
|
|
|
- List<String> urlList = unzipFiles(shiwanFileUpload, path.toString());
|
|
|
+ List<String> urlList = unzipFiles(shiwanFileUpload, path.toString(), newFileUrl);
|
|
|
saveInnerFiles(shiwanFileUpload, urlList);
|
|
|
}
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
+ log.info("文件更新完成,耗时:{} ms", (end - start));
|
|
|
} else {
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
ShiwanFileUpload fileUpload = new ShiwanFileUpload();
|
|
|
fileUpload.setSignature(md5);
|
|
|
fileUpload.setUserId(userId);
|
|
|
fileUpload.setAccountId(accountId);
|
|
|
fileUpload.setFileType(fileType);
|
|
|
- fileUpload.setParentId(parentId);
|
|
|
- shiwanFileUpload = saveFile(file, fileUpload, uuid);
|
|
|
+ try {
|
|
|
+ //新增zip包
|
|
|
+ shiwanFileUpload = saveFile(file, fileUpload, null);
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("新增文件,上传SOC异常", e);
|
|
|
+ }
|
|
|
if (!Check.isNull(shiwanFileUpload)) {
|
|
|
- List<String> urlList = unzipFiles(shiwanFileUpload, path.toString());
|
|
|
+ List<String> urlList = unzipFiles(shiwanFileUpload, path.toString(), newFileUrl);
|
|
|
saveInnerFiles(shiwanFileUpload, urlList);
|
|
|
}
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
+ log.info("文件新增完成,耗时:{} ms", (end - start));
|
|
|
}
|
|
|
return shiwanFileUpload;
|
|
|
}
|
|
@@ -95,7 +109,7 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
|
|
|
* @throws
|
|
|
* @author ZHAOXA
|
|
|
*/
|
|
|
- private ShiwanFileUpload updateFile(MultipartFile file, ShiwanFileUpload existFiles) {
|
|
|
+ private ShiwanFileUpload updateFile(MultipartFile file, ShiwanFileUpload existFiles) throws IOException {
|
|
|
ResFileDTO resultFile = null;
|
|
|
String url = "";
|
|
|
ShiwanFileUpload fileUpload = new ShiwanFileUpload();
|
|
@@ -104,15 +118,23 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
|
|
|
} else {
|
|
|
url = "try-play/zip/";
|
|
|
}
|
|
|
+ InputStream inputStream = null;
|
|
|
try {
|
|
|
- resultFile = CosUtils.uploadDetailInputStreamV2(file.getInputStream(), existFiles.getCfileName(), existFiles.getFileSuffix(), file.getSize(), url);
|
|
|
+ inputStream = file.getInputStream();
|
|
|
+ resultFile = CosUtils.uploadDetailInputStreamV2(inputStream, existFiles.getFileName(), existFiles.getFileSuffix(), file.getSize(), url);
|
|
|
} catch (IOException e) {
|
|
|
log.error("更新文件,上传COS失败", e);
|
|
|
+ } finally {
|
|
|
+ if (inputStream != null) {
|
|
|
+ inputStream.close();
|
|
|
+ }
|
|
|
}
|
|
|
QueryWrapper<ShiwanFileUpload> queryWrapper = new QueryWrapper<>();
|
|
|
queryWrapper.eq("id", existFiles.getId());
|
|
|
- existFiles.setFileSize(resultFile.getFileSize() + "b");
|
|
|
+ existFiles.setFileSize(resultFile.getFileSize() + "B");
|
|
|
existFiles.setUploadTime(new Date());
|
|
|
+ existFiles.setFileUrl(resultFile.getFileUrl());
|
|
|
+ existFiles.setCfileName(resultFile.getCFileName());
|
|
|
shiwanFileUploadMapper.update(existFiles, queryWrapper);
|
|
|
shiwanFileUploadMapper.deleteByParentId(existFiles.getId());
|
|
|
return existFiles;
|
|
@@ -126,7 +148,7 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
|
|
|
* @throws
|
|
|
* @author ZHAOXA
|
|
|
*/
|
|
|
- private ShiwanFileUpload saveFile(MultipartFile file, ShiwanFileUpload fileUpload, String uuid) {
|
|
|
+ private ShiwanFileUpload saveFile(MultipartFile file, ShiwanFileUpload fileUpload, String uuid) throws IOException {
|
|
|
String filename = file.getOriginalFilename();
|
|
|
ResFileDTO resultFile = null;
|
|
|
String url = "";
|
|
@@ -137,10 +159,16 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
|
|
|
url = "try-play/inner-file/".concat(uuid).concat("/");
|
|
|
}
|
|
|
String fileSuffix = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
|
|
|
+ InputStream inputStream = null;
|
|
|
try {
|
|
|
- resultFile = CosUtils.uploadDetailInputStream(file.getInputStream(), filename, fileSuffix, file.getSize(), url);
|
|
|
+ inputStream = file.getInputStream();
|
|
|
+ resultFile = CosUtils.uploadDetailInputStreamV2(inputStream, filename, fileSuffix, file.getSize(), url);
|
|
|
} catch (IOException e) {
|
|
|
log.error("上传COS异常", e);
|
|
|
+ } finally {
|
|
|
+ if (inputStream != null) {
|
|
|
+ inputStream.close();
|
|
|
+ }
|
|
|
}
|
|
|
if (!Check.isNull(resultFile)) {
|
|
|
fileUpload.setCfileName(resultFile.getCFileName());
|
|
@@ -152,7 +180,7 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
|
|
|
fileUpload.setFileLevel(NoEn.NO1.valueInt());
|
|
|
}
|
|
|
fileUpload.setFileStatus(NoEn.NO1.valueStr());
|
|
|
- fileUpload.setFileSize(resultFile.getFileSize() + "b");
|
|
|
+ fileUpload.setFileSize(resultFile.getFileSize() + "B");
|
|
|
fileUpload.setFileUrl(resultFile.getFileUrl());
|
|
|
fileUpload.setUploadTime(new Date());
|
|
|
shiwanFileUploadMapper.insert(fileUpload);
|
|
@@ -162,31 +190,53 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
|
|
|
|
|
|
|
|
|
@Override
|
|
|
- public ShiwanFileUpload createNewZipFile(MultipartFile file, String id, String updateName, String userId, Long accountId, String fileType) throws IOException {
|
|
|
- ShiwanFileUpload fileUpload = shiwanFileUploadMapper.selectById(id);
|
|
|
+ public Result<Object> createNewZipFile(MultipartFile file, String id, String updateName, String userId, Long accountId, String fileType, String isCreate, String newFileName) throws IOException {
|
|
|
+ ShiwanFileUpload oldFile = shiwanFileUploadMapper.selectById(id);
|
|
|
+ if (Check.isNull(oldFile)) {
|
|
|
+ return Result.error("查询原文件数据失败");
|
|
|
+ }
|
|
|
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());
|
|
|
+ List<String> urlList = unzipFiles(oldFile, path.toString(), null);
|
|
|
+ String newFilePath = "";
|
|
|
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)) {
|
|
|
+ newFilePath = url.substring(0, url.lastIndexOf("/") + 1);
|
|
|
//移除被替换的对象
|
|
|
it.remove();
|
|
|
+ deletelocalFiles(url);
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ if (Check.isNull(newFilePath)) {
|
|
|
+ return Result.error("被替换文件名称有误");
|
|
|
+ }
|
|
|
//添加上新的对象
|
|
|
- String newUrl = FileUtil.approvalFile(file, path.toString());
|
|
|
+ String newUrl = FileUtil.approvalFile(file, newFilePath);
|
|
|
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);
|
|
|
+ if (Check.isNull(newFileName) && NoEn.NO1.valueStr().equals(isCreate)) {
|
|
|
+ newFileName = DateUtils.getNowDate(DateUtils.LONG_FORMAT).concat("_COPY-FROM_").concat(oldFile.getFileName());
|
|
|
+ }
|
|
|
+ ShiwanFileUpload newFile = new ShiwanFileUpload();
|
|
|
+ newFile.setUserId(userId);
|
|
|
+ newFile.setAccountId(accountId);
|
|
|
+ newFile.setFileType(fileType);
|
|
|
+ newFile.setFileName(newFileName);
|
|
|
+ oldFile.setUserId(userId);
|
|
|
+ oldFile.setAccountId(accountId);
|
|
|
+ oldFile.setFileType(fileType);
|
|
|
+ oldFile.setFileName(newFileName);
|
|
|
+ ShiwanFileUpload newZipFiles = createNewZipFiles(urlList, path.toString(), newFile, isCreate, oldFile);
|
|
|
+ return Result.ok(newZipFiles);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -202,25 +252,28 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
|
|
|
* @throws
|
|
|
* @author ZHAOXA
|
|
|
*/
|
|
|
- private List<String> unzipFiles(ShiwanFileUpload fileUpload, String path) {
|
|
|
+ private List<String> unzipFiles(ShiwanFileUpload fileUpload, String path, String innerZipPath) {
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
List<String> urlList = new ArrayList<>();
|
|
|
+ //下载到本地服务器
|
|
|
+ if (Check.isNull(innerZipPath)) {
|
|
|
+ innerZipPath = FileUtil.writeFiles(path.concat("oldZip/"), fileUpload.getFileUrl(), fileUpload.getCfileName());
|
|
|
+ }
|
|
|
try {
|
|
|
- //下载到本地服务器
|
|
|
- String innerZipPath = FileUtil.writeFiles(path.concat("oldZip/"), fileUpload.getFileUrl(), fileUpload.getCfileName());
|
|
|
- log.info("----------zip包解压地址:{}", innerZipPath);
|
|
|
+ log.info("----------zip包下载地址:{}", innerZipPath);
|
|
|
ZipFile zipFile = new ZipFile(innerZipPath, Charset.forName("GBK"));
|
|
|
Enumeration<?> enumeration = zipFile.entries();
|
|
|
ZipEntry zipEntry = null;
|
|
|
- path.concat("innerFile/");
|
|
|
+ String innerPath = path.concat("innerFiles/");
|
|
|
while (enumeration.hasMoreElements()) {
|
|
|
zipEntry = (ZipEntry) enumeration.nextElement();
|
|
|
// 如果是文件夹,就创建个文件夹
|
|
|
if (zipEntry.isDirectory()) {
|
|
|
- String dirPath = path + zipEntry.getName();
|
|
|
+ String dirPath = innerPath + zipEntry.getName();
|
|
|
File dir = new File(dirPath);
|
|
|
dir.mkdirs();
|
|
|
} else {
|
|
|
- String url = path + zipEntry.getName();
|
|
|
+ String url = innerPath + zipEntry.getName();
|
|
|
// 如果是文件,就先创建一个文件,然后用io流把内容copy过去
|
|
|
File targetFile = new File(url);
|
|
|
// 保证这个文件的父文件夹必须要存在
|
|
@@ -232,20 +285,22 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
|
|
|
InputStream is = zipFile.getInputStream(zipEntry);
|
|
|
FileOutputStream fos = new FileOutputStream(targetFile);
|
|
|
int len;
|
|
|
- byte[] buf = new byte[8192];
|
|
|
+ byte[] buf = new byte[2048];
|
|
|
while ((len = is.read(buf)) != -1) {
|
|
|
fos.write(buf, 0, len);
|
|
|
}
|
|
|
- urlList.add(url);
|
|
|
// 关流顺序,先打开的后关闭
|
|
|
fos.close();
|
|
|
is.close();
|
|
|
+ urlList.add(url);
|
|
|
}
|
|
|
}
|
|
|
- deletelocalFiles(path.concat("oldZip/").concat(fileUpload.getCfileName()));
|
|
|
} catch (IOException e) {
|
|
|
- log.error("解压原zip失败 ", e);
|
|
|
+ log.error("解压原zip文件失败 ", e);
|
|
|
}
|
|
|
+ deletelocalFiles(innerZipPath);
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
+ log.info("文件解压完成,耗时:{} ms", (end - start));
|
|
|
return urlList;
|
|
|
}
|
|
|
|
|
@@ -257,22 +312,30 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
|
|
|
* @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);
|
|
|
+ private ShiwanFileUpload createNewZipFiles(List<String> list, String path, ShiwanFileUpload addFile, String isCreate, ShiwanFileUpload updateFile) throws IOException {
|
|
|
+ String zipUrl = FileUtil.zipCompress(path.concat(addFile.getFileName()), new File(path.concat("innerFiles/")));
|
|
|
try {
|
|
|
- shiwanFileUpload.setSignature(LoadFileUtil.getMD5(zipUrl));
|
|
|
+ String md5 = LoadFileUtil.getMD5(zipUrl);
|
|
|
+ addFile.setSignature(md5);
|
|
|
+ updateFile.setSignature(md5);
|
|
|
} 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);
|
|
|
+ ShiwanFileUpload newZipEntity = null;
|
|
|
+ //判断是新增还是更新
|
|
|
+ if (NoEn.NO1.valueStr().equals(isCreate)) {
|
|
|
+ //存储新ZIP包
|
|
|
+ newZipEntity = saveFile(FileUtil.getMulFileByPath(zipUrl), addFile, null);
|
|
|
+ deletelocalFiles(zipUrl);
|
|
|
+ if (!Check.isNull(newZipEntity)) {
|
|
|
+ saveInnerFiles(newZipEntity, list);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ newZipEntity = updateFile(FileUtil.getMulFileByPath(zipUrl), updateFile);
|
|
|
+ deletelocalFiles(zipUrl);
|
|
|
+ if (!Check.isNull(newZipEntity)) {
|
|
|
+ saveInnerFiles(newZipEntity, list);
|
|
|
+ }
|
|
|
}
|
|
|
return newZipEntity;
|
|
|
}
|
|
@@ -293,13 +356,25 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
|
|
|
shiwanFileUpload.setAccountId(newZipEntity.getAccountId());
|
|
|
shiwanFileUpload.setFileType(newZipEntity.getFileType());
|
|
|
shiwanFileUpload.setParentId(newZipEntity.getId());
|
|
|
+ String urlpath = url.substring(0, url.lastIndexOf("/"));
|
|
|
+ String lastFileName = urlpath.substring(urlpath.lastIndexOf("/") + 1);
|
|
|
+ String cosFilePath = "";
|
|
|
+ if ("innerFiles".equals(lastFileName)) {
|
|
|
+ cosFilePath = newZipEntity.getCfileName();
|
|
|
+ } else {
|
|
|
+ cosFilePath = newZipEntity.getCfileName().concat("/").concat(lastFileName);
|
|
|
+ }
|
|
|
try {
|
|
|
shiwanFileUpload.setSignature(LoadFileUtil.getMD5(url));
|
|
|
} catch (IOException e) {
|
|
|
log.error("获取MD5签名失败", e);
|
|
|
}
|
|
|
mulFileByPath = FileUtil.getMulFileByPath(url);
|
|
|
- saveFile(mulFileByPath, shiwanFileUpload, newZipEntity.getCfileName());
|
|
|
+ try {
|
|
|
+ saveFile(mulFileByPath, shiwanFileUpload, cosFilePath);
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("新增内部文件,上传SOC异常", e);
|
|
|
+ }
|
|
|
deletelocalFiles(url);
|
|
|
}
|
|
|
}
|
|
@@ -327,13 +402,14 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
|
|
|
* @throws
|
|
|
* @author ZHAOXA
|
|
|
*/
|
|
|
- private ShiwanFileUpload getExistFiles(String userId, Long accountId, String fileName, String fileType, Long parentId) {
|
|
|
+ private ShiwanFileUpload getExistFiles(String userId, Long accountId, String fileName, String fileType, Long parentId, String md5) {
|
|
|
JSONObject job = new JSONObject();
|
|
|
job.put("useId", userId);
|
|
|
job.put("accountId", accountId);
|
|
|
job.put("fileName", fileName);
|
|
|
job.put("fileType", fileType);
|
|
|
job.put("parentId", parentId);
|
|
|
+ job.put("signature", md5);
|
|
|
ShiwanFileUpload fileEntity = shiwanFileUploadMapper.selectEntity(job);
|
|
|
if (Check.isNull(fileEntity)) {
|
|
|
return null;
|