|
@@ -1,499 +1,495 @@
|
|
|
package cn.com.qmth.examcloud.web.filestorage.impl;
|
|
|
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.FileNotFoundException;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.util.Base64;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.Map;
|
|
|
-
|
|
|
-import org.apache.commons.codec.DecoderException;
|
|
|
-import org.apache.commons.codec.binary.Hex;
|
|
|
-import org.apache.commons.lang3.StringUtils;
|
|
|
-import org.apache.commons.lang3.time.DateUtils;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-
|
|
|
-import com.aliyun.oss.OSS;
|
|
|
-import com.aliyun.oss.model.ObjectMetadata;
|
|
|
-import com.google.common.collect.Maps;
|
|
|
-
|
|
|
import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
|
import cn.com.qmth.examcloud.commons.util.DateUtil;
|
|
|
import cn.com.qmth.examcloud.commons.util.FreeMarkerUtil;
|
|
|
import cn.com.qmth.examcloud.web.aliyun.AliYunAccount;
|
|
|
import cn.com.qmth.examcloud.web.aliyun.AliyunSite;
|
|
|
import cn.com.qmth.examcloud.web.aliyun.AliyunSiteManager;
|
|
|
-import cn.com.qmth.examcloud.web.filestorage.FileStorage;
|
|
|
-import cn.com.qmth.examcloud.web.filestorage.FileStorageHelper;
|
|
|
-import cn.com.qmth.examcloud.web.filestorage.FileStoragePathEnvInfo;
|
|
|
-import cn.com.qmth.examcloud.web.filestorage.FileStorageType;
|
|
|
-import cn.com.qmth.examcloud.web.filestorage.YunHttpRequest;
|
|
|
-import cn.com.qmth.examcloud.web.filestorage.YunPathInfo;
|
|
|
+import cn.com.qmth.examcloud.web.filestorage.*;
|
|
|
+import com.aliyun.oss.OSS;
|
|
|
+import com.aliyun.oss.model.ObjectMetadata;
|
|
|
+import com.google.common.collect.Maps;
|
|
|
+import org.apache.commons.codec.DecoderException;
|
|
|
+import org.apache.commons.codec.binary.Hex;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.commons.lang3.time.DateUtils;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.util.Base64;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
@Service(value = "aliyunFileStorage")
|
|
|
public class AliyunFileStorageImpl implements FileStorage {
|
|
|
-// private ExamCloudLog log = ExamCloudLogFactory.getLog(this.getClass());
|
|
|
-
|
|
|
- // 文件最大大小(byte)
|
|
|
- private static int maxFileSize = 100 * 1024 * 1024;
|
|
|
-
|
|
|
- @Override
|
|
|
- public YunPathInfo saveFile(File file, String path) {
|
|
|
- String siteId = "transPath";
|
|
|
- FileStoragePathEnvInfo env = new FileStoragePathEnvInfo();
|
|
|
- env.setRelativePath(path);
|
|
|
- return saveFile(siteId, env, file, null);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public String realPath(String path) {
|
|
|
- String yunId = FileStorageHelper.getYunId(path);
|
|
|
- String urlpath=FileStorageHelper.getPath(path);
|
|
|
- AliYunAccount ac = AliyunSiteManager.getAliYunAccountByAliyunId(yunId);
|
|
|
- return FileStorageHelper.getUrl(ac.getDomain(), urlpath);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 表单上传
|
|
|
- *
|
|
|
- * @param siteId
|
|
|
- * @param env
|
|
|
- * @param file
|
|
|
- * @param md5
|
|
|
- * @return 不带域名的完整路径
|
|
|
- * @throws IOException
|
|
|
- */
|
|
|
-// private String postObject(String siteId, FileStoragePathEnvInfo env, File file, String md5) throws IOException {
|
|
|
-// AliyunSite as = AliyunSiteManager.getAliyunSite(siteId);
|
|
|
-// AliYunAccount ac = AliyunSiteManager.getAliYunAccountByAliyunId(as.getAliyunId());
|
|
|
-// String ossEndpoint = ac.getOssEndpoint();
|
|
|
-// String bucket = ac.getBucket();
|
|
|
-// String accessKeyId = ac.getAccessKeyId();
|
|
|
-// String accessKeySecret = ac.getAccessKeySecret();
|
|
|
-// // 阿里云文件路径
|
|
|
-// String path = FreeMarkerUtil.process(as.getPath(), env);
|
|
|
-// if (path.startsWith("/")) {
|
|
|
-// path = path.substring(1);
|
|
|
-// }
|
|
|
-//
|
|
|
-// String filepath = file.getAbsolutePath();
|
|
|
-// String filename = path.substring(path.lastIndexOf("/") + 1);
|
|
|
-// String urlStr = null; // 提交表单的URL为bucket域名
|
|
|
-// if(ossEndpoint.startsWith("https://")) {
|
|
|
-// urlStr = ossEndpoint.replace("https://", "https://" + bucket + ".");
|
|
|
-// }else if(ossEndpoint.startsWith("http://")) {
|
|
|
-// urlStr = ossEndpoint.replace("http://", "http://" + bucket + ".");
|
|
|
-// }
|
|
|
-//
|
|
|
-// LinkedHashMap<String, String> textMap = new LinkedHashMap<String, String>();
|
|
|
-// // key
|
|
|
-// textMap.put("key", path);
|
|
|
-// // Content-Disposition
|
|
|
-// textMap.put("Content-Disposition", "attachment;filename=" + filename);
|
|
|
-// // OSSAccessKeyId
|
|
|
-// textMap.put("OSSAccessKeyId", accessKeyId);
|
|
|
-// // policy
|
|
|
-// String policy = "{\"expiration\": \"2120-01-01T12:00:00.000Z\",\"conditions\": [[\"content-length-range\", 0, "
|
|
|
-// + maxFileSize + "]]}";
|
|
|
-// String encodePolicy = java.util.Base64.getEncoder().encodeToString(policy.getBytes());
|
|
|
-// textMap.put("policy", encodePolicy);
|
|
|
-// // Signature
|
|
|
-// String signaturecom = com.aliyun.oss.common.auth.ServiceSignature.create().computeSignature(accessKeySecret,
|
|
|
-// encodePolicy);
|
|
|
-// textMap.put("Signature", signaturecom);
|
|
|
-//
|
|
|
-// Map<String, String> fileMap = new HashMap<String, String>();
|
|
|
-// fileMap.put("file", filepath);
|
|
|
-//
|
|
|
-// String ret = formUpload(urlStr, textMap, fileMap);
|
|
|
-// log.info("oss上传:" + ret);
|
|
|
-// return path;
|
|
|
-// }
|
|
|
-
|
|
|
-// @SuppressWarnings("rawtypes")
|
|
|
-// private static String formUpload(String urlStr, Map<String, String> textMap, Map<String, String> fileMap)
|
|
|
-// throws IOException {
|
|
|
-// String res = "";
|
|
|
-// HttpURLConnection conn = null;
|
|
|
-// String BOUNDARY = "9431149156168";
|
|
|
-// try {
|
|
|
-// URL url = new URL(urlStr);
|
|
|
-// conn = (HttpURLConnection) url.openConnection();
|
|
|
-// conn.setConnectTimeout(5000);
|
|
|
-// conn.setReadTimeout(30000);
|
|
|
-// conn.setDoOutput(true);
|
|
|
-// conn.setDoInput(true);
|
|
|
-// conn.setRequestMethod("POST");
|
|
|
-// conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");
|
|
|
-// conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
|
|
|
-//
|
|
|
-// OutputStream out = new DataOutputStream(conn.getOutputStream());
|
|
|
-// // text
|
|
|
-// if (textMap != null) {
|
|
|
-// StringBuffer strBuf = new StringBuffer();
|
|
|
-// Iterator iter = textMap.entrySet().iterator();
|
|
|
-// int i = 0;
|
|
|
-// while (iter.hasNext()) {
|
|
|
-// Map.Entry entry = (Map.Entry) iter.next();
|
|
|
-// String inputName = (String) entry.getKey();
|
|
|
-// String inputValue = (String) entry.getValue();
|
|
|
-// if (inputValue == null) {
|
|
|
-// continue;
|
|
|
-// }
|
|
|
-// if (i == 0) {
|
|
|
-// strBuf.append("--").append(BOUNDARY).append("\r\n");
|
|
|
-// strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n");
|
|
|
-// strBuf.append(inputValue);
|
|
|
-// } else {
|
|
|
-// strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
|
|
|
-// strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n");
|
|
|
-//
|
|
|
-// strBuf.append(inputValue);
|
|
|
-// }
|
|
|
-//
|
|
|
-// i++;
|
|
|
-// }
|
|
|
-// out.write(strBuf.toString().getBytes());
|
|
|
-// }
|
|
|
-//
|
|
|
-// // file
|
|
|
-// if (fileMap != null) {
|
|
|
-// Iterator iter = fileMap.entrySet().iterator();
|
|
|
-// while (iter.hasNext()) {
|
|
|
-// Map.Entry entry = (Map.Entry) iter.next();
|
|
|
-// String inputName = (String) entry.getKey();
|
|
|
-// String inputValue = (String) entry.getValue();
|
|
|
-// if (inputValue == null) {
|
|
|
-// continue;
|
|
|
-// }
|
|
|
-// File file = new File(inputValue);
|
|
|
-// String filename = file.getName();
|
|
|
-// String contentType = new MimetypesFileTypeMap().getContentType(file);
|
|
|
-// if (contentType == null || contentType.equals("")) {
|
|
|
-// contentType = "application/octet-stream";
|
|
|
-// }
|
|
|
-//
|
|
|
-// StringBuffer strBuf = new StringBuffer();
|
|
|
-// strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
|
|
|
-// strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"; filename=\"" + filename
|
|
|
-// + "\"\r\n");
|
|
|
-// strBuf.append("Content-Type: " + contentType + "\r\n\r\n");
|
|
|
-//
|
|
|
-// out.write(strBuf.toString().getBytes());
|
|
|
-//
|
|
|
-// DataInputStream in = new DataInputStream(new FileInputStream(file));
|
|
|
-// int bytes = 0;
|
|
|
-// byte[] bufferOut = new byte[1024];
|
|
|
-// while ((bytes = in.read(bufferOut)) != -1) {
|
|
|
-// out.write(bufferOut, 0, bytes);
|
|
|
-// }
|
|
|
-// in.close();
|
|
|
-// }
|
|
|
-// StringBuffer strBuf = new StringBuffer();
|
|
|
-// out.write(strBuf.toString().getBytes());
|
|
|
-// }
|
|
|
-//
|
|
|
-// byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
|
|
|
-// out.write(endData);
|
|
|
-// out.flush();
|
|
|
-// out.close();
|
|
|
-//
|
|
|
-// // 读取返回数据
|
|
|
-// StringBuffer strBuf = new StringBuffer();
|
|
|
-// BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
|
|
-// String line = null;
|
|
|
-// while ((line = reader.readLine()) != null) {
|
|
|
-// strBuf.append(line).append("\n");
|
|
|
-// }
|
|
|
-// res = strBuf.toString();
|
|
|
-// reader.close();
|
|
|
-// reader = null;
|
|
|
-// } finally {
|
|
|
-// if (conn != null) {
|
|
|
-// conn.disconnect();
|
|
|
-// conn = null;
|
|
|
-// }
|
|
|
-// }
|
|
|
-// return res;
|
|
|
-// }
|
|
|
-
|
|
|
- @Override
|
|
|
- public YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, File file,String md5) {
|
|
|
- InputStream in=null;
|
|
|
- try {
|
|
|
- in = new FileInputStream(file);
|
|
|
- return saveFile(siteId, env, in, md5);
|
|
|
- } catch (FileNotFoundException e) {
|
|
|
- throw new StatusException("5001", "上传出错", e);
|
|
|
- } finally {
|
|
|
- if(in!=null) {
|
|
|
- try {
|
|
|
- in.close();
|
|
|
- } catch (IOException e) {
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public YunHttpRequest getSignature(String siteId, FileStoragePathEnvInfo env, String md5) {
|
|
|
- AliyunSite site = AliyunSiteManager.getAliyunSite(siteId);
|
|
|
- AliYunAccount ac = AliyunSiteManager.getAliYunAccountByAliyunId(site.getAliyunId());
|
|
|
- String ossEndpoint = ac.getOssEndpoint();
|
|
|
- String bucket = ac.getBucket();
|
|
|
- String accessKeyId = ac.getAccessKeyId();
|
|
|
- String accessKeySecret = ac.getAccessKeySecret();
|
|
|
- String urlStr = null; // 提交表单的URL为bucket域名
|
|
|
- if(ossEndpoint.startsWith("https://")) {
|
|
|
- urlStr = ossEndpoint.replace("https://", "https://" + bucket + ".");
|
|
|
- }else if(ossEndpoint.startsWith("http://")) {
|
|
|
- urlStr = ossEndpoint.replace("http://", "http://" + bucket + ".");
|
|
|
- }
|
|
|
- env.setTimeMillis(String.valueOf(System.currentTimeMillis()));
|
|
|
-
|
|
|
- String path = FreeMarkerUtil.process(site.getPath(), env);
|
|
|
- if (path.startsWith("/")) {
|
|
|
- path = path.substring(1);
|
|
|
- }
|
|
|
-
|
|
|
- String accessUrl = FileStorageHelper.getUrl(ac.getDomain(), path);
|
|
|
-
|
|
|
- Map<String, String> params = Maps.newHashMap();
|
|
|
- // key
|
|
|
- params.put("key", path);
|
|
|
- // OSSAccessKeyId
|
|
|
- params.put("OSSAccessKeyId", accessKeyId);
|
|
|
- // policy
|
|
|
- Date expiration = DateUtils.addSeconds(new Date(), 600);
|
|
|
- String expirationStr = DateUtil.format(expiration, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
|
|
|
- String policy = "{\"expiration\": \"" + expirationStr + "\",\"conditions\": [[\"content-length-range\", 0, "
|
|
|
- + maxFileSize + "]]}";
|
|
|
- String encodePolicy = java.util.Base64.getEncoder().encodeToString(policy.getBytes());
|
|
|
- params.put("policy", encodePolicy);
|
|
|
- // Signature
|
|
|
- String signaturecom = com.aliyun.oss.common.auth.ServiceSignature.create().computeSignature(accessKeySecret,
|
|
|
- encodePolicy);
|
|
|
- params.put("Signature", signaturecom);
|
|
|
-
|
|
|
- YunHttpRequest request = new YunHttpRequest();
|
|
|
- request.setAccessUrl(accessUrl);
|
|
|
- request.setFormParams(params);
|
|
|
- request.setFormUrl(urlStr);
|
|
|
- return request;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-// @SuppressWarnings("unused")
|
|
|
-// private String postObjectByInputStream(String siteId, FileStoragePathEnvInfo env, InputStream in, String md5) throws IOException {
|
|
|
-// AliyunSite as = AliyunSiteManager.getAliyunSite(siteId);
|
|
|
-// AliYunAccount ac = AliyunSiteManager.getAliYunAccountByAliyunId(as.getAliyunId());
|
|
|
-// String ossEndpoint = ac.getOssEndpoint();
|
|
|
-// String bucket = ac.getBucket();
|
|
|
-// String accessKeyId = ac.getAccessKeyId();
|
|
|
-// String accessKeySecret = ac.getAccessKeySecret();
|
|
|
-// // 阿里云文件路径
|
|
|
-// String path = FreeMarkerUtil.process(as.getPath(), env);
|
|
|
-// if (path.startsWith("/")) {
|
|
|
-// path = path.substring(1);
|
|
|
-// }
|
|
|
-//
|
|
|
-// String filename = path.substring(path.lastIndexOf("/") + 1);
|
|
|
-// String urlStr = null; // 提交表单的URL为bucket域名
|
|
|
-// if(ossEndpoint.startsWith("https://")) {
|
|
|
-// urlStr = ossEndpoint.replace("https://", "https://" + bucket + ".");
|
|
|
-// }else if(ossEndpoint.startsWith("http://")) {
|
|
|
-// urlStr = ossEndpoint.replace("http://", "http://" + bucket + ".");
|
|
|
-// }
|
|
|
-//
|
|
|
-// LinkedHashMap<String, String> textMap = new LinkedHashMap<String, String>();
|
|
|
-// // key
|
|
|
-// textMap.put("key", path);
|
|
|
-// // Content-Disposition
|
|
|
-// textMap.put("Content-Disposition", "attachment;filename=" + filename);
|
|
|
-// // OSSAccessKeyId
|
|
|
-// textMap.put("OSSAccessKeyId", accessKeyId);
|
|
|
-// // policy
|
|
|
-// String policy = "{\"expiration\": \"2120-01-01T12:00:00.000Z\",\"conditions\": [[\"content-length-range\", 0, "
|
|
|
-// + maxFileSize + "]]}";
|
|
|
-// String encodePolicy = java.util.Base64.getEncoder().encodeToString(policy.getBytes());
|
|
|
-// textMap.put("policy", encodePolicy);
|
|
|
-// // Signature
|
|
|
-// String signaturecom = com.aliyun.oss.common.auth.ServiceSignature.create().computeSignature(accessKeySecret,
|
|
|
-// encodePolicy);
|
|
|
-// textMap.put("Signature", signaturecom);
|
|
|
-//
|
|
|
-//
|
|
|
-// String ret = formUploadByInputStream(urlStr, textMap, in,filename);
|
|
|
-// log.info("oss上传:" + ret);
|
|
|
-// return path;
|
|
|
-// }
|
|
|
-
|
|
|
-// @SuppressWarnings("rawtypes")
|
|
|
-// private static String formUploadByInputStream(String urlStr, Map<String, String> textMap, InputStream filein,String fileName )
|
|
|
-// throws IOException {
|
|
|
-// String res = "";
|
|
|
-// HttpURLConnection conn = null;
|
|
|
-// String BOUNDARY = "9431149156168";
|
|
|
-// try {
|
|
|
-// URL url = new URL(urlStr);
|
|
|
-// conn = (HttpURLConnection) url.openConnection();
|
|
|
-// conn.setConnectTimeout(5000);
|
|
|
-// conn.setReadTimeout(30000);
|
|
|
-// conn.setDoOutput(true);
|
|
|
-// conn.setDoInput(true);
|
|
|
-// conn.setRequestMethod("POST");
|
|
|
-// conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");
|
|
|
-// conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
|
|
|
-//
|
|
|
-// OutputStream out = new DataOutputStream(conn.getOutputStream());
|
|
|
-// // text
|
|
|
-// if (textMap != null) {
|
|
|
-// StringBuffer strBuf = new StringBuffer();
|
|
|
-// Iterator iter = textMap.entrySet().iterator();
|
|
|
-// int i = 0;
|
|
|
-// while (iter.hasNext()) {
|
|
|
-// Map.Entry entry = (Map.Entry) iter.next();
|
|
|
-// String inputName = (String) entry.getKey();
|
|
|
-// String inputValue = (String) entry.getValue();
|
|
|
-// if (inputValue == null) {
|
|
|
-// continue;
|
|
|
-// }
|
|
|
-// if (i == 0) {
|
|
|
-// strBuf.append("--").append(BOUNDARY).append("\r\n");
|
|
|
-// strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n");
|
|
|
-// strBuf.append(inputValue);
|
|
|
-// } else {
|
|
|
-// strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
|
|
|
-// strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n");
|
|
|
-//
|
|
|
-// strBuf.append(inputValue);
|
|
|
-// }
|
|
|
-//
|
|
|
-// i++;
|
|
|
-// }
|
|
|
-// out.write(strBuf.toString().getBytes());
|
|
|
-// }
|
|
|
-//
|
|
|
-// // file
|
|
|
-//
|
|
|
-// StringBuffer strBufFile = new StringBuffer();
|
|
|
-// strBufFile.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
|
|
|
-// strBufFile.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + fileName
|
|
|
-// + "\"\r\n");
|
|
|
-// strBufFile.append("Content-Type: application/octet-stream\r\n\r\n");
|
|
|
-//
|
|
|
-// out.write(strBufFile.toString().getBytes());
|
|
|
-//
|
|
|
-// DataInputStream in = new DataInputStream(filein);
|
|
|
-// int bytes = 0;
|
|
|
-// byte[] bufferOut = new byte[1024];
|
|
|
-// while ((bytes = in.read(bufferOut)) != -1) {
|
|
|
-// out.write(bufferOut, 0, bytes);
|
|
|
-// }
|
|
|
-// in.close();
|
|
|
-// StringBuffer strBufTag = new StringBuffer();
|
|
|
-// out.write(strBufTag.toString().getBytes());
|
|
|
-//
|
|
|
-// byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
|
|
|
-// out.write(endData);
|
|
|
-// out.flush();
|
|
|
-// out.close();
|
|
|
-//
|
|
|
-// // 读取返回数据
|
|
|
-// StringBuffer strBuf = new StringBuffer();
|
|
|
-// BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
|
|
-// String line = null;
|
|
|
-// while ((line = reader.readLine()) != null) {
|
|
|
-// strBuf.append(line).append("\n");
|
|
|
-// }
|
|
|
-// res = strBuf.toString();
|
|
|
-// reader.close();
|
|
|
-// reader = null;
|
|
|
-// } finally {
|
|
|
-// if (conn != null) {
|
|
|
-// conn.disconnect();
|
|
|
-// conn = null;
|
|
|
-// }
|
|
|
-// }
|
|
|
-// return res;
|
|
|
-// }
|
|
|
-
|
|
|
- @Override
|
|
|
- public YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, InputStream in, String md5) {
|
|
|
- try {
|
|
|
- String relativePath = uploadObject(siteId, env, in, md5);
|
|
|
- AliyunSite as = AliyunSiteManager.getAliyunSite(siteId);
|
|
|
- AliYunAccount ac = AliyunSiteManager.getAliYunAccountByAliyunId(as.getAliyunId());
|
|
|
- String url=FileStorageHelper.getUrl(ac.getDomain(), relativePath);
|
|
|
- YunPathInfo pi=new YunPathInfo(url, getTreatyPath(as.getAliyunId(), relativePath));
|
|
|
- return pi;
|
|
|
- } catch (Exception e) {
|
|
|
- throw new StatusException("6001", "上传出错", e);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public String realPathBackup(String path) {
|
|
|
- String yunId = FileStorageHelper.getYunId(path);
|
|
|
- String urlpath=FileStorageHelper.getPath(path);
|
|
|
- AliYunAccount ac = AliyunSiteManager.getAliYunAccountByAliyunId(yunId);
|
|
|
- String bk=ac.getDomainBackup();
|
|
|
- if(StringUtils.isNotBlank(bk)) {
|
|
|
- return FileStorageHelper.getUrl(bk, urlpath);
|
|
|
- }
|
|
|
- return FileStorageHelper.getUrl(ac.getDomain(), urlpath);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void deleteFile(String path) {
|
|
|
- //无删除权限
|
|
|
-// String yunId=FileStorageHelper.getYunId(path);
|
|
|
-// AliYunAccount ac = AliyunSiteManager.getAliYunAccountByAliyunId(yunId);
|
|
|
-// String bucket = ac.getBucket();
|
|
|
-// OSS oss=AliyunSiteManager.getAliYunClientByAliyunId(yunId);
|
|
|
-// // 阿里云文件路径
|
|
|
-// String urlpath = FileStorageHelper.getPath(path);
|
|
|
-// if (urlpath.startsWith("/")) {
|
|
|
-// urlpath = urlpath.substring(1);
|
|
|
-// }
|
|
|
-// oss.deleteObject(bucket, urlpath);
|
|
|
-
|
|
|
- }
|
|
|
- private String uploadObject(String siteId, FileStoragePathEnvInfo env,InputStream in, String md5) throws IOException, DecoderException {
|
|
|
- AliyunSite as = AliyunSiteManager.getAliyunSite(siteId);
|
|
|
- AliYunAccount ac = AliyunSiteManager.getAliYunAccountByAliyunId(as.getAliyunId());
|
|
|
- String bucket = ac.getBucket();
|
|
|
- OSS oss=AliyunSiteManager.getAliYunClientBySiteId(siteId);
|
|
|
- // 阿里云文件路径
|
|
|
- String path = FreeMarkerUtil.process(as.getPath(), env);
|
|
|
- path=disposePath(path);
|
|
|
- if(StringUtils.isNotBlank(md5)) {
|
|
|
- md5=Base64.getEncoder().encodeToString(Hex.decodeHex(md5));
|
|
|
- ObjectMetadata meta = new ObjectMetadata();
|
|
|
+
|
|
|
+ // private ExamCloudLog log = ExamCloudLogFactory.getLog(this.getClass());
|
|
|
+
|
|
|
+ // 文件最大大小(byte)
|
|
|
+ private static int maxFileSize = 100 * 1024 * 1024;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public YunPathInfo saveFile(File file, String path) {
|
|
|
+ String siteId = "transPath";
|
|
|
+ FileStoragePathEnvInfo env = new FileStoragePathEnvInfo();
|
|
|
+ env.setRelativePath(path);
|
|
|
+ return saveFile(siteId, env, file, null, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String realPath(String path) {
|
|
|
+ String yunId = FileStorageHelper.getYunId(path);
|
|
|
+ String urlpath = FileStorageHelper.getPath(path);
|
|
|
+ AliYunAccount ac = AliyunSiteManager.getAliYunAccountByAliyunId(yunId);
|
|
|
+ return FileStorageHelper.getUrl(ac.getDomain(), urlpath);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 表单上传
|
|
|
+ *
|
|
|
+ * @param siteId
|
|
|
+ * @param env
|
|
|
+ * @param file
|
|
|
+ * @param md5
|
|
|
+ * @return 不带域名的完整路径
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ /*private String postObject(String siteId, FileStoragePathEnvInfo env, File file, String md5) throws IOException {
|
|
|
+ AliyunSite as = AliyunSiteManager.getAliyunSite(siteId);
|
|
|
+ AliYunAccount ac = AliyunSiteManager.getAliYunAccountByAliyunId(as.getAliyunId());
|
|
|
+ String ossEndpoint = ac.getOssEndpoint();
|
|
|
+ String bucket = ac.getBucket();
|
|
|
+ String accessKeyId = ac.getAccessKeyId();
|
|
|
+ String accessKeySecret = ac.getAccessKeySecret();
|
|
|
+ // 阿里云文件路径
|
|
|
+ String path = FreeMarkerUtil.process(as.getPath(), env);
|
|
|
+ if (path.startsWith("/")) {
|
|
|
+ path = path.substring(1);
|
|
|
+ }
|
|
|
+
|
|
|
+ String filepath = file.getAbsolutePath();
|
|
|
+ String filename = path.substring(path.lastIndexOf("/") + 1);
|
|
|
+ String urlStr = null; // 提交表单的URL为bucket域名
|
|
|
+ if (ossEndpoint.startsWith("https://")) {
|
|
|
+ urlStr = ossEndpoint.replace("https://", "https://" + bucket + ".");
|
|
|
+ } else if (ossEndpoint.startsWith("http://")) {
|
|
|
+ urlStr = ossEndpoint.replace("http://", "http://" + bucket + ".");
|
|
|
+ }
|
|
|
+
|
|
|
+ LinkedHashMap<String, String> textMap = new LinkedHashMap<String, String>();
|
|
|
+ // key
|
|
|
+ textMap.put("key", path);
|
|
|
+ // Content-Disposition
|
|
|
+ textMap.put("Content-Disposition", "attachment;filename=" + filename);
|
|
|
+ // OSSAccessKeyId
|
|
|
+ textMap.put("OSSAccessKeyId", accessKeyId);
|
|
|
+ // policy
|
|
|
+ String policy = "{\"expiration\": \"2120-01-01T12:00:00.000Z\",\"conditions\": [[\"content-length-range\", 0, "
|
|
|
+ + maxFileSize + "]]}";
|
|
|
+ String encodePolicy = java.util.Base64.getEncoder().encodeToString(policy.getBytes());
|
|
|
+ textMap.put("policy", encodePolicy);
|
|
|
+ // Signature
|
|
|
+ String signaturecom = com.aliyun.oss.common.auth.ServiceSignature.create().computeSignature(accessKeySecret,
|
|
|
+ encodePolicy);
|
|
|
+ textMap.put("Signature", signaturecom);
|
|
|
+
|
|
|
+ Map<String, String> fileMap = new HashMap<String, String>();
|
|
|
+ fileMap.put("file", filepath);
|
|
|
+
|
|
|
+ String ret = formUpload(urlStr, textMap, fileMap);
|
|
|
+ log.info("oss上传:" + ret);
|
|
|
+ return path;
|
|
|
+ }*/
|
|
|
+
|
|
|
+ /*@SuppressWarnings("rawtypes")
|
|
|
+ private static String formUpload(String urlStr, Map<String, String> textMap, Map<String, String> fileMap)
|
|
|
+ throws IOException {
|
|
|
+ String res = "";
|
|
|
+ HttpURLConnection conn = null;
|
|
|
+ String BOUNDARY = "9431149156168";
|
|
|
+ try {
|
|
|
+ URL url = new URL(urlStr);
|
|
|
+ conn = (HttpURLConnection) url.openConnection();
|
|
|
+ conn.setConnectTimeout(5000);
|
|
|
+ conn.setReadTimeout(30000);
|
|
|
+ conn.setDoOutput(true);
|
|
|
+ conn.setDoInput(true);
|
|
|
+ conn.setRequestMethod("POST");
|
|
|
+ conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");
|
|
|
+ conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
|
|
|
+
|
|
|
+ OutputStream out = new DataOutputStream(conn.getOutputStream());
|
|
|
+ // text
|
|
|
+ if (textMap != null) {
|
|
|
+ StringBuffer strBuf = new StringBuffer();
|
|
|
+ Iterator iter = textMap.entrySet().iterator();
|
|
|
+ int i = 0;
|
|
|
+ while (iter.hasNext()) {
|
|
|
+ Map.Entry entry = (Map.Entry) iter.next();
|
|
|
+ String inputName = (String) entry.getKey();
|
|
|
+ String inputValue = (String) entry.getValue();
|
|
|
+ if (inputValue == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (i == 0) {
|
|
|
+ strBuf.append("--").append(BOUNDARY).append("\r\n");
|
|
|
+ strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n");
|
|
|
+ strBuf.append(inputValue);
|
|
|
+ } else {
|
|
|
+ strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
|
|
|
+ strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n");
|
|
|
+
|
|
|
+ strBuf.append(inputValue);
|
|
|
+ }
|
|
|
+
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+ out.write(strBuf.toString().getBytes());
|
|
|
+ }
|
|
|
+
|
|
|
+ // file
|
|
|
+ if (fileMap != null) {
|
|
|
+ Iterator iter = fileMap.entrySet().iterator();
|
|
|
+ while (iter.hasNext()) {
|
|
|
+ Map.Entry entry = (Map.Entry) iter.next();
|
|
|
+ String inputName = (String) entry.getKey();
|
|
|
+ String inputValue = (String) entry.getValue();
|
|
|
+ if (inputValue == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ File file = new File(inputValue);
|
|
|
+ String filename = file.getName();
|
|
|
+ String contentType = new MimetypesFileTypeMap().getContentType(file);
|
|
|
+ if (contentType == null || contentType.equals("")) {
|
|
|
+ contentType = "application/octet-stream";
|
|
|
+ }
|
|
|
+
|
|
|
+ StringBuffer strBuf = new StringBuffer();
|
|
|
+ strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
|
|
|
+ strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"; filename=\"" + filename
|
|
|
+ + "\"\r\n");
|
|
|
+ strBuf.append("Content-Type: " + contentType + "\r\n\r\n");
|
|
|
+
|
|
|
+ out.write(strBuf.toString().getBytes());
|
|
|
+
|
|
|
+ DataInputStream in = new DataInputStream(new FileInputStream(file));
|
|
|
+ int bytes = 0;
|
|
|
+ byte[] bufferOut = new byte[1024];
|
|
|
+ while ((bytes = in.read(bufferOut)) != -1) {
|
|
|
+ out.write(bufferOut, 0, bytes);
|
|
|
+ }
|
|
|
+ in.close();
|
|
|
+ }
|
|
|
+ StringBuffer strBuf = new StringBuffer();
|
|
|
+ out.write(strBuf.toString().getBytes());
|
|
|
+ }
|
|
|
+
|
|
|
+ byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
|
|
|
+ out.write(endData);
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+
|
|
|
+ // 读取返回数据
|
|
|
+ StringBuffer strBuf = new StringBuffer();
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
|
|
+ String line = null;
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ strBuf.append(line).append("\n");
|
|
|
+ }
|
|
|
+ res = strBuf.toString();
|
|
|
+ reader.close();
|
|
|
+ reader = null;
|
|
|
+ } finally {
|
|
|
+ if (conn != null) {
|
|
|
+ conn.disconnect();
|
|
|
+ conn = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+ }*/
|
|
|
+ @Override
|
|
|
+ public YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, File file, String md5, boolean refreshCDN) {
|
|
|
+ try (InputStream in = new FileInputStream(file);) {
|
|
|
+ return saveFile(siteId, env, in, md5, refreshCDN);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new StatusException("5001", "上传出错", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, byte[] bytes, boolean refreshCDN) {
|
|
|
+ try (ByteArrayInputStream in = new ByteArrayInputStream(bytes);) {
|
|
|
+ return saveFile(siteId, env, in, null, refreshCDN);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new StatusException("5002", "上传出错", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public YunHttpRequest getSignature(String siteId, FileStoragePathEnvInfo env, String md5) {
|
|
|
+ AliyunSite site = AliyunSiteManager.getAliyunSite(siteId);
|
|
|
+ AliYunAccount ac = AliyunSiteManager.getAliYunAccountByAliyunId(site.getAliyunId());
|
|
|
+ String ossEndpoint = ac.getOssEndpoint();
|
|
|
+ String bucket = ac.getBucket();
|
|
|
+ String accessKeyId = ac.getAccessKeyId();
|
|
|
+ String accessKeySecret = ac.getAccessKeySecret();
|
|
|
+ String urlStr = null; // 提交表单的URL为bucket域名
|
|
|
+ if (ossEndpoint.startsWith("https://")) {
|
|
|
+ urlStr = ossEndpoint.replace("https://", "https://" + bucket + ".");
|
|
|
+ } else if (ossEndpoint.startsWith("http://")) {
|
|
|
+ urlStr = ossEndpoint.replace("http://", "http://" + bucket + ".");
|
|
|
+ }
|
|
|
+ env.setTimeMillis(String.valueOf(System.currentTimeMillis()));
|
|
|
+
|
|
|
+ String path = FreeMarkerUtil.process(site.getPath(), env);
|
|
|
+ if (path.startsWith("/")) {
|
|
|
+ path = path.substring(1);
|
|
|
+ }
|
|
|
+
|
|
|
+ String accessUrl = FileStorageHelper.getUrl(ac.getDomain(), path);
|
|
|
+
|
|
|
+ Map<String, String> params = Maps.newHashMap();
|
|
|
+ // key
|
|
|
+ params.put("key", path);
|
|
|
+ // OSSAccessKeyId
|
|
|
+ params.put("OSSAccessKeyId", accessKeyId);
|
|
|
+ // policy
|
|
|
+ Date expiration = DateUtils.addSeconds(new Date(), 600);
|
|
|
+ String expirationStr = DateUtil.format(expiration, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
|
|
|
+ String policy = "{\"expiration\": \"" + expirationStr + "\",\"conditions\": [[\"content-length-range\", 0, "
|
|
|
+ + maxFileSize + "]]}";
|
|
|
+ String encodePolicy = java.util.Base64.getEncoder().encodeToString(policy.getBytes());
|
|
|
+ params.put("policy", encodePolicy);
|
|
|
+ // Signature
|
|
|
+ String signaturecom = com.aliyun.oss.common.auth.ServiceSignature.create().computeSignature(accessKeySecret,
|
|
|
+ encodePolicy);
|
|
|
+ params.put("Signature", signaturecom);
|
|
|
+
|
|
|
+ YunHttpRequest request = new YunHttpRequest();
|
|
|
+ request.setAccessUrl(accessUrl);
|
|
|
+ request.setFormParams(params);
|
|
|
+ request.setFormUrl(urlStr);
|
|
|
+ return request;
|
|
|
+ }
|
|
|
+
|
|
|
+ /*@SuppressWarnings("unused")
|
|
|
+ private String postObjectByInputStream(String siteId, FileStoragePathEnvInfo env, InputStream in, String md5) throws IOException {
|
|
|
+ AliyunSite as = AliyunSiteManager.getAliyunSite(siteId);
|
|
|
+ AliYunAccount ac = AliyunSiteManager.getAliYunAccountByAliyunId(as.getAliyunId());
|
|
|
+ String ossEndpoint = ac.getOssEndpoint();
|
|
|
+ String bucket = ac.getBucket();
|
|
|
+ String accessKeyId = ac.getAccessKeyId();
|
|
|
+ String accessKeySecret = ac.getAccessKeySecret();
|
|
|
+ // 阿里云文件路径
|
|
|
+ String path = FreeMarkerUtil.process(as.getPath(), env);
|
|
|
+ if (path.startsWith("/")) {
|
|
|
+ path = path.substring(1);
|
|
|
+ }
|
|
|
+
|
|
|
+ String filename = path.substring(path.lastIndexOf("/") + 1);
|
|
|
+ String urlStr = null; // 提交表单的URL为bucket域名
|
|
|
+ if (ossEndpoint.startsWith("https://")) {
|
|
|
+ urlStr = ossEndpoint.replace("https://", "https://" + bucket + ".");
|
|
|
+ } else if (ossEndpoint.startsWith("http://")) {
|
|
|
+ urlStr = ossEndpoint.replace("http://", "http://" + bucket + ".");
|
|
|
+ }
|
|
|
+
|
|
|
+ LinkedHashMap<String, String> textMap = new LinkedHashMap<String, String>();
|
|
|
+ // key
|
|
|
+ textMap.put("key", path);
|
|
|
+ // Content-Disposition
|
|
|
+ textMap.put("Content-Disposition", "attachment;filename=" + filename);
|
|
|
+ // OSSAccessKeyId
|
|
|
+ textMap.put("OSSAccessKeyId", accessKeyId);
|
|
|
+ // policy
|
|
|
+ String policy = "{\"expiration\": \"2120-01-01T12:00:00.000Z\",\"conditions\": [[\"content-length-range\", 0, "
|
|
|
+ + maxFileSize + "]]}";
|
|
|
+ String encodePolicy = java.util.Base64.getEncoder().encodeToString(policy.getBytes());
|
|
|
+ textMap.put("policy", encodePolicy);
|
|
|
+ // Signature
|
|
|
+ String signaturecom = com.aliyun.oss.common.auth.ServiceSignature.create().computeSignature(accessKeySecret,
|
|
|
+ encodePolicy);
|
|
|
+ textMap.put("Signature", signaturecom);
|
|
|
+
|
|
|
+
|
|
|
+ String ret = formUploadByInputStream(urlStr, textMap, in, filename);
|
|
|
+ log.info("oss上传:" + ret);
|
|
|
+ return path;
|
|
|
+ }*/
|
|
|
+
|
|
|
+ /*@SuppressWarnings("rawtypes")
|
|
|
+ private static String formUploadByInputStream(String urlStr, Map<String, String> textMap, InputStream filein, String fileName)
|
|
|
+ throws IOException {
|
|
|
+ String res = "";
|
|
|
+ HttpURLConnection conn = null;
|
|
|
+ String BOUNDARY = "9431149156168";
|
|
|
+ try {
|
|
|
+ URL url = new URL(urlStr);
|
|
|
+ conn = (HttpURLConnection) url.openConnection();
|
|
|
+ conn.setConnectTimeout(5000);
|
|
|
+ conn.setReadTimeout(30000);
|
|
|
+ conn.setDoOutput(true);
|
|
|
+ conn.setDoInput(true);
|
|
|
+ conn.setRequestMethod("POST");
|
|
|
+ conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");
|
|
|
+ conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
|
|
|
+
|
|
|
+ OutputStream out = new DataOutputStream(conn.getOutputStream());
|
|
|
+ // text
|
|
|
+ if (textMap != null) {
|
|
|
+ StringBuffer strBuf = new StringBuffer();
|
|
|
+ Iterator iter = textMap.entrySet().iterator();
|
|
|
+ int i = 0;
|
|
|
+ while (iter.hasNext()) {
|
|
|
+ Map.Entry entry = (Map.Entry) iter.next();
|
|
|
+ String inputName = (String) entry.getKey();
|
|
|
+ String inputValue = (String) entry.getValue();
|
|
|
+ if (inputValue == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (i == 0) {
|
|
|
+ strBuf.append("--").append(BOUNDARY).append("\r\n");
|
|
|
+ strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n");
|
|
|
+ strBuf.append(inputValue);
|
|
|
+ } else {
|
|
|
+ strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
|
|
|
+ strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n");
|
|
|
+
|
|
|
+ strBuf.append(inputValue);
|
|
|
+ }
|
|
|
+
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+ out.write(strBuf.toString().getBytes());
|
|
|
+ }
|
|
|
+
|
|
|
+ // file
|
|
|
+
|
|
|
+ StringBuffer strBufFile = new StringBuffer();
|
|
|
+ strBufFile.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
|
|
|
+ strBufFile.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + fileName
|
|
|
+ + "\"\r\n");
|
|
|
+ strBufFile.append("Content-Type: application/octet-stream\r\n\r\n");
|
|
|
+
|
|
|
+ out.write(strBufFile.toString().getBytes());
|
|
|
+
|
|
|
+ DataInputStream in = new DataInputStream(filein);
|
|
|
+ int bytes = 0;
|
|
|
+ byte[] bufferOut = new byte[1024];
|
|
|
+ while ((bytes = in.read(bufferOut)) != -1) {
|
|
|
+ out.write(bufferOut, 0, bytes);
|
|
|
+ }
|
|
|
+ in.close();
|
|
|
+ StringBuffer strBufTag = new StringBuffer();
|
|
|
+ out.write(strBufTag.toString().getBytes());
|
|
|
+
|
|
|
+ byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
|
|
|
+ out.write(endData);
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+
|
|
|
+ // 读取返回数据
|
|
|
+ StringBuffer strBuf = new StringBuffer();
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
|
|
+ String line = null;
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ strBuf.append(line).append("\n");
|
|
|
+ }
|
|
|
+ res = strBuf.toString();
|
|
|
+ reader.close();
|
|
|
+ reader = null;
|
|
|
+ } finally {
|
|
|
+ if (conn != null) {
|
|
|
+ conn.disconnect();
|
|
|
+ conn = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+ }*/
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, InputStream in, String md5, boolean refreshCDN) {
|
|
|
+ try {
|
|
|
+ String relativePath = uploadObject(siteId, env, in, md5);
|
|
|
+ AliyunSite as = AliyunSiteManager.getAliyunSite(siteId);
|
|
|
+ AliYunAccount ac = AliyunSiteManager.getAliYunAccountByAliyunId(as.getAliyunId());
|
|
|
+ String url = FileStorageHelper.getUrl(ac.getDomain(), relativePath);
|
|
|
+ YunPathInfo result = new YunPathInfo(url, getTreatyPath(as.getAliyunId(), relativePath));
|
|
|
+
|
|
|
+ if (refreshCDN) {
|
|
|
+ AliyunRefreshCdn.refreshCDN(ac.getAccessKeyId(), ac.getAccessKeySecret(), result.getUrl());
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new StatusException("6001", "上传出错", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String realPathBackup(String path) {
|
|
|
+ String yunId = FileStorageHelper.getYunId(path);
|
|
|
+ String urlpath = FileStorageHelper.getPath(path);
|
|
|
+ AliYunAccount ac = AliyunSiteManager.getAliYunAccountByAliyunId(yunId);
|
|
|
+ String bk = ac.getDomainBackup();
|
|
|
+ if (StringUtils.isNotBlank(bk)) {
|
|
|
+ return FileStorageHelper.getUrl(bk, urlpath);
|
|
|
+ }
|
|
|
+ return FileStorageHelper.getUrl(ac.getDomain(), urlpath);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deleteFile(String path) {
|
|
|
+ // 无删除权限
|
|
|
+ /*String yunId = FileStorageHelper.getYunId(path);
|
|
|
+ AliYunAccount ac = AliyunSiteManager.getAliYunAccountByAliyunId(yunId);
|
|
|
+ String bucket = ac.getBucket();
|
|
|
+ OSS oss = AliyunSiteManager.getAliYunClientByAliyunId(yunId);
|
|
|
+ // 阿里云文件路径
|
|
|
+ String urlpath = FileStorageHelper.getPath(path);
|
|
|
+ if (urlpath.startsWith("/")) {
|
|
|
+ urlpath = urlpath.substring(1);
|
|
|
+ }
|
|
|
+ oss.deleteObject(bucket, urlpath);*/
|
|
|
+ }
|
|
|
+
|
|
|
+ private String uploadObject(String siteId, FileStoragePathEnvInfo env, InputStream in, String md5) throws IOException, DecoderException {
|
|
|
+ AliyunSite as = AliyunSiteManager.getAliyunSite(siteId);
|
|
|
+ AliYunAccount ac = AliyunSiteManager.getAliYunAccountByAliyunId(as.getAliyunId());
|
|
|
+ String bucket = ac.getBucket();
|
|
|
+ OSS oss = AliyunSiteManager.getAliYunClientBySiteId(siteId);
|
|
|
+ // 阿里云文件路径
|
|
|
+ String path = FreeMarkerUtil.process(as.getPath(), env);
|
|
|
+ path = disposePath(path);
|
|
|
+ if (StringUtils.isNotBlank(md5)) {
|
|
|
+ md5 = Base64.getEncoder().encodeToString(Hex.decodeHex(md5));
|
|
|
+ ObjectMetadata meta = new ObjectMetadata();
|
|
|
meta.setContentMD5(md5);
|
|
|
- oss.putObject(bucket, path, in,meta);
|
|
|
- }else {
|
|
|
- oss.putObject(bucket, path, in);
|
|
|
- }
|
|
|
-
|
|
|
- return path;
|
|
|
- }
|
|
|
- private String getTreatyPath(String yunId,String relativePath) {
|
|
|
- if(relativePath.startsWith("/")) {
|
|
|
- relativePath=relativePath.substring(1);
|
|
|
- }
|
|
|
- String path=FileStorageType.ALIYUN.name().toLowerCase()+"-"+yunId+"://"+relativePath;
|
|
|
- return path;
|
|
|
- }
|
|
|
- private String disposePath(String path) {
|
|
|
- for(;;) {
|
|
|
- if(path.startsWith("/")) {
|
|
|
- path=path.substring(1);
|
|
|
- }else {
|
|
|
- return path;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ oss.putObject(bucket, path, in, meta);
|
|
|
+ } else {
|
|
|
+ oss.putObject(bucket, path, in);
|
|
|
+ }
|
|
|
+
|
|
|
+ return path;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getTreatyPath(String yunId, String relativePath) {
|
|
|
+ if (relativePath.startsWith("/")) {
|
|
|
+ relativePath = relativePath.substring(1);
|
|
|
+ }
|
|
|
+ String path = FileStorageType.ALIYUN.name().toLowerCase() + "-" + yunId + "://" + relativePath;
|
|
|
+ return path;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String disposePath(String path) {
|
|
|
+ for (; ; ) {
|
|
|
+ if (path.startsWith("/")) {
|
|
|
+ path = path.substring(1);
|
|
|
+ } else {
|
|
|
+ return path;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|