Przeglądaj źródła

upgrade fss api...

deason 11 miesięcy temu
rodzic
commit
68615eefab

+ 1 - 54
examcloud-core-print-common/src/main/java/cn/com/qmth/examcloud/core/print/common/SysProperty.java

@@ -23,20 +23,7 @@ public class SysProperty implements Serializable {
 
     private static final long serialVersionUID = 1L;
 
-    @Value("${$upyun.site.1.domain}")
-    protected String domain;
-
-    @Value("${$upyun.site.1.bucketName}")
-    protected String bucketName;
-
-    @Value("${$upyun.site.1.userName}")
-    protected String userName;
-
-    @Value("${$upyun.site.1.password}")
-    protected String password;
-
-    @Value("${$upyun.site.1.uploadUrl}")
-    private String uploadUrl;
+    public static String UPLOAD_DIR = "/ecs-print/prod/";
 
     /**
      * 题库访问地址前缀
@@ -44,46 +31,6 @@ public class SysProperty implements Serializable {
     @Value("${$question.url.prefix}")
     private String questionUrlPrefix;
 
-    public String getDomain() {
-        return domain;
-    }
-
-    public void setDomain(String domain) {
-        this.domain = domain;
-    }
-
-    public String getBucketName() {
-        return bucketName;
-    }
-
-    public void setBucketName(String bucketName) {
-        this.bucketName = bucketName;
-    }
-
-    public String getUserName() {
-        return userName;
-    }
-
-    public void setUserName(String userName) {
-        this.userName = userName;
-    }
-
-    public String getPassword() {
-        return password;
-    }
-
-    public void setPassword(String password) {
-        this.password = password;
-    }
-
-    public String getUploadUrl() {
-        return uploadUrl;
-    }
-
-    public void setUploadUrl(String uploadUrl) {
-        this.uploadUrl = uploadUrl;
-    }
-
     public String getQuestionUrlPrefix() {
         return questionUrlPrefix;
     }

+ 100 - 107
examcloud-core-print-common/src/main/java/cn/com/qmth/examcloud/core/print/common/upyun/UpYunClient.java

@@ -1,107 +1,100 @@
-/*
- * *************************************************
- * Copyright (c) 2018 QMTH. All Rights Reserved.
- * Created by Deason on 2018-11-01 17:02:06.
- * *************************************************
- */
-
-package cn.com.qmth.examcloud.core.print.common.upyun;
-
-import cn.com.qmth.examcloud.commons.exception.StatusException;
-import cn.com.qmth.examcloud.core.print.common.SysProperty;
-import cn.com.qmth.examcloud.core.print.common.utils.Check;
-import cn.com.qmth.examcloud.core.print.common.utils.FileUtils;
-import cn.com.qmth.examcloud.web.config.SystemProperties;
-import main.java.com.UpYun;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Component;
-
-import java.io.File;
-
-import static cn.com.qmth.examcloud.core.print.common.Constants.SYS_CODE_400;
-import static cn.com.qmth.examcloud.core.print.common.Constants.SYS_CODE_500;
-
-/**
- * @author: QMTH
- * @since: 2018/11/1
- */
-@Component
-public class UpYunClient {
-
-    private static final Logger log = LoggerFactory.getLogger(UpYunClient.class);
-
-    @Autowired
-    private SystemProperties systemProperties;
-
-    @Autowired
-    private SysProperty sysProperty;
-
-    public String upload(File file) {
-        Check.isEmpty(file, "上传的文件不能为空!");
-        if (!file.exists() || !file.isFile()) {
-            throw new StatusException(SYS_CODE_400, "上传的文件不存在!");
-        }
-
-        try {
-            final String newFileName = FileUtils.newFileName(file.getName());
-            final String newFilePath = sysProperty.getUploadUrl() + FileUtils.dateDir() + newFileName;
-            this.getInstance().writeFile(newFilePath, file, true);
-
-            //成功后,返回文件访问地址
-            return this.getUrlPrefix() + newFilePath;
-        } catch (Exception e) {
-            log.error(e.getMessage(), e);
-            throw new StatusException(SYS_CODE_500, "上传文件异常!");
-        }
-    }
-
-    public String upload(byte[] bytes, String originalFilename) {
-        Check.isEmpty(bytes, "上传的文件不能为空!");
-        Check.isBlank(originalFilename, "上传的文件名不正确!");
-
-        try {
-            final String newFileName = FileUtils.newFileName(originalFilename);
-            final String newFilePath = sysProperty.getUploadUrl() + FileUtils.dateDir() + newFileName;
-            this.getInstance().writeFile(newFilePath, bytes, true);
-
-            //成功后,返回文件访问地址
-            return this.getUrlPrefix() + newFilePath;
-        } catch (Exception e) {
-            log.error(e.getMessage(), e);
-            throw new StatusException(SYS_CODE_500, "上传文件异常!");
-        }
-    }
-
-    public File download(String filePath) {
-        try {
-            //final String rootDir = Constants.rootFileDir();
-            final String rootDir = systemProperties.getTempDataDir();
-            FileUtils.makeDirs(rootDir);
-            final String newFilePath = rootDir + "/" + FileUtils.newFileName(filePath);
-            File file = new File(newFilePath);
-            this.getInstance().readFile(filePath, file);
-            return file;
-        } catch (Exception e) {
-            log.error(e.getMessage(), e);
-        }
-        return null;
-    }
-
-    public UpYun getInstance() {
-        UpYun upyun = new UpYun(sysProperty.getBucketName(), sysProperty.getUserName(), sysProperty.getPassword());
-        upyun.setDebug(true);
-        upyun.setApiDomain(UpYun.ED_AUTO);
-        return upyun;
-    }
-
-    public String getUrlPrefix() {
-        return sysProperty.getDomain();
-    }
-
-    public SysProperty getProperty() {
-        return sysProperty;
-    }
-
-}
+// package cn.com.qmth.examcloud.core.print.common.upyun;
+//
+// import cn.com.qmth.examcloud.commons.exception.StatusException;
+// import cn.com.qmth.examcloud.core.print.common.SysProperty;
+// import cn.com.qmth.examcloud.core.print.common.utils.Check;
+// import cn.com.qmth.examcloud.core.print.common.utils.FileUtils;
+// import cn.com.qmth.examcloud.web.config.SystemProperties;
+// import main.java.com.UpYun;
+// import org.slf4j.Logger;
+// import org.slf4j.LoggerFactory;
+// import org.springframework.beans.factory.annotation.Autowired;
+// import org.springframework.stereotype.Component;
+//
+// import java.io.File;
+//
+// import static cn.com.qmth.examcloud.core.print.common.Constants.SYS_CODE_400;
+// import static cn.com.qmth.examcloud.core.print.common.Constants.SYS_CODE_500;
+//
+// /**
+//  * @author: QMTH
+//  * @since: 2018/11/1
+//  */
+// @Component
+// public class UpYunClient {
+//
+//     private static final Logger log = LoggerFactory.getLogger(UpYunClient.class);
+//
+//     @Autowired
+//     private SystemProperties systemProperties;
+//
+//     @Autowired
+//     private SysProperty sysProperty;
+//
+//     public String upload(File file) {
+//         Check.isEmpty(file, "上传的文件不能为空!");
+//         if (!file.exists() || !file.isFile()) {
+//             throw new StatusException(SYS_CODE_400, "上传的文件不存在!");
+//         }
+//
+//         try {
+//             final String newFileName = FileUtils.newFileName(file.getName());
+//             final String newFilePath = SysProperty.UPLOAD_DIR + FileUtils.dateDir() + newFileName;
+//             this.getInstance().writeFile(newFilePath, file, true);
+//
+//             //成功后,返回文件访问地址
+//             return this.getUrlPrefix() + newFilePath;
+//         } catch (Exception e) {
+//             log.error(e.getMessage(), e);
+//             throw new StatusException(SYS_CODE_500, "上传文件异常!");
+//         }
+//     }
+//
+//     public String upload(byte[] bytes, String originalFilename) {
+//         Check.isEmpty(bytes, "上传的文件不能为空!");
+//         Check.isBlank(originalFilename, "上传的文件名不正确!");
+//
+//         try {
+//             final String newFileName = FileUtils.newFileName(originalFilename);
+//             final String newFilePath = SysProperty.UPLOAD_DIR + FileUtils.dateDir() + newFileName;
+//             this.getInstance().writeFile(newFilePath, bytes, true);
+//
+//             //成功后,返回文件访问地址
+//             return this.getUrlPrefix() + newFilePath;
+//         } catch (Exception e) {
+//             log.error(e.getMessage(), e);
+//             throw new StatusException(SYS_CODE_500, "上传文件异常!");
+//         }
+//     }
+//
+//     public File download(String filePath) {
+//         try {
+//             //final String rootDir = Constants.rootFileDir();
+//             final String rootDir = systemProperties.getTempDataDir();
+//             FileUtils.makeDirs(rootDir);
+//             final String newFilePath = rootDir + "/" + FileUtils.newFileName(filePath);
+//             File file = new File(newFilePath);
+//             this.getInstance().readFile(filePath, file);
+//             return file;
+//         } catch (Exception e) {
+//             log.error(e.getMessage(), e);
+//         }
+//         return null;
+//     }
+//
+//     public UpYun getInstance() {
+//         UpYun upyun = new UpYun(sysProperty.getBucketName(), sysProperty.getUserName(), sysProperty.getPassword());
+//         upyun.setDebug(true);
+//         upyun.setApiDomain(UpYun.ED_AUTO);
+//         return upyun;
+//     }
+//
+//     public String getUrlPrefix() {
+//         return sysProperty.getDomain();
+//     }
+//
+//     public SysProperty getProperty() {
+//         return sysProperty;
+//     }
+//
+// }

+ 21 - 13
examcloud-core-print-provider/src/main/java/cn/com/qmth/examcloud/core/print/api/controller/CommonController.java

@@ -9,13 +9,15 @@ package cn.com.qmth.examcloud.core.print.api.controller;
 
 import cn.com.qmth.examcloud.commons.exception.StatusException;
 import cn.com.qmth.examcloud.core.print.common.Result;
-import cn.com.qmth.examcloud.core.print.common.upyun.UpYunClient;
+import cn.com.qmth.examcloud.core.print.common.SysProperty;
 import cn.com.qmth.examcloud.core.print.common.utils.FileUtils;
+import cn.com.qmth.examcloud.support.fss.FssFactory;
+import cn.com.qmth.examcloud.support.fss.model.FssFileInfo;
+import cn.com.qmth.examcloud.web.config.SystemProperties;
 import cn.com.qmth.examcloud.web.support.ControllerSupport;
 import cn.com.qmth.examcloud.web.support.Naked;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
-import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -27,7 +29,6 @@ import javax.servlet.http.HttpServletRequest;
 import java.io.File;
 
 import static cn.com.qmth.examcloud.core.print.common.Constants.SYS_CODE_400;
-import static cn.com.qmth.examcloud.core.print.common.Constants.SYS_CODE_500;
 
 /**
  * 常用相关接口
@@ -39,9 +40,11 @@ import static cn.com.qmth.examcloud.core.print.common.Constants.SYS_CODE_500;
 @Api(tags = "常用相关接口")
 @RequestMapping("${$rmp.ctrl.print}/common")
 public class CommonController extends ControllerSupport {
+
     private static final Logger log = LoggerFactory.getLogger(CommonController.class);
+
     @Autowired
-    private UpYunClient upYunClient;
+    private SystemProperties systemProperties;
 
     @Naked
     @PostMapping("/upload")
@@ -59,25 +62,30 @@ public class CommonController extends ControllerSupport {
             throw new StatusException(SYS_CODE_400, "上传的文件不能为空!");
         }
 
-        String fileUrl = upYunClient.upload(file.getBytes(), file.getOriginalFilename());
-        if (StringUtils.isNotEmpty(fileUrl)) {
-            return Result.success(fileUrl);
-        }
+        final String newFileName = FileUtils.newFileName(file.getOriginalFilename());
+        final String newFilePath = SysProperty.UPLOAD_DIR + FileUtils.dateDir() + newFileName;
+        FssFileInfo result = FssFactory.getInstance().writeFile(newFilePath, file.getBytes(), null);
 
-        throw new StatusException(SYS_CODE_500, "上传失败!");
+        return Result.success(result.getFileUrl());
     }
 
     @Naked
     @GetMapping("/download")
     @ApiOperation(value = "下载文件")
     public void download(@RequestParam String filePath) throws Exception {
-        File file = upYunClient.download(filePath);
-        if (file != null) {
+        try {
+            final String rootDir = systemProperties.getTempDataDir();
+            FileUtils.makeDirs(rootDir);
+            final String newFilePath = rootDir + "/" + FileUtils.newFileName(filePath);
+            File file = new File(newFilePath);
+            FssFactory.getInstance().readFile(filePath, file);
+
             super.exportFile(FileUtils.getFileName(filePath), file);
-        } else {
+        } catch (Exception e) {
+            log.error(e.getMessage(), e);
             //文件不存在
             super.exportFile("404", new byte[0]);
         }
     }
 
-}
+}

+ 14 - 7
examcloud-core-print-service/src/main/java/cn/com/qmth/examcloud/core/print/service/impl/CoursePaperServiceImpl.java

@@ -11,7 +11,6 @@ import cn.com.qmth.examcloud.commons.exception.StatusException;
 import cn.com.qmth.examcloud.core.print.common.SysProperty;
 import cn.com.qmth.examcloud.core.print.common.jpa.SearchBuilder;
 import cn.com.qmth.examcloud.core.print.common.jpa.SpecUtils;
-import cn.com.qmth.examcloud.core.print.common.upyun.UpYunClient;
 import cn.com.qmth.examcloud.core.print.common.utils.*;
 import cn.com.qmth.examcloud.core.print.entity.*;
 import cn.com.qmth.examcloud.core.print.enums.PaperStatus;
@@ -21,6 +20,8 @@ import cn.com.qmth.examcloud.core.print.repository.PrintingProjectRepository;
 import cn.com.qmth.examcloud.core.print.service.CoursePaperService;
 import cn.com.qmth.examcloud.core.print.service.ExamQuestionStructureService;
 import cn.com.qmth.examcloud.core.print.service.bean.coursepaper.*;
+import cn.com.qmth.examcloud.support.fss.FssFactory;
+import cn.com.qmth.examcloud.support.fss.model.FssFileInfo;
 import cn.com.qmth.examcloud.web.config.SystemProperties;
 import com.itextpdf.text.pdf.PdfReader;
 import org.apache.commons.lang3.StringUtils;
@@ -67,9 +68,6 @@ public class CoursePaperServiceImpl implements CoursePaperService {
     @Autowired
     private SysProperty sysProperty;
 
-    @Autowired
-    private UpYunClient upYunClient;
-
     @Override
     public List<CoursePaperLessInfo> getCoursePaperList(CoursePaperQuery query) {
         Check.isNull(query, "查询参数不能为空!");
@@ -189,9 +187,18 @@ public class CoursePaperServiceImpl implements CoursePaperService {
                 continue;
             }
 
-            //上传PDF至又拍云
-            String paperPdfUrl = upYunClient.upload(new File(paperPdfPath));
-            String answerPdfUrl = upYunClient.upload(new File(answerPdfPath));
+            //上传PDF
+            File paperPdf = new File(paperPdfPath);
+            File answerPdf = new File(answerPdfPath);
+
+            final String paperPdfFilePath = SysProperty.UPLOAD_DIR + FileUtils.dateDir() + paperPdf.getName();
+            final String answerPdfFilePath = SysProperty.UPLOAD_DIR + FileUtils.dateDir() + answerPdf.getName();
+
+            FssFileInfo result1 = FssFactory.getInstance().writeFile(paperPdfFilePath, paperPdf, null);
+            FssFileInfo result2 = FssFactory.getInstance().writeFile(answerPdfFilePath, answerPdf, null);
+
+            String paperPdfUrl = result1.getFileUrl();
+            String answerPdfUrl = result2.getFileUrl();
 
             //获取试卷PDF的页数
             int paperP = 0;

+ 4 - 6
examcloud-core-print-service/src/main/java/cn/com/qmth/examcloud/core/print/service/impl/PrintingTemplateServiceImpl.java

@@ -10,7 +10,6 @@ package cn.com.qmth.examcloud.core.print.service.impl;
 import cn.com.qmth.examcloud.commons.exception.StatusException;
 import cn.com.qmth.examcloud.core.print.common.jpa.SearchBuilder;
 import cn.com.qmth.examcloud.core.print.common.jpa.SpecUtils;
-import cn.com.qmth.examcloud.core.print.common.upyun.UpYunClient;
 import cn.com.qmth.examcloud.core.print.common.utils.Check;
 import cn.com.qmth.examcloud.core.print.entity.PrintingTemplate;
 import cn.com.qmth.examcloud.core.print.enums.TemplateType;
@@ -18,6 +17,7 @@ import cn.com.qmth.examcloud.core.print.repository.PrintingTemplateRepository;
 import cn.com.qmth.examcloud.core.print.service.PrintingTemplateService;
 import cn.com.qmth.examcloud.core.print.service.bean.printingtemplate.PrintingTemplateConvert;
 import cn.com.qmth.examcloud.core.print.service.bean.printingtemplate.PrintingTemplateInfo;
+import cn.com.qmth.examcloud.support.fss.FssHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -36,10 +36,9 @@ import static cn.com.qmth.examcloud.core.print.common.Constants.SYS_CODE_500;
 @Service
 public class PrintingTemplateServiceImpl implements PrintingTemplateService {
     private static final Logger log = LoggerFactory.getLogger(PrintingTemplateServiceImpl.class);
+
     @Autowired
     private PrintingTemplateRepository printingTemplateRepository;
-    @Autowired
-    private UpYunClient upYunClient;
 
     @Override
     public List<PrintingTemplateInfo> getPrintingTemplateList(Long orgId, Long examId) {
@@ -75,8 +74,7 @@ public class PrintingTemplateServiceImpl implements PrintingTemplateService {
                 .eq("templateType", info.getTemplateType());
         Specification<PrintingTemplate> spec = SpecUtils.buildSearchers(PrintingTemplate.class, searches.build());
 
-        //截取文件名
-        info.setFileName(info.getFileUrl().replace(upYunClient.getUrlPrefix(), ""));
+        info.setFileName(FssHelper.fixFilePath(info.getFileUrl()));
 
         //某学校考试下一种模板类型只有一条记录
         Optional<PrintingTemplate> optional = printingTemplateRepository.findOne(spec);
@@ -93,4 +91,4 @@ public class PrintingTemplateServiceImpl implements PrintingTemplateService {
         printingTemplateRepository.save(PrintingTemplateConvert.of(info));
     }
 
-}
+}