|
@@ -5,11 +5,16 @@ import cn.iselab.mooctest.site.data.ExamPDF;
|
|
|
import cn.iselab.mooctest.site.data.ReportPDF;
|
|
|
import cn.iselab.mooctest.site.data.UserDTOForMT;
|
|
|
import cn.iselab.mooctest.site.data.config.OSSClientConfig;
|
|
|
+import cn.iselab.mooctest.site.data.config.STSConfig;
|
|
|
import cn.iselab.mooctest.site.models.Exam;
|
|
|
import cn.iselab.mooctest.site.models.User;
|
|
|
-import cn.iselab.mooctest.site.service.*;
|
|
|
+import cn.iselab.mooctest.site.service.EmailService;
|
|
|
+import cn.iselab.mooctest.site.service.ExamService;
|
|
|
+import cn.iselab.mooctest.site.service.GroupService;
|
|
|
+import cn.iselab.mooctest.site.service.UserService;
|
|
|
import cn.iselab.mooctest.site.service.common.PdfService;
|
|
|
import cn.iselab.mooctest.site.service.common.ZipService;
|
|
|
+import cn.iselab.mooctest.site.web.data.OSSClientConfigVO;
|
|
|
import cn.iselab.mooctest.site.web.exception.HttpBadRequestException;
|
|
|
import cn.iselab.mooctest.site.web.logic.OSSLogic;
|
|
|
import com.aliyun.oss.HttpMethod;
|
|
@@ -19,8 +24,12 @@ import com.aliyuncs.DefaultAcsClient;
|
|
|
import com.aliyuncs.exceptions.ClientException;
|
|
|
import com.aliyuncs.http.MethodType;
|
|
|
import com.aliyuncs.http.ProtocolType;
|
|
|
+import com.aliyuncs.profile.DefaultProfile;
|
|
|
+import com.aliyuncs.profile.IClientProfile;
|
|
|
import com.aliyuncs.sts.model.v20150401.AssumeRoleRequest;
|
|
|
import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse;
|
|
|
+import com.google.common.collect.Maps;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import net.lingala.zip4j.core.ZipFile;
|
|
|
import net.lingala.zip4j.model.ZipParameters;
|
|
|
import net.lingala.zip4j.util.Zip4jConstants;
|
|
@@ -32,7 +41,6 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.*;
|
|
|
-import java.net.InetAddress;
|
|
|
import java.net.URL;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
@@ -45,6 +53,7 @@ import java.util.stream.Collectors;
|
|
|
* @date 2017-03-27.
|
|
|
*/
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class OSSLogicImpl implements OSSLogic {
|
|
|
|
|
|
@Autowired
|
|
@@ -52,6 +61,8 @@ public class OSSLogicImpl implements OSSLogic {
|
|
|
|
|
|
@Autowired
|
|
|
private OSSClientConfig ossClientConfig;
|
|
|
+ @Autowired
|
|
|
+ private STSConfig stsConfig;
|
|
|
|
|
|
@Autowired
|
|
|
private DefaultAcsClient acsClient;
|
|
@@ -112,8 +123,6 @@ public class OSSLogicImpl implements OSSLogic {
|
|
|
|
|
|
@Override
|
|
|
public boolean verificationCertificate(String userId) {
|
|
|
- //打开OSS连接
|
|
|
- OSSClient client = new OSSClient(endPoint, accessKeyId, accessKeySecret);
|
|
|
return ossClient.doesObjectExist(bucketName, getCertificateKey(userId));
|
|
|
}
|
|
|
|
|
@@ -134,8 +143,7 @@ public class OSSLogicImpl implements OSSLogic {
|
|
|
|
|
|
@Override
|
|
|
public String verificationHeadImageKey(String userId) {
|
|
|
- OSSClient client = new OSSClient(endPoint, accessKeyId, accessKeySecret);
|
|
|
- if (client.doesObjectExist(bucketName, getHeadImageKey(userId))) {
|
|
|
+ if (ossClient.doesObjectExist(bucketName, getHeadImageKey(userId))) {
|
|
|
return getHeadImageKey(userId);
|
|
|
}
|
|
|
String fail = "未上传照片";
|
|
@@ -175,15 +183,45 @@ public class OSSLogicImpl implements OSSLogic {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ public OSSClientConfigVO getSTSToken() {
|
|
|
+ String endpoint = stsConfig.getEndpoint();
|
|
|
+ String accessKeyId = stsConfig.getAccessKeyId();
|
|
|
+ String accessKeySecret = stsConfig.getAccessKeySecret();
|
|
|
+ String roleArn = stsConfig.getRoleArn();
|
|
|
+ String roleSessionName = stsConfig.getRoleSessionName();
|
|
|
+ String policy = stsConfig.getPolicy();
|
|
|
+ OSSClientConfigVO ossClientConfigVO = new OSSClientConfigVO();
|
|
|
+ try {
|
|
|
+ // 添加endpoint(直接使用STS endpoint,前两个参数留空,无需添加region ID)
|
|
|
+ DefaultProfile.addEndpoint("", "", "Sts", endpoint);
|
|
|
+ // 构造default profile(参数留空,无需添加region ID)
|
|
|
+ IClientProfile profile = DefaultProfile.getProfile("", accessKeyId, accessKeySecret);
|
|
|
+ // 用profile构造client
|
|
|
+ DefaultAcsClient client = new DefaultAcsClient(profile);
|
|
|
+ final AssumeRoleRequest request = new AssumeRoleRequest();
|
|
|
+ request.setMethod(MethodType.POST);
|
|
|
+ request.setRoleArn(roleArn);
|
|
|
+ request.setRoleSessionName(roleSessionName);
|
|
|
+ request.setPolicy(policy); // 若policy为空,则用户将获得该角色下所有权限
|
|
|
+ request.setDurationSeconds(1000L); // 设置凭证有效时间
|
|
|
+ final AssumeRoleResponse response = client.getAcsResponse(request);
|
|
|
+ ossClientConfigVO.setAccessKeyId(response.getCredentials().getAccessKeyId());
|
|
|
+ ossClientConfigVO.setAccessKeySecret(response.getCredentials().getAccessKeySecret());
|
|
|
+ ossClientConfigVO.setExpiration(response.getCredentials().getExpiration());
|
|
|
+ ossClientConfigVO.setSecurityToken(response.getCredentials().getSecurityToken());
|
|
|
+ } catch (ClientException e) {
|
|
|
+ log.error("获取STS权限失败",e);
|
|
|
+ }
|
|
|
+ return ossClientConfigVO;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
public String getSubmitSiganature(String path, Long examId, Long caseId, Long workerId, String fileName) {
|
|
|
if (examId == null || caseId == null || workerId == null) {
|
|
|
return null;
|
|
|
}
|
|
|
String url = path + "/" + examId + "/" + caseId + "/" + workerId + "/" + System.currentTimeMillis() + "_" + fileName;
|
|
|
|
|
|
- //打开OSS连接
|
|
|
- OSSClient client = new OSSClient(endPoint, accessKeyId, accessKeySecret);
|
|
|
-
|
|
|
//生成签名
|
|
|
Date expiration = new Date(System.currentTimeMillis() + 5 * 1000 * 60);//签名5分钟过期
|
|
|
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketNameDev, url, HttpMethod.PUT);
|
|
@@ -191,10 +229,8 @@ public class OSSLogicImpl implements OSSLogic {
|
|
|
request.setContentType("application/octet-stream");
|
|
|
// 添加user meta
|
|
|
request.addUserMetadata("author", "aliy");
|
|
|
- URL signedUrl = client.generatePresignedUrl(request);
|
|
|
+ URL signedUrl = ossClient.generatePresignedUrl(request);
|
|
|
|
|
|
- //关闭OSS连接
|
|
|
- client.shutdown();
|
|
|
return signedUrl.toString();
|
|
|
}
|
|
|
|
|
@@ -208,8 +244,6 @@ public class OSSLogicImpl implements OSSLogic {
|
|
|
}
|
|
|
String downloadPath = FilenameUtils.concat(filePath, fileName);
|
|
|
|
|
|
-// OSSClient client = new OSSClient(endPoint, accessKeyId, accessKeySecret);
|
|
|
-// OSSObject ossObj = client.getObject(bucketName, url);
|
|
|
URL url1 = new URL(url);
|
|
|
try {
|
|
|
File file = new File(downloadPath);
|
|
@@ -223,7 +257,7 @@ public class OSSLogicImpl implements OSSLogic {
|
|
|
|
|
|
@Override
|
|
|
public String downloadContentByUrlToString(String url) throws IOException {
|
|
|
- OSSClient ossClient = new OSSClient(endPoint,accessKeyId,accessKeySecret);
|
|
|
+
|
|
|
OSSObject ossObject = ossClient.getObject(bucketName,url);
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(ossObject.getObjectContent()));
|
|
|
StringBuilder sb = new StringBuilder();
|
|
@@ -234,7 +268,6 @@ public class OSSLogicImpl implements OSSLogic {
|
|
|
}
|
|
|
|
|
|
reader.close();
|
|
|
- ossClient.shutdown();
|
|
|
return sb.toString();
|
|
|
}
|
|
|
|
|
@@ -259,7 +292,6 @@ public class OSSLogicImpl implements OSSLogic {
|
|
|
System.out.println("编程社区代码上传出错");
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
- System.out.println("url: "+host+UPLOADED_PATH+ossfilenameWithPath);
|
|
|
return host+UPLOADED_PATH+ossfilenameWithPath;
|
|
|
|
|
|
}
|
|
@@ -276,15 +308,19 @@ public class OSSLogicImpl implements OSSLogic {
|
|
|
*/
|
|
|
@Override
|
|
|
public String uploadByUrl(String bucketName, String ossfilenameWithPath, InputStream inputStream) {
|
|
|
- OSSClient client = new OSSClient(endPoint, accessKeyId, accessKeySecret);
|
|
|
- PutObjectResult result = client.putObject(bucketName, ossfilenameWithPath , inputStream);
|
|
|
- client.shutdown();
|
|
|
+ PutObjectResult result = ossClient.putObject(bucketName, ossfilenameWithPath , inputStream);
|
|
|
+
|
|
|
StringBuilder sb = new StringBuilder("http://");
|
|
|
String nohttpEndPoint = endPoint.replace("http://","");
|
|
|
sb.append(bucketName).append(".").append(nohttpEndPoint).append("/").append(ossfilenameWithPath);
|
|
|
return sb.toString();
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public String uploadByUrl(String ossfilenameWithPath, InputStream inputStream) {
|
|
|
+ return uploadByUrl(bucketName, ossfilenameWithPath, inputStream);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 根据签名上传文件
|
|
|
* @param signedUrl oss上传许可签名
|
|
@@ -294,16 +330,12 @@ public class OSSLogicImpl implements OSSLogic {
|
|
|
@Override
|
|
|
public boolean upload2DevBySigUrl(String signedUrl, File localFilePath) {
|
|
|
// 使用签名URL发送请求。
|
|
|
- OSSClient client = new OSSClient(endPoint, accessKeyId, accessKeySecret);
|
|
|
try(FileInputStream fin = new FileInputStream(localFilePath)) {
|
|
|
// 添加PutObject请求头。
|
|
|
Map<String, String> customHeaders = new HashMap<String, String>();
|
|
|
customHeaders.put("Content-Type", "application/octet-stream");
|
|
|
customHeaders.put("x-oss-meta-author", "aliy");
|
|
|
-
|
|
|
- PutObjectResult result = client.putObject(new URL(signedUrl), fin, localFilePath.length(), customHeaders);
|
|
|
- // 关闭OSSClient。
|
|
|
- client.shutdown();
|
|
|
+ PutObjectResult result = ossClient.putObject(new URL(signedUrl), fin, localFilePath.length(), customHeaders);
|
|
|
return true;
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
@@ -315,9 +347,7 @@ public class OSSLogicImpl implements OSSLogic {
|
|
|
|
|
|
@Override
|
|
|
public List<String> listObjectsByPrefix(String prefix) {
|
|
|
- OSSClient client = new OSSClient(endPoint,accessKeyId,accessKeySecret);
|
|
|
- ObjectListing objectListing = client.listObjects(bucketName,prefix);
|
|
|
- client.shutdown();
|
|
|
+ ObjectListing objectListing = ossClient.listObjects(bucketName,prefix);
|
|
|
List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
|
|
|
return sums.stream().map(OSSObjectSummary::getKey).collect(Collectors.toList());
|
|
|
}
|
|
@@ -333,8 +363,8 @@ public class OSSLogicImpl implements OSSLogic {
|
|
|
}
|
|
|
String fileName = UPLOADED_PATH + path;
|
|
|
File saveFile = new File("/var/www" + fileName);
|
|
|
+
|
|
|
//判断文件父目录是否存在
|
|
|
- System.out.println(saveFile.getParentFile());
|
|
|
if (!saveFile.getParentFile().exists()) {
|
|
|
saveFile.getParentFile().mkdirs();
|
|
|
}
|
|
@@ -344,21 +374,20 @@ public class OSSLogicImpl implements OSSLogic {
|
|
|
|
|
|
@Override
|
|
|
public String saveFileTemp(MultipartFile file, String filePath) throws IOException {
|
|
|
- Long size = file.getSize ();
|
|
|
- if (file.isEmpty ()) {
|
|
|
+ Long size = file.getSize();
|
|
|
+ if (file.isEmpty()) {
|
|
|
throw new HttpBadRequestException ("please select a file to upload");
|
|
|
}
|
|
|
if (size > maxUploadSizeInMb) {
|
|
|
throw new HttpBadRequestException ("file size is over limit");
|
|
|
}
|
|
|
|
|
|
- File saveFile = new File (filePath);
|
|
|
+ File saveFile = new File(filePath);
|
|
|
//判断文件父目录是否存在
|
|
|
- System.out.println (saveFile.getParentFile ());
|
|
|
- if (!saveFile.getParentFile ().exists ()) {
|
|
|
- saveFile.getParentFile ().mkdirs ();
|
|
|
+ if (!saveFile.getParentFile().exists()) {
|
|
|
+ saveFile.getParentFile().mkdirs();
|
|
|
}
|
|
|
- file.transferTo (saveFile);
|
|
|
+ file.transferTo(saveFile);
|
|
|
return filePath;
|
|
|
}
|
|
|
|
|
@@ -380,9 +409,10 @@ public class OSSLogicImpl implements OSSLogic {
|
|
|
File file = new File(SAVE_PATH + ZIP_PATH + fileName + ".zip");
|
|
|
if (!file.exists()) {
|
|
|
File folder = new File(FileUtils.getUserDirectory() + "/" + fileName);
|
|
|
- if (!folder.exists())
|
|
|
+ if (!folder.exists()) {
|
|
|
folder.mkdirs();
|
|
|
- ossClient = new OSSClient(endPoint, accessKeyId, accessKeySecret);
|
|
|
+ }
|
|
|
+
|
|
|
ObjectListing listing = ossClient.listObjects(bucketNameDev, "data/answers" + "/" + examId + "/" + workerId + "/");
|
|
|
List<OSSObjectSummary> sums = listing.getObjectSummaries();
|
|
|
for (OSSObjectSummary s : sums) {
|
|
@@ -390,7 +420,7 @@ public class OSSLogicImpl implements OSSLogic {
|
|
|
String name = key.split("/")[key.split("/").length - 1];
|
|
|
ossClient.getObject(new GetObjectRequest(bucketNameDev, key), new File(FileUtils.getUserDirectory() + "/" + fileName + "/" + name));
|
|
|
}
|
|
|
- ossClient.shutdown();
|
|
|
+
|
|
|
zipService.zipForReport(examId, workerId, FileUtils.getUserDirectory() + "/" + fileName);
|
|
|
ZipFile zipFile = new ZipFile(file);
|
|
|
ZipParameters parameters = new ZipParameters();
|
|
@@ -527,9 +557,12 @@ public class OSSLogicImpl implements OSSLogic {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
private String getStatisticalDiagramKey(String s) {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
|
|
|
+
|
|
|
}
|