|
@@ -1,15 +1,25 @@
|
|
|
package com.fdkankan.manage.service.impl;
|
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
import com.fdkankan.manage.common.OssPath;
|
|
|
+import com.fdkankan.manage.common.ResultCode;
|
|
|
import com.fdkankan.manage.common.ResultData;
|
|
|
import com.fdkankan.common.util.DateExtUtil;
|
|
|
import com.fdkankan.fyun.face.FYunFileServiceInterface;
|
|
|
+import com.fdkankan.manage.constant.FileTypeEnum;
|
|
|
+import com.fdkankan.manage.entity.CommonUpload;
|
|
|
+import com.fdkankan.manage.exception.BusinessException;
|
|
|
import com.fdkankan.manage.service.ICommonService;
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
-import java.util.Calendar;
|
|
|
-import java.util.UUID;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+import com.fdkankan.manage.service.ICommonUploadService;
|
|
|
+import com.fdkankan.manage.util.FileWriterUtil;
|
|
|
+import com.fdkankan.manage.util.OBJToGLBUtil;
|
|
|
+import com.fdkankan.manage.util.ShellUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -26,6 +36,7 @@ import javax.annotation.Resource;
|
|
|
* @since 2022/6/7
|
|
|
**/
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class CommonServiceImpl implements ICommonService {
|
|
|
|
|
|
@Value("${fyun.host:https://4dkk.4dage.com/}")
|
|
@@ -34,18 +45,132 @@ public class CommonServiceImpl implements ICommonService {
|
|
|
private FYunFileServiceInterface fYunFileServiceInterface;
|
|
|
|
|
|
@Override
|
|
|
- public ResultData uploadFile(MultipartFile file) throws IOException {
|
|
|
- String uuid = UUID.randomUUID().toString();
|
|
|
- String originalFilename = file.getOriginalFilename();
|
|
|
- String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
|
|
|
- String ossPath = String.format(OssPath.MANAGE_FILE_PATH, DateUtil.format(Calendar.getInstance()
|
|
|
- .getTime(), DateExtUtil.dateStyle6), uuid + extName);
|
|
|
- File tempFile = File.createTempFile(uuid ,extName);
|
|
|
- file.transferTo(tempFile);
|
|
|
- fYunFileServiceInterface.uploadFile(tempFile.getPath(), ossPath);
|
|
|
- tempFile.deleteOnExit();
|
|
|
- String url = this.ossUrlPrefix + ossPath;
|
|
|
- return ResultData.ok(url);
|
|
|
+ public ResultData uploadFile(MultipartFile file) {
|
|
|
+ File tempFile = null;
|
|
|
+ try {
|
|
|
+ String uuid = UUID.randomUUID().toString();
|
|
|
+ String originalFilename = file.getOriginalFilename();
|
|
|
+ String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
|
|
|
+ String ossPath = String.format(OssPath.MANAGE_FILE_PATH, DateUtil.format(Calendar.getInstance()
|
|
|
+ .getTime(), DateExtUtil.dateStyle6), uuid + extName);
|
|
|
+
|
|
|
+ tempFile = new File(OssPath.localPath + ossPath);
|
|
|
+ file.transferTo(tempFile);
|
|
|
+ fYunFileServiceInterface.uploadFile(tempFile.getPath(), ossPath);
|
|
|
+ String url = this.ossUrlPrefix + ossPath;
|
|
|
+ return ResultData.ok(url);
|
|
|
+ }catch (Exception e){
|
|
|
+ log.info("upload-file-error:{}",e);
|
|
|
+ throw new BusinessException(ResultCode.UPLOAD_ERROR);
|
|
|
+ }finally {
|
|
|
+ if(tempFile != null){
|
|
|
+ tempFile.delete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ ICommonUploadService commonUploadService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResultData uploadFileNew(MultipartFile file) {
|
|
|
+ if(file.isEmpty() ){
|
|
|
+ throw new BusinessException(ResultCode.UPLOAD_ERROR);
|
|
|
+ }
|
|
|
+ File tempFile = null;
|
|
|
+ try {
|
|
|
+ String uuid = UUID.randomUUID().toString();
|
|
|
+ String originalFilename = file.getOriginalFilename();
|
|
|
+ String extName = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase();
|
|
|
+ String ossPath = String.format(OssPath.MANAGE_MODEL_FILE_PATH, DateUtil.format(Calendar.getInstance()
|
|
|
+ .getTime(), DateExtUtil.dateStyle6), uuid + extName);
|
|
|
+
|
|
|
+ tempFile = new File(OssPath.localPath + ossPath);
|
|
|
+ file.transferTo(tempFile);
|
|
|
+ if(extName.equals(".zip")){
|
|
|
+ return uploadModelZip(tempFile);
|
|
|
+ }
|
|
|
+
|
|
|
+ fYunFileServiceInterface.uploadFile(tempFile.getPath(), ossPath);
|
|
|
+ String url = this.ossUrlPrefix + ossPath;
|
|
|
+
|
|
|
+ FileTypeEnum fileTypeEnum = FileTypeEnum.getByType(extName.replace(".", ""));
|
|
|
+ if(fileTypeEnum == null){
|
|
|
+ throw new BusinessException(ResultCode.FILE_TYPE_ERROR2);
|
|
|
+ }
|
|
|
+ CommonUpload commonUpload = commonUploadService.add(originalFilename.replace(extName, ""), url, String.valueOf(file.getSize()), uuid, fileTypeEnum.getMsg(), extName.replace(".", ""),1,null);
|
|
|
+ return ResultData.ok(commonUpload);
|
|
|
+ }catch (Exception e){
|
|
|
+ log.info("upload-file-error:{}",e);
|
|
|
+ throw new BusinessException(ResultCode.UPLOAD_ERROR);
|
|
|
+ }finally {
|
|
|
+ if(tempFile != null){
|
|
|
+ tempFile.delete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private ResultData uploadModelZip(File tempFile) {
|
|
|
+ String unzipPath = tempFile.getParentFile().getPath() +"/result";
|
|
|
+ ShellUtil.unZip(tempFile.getPath(),unzipPath);
|
|
|
+
|
|
|
+ File unZipFile = new File(unzipPath);
|
|
|
+ if(!unZipFile.exists() || !unZipFile.isDirectory()){
|
|
|
+ throw new BusinessException(ResultCode.UNZIP_ERROR);
|
|
|
+ }
|
|
|
+ List<File> fileList = new ArrayList<>();
|
|
|
+ FileWriterUtil.getCanRunList(fileList,unZipFile);
|
|
|
+ if(fileList.size() <=0){
|
|
|
+ throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
|
|
|
+ }
|
|
|
+ File modelFile = fileList.get(0);
|
|
|
+
|
|
|
+ if(FileWriterUtil.isChinese(modelFile.getName())){
|
|
|
+ throw new BusinessException(ResultCode.FILE_TYPE_ERROR2);
|
|
|
+ }
|
|
|
+ String modelFileFormat = modelFile.getName().split("\\.")[1].toLowerCase();
|
|
|
+ switch (modelFileFormat){
|
|
|
+ case "obj" : OBJToGLBUtil.checkObj(modelFile.getPath());
|
|
|
+ case "laz" :
|
|
|
+ case "b3dm" :
|
|
|
+ case "shp" : uploadOss(unzipPath,modelFile); break;
|
|
|
+ case "las" :
|
|
|
+ case "ply" : uploadLasOrPly(modelFile);break;
|
|
|
+ case "osgb": uploadOsgb(unzipPath,modelFile) ;break;
|
|
|
+ default: break;
|
|
|
+ }
|
|
|
+ String url = "";
|
|
|
+ String extName= ".zip";
|
|
|
+ FileTypeEnum fileTypeEnum = FileTypeEnum.getByType(modelFileFormat);
|
|
|
+ if(fileTypeEnum == null){
|
|
|
+ throw new BusinessException(ResultCode.FILE_TYPE_ERROR2);
|
|
|
+ }
|
|
|
+ CommonUpload commonUpload = commonUploadService.add(tempFile.getName().replace(extName, ""),url, String.valueOf(tempFile.getTotalSpace()),
|
|
|
+ null, fileTypeEnum.getMsg(), extName.replace(".", ""),0,unzipPath);
|
|
|
+
|
|
|
+ return ResultData.ok(commonUpload);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void uploadOsgb(String unzipPath, File modelFile) {
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void uploadOss(String unzipPath,File modelFile) {
|
|
|
+ String ossPath = unzipPath.replace(OssPath.localPath,"");
|
|
|
+ String modelOssPath = modelFile.getPath().replace(OssPath.localPath, "");
|
|
|
+ ShellUtil.yunUpload(unzipPath,ossPath);
|
|
|
+ if(!fYunFileServiceInterface.fileExist(modelOssPath)){
|
|
|
+ throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ private void uploadLasOrPly(File modelFile) {
|
|
|
+ File mntFile = OBJToGLBUtil.lasOrPlyToBin(modelFile);
|
|
|
+ String ossPath = mntFile.getPath().replace(OssPath.localPath,"");
|
|
|
+ ShellUtil.yunUpload(mntFile.getPath(),ossPath);
|
|
|
+ if(!fYunFileServiceInterface.fileExist(ossPath+"/webcloud/cloud.js")){
|
|
|
+ throw new BusinessException(-1,"缺少cloud.js文件");
|
|
|
+ }
|
|
|
}
|
|
|
}
|