Ver Fonte

通用存储删除实现

xiatian há 5 anos atrás
pai
commit
1563177710

+ 24 - 1
src/main/java/cn/com/qmth/examcloud/web/aliyun/AliyunSiteManager.java

@@ -7,6 +7,8 @@ import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.commons.lang3.StringUtils;
 
+import com.aliyun.oss.OSS;
+import com.aliyun.oss.OSSClientBuilder;
 import com.thoughtworks.xstream.XStream;
 
 import cn.com.qmth.examcloud.commons.exception.StatusException;
@@ -21,7 +23,9 @@ import cn.com.qmth.examcloud.web.bootstrap.PropertyHolder;
 public class AliyunSiteManager {
 
 	private static final Map<String, AliyunSite> SITE_HOLDERS = new ConcurrentHashMap<>();
-
+	
+	private static final Map<String, OSS> CLIENT_HOLDERS = new ConcurrentHashMap<>();
+	 
 	private static final Map<String, AliYunAccount> ACCOUNT_HOLDERS = new ConcurrentHashMap<>();
 
 	public static void init() {
@@ -75,6 +79,11 @@ public class AliyunSiteManager {
 						domainBackup);
 				ACCOUNT_HOLDERS.put(aliyunId, ac);
 			}
+			if (null == CLIENT_HOLDERS.get(aliyunId)) {
+				OSS ossClient = new OSSClientBuilder().build(ossEndpoint, accessKeyId, accessKeySecret);
+				CLIENT_HOLDERS.put(aliyunId, ossClient);
+			}
+			
 		}
 
 	}
@@ -103,5 +112,19 @@ public class AliyunSiteManager {
 		}
 		return ac;
 	}
+	
+	public static OSS getAliYunClientByAliyunId(String aliyunId) {
+		OSS oss = CLIENT_HOLDERS.get(aliyunId);
+
+		if (null == oss) {
+			throw new StatusException("520008", "aliYunClient is null");
+		}
+		return oss;
+	}
+
+	public static OSS getAliYunClientBySiteId(String siteId) {
+		AliyunSite site = getAliyunSite(siteId);
+		return getAliYunClientByAliyunId(site.getAliyunId());
+	}
 
 }

+ 41 - 15
src/main/java/cn/com/qmth/examcloud/web/filestorage/FileStorageUtil.java

@@ -218,22 +218,7 @@ public class FileStorageUtil {
 		return path;
 	}
 
-	public static String getYunId(String path) {
-		String hpath = path.substring(0, path.indexOf(connector));
-		String yunId = hpath.substring(path.indexOf("-") + 1);
-		return yunId;
-	}
 
-	public static String getHead(String path) {
-		String hpath = path.substring(0, path.indexOf(connector));
-		String head = hpath.substring(0, hpath.indexOf("-"));
-		return head;
-	}
-
-	public static String getPath(String path) {
-		String rpath = path.substring(path.indexOf(connector) + 4);
-		return rpath;
-	}
 
 	public static String getUrl(String domain, String path) {
 		if (path.startsWith("/")) {
@@ -365,4 +350,45 @@ public class FileStorageUtil {
 		return fs.realPathBackup(path);
 
 	}
+	
+	public static String getYunId(String path) {
+		if(path.indexOf(connector)==-1) {
+			throw new StatusException("7001", "文件路径格式错误:"+path);
+		}
+		String hpath = path.substring(0, path.indexOf(connector));
+		if(hpath.indexOf("-") ==-1) {
+			throw new StatusException("7002", "文件路径格式错误:"+path);
+		}
+		String yunId = hpath.substring(hpath.indexOf("-") + 1);
+		if(StringUtils.isBlank(yunId)) {
+			throw new StatusException("7003", "文件路径格式错误:"+path);
+		}
+		return yunId;
+	}
+
+	public static String getHead(String path) {
+		if(path.indexOf(connector)==-1) {
+			throw new StatusException("8001", "文件路径格式错误:"+path);
+		}
+		String hpath = path.substring(0, path.indexOf(connector));
+		if(hpath.indexOf("-") ==-1) {
+			throw new StatusException("8002", "文件路径格式错误:"+path);
+		}
+		String head = hpath.substring(0, hpath.indexOf("-"));
+		if(StringUtils.isBlank(head)) {
+			throw new StatusException("8003", "文件路径格式错误:"+path);
+		}
+		return head;
+	}
+
+	public static String getPath(String path) {
+		if(path.indexOf(connector)==-1) {
+			throw new StatusException("9001", "文件路径格式错误:"+path);
+		}
+		String rpath = path.substring(path.indexOf(connector) + 4);
+		if(StringUtils.isBlank(rpath)) {
+			throw new StatusException("9002", "文件路径格式错误:"+path);
+		}
+		return rpath;
+	}
 }

+ 49 - 10
src/main/java/cn/com/qmth/examcloud/web/filestorage/impl/AliyunFileStorageImpl.java

@@ -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);
 	}
 }

