huangtf 2 жил өмнө
parent
commit
ffede840b0

+ 7 - 2
blade-common/src/main/java/org/springblade/common/constant/OssConstant.java

@@ -5,11 +5,16 @@ public interface OssConstant {
     /**
      * 临时目录路径
      */
-    String TEMP_DIRECTORY = "/showtmp";
+    String TEMP_DIRECTORY = "showtmp";
     /**
      * 归档目录路径
      */
-    String ARCHIVE_DIRECTORY = "/archive";
+    String ARCHIVE_DIRECTORY = "archive";
+
+    /**
+     * 归档目录路径
+     */
+    String NORMAL_DIRECTORY = "upload";
 
     /**
      * 分隔符

+ 13 - 1
blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/feign/NewIOSSClient.java

@@ -27,8 +27,20 @@ public interface NewIOSSClient {
     @PostMapping(UPLOAD_FILE_INFO)
     BladeFile uploadFile(@RequestParam String fileName, @RequestParam String localFileUrl);
 
+    /**
+     *  规则  projectId/filePath/fileName,
+     *  如果projectId为空则为filePath/fileName
+     *  如果filePath为空,则为 upload/20230202/fileName
+     *  如果filePath为OssConstant.TEMP_DIRECTORY,一律放进临时目录 OssConstant.TEMP_DIRECTORY/fileName
+     * @param fileName
+     * @param localFileUrl
+     * @param filePath
+     * @param projectId
+     * @return
+     */
     @PostMapping(UPLOAD_FILE_INFO_WITH_PATH)
-    BladeFile uploadFile(@RequestParam String fileName, @RequestParam  String filePath, @RequestParam  String localFileUrl);
+    BladeFile uploadFile(@RequestParam String fileName, @RequestParam  String localFileUrl,
+                         @RequestParam(required=false)  String filePath,@RequestParam(required=false) Long projectId);
 
     @PostMapping(UPLOAD_FILE_INFO_BYTE)
     BladeFile updateFile(@RequestBody byte[] fileByte, @RequestParam String fileName);

+ 19 - 2
blade-ops/blade-resource/src/main/java/org/springblade/resource/builder/ossre/AliossTemplateRe.java

@@ -7,16 +7,19 @@ import com.aliyun.oss.model.*;
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
+import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
 import lombok.SneakyThrows;
+import org.springblade.common.constant.OssConstant;
 import org.springblade.core.oss.model.BladeFile;
 import org.springblade.core.oss.model.OssFile;
 import org.springblade.core.oss.props.OssProperties;
 import org.springblade.core.oss.rule.OssRule;
+import org.springblade.core.secure.utils.AuthUtil;
 import org.springblade.core.tool.jackson.JsonUtil;
 import org.springframework.util.StringUtils;
 import org.springframework.web.multipart.MultipartFile;
@@ -307,7 +310,7 @@ public class AliossTemplateRe implements OssTemplateRe {
         return initResult;
     }
 
-    public BladeFile putFileWithPath(String fileName, String filePath,InputStream stream) {
+    public BladeFile putFileWithPath(String fileName, String filePath,Long projectId, InputStream stream) {
         try {
 
             String suffix = "";
@@ -316,7 +319,6 @@ public class AliossTemplateRe implements OssTemplateRe {
                 suffix = fileName.substring(indexOfDot + 1);
             }
 
-
             String ossUrl = "";
             // 判断容器是否存在,不存在就创建
             String bucket=getBucketName();
@@ -327,6 +329,21 @@ public class AliossTemplateRe implements OssTemplateRe {
                 ossClient.createBucket(createBucketRequest);
             }
             // 设置文件路径和名称
+            //判断是否为存储成   upload/20230322/xxxx.pdf 还是指定目录 aaaa/bbbb.pdf
+            if (StringUtils.isEmpty(filePath)) {
+                Date now = new Date();
+                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
+                String formattedDate = sdf.format(now);
+                filePath = OssConstant.NORMAL_DIRECTORY + "/" + formattedDate;
+            }
+
+            //如果有项目ID,则存为 项目id/....  ,否则直接在最外层, 临时目录除外
+            if (!filePath.equals(OssConstant.TEMP_DIRECTORY)) {
+                if (projectId != null  ) {
+                    filePath = projectId + "/" + filePath;
+                }
+            }
+
             String fileUrl = filePath + "/" + fileName;
             ObjectMetadata objectMetadata = new ObjectMetadata();
             objectMetadata.setContentType(getcontentType(suffix));

+ 1 - 1
blade-ops/blade-resource/src/main/java/org/springblade/resource/builder/ossre/OssTemplateRe.java

@@ -19,5 +19,5 @@ public interface OssTemplateRe extends OssTemplate {
     InitiateMultipartUploadResult initiateMultipartUpload(InitiateMultipartUploadRequest request);
 
     //指定路径上传
-    BladeFile putFileWithPath(String fileName, String filePath,InputStream stream);
+    BladeFile putFileWithPath(String fileName, String filePath,Long projectId, InputStream stream);
 }

+ 2 - 2
blade-ops/blade-resource/src/main/java/org/springblade/resource/feign/NewIOSSClientImpl.java

@@ -68,11 +68,11 @@ public class NewIOSSClientImpl implements NewIOSSClient {
     }
 
     @Override
-    public BladeFile uploadFile(String fileName, String filePath, String localFileUrl) {
+    public BladeFile uploadFile(String fileName, String localFileUrl,String filePath, Long projectId) {
         try {
             //获取文件流
             InputStream inputStream = new FileInputStream(new File(localFileUrl));
-            return this.ossBuilder.template().putFileWithPath(fileName,filePath, inputStream);
+            return this.ossBuilder.template().putFileWithPath(fileName,filePath, projectId,inputStream);
         } catch (Exception e) {
             e.printStackTrace();
         }

+ 2 - 2
blade-service/blade-archive/src/main/java/org/springblade/archive/service/IArchiveAutoPdfService.java

@@ -18,7 +18,7 @@ public interface IArchiveAutoPdfService {
     void builtFilePageNo(ArchivesAuto archivesAuto, List<ArchiveFile> waitArchiveFiles);
 
     // 单个pdf 生成
-    String getBussPdfInfo(String fileName, Map<String, Object> DataInfo, String excelUrl, String filePath )throws Exception;
+    String getBussPdfInfo(String fileName, Map<String, Object> DataInfo, String excelUrl, String localPath ,String ossPath, Long projectId )throws Exception;
 
     //获取文件的pdf文件的url
     String getPdfFileUrl(ArchiveFile file);
@@ -27,5 +27,5 @@ public interface IArchiveAutoPdfService {
     boolean refreshFileNumber(ArchivesAuto archive,String fileNumber);
 
     //合并pdf
-    String MergePdfAndUpload(List<String> urlList,String fileName,String filePath) ;
+    String MergePdfAndUpload(List<String> urlList,String fileName,String filePath,Long projectId) ;
 }

+ 24 - 19
blade-service/blade-archive/src/main/java/org/springblade/archive/service/impl/ArchiveAutoPdfServiceImpl.java

@@ -25,6 +25,7 @@ import org.springblade.common.utils.SnowFlakeUtil;
 import org.springblade.common.vo.DataVO;
 import org.springblade.common.vo.FileSize;
 import org.springblade.core.oss.model.BladeFile;
+import org.springblade.core.secure.utils.AuthUtil;
 import org.springblade.core.tool.utils.ObjectUtil;
 import org.springblade.core.tool.utils.ResourceUtil;
 import org.springblade.core.tool.utils.StringUtil;
@@ -93,7 +94,7 @@ public class ArchiveAutoPdfServiceImpl implements IArchiveAutoPdfService {
         String key1 =  "2__"+ dataVO1.getY() + "_" + dataVO1.getX();
         DataInfo.put(key1,"hahahahaa");
         try {
-            String url = getBussPdfInfo(pkeyId.toString(),DataInfo,excelUrl,file_path);
+            String url = getBussPdfInfo(pkeyId.toString(),DataInfo,excelUrl,file_path,null,null);
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -312,11 +313,11 @@ public class ArchiveAutoPdfServiceImpl implements IArchiveAutoPdfService {
         if (multiLineconfig != null) {
             String coords = multiLineconfig.getCoords();
             String formula = multiLineconfig.getFormula();
-            handleArchiveFile(coords, formula, variables, file_path, excelUrl, dataInfo,urls,fileName);
+            handleArchiveFile(coords, formula, variables, file_path, excelUrl, dataInfo,urls,fileName, archivesAuto.getProjectId());
 
         }else {
             try {
-                String url = getBussPdfInfo(fileName, dataInfo, excelUrl, file_path);
+                String url = getBussPdfInfo(fileName, dataInfo, excelUrl, file_path,OssConstant.ARCHIVE_DIRECTORY,archivesAuto.getProjectId());
                 urls.add(url);
             } catch (Exception e) {
                 e.printStackTrace();
@@ -337,7 +338,7 @@ public class ArchiveAutoPdfServiceImpl implements IArchiveAutoPdfService {
      */
     private void handleArchiveFile(String coords, String formula,
                                    Map<String,Object> variables,String file_path,
-                                   String excelUrl, Map<String, Object> dataInfo,List<String> urls,String fileName){
+                                   String excelUrl, Map<String, Object> dataInfo,List<String> urls,String fileName,Long projectId){
         //todo  目录多页合并一页,文件名带上
 
 
@@ -380,7 +381,7 @@ public class ArchiveAutoPdfServiceImpl implements IArchiveAutoPdfService {
                         subIndex++;
                     }
                     try {
-                        String url = getBussPdfInfo(fileName + subIndex, pageMap, excelUrl, file_path);
+                        String url = getBussPdfInfo(fileName + subIndex, pageMap, excelUrl, file_path,OssConstant.ARCHIVE_DIRECTORY,projectId);
                         localUrls.add(url);
                     } catch (Exception e) {
                         e.printStackTrace();
@@ -398,11 +399,11 @@ public class ArchiveAutoPdfServiceImpl implements IArchiveAutoPdfService {
         //合并成一个
         if (localUrls.size() > 1) {
             Long id = SnowFlakeUtil.getId();
-            String trialPdf = file_path + "/pdf/" + id + ".pdf";
+            String localPdf = file_path + "/pdf/" + id + ".pdf";
 
             //合并当前所有选择的试验pdf
-            FileUtils.mergePdfPublicMethods(localUrls, trialPdf);
-            BladeFile bladeFile = this.newIOSSClient.uploadFile(fileName + ".pdf", trialPdf);
+            FileUtils.mergePdfPublicMethods(localUrls, localPdf);
+            BladeFile bladeFile = this.newIOSSClient.uploadFile(fileName + ".pdf", localPdf, OssConstant.ARCHIVE_DIRECTORY,projectId);
 
 
 //            ByteArrayOutputStream bos = new ByteArrayOutputStream();
@@ -455,19 +456,23 @@ public class ArchiveAutoPdfServiceImpl implements IArchiveAutoPdfService {
      * @param fileName   文件名
      * @param DataInfo  excel和数据
      * @param excelUrl  excel模板
-     * @param filePath  本地路径
+     * @param localPath  本地路径
+     * @param ossPath  oss上的路径
+     * @param projectId 项目ID
      * @return
      * @throws Exception
      */
-    public String getBussPdfInfo(String fileName,Map<String, Object> DataInfo,String excelUrl,String filePath) throws Exception{
+    public String getBussPdfInfo(String fileName,Map<String, Object> DataInfo,String excelUrl,String localPath,
+         String ossPath, Long projectId) throws Exception{
 
         if (fileName == null) {
             fileName = SnowFlakeUtil.getId().toString();
         }
 
-        String pdfPath = filePath + "/pdf//" + fileName + ".pdf";
-        String excelPath = filePath + "/pdf//" + fileName + ".xlsx";
-        File tabPdf = ResourceUtil.getFile(pdfPath);
+        //本地文件路径
+        String localPdfPath = localPath + "/pdf//" + fileName + ".pdf";
+        String excelPath = localPath + "/pdf//" + fileName + ".xlsx";
+        File tabPdf = ResourceUtil.getFile(localPdfPath);
         if (tabPdf.exists()) {
             tabPdf.delete();
         }
@@ -549,8 +554,8 @@ public class ArchiveAutoPdfServiceImpl implements IArchiveAutoPdfService {
 
         FileOutputStream outputStream = new FileOutputStream(excelPath);
         workbook.write(outputStream);
-        FileUtils.setExcelScaleToPdf(excelPath, pdfPath);
-        BladeFile bladeFile = newIOSSClient.uploadFile(fileName + ".pdf", OssConstant.TEMP_DIRECTORY, pdfPath);
+        FileUtils.setExcelScaleToPdf(excelPath, localPdfPath);
+        BladeFile bladeFile = newIOSSClient.uploadFile(fileName + ".pdf", localPdfPath,ossPath, projectId);
 
         String pdfLink = "";
         if (bladeFile!= null ){
@@ -743,14 +748,14 @@ public class ArchiveAutoPdfServiceImpl implements IArchiveAutoPdfService {
      * @param fileName
      * @return
      */
-    public String MergePdfAndUpload(List<String> urlList,String fileName,String filePath) {
+    public String MergePdfAndUpload(List<String> urlList,String fileName,String filePath,Long pojectId) {
        String url = "";
        Long id = SnowFlakeUtil.getId();
-       String trialPdf = FileUtils.LocalPath + "/pdf/" + id + ".pdf";
+       String localPdf = FileUtils.LocalPath + "/pdf/" + id + ".pdf";
 
        try {
             //合并pdf
-            FileUtils.mergePdfPublicMethods(urlList,trialPdf);
+            FileUtils.mergePdfPublicMethods(urlList,localPdf);
 
             if (StringUtils.isEmpty(filePath)) {
                 filePath = OssConstant.TEMP_DIRECTORY;
@@ -759,7 +764,7 @@ public class ArchiveAutoPdfServiceImpl implements IArchiveAutoPdfService {
             fileName += (OssConstant.SEPARATOR + id);
 
             //上传到oss
-            BladeFile file  = newIOSSClient.uploadFile(fileName+ ".pdf", filePath,trialPdf);
+            BladeFile file  = newIOSSClient.uploadFile(fileName+ ".pdf", localPdf,filePath,pojectId);
             url = file.getLink();
         } catch (Exception e) {
             e.printStackTrace();

+ 1 - 1
blade-service/blade-archive/src/main/java/org/springblade/archive/service/impl/ArchivesAutoServiceImpl.java

@@ -1042,7 +1042,7 @@ public class ArchivesAutoServiceImpl extends BaseServiceImpl<ArchivesAutoMapper,
 				urlList.add(frontUrls[3]);
 			}
 
-			url = archiveAutoPdfService.MergePdfAndUpload(urlList,archivesAuto.getName(),null);
+			url = archiveAutoPdfService.MergePdfAndUpload(urlList,archivesAuto.getName(),null,archivesAuto.getProjectId());
 		}
 		return  url;
 	}