|
@@ -23,6 +23,7 @@ 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.google.common.collect.Maps;
|
|
|
|
|
|
import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
@@ -67,8 +68,9 @@ public class AliyunFileStorageImpl implements FileStorage {
|
|
|
@Override
|
|
|
public String realPath(String path) {
|
|
|
String yunId = FileStorageUtil.getYunId(path);
|
|
|
+ String urlpath=FileStorageUtil.getPath(path);
|
|
|
AliYunAccount ac = AliyunSiteManager.getAliYunAccountByAliyunId(yunId);
|
|
|
- return FileStorageUtil.getUrl(ac.getDomain(), path);
|
|
|
+ return FileStorageUtil.getUrl(ac.getDomain(), urlpath);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -238,7 +240,7 @@ public class AliyunFileStorageImpl implements FileStorage {
|
|
|
@Override
|
|
|
public YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, File file,String md5) {
|
|
|
try {
|
|
|
- String relativePath = postObject(siteId, env, file, null);
|
|
|
+ String relativePath = uploadObject(siteId, env, file, null);
|
|
|
AliyunSite as = AliyunSiteManager.getAliyunSite(siteId);
|
|
|
AliYunAccount ac = AliyunSiteManager.getAliYunAccountByAliyunId(as.getAliyunId());
|
|
|
String url=FileStorageUtil.getUrl(ac.getDomain(), relativePath);
|
|
@@ -296,12 +298,8 @@ public class AliyunFileStorageImpl implements FileStorage {
|
|
|
return request;
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public void deleteFile(String path) {
|
|
|
- // TODO Auto-generated method stub
|
|
|
-
|
|
|
- }
|
|
|
|
|
|
+ @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());
|
|
@@ -439,7 +437,7 @@ public class AliyunFileStorageImpl implements FileStorage {
|
|
|
@Override
|
|
|
public YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, InputStream in, String md5) {
|
|
|
try {
|
|
|
- String relativePath = postObjectByInputStream(siteId, env, in, null);
|
|
|
+ String relativePath = uploadObject(siteId, env, in, null);
|
|
|
AliyunSite as = AliyunSiteManager.getAliyunSite(siteId);
|
|
|
AliYunAccount ac = AliyunSiteManager.getAliYunAccountByAliyunId(as.getAliyunId());
|
|
|
String url=FileStorageUtil.getUrl(ac.getDomain(), relativePath);
|
|
@@ -453,11 +451,52 @@ public class AliyunFileStorageImpl implements FileStorage {
|
|
|
@Override
|
|
|
public String realPathBackup(String path) {
|
|
|
String yunId = FileStorageUtil.getYunId(path);
|
|
|
+ String urlpath=FileStorageUtil.getPath(path);
|
|
|
AliYunAccount ac = AliyunSiteManager.getAliYunAccountByAliyunId(yunId);
|
|
|
String bk=ac.getDomainBackup();
|
|
|
if(StringUtils.isNotBlank(bk)) {
|
|
|
- return FileStorageUtil.getUrl(bk, path);
|
|
|
+ return FileStorageUtil.getUrl(bk, urlpath);
|
|
|
+ }
|
|
|
+ return FileStorageUtil.getUrl(ac.getDomain(), urlpath);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deleteFile(String path) {
|
|
|
+ String yunId=FileStorageUtil.getYunId(path);
|
|
|
+ AliYunAccount ac = AliyunSiteManager.getAliYunAccountByAliyunId(yunId);
|
|
|
+ String bucket = ac.getBucket();
|
|
|
+ OSS oss=AliyunSiteManager.getAliYunClientByAliyunId(yunId);
|
|
|
+ // 阿里云文件路径
|
|
|
+ String urlpath = FileStorageUtil.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 {
|
|
|
+ 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);
|
|
|
+ if (path.startsWith("/")) {
|
|
|
+ path = path.substring(1);
|
|
|
+ }
|
|
|
+ oss.putObject(bucket, path, in);
|
|
|
+
|
|
|
+ return path;
|
|
|
+ }
|
|
|
+ private String uploadObject(String siteId, FileStoragePathEnvInfo env,File file, String md5) throws IOException {
|
|
|
+ InputStream in=null;
|
|
|
+ try {
|
|
|
+ in = new FileInputStream(file);
|
|
|
+ return uploadObject(siteId, env, in, md5);
|
|
|
+ } finally {
|
|
|
+ if(in!=null) {
|
|
|
+ in.close();
|
|
|
+ }
|
|
|
}
|
|
|
- return FileStorageUtil.getUrl(ac.getDomain(), path);
|
|
|
}
|
|
|
}
|