+ 8 - 5
src/main/java/cn/com/qmth/examcloud/web/filestorage/impl/UpyunFileStorageImpl.java

@@ -44,8 +44,9 @@ public class UpyunFileStorageImpl implements FileStorage {
 	@Override
 	public String realPath(String path) {
 		String upyunId=FileStorageUtil.getYunId(path);
+		String urlpath=FileStorageUtil.getPath(path);
 		UpYunClient c=UpyunSiteManager.getUpYunClientByUpyunId(upyunId);
-		return FileStorageUtil.getUrl(c.getDomain(), path);
+		return FileStorageUtil.getUrl(c.getDomain(), urlpath);
 	}
 
 	@Override
@@ -87,8 +88,9 @@ public class UpyunFileStorageImpl implements FileStorage {
 
 	@Override
 	public void deleteFile(String path) {
-		// TODO Auto-generated method stub
-		
+		String upyunId=FileStorageUtil.getYunId(path);
+		String urlpath=FileStorageUtil.getPath(path);
+		upyunService.deleteByUpyunId(upyunId, urlpath);;
 	}
 
 	@Override
@@ -105,12 +107,13 @@ public class UpyunFileStorageImpl implements FileStorage {
 	@Override
 	public String realPathBackup(String path) {
 		String upyunId=FileStorageUtil.getYunId(path);
+		String urlpath=FileStorageUtil.getPath(path);
 		UpYunClient c=UpyunSiteManager.getUpYunClientByUpyunId(upyunId);
 		String bk=c.getDomainBackup();
 		if(StringUtils.isNotBlank(bk)) {
-			return FileStorageUtil.getUrl(bk, path);
+			return FileStorageUtil.getUrl(bk, urlpath);
 		}
-		return FileStorageUtil.getUrl(c.getDomain(), path);
+		return FileStorageUtil.getUrl(c.getDomain(), urlpath);
 	}
 
 }

+ 8 - 0
src/main/java/cn/com/qmth/examcloud/web/upyun/UpyunService.java

@@ -58,6 +58,14 @@ public interface UpyunService {
 	 * @param filePath
 	 */
 	void delete(String siteId, String filePath);
+	
+	/**
+	 * 删除文件
+	 *
+	 * @param upyunId
+	 * @param filePath
+	 */
+	void deleteByUpyunId(String upyunId, String filePath);
 
 	/**
 	 * 创建http请求

+ 8 - 0
src/main/java/cn/com/qmth/examcloud/web/upyun/UpyunServiceImpl.java

@@ -85,4 +85,12 @@ public class UpyunServiceImpl implements UpyunService {
 		return request;
 	}
 
+	@Override
+	public void deleteByUpyunId(String upyunId, String filePath) {
+
+		UpYunClient upYunClient = UpyunSiteManager.getUpYunClientByUpyunId(upyunId);
+
+		upYunClient.deleteFile(filePath);
+	}
+
 }