|
@@ -30,6 +30,7 @@ import com.aliyuncs.profile.DefaultProfile;
|
|
import com.aliyuncs.profile.IClientProfile;
|
|
import com.aliyuncs.profile.IClientProfile;
|
|
import com.aliyuncs.sts.model.v20150401.AssumeRoleRequest;
|
|
import com.aliyuncs.sts.model.v20150401.AssumeRoleRequest;
|
|
import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse;
|
|
import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse;
|
|
|
|
+import com.google.common.collect.Maps;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import net.lingala.zip4j.core.ZipFile;
|
|
import net.lingala.zip4j.core.ZipFile;
|
|
import net.lingala.zip4j.model.ZipParameters;
|
|
import net.lingala.zip4j.model.ZipParameters;
|
|
@@ -211,7 +212,7 @@ public class OSSLogicImpl implements OSSLogic {
|
|
ossClientConfigVO.setExpiration(response.getCredentials().getExpiration());
|
|
ossClientConfigVO.setExpiration(response.getCredentials().getExpiration());
|
|
ossClientConfigVO.setSecurityToken(response.getCredentials().getSecurityToken());
|
|
ossClientConfigVO.setSecurityToken(response.getCredentials().getSecurityToken());
|
|
} catch (ClientException e) {
|
|
} catch (ClientException e) {
|
|
- log.error("获取STS权限失败", e);
|
|
|
|
|
|
+ log.error("获取STS权限失败",e);
|
|
}
|
|
}
|
|
return ossClientConfigVO;
|
|
return ossClientConfigVO;
|
|
}
|
|
}
|
|
@@ -259,7 +260,7 @@ public class OSSLogicImpl implements OSSLogic {
|
|
@Override
|
|
@Override
|
|
public String downloadContentByUrlToString(String url) throws IOException {
|
|
public String downloadContentByUrlToString(String url) throws IOException {
|
|
|
|
|
|
- OSSObject ossObject = ossClient.getObject(bucketName, url);
|
|
|
|
|
|
+ OSSObject ossObject = ossClient.getObject(bucketName,url);
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(ossObject.getObjectContent()));
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(ossObject.getObjectContent()));
|
|
StringBuilder sb = new StringBuilder();
|
|
StringBuilder sb = new StringBuilder();
|
|
String line;
|
|
String line;
|
|
@@ -274,47 +275,45 @@ public class OSSLogicImpl implements OSSLogic {
|
|
|
|
|
|
/**
|
|
/**
|
|
* (默认)文件流上传到oss的main bucket
|
|
* (默认)文件流上传到oss的main bucket
|
|
- *
|
|
|
|
* @param ossfilenameWithPath 文件流在oss中的全路径
|
|
* @param ossfilenameWithPath 文件流在oss中的全路径
|
|
- * @param inputStream 要上传的文件流
|
|
|
|
|
|
+ * @param inputStream 要上传的文件流
|
|
* @return 上传的完整url
|
|
* @return 上传的完整url
|
|
* @throws Exception
|
|
* @throws Exception
|
|
*/
|
|
*/
|
|
@Override
|
|
@Override
|
|
- public String uploadByUrl(String ossfilenameWithPath, InputStream inputStream, String host) {
|
|
|
|
- if (!ossFeatureSwitch) {
|
|
|
|
- String fileName = SAVE_PATH + ossfilenameWithPath;
|
|
|
|
|
|
+ public String uploadByUrl(String ossfilenameWithPath, InputStream inputStream, String host){
|
|
|
|
+ if(!ossFeatureSwitch){
|
|
|
|
+ String fileName = SAVE_PATH+ossfilenameWithPath;
|
|
File file = new File(fileName);
|
|
File file = new File(fileName);
|
|
- if (!file.getParentFile().exists()) {
|
|
|
|
|
|
+ if(!file.getParentFile().exists()){
|
|
file.getParentFile().mkdirs();
|
|
file.getParentFile().mkdirs();
|
|
}
|
|
}
|
|
try {
|
|
try {
|
|
- FileUtils.copyInputStreamToFile(inputStream, file);
|
|
|
|
|
|
+ FileUtils.copyInputStreamToFile(inputStream,file);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
System.out.println("编程社区代码上传出错");
|
|
System.out.println("编程社区代码上传出错");
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
- return host + UPLOADED_PATH + ossfilenameWithPath;
|
|
|
|
|
|
+ return host+UPLOADED_PATH+ossfilenameWithPath;
|
|
|
|
|
|
}
|
|
}
|
|
//打开OSS连接
|
|
//打开OSS连接
|
|
- return uploadByUrl(bucketName, ossfilenameWithPath, inputStream);
|
|
|
|
|
|
+ return uploadByUrl(bucketName,ossfilenameWithPath,inputStream);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 文件流上传到oss指定的bucket中
|
|
* 文件流上传到oss指定的bucket中
|
|
- *
|
|
|
|
- * @param bucketName 指定bucket
|
|
|
|
|
|
+ * @param bucketName 指定bucket
|
|
* @param ossfilenameWithPath 文件流在oss中的全路径
|
|
* @param ossfilenameWithPath 文件流在oss中的全路径
|
|
- * @param inputStream 要上传的文件
|
|
|
|
|
|
+ * @param inputStream 要上传的文件
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
@Override
|
|
@Override
|
|
public String uploadByUrl(String bucketName, String ossfilenameWithPath, InputStream inputStream) {
|
|
public String uploadByUrl(String bucketName, String ossfilenameWithPath, InputStream inputStream) {
|
|
- PutObjectResult result = ossClient.putObject(bucketName, ossfilenameWithPath, inputStream);
|
|
|
|
|
|
+ PutObjectResult result = ossClient.putObject(bucketName, ossfilenameWithPath , inputStream);
|
|
|
|
|
|
StringBuilder sb = new StringBuilder("http://");
|
|
StringBuilder sb = new StringBuilder("http://");
|
|
- String nohttpEndPoint = endPoint.replace("http://", "");
|
|
|
|
|
|
+ String nohttpEndPoint = endPoint.replace("http://","");
|
|
sb.append(bucketName).append(".").append(nohttpEndPoint).append("/").append(ossfilenameWithPath);
|
|
sb.append(bucketName).append(".").append(nohttpEndPoint).append("/").append(ossfilenameWithPath);
|
|
return sb.toString();
|
|
return sb.toString();
|
|
}
|
|
}
|
|
@@ -326,15 +325,14 @@ public class OSSLogicImpl implements OSSLogic {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 根据签名上传文件
|
|
* 根据签名上传文件
|
|
- *
|
|
|
|
- * @param signedUrl oss上传许可签名
|
|
|
|
|
|
+ * @param signedUrl oss上传许可签名
|
|
* @param localFilePath 本地文件所在位置
|
|
* @param localFilePath 本地文件所在位置
|
|
* @return 上传成功或者失败
|
|
* @return 上传成功或者失败
|
|
*/
|
|
*/
|
|
@Override
|
|
@Override
|
|
public boolean upload2DevBySigUrl(String signedUrl, File localFilePath) {
|
|
public boolean upload2DevBySigUrl(String signedUrl, File localFilePath) {
|
|
// 使用签名URL发送请求。
|
|
// 使用签名URL发送请求。
|
|
- try (FileInputStream fin = new FileInputStream(localFilePath)) {
|
|
|
|
|
|
+ try(FileInputStream fin = new FileInputStream(localFilePath)) {
|
|
// 添加PutObject请求头。
|
|
// 添加PutObject请求头。
|
|
Map<String, String> customHeaders = new HashMap<String, String>();
|
|
Map<String, String> customHeaders = new HashMap<String, String>();
|
|
customHeaders.put("Content-Type", "application/octet-stream");
|
|
customHeaders.put("Content-Type", "application/octet-stream");
|
|
@@ -351,10 +349,10 @@ public class OSSLogicImpl implements OSSLogic {
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public boolean deleteByUrl(String bucketName, String ossFileNameWithPath) {
|
|
public boolean deleteByUrl(String bucketName, String ossFileNameWithPath) {
|
|
- try {
|
|
|
|
|
|
+ try{
|
|
ossClient.deleteObject(bucketName, ossFileNameWithPath);
|
|
ossClient.deleteObject(bucketName, ossFileNameWithPath);
|
|
return true;
|
|
return true;
|
|
- } catch (Exception e) {
|
|
|
|
|
|
+ } catch (Exception e){
|
|
log.error("删除OSS存储对象出错", e);
|
|
log.error("删除OSS存储对象出错", e);
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
@@ -365,7 +363,7 @@ public class OSSLogicImpl implements OSSLogic {
|
|
public String copyFileInOss(String bucketName, String sourceFileNameWithPath, String targetFileNameWithPath) {
|
|
public String copyFileInOss(String bucketName, String sourceFileNameWithPath, String targetFileNameWithPath) {
|
|
ossClient.copyObject(bucketName, sourceFileNameWithPath, bucketName, targetFileNameWithPath);
|
|
ossClient.copyObject(bucketName, sourceFileNameWithPath, bucketName, targetFileNameWithPath);
|
|
StringBuilder sb = new StringBuilder("http://");
|
|
StringBuilder sb = new StringBuilder("http://");
|
|
- String nohttpEndPoint = endPoint.replace("http://", "");
|
|
|
|
|
|
+ String nohttpEndPoint = endPoint.replace("http://","");
|
|
sb.append(bucketName).append(".").append(nohttpEndPoint).append("/").append(targetFileNameWithPath);
|
|
sb.append(bucketName).append(".").append(nohttpEndPoint).append("/").append(targetFileNameWithPath);
|
|
return sb.toString();
|
|
return sb.toString();
|
|
}
|
|
}
|
|
@@ -379,7 +377,7 @@ public class OSSLogicImpl implements OSSLogic {
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public List<String> listObjectsByPrefix(String prefix) {
|
|
public List<String> listObjectsByPrefix(String prefix) {
|
|
- ObjectListing objectListing = ossClient.listObjects(bucketName, prefix);
|
|
|
|
|
|
+ ObjectListing objectListing = ossClient.listObjects(bucketName,prefix);
|
|
List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
|
|
List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
|
|
return sums.stream().map(OSSObjectSummary::getKey).collect(Collectors.toList());
|
|
return sums.stream().map(OSSObjectSummary::getKey).collect(Collectors.toList());
|
|
}
|
|
}
|
|
@@ -408,10 +406,10 @@ public class OSSLogicImpl implements OSSLogic {
|
|
public String saveFileTemp(MultipartFile file, String filePath) throws IOException {
|
|
public String saveFileTemp(MultipartFile file, String filePath) throws IOException {
|
|
Long size = file.getSize();
|
|
Long size = file.getSize();
|
|
if (file.isEmpty()) {
|
|
if (file.isEmpty()) {
|
|
- throw new HttpBadRequestException("please select a file to upload");
|
|
|
|
|
|
+ throw new HttpBadRequestException ("please select a file to upload");
|
|
}
|
|
}
|
|
if (size > maxUploadSizeInMb) {
|
|
if (size > maxUploadSizeInMb) {
|
|
- throw new HttpBadRequestException("file size is over limit");
|
|
|
|
|
|
+ throw new HttpBadRequestException ("file size is over limit");
|
|
}
|
|
}
|
|
|
|
|
|
File saveFile = new File(filePath);
|
|
File saveFile = new File(filePath);
|
|
@@ -430,7 +428,7 @@ public class OSSLogicImpl implements OSSLogic {
|
|
if (!saveFile.getParentFile().exists()) {
|
|
if (!saveFile.getParentFile().exists()) {
|
|
saveFile.getParentFile().mkdirs();
|
|
saveFile.getParentFile().mkdirs();
|
|
}
|
|
}
|
|
- FileUtils.copyFile(file, saveFile);
|
|
|
|
|
|
+ FileUtils.copyFile(file,saveFile);
|
|
return fileName;
|
|
return fileName;
|
|
|
|
|
|
|
|
|
|
@@ -516,29 +514,18 @@ public class OSSLogicImpl implements OSSLogic {
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public String getPdf(Long examId, Long workerId, String fileName) throws Exception {
|
|
public String getPdf(Long examId, Long workerId, String fileName) throws Exception {
|
|
- //MD5加密
|
|
|
|
-
|
|
|
|
- String md5_fileName = MD5Util.string2MD5(fileName);
|
|
|
|
- //判断文件是否存在 如果存在 直接返回路径不用重新生成pdf
|
|
|
|
- File file1 = new File(PdfServiceImpl.SAVE_PATH + PdfServiceImpl.PDF_PATH + md5_fileName + ".pdf");
|
|
|
|
- if (file1.exists()) {
|
|
|
|
- log.info("文件已经存在 直接返回下载链接");
|
|
|
|
- return UPLOADED_PATH + PDF_PATH + md5_fileName + ".pdf";
|
|
|
|
|
|
+ ReportPDF reportPDF = pdfService.getReportPDF(examId, workerId);
|
|
|
|
+ if(reportPDF.getFailReason()!=null){
|
|
|
|
+ return reportPDF.getFailReason();
|
|
|
|
+ }
|
|
|
|
+ File file = new File(SAVE_PATH + getBarGraphKey(examId.toString()));
|
|
|
|
+ if (file.exists()){
|
|
|
|
+ reportPDF.setBarGraph(SAVE_PATH + getBarGraphKey(examId.toString()));
|
|
} else {
|
|
} else {
|
|
-
|
|
|
|
- ReportPDF reportPDF = pdfService.getReportPDF(examId, workerId);
|
|
|
|
- if (reportPDF.getFailReason() != null) {
|
|
|
|
- return reportPDF.getFailReason();
|
|
|
|
- }
|
|
|
|
- File file = new File(SAVE_PATH + getBarGraphKey(examId.toString()));
|
|
|
|
- if (file.exists()) {
|
|
|
|
- reportPDF.setBarGraph(SAVE_PATH + getBarGraphKey(examId.toString()));
|
|
|
|
- } else {
|
|
|
|
- reportPDF.setBarGraph(null);
|
|
|
|
- }
|
|
|
|
- pdfService.generatePDF(reportPDF, fileName);
|
|
|
|
- return UPLOADED_PATH + PDF_PATH + fileName + ".pdf";
|
|
|
|
|
|
+ reportPDF.setBarGraph(null);
|
|
}
|
|
}
|
|
|
|
+ pdfService.generatePDF(reportPDF, fileName);
|
|
|
|
+ return UPLOADED_PATH + PDF_PATH + fileName + ".pdf";
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -598,27 +585,27 @@ public class OSSLogicImpl implements OSSLogic {
|
|
Map<String, Object> result = new HashMap<>();
|
|
Map<String, Object> result = new HashMap<>();
|
|
try {
|
|
try {
|
|
inputStream = file.getInputStream();
|
|
inputStream = file.getInputStream();
|
|
- File localFile = new File(SAVE_PATH + "slides/" + file.getOriginalFilename());
|
|
|
|
|
|
+ File localFile = new File( SAVE_PATH + "slides/" + file.getOriginalFilename());
|
|
if (!localFile.getParentFile().exists())
|
|
if (!localFile.getParentFile().exists())
|
|
localFile.getParentFile().mkdirs();
|
|
localFile.getParentFile().mkdirs();
|
|
log.info("开始本地暂存...");
|
|
log.info("开始本地暂存...");
|
|
FileUtils.copyInputStreamToFile(inputStream, localFile);
|
|
FileUtils.copyInputStreamToFile(inputStream, localFile);
|
|
log.info("文件上传OSS...");
|
|
log.info("文件上传OSS...");
|
|
String slidesUrl = uploadByUrl("mooctest-course",
|
|
String slidesUrl = uploadByUrl("mooctest-course",
|
|
- "slides/" + file.getOriginalFilename(), new FileInputStream(localFile));
|
|
|
|
|
|
+ "slides/"+file.getOriginalFilename(), new FileInputStream(localFile));
|
|
log.info("开始进行PDF转图片...");
|
|
log.info("开始进行PDF转图片...");
|
|
- Map<String, Object> imageResult = pdfService.pdf2imageList(localFile, "png");
|
|
|
|
|
|
+ Map<String,Object> imageResult = pdfService.pdf2imageList(localFile, "png");
|
|
log.info(String.format("转换成功,结果信息:%s", imageResult.toString()));
|
|
log.info(String.format("转换成功,结果信息:%s", imageResult.toString()));
|
|
File imagePath = new File((String) imageResult.get("url"));
|
|
File imagePath = new File((String) imageResult.get("url"));
|
|
File[] files = imagePath.listFiles();
|
|
File[] files = imagePath.listFiles();
|
|
log.info("上传图片至OSS...");
|
|
log.info("上传图片至OSS...");
|
|
- for (File f : files) {
|
|
|
|
- uploadByUrl("mooctest-course", "slides/images/" + imagePath.getName() + "/" + f.getName(), new FileInputStream(f));
|
|
|
|
|
|
+ for (File f:files){
|
|
|
|
+ uploadByUrl("mooctest-course", "slides/images/"+imagePath.getName()+"/"+f.getName(), new FileInputStream(f));
|
|
}
|
|
}
|
|
log.info("上传结束");
|
|
log.info("上传结束");
|
|
StringBuilder sb = new StringBuilder("http://");
|
|
StringBuilder sb = new StringBuilder("http://");
|
|
- String nohttpEndPoint = endPoint.replace("http://", "");
|
|
|
|
- sb.append(courseBucket).append(".").append(nohttpEndPoint).append("/").append("slides/images/" + imagePath.getName());
|
|
|
|
|
|
+ String nohttpEndPoint = endPoint.replace("http://","");
|
|
|
|
+ sb.append(courseBucket).append(".").append(nohttpEndPoint).append("/").append("slides/images/"+imagePath.getName());
|
|
String imagesUrl = sb.toString();
|
|
String imagesUrl = sb.toString();
|
|
result.put("slidesUrl", slidesUrl);
|
|
result.put("slidesUrl", slidesUrl);
|
|
result.put("pageCount", imageResult.get("pageCount"));
|
|
result.put("pageCount", imageResult.get("pageCount"));
|
|
@@ -631,6 +618,7 @@ public class OSSLogicImpl implements OSSLogic {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -647,9 +635,11 @@ public class OSSLogicImpl implements OSSLogic {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
private String getStatisticalDiagramKey(String s) {
|
|
private String getStatisticalDiagramKey(String s) {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
}
|
|
}
|