Parcourir la source

文件存储返回结果改动

xiatian il y a 5 ans
Parent
commit
39f7684ce7

+ 5 - 4
src/main/java/cn/com/qmth/examcloud/web/filestorage/FileStorage.java

@@ -7,9 +7,10 @@ public interface FileStorage {
 	 * @param siteId 
 	 * @param env
 	 * @param file 文件
-	 * @return 返回包含协议名的地址,数据库直接存储用
+	 * @param md5 文件MD5 可为空
+	 * @return 返回路径
 	 */
-	public String saveFile(String siteId,FileStoragePathEnvInfo env,File file);
+	public YunPathInfo saveFile(String siteId,FileStoragePathEnvInfo env,File file,String md5);
 	
 	/**获取可直接访问的文件地址
 	 * @param path 全路径,包含根目录,不含协议名
@@ -20,9 +21,9 @@ public interface FileStorage {
 	/**保存文件到存储服务,siteId为1,转换接口用
 	 * @param file 文件
 	 * @param path 全路径,包含根目录,不含协议名
-	 * @return 返回包含协议名的地址,数据库直接存储用
+	 * @return 返回路径
 	 */
-	public String saveFile(File file, String path);
+	public YunPathInfo saveFile(File file, String path);
 	
 	/**获取云存储签名
 	 * @param siteId

+ 8 - 6
src/main/java/cn/com/qmth/examcloud/web/filestorage/FileStorageUtil.java

@@ -30,11 +30,12 @@ public class FileStorageUtil {
 	 * @param siteId
 	 * @param env
 	 * @param file   文件
+	 * @param md5 文件MD5 可为空
 	 * @return 返回包含协议名的地址,数据库直接存储用 如:upyun-1://student_photo/001.jpg
 	 */
-	public static String saveFile(String siteId, FileStoragePathEnvInfo env, File file) {
+	public static YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, File file,String md5) {
 		FileStorageType fsType = FileStorageType.valueOf(fileStorageType);
-		return saveFile(siteId, env, file, fsType);
+		return saveFile(siteId, env, file, fsType,md5);
 	}
 
 	/**
@@ -44,9 +45,10 @@ public class FileStorageUtil {
 	 * @param env
 	 * @param file   文件
 	 * @param fsType 存储类型
+	 * @param md5 文件MD5 可为空
 	 * @return 返回包含协议名的地址,数据库直接存储用 如:upyun-1://student_photo/001.jpg
 	 */
-	private static String saveFile(String siteId, FileStoragePathEnvInfo env, File file, FileStorageType fsType) {
+	private static YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, File file, FileStorageType fsType,String md5) {
 		if (siteId == null) {
 			throw new StatusException("2000", "siteId是空");
 		}
@@ -57,7 +59,7 @@ public class FileStorageUtil {
 			throw new StatusException("2002", "文件上传路径信息是空");
 		}
 		FileStorage fs = SpringContextHolder.getBean(fsType.name().toLowerCase() + beanSuff, FileStorage.class);
-		return fs.saveFile(siteId, env, file);
+		return fs.saveFile(siteId, env, file,md5);
 	}
 
 	/**
@@ -119,8 +121,8 @@ public class FileStorageUtil {
 			String httppath = fs.realPath(getPath(path));
 			file = downFile(httppath);
 			// 保存文件到指定存储服务器,并返回路径
-			String ret = fs.saveFile(file, "from" + sourseType.name().toLowerCase() + "/" + getPath(path));
-			return ret;
+			YunPathInfo ret = fs.saveFile(file, "from" + sourseType.name().toLowerCase() + "/" + getPath(path));
+			return ret.getRelativePath();
 		} catch (IOException e) {
 			throw new StatusException("4003", "下载文件出错 " + e.getMessage());
 		} finally {

+ 47 - 0
src/main/java/cn/com/qmth/examcloud/web/filestorage/YunPathInfo.java

@@ -0,0 +1,47 @@
+package cn.com.qmth.examcloud.web.filestorage;
+
+/**
+ * 路径
+ *
+ */
+public class YunPathInfo {
+
+	/**
+	 * 文件访问地址,http
+	 */
+	private String url;
+
+	/**
+	 * 返回包含协议名的地址,数据库直接存储用
+	 */
+	private String relativePath;
+
+	/**
+	 * 构造函数
+	 *
+	 * @param url
+	 * @param relativePath
+	 */
+	public YunPathInfo(String url, String relativePath) {
+		super();
+		this.url = url;
+		this.relativePath = relativePath;
+	}
+
+	public String getUrl() {
+		return url;
+	}
+
+	public void setUrl(String url) {
+		this.url = url;
+	}
+
+	public String getRelativePath() {
+		return relativePath;
+	}
+
+	public void setRelativePath(String relativePath) {
+		this.relativePath = relativePath;
+	}
+
+}

+ 11 - 4
src/main/java/cn/com/qmth/examcloud/web/filestorage/impl/AliyunFileStorageImpl.java

@@ -36,6 +36,7 @@ import cn.com.qmth.examcloud.web.filestorage.FileStoragePathEnvInfo;
 import cn.com.qmth.examcloud.web.filestorage.FileStorageType;
 import cn.com.qmth.examcloud.web.filestorage.FileStorageUtil;
 import cn.com.qmth.examcloud.web.filestorage.YunHttpRequest;
+import cn.com.qmth.examcloud.web.filestorage.YunPathInfo;
 
 @Service(value = "aliyunFileStorage")
 public class AliyunFileStorageImpl implements FileStorage {
@@ -45,14 +46,17 @@ public class AliyunFileStorageImpl implements FileStorage {
 	private static int maxFileSize = 100 * 1024 * 1024;
 
 	@Override
-	public String saveFile(File file, String path) {
+	public YunPathInfo saveFile(File file, String path) {
 		try {
 			String siteId = "transPath";
 			FileStoragePathEnvInfo env = new FileStoragePathEnvInfo();
 			env.setRelativePath(path);
 			String relativePath = postObject(siteId, env, file, null);
 			AliyunSite as = AliyunSiteManager.getAliyunSite(siteId);
-			return FileStorageType.ALIYUN + "-" + as.getAliyunId() + "://" + relativePath;
+			AliYunAccount ac = AliyunSiteManager.getAliYunAccountByAliyunId(as.getAliyunId());
+			String url=FileStorageUtil.getUrl(ac.getDomain(), relativePath);
+			YunPathInfo pi=new YunPathInfo(url, FileStorageType.ALIYUN + "-" + as.getAliyunId() + "://" + relativePath);
+			return pi;
 		} catch (IOException e) {
 			throw new StatusException("1001", "上传出错", e);
 		}
@@ -225,11 +229,14 @@ public class AliyunFileStorageImpl implements FileStorage {
 	}
 
 	@Override
-	public String saveFile(String siteId, FileStoragePathEnvInfo env, File file) {
+	public YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, File file,String md5) {
 		try {
 			String relativePath = postObject(siteId, env, file, null);
 			AliyunSite as = AliyunSiteManager.getAliyunSite(siteId);
-			return FileStorageType.ALIYUN + "-" + as.getAliyunId() + "://" + relativePath;
+			AliYunAccount ac = AliyunSiteManager.getAliYunAccountByAliyunId(as.getAliyunId());
+			String url=FileStorageUtil.getUrl(ac.getDomain(), relativePath);
+			YunPathInfo pi=new YunPathInfo(url, FileStorageType.ALIYUN + "-" + as.getAliyunId() + "://" + relativePath);
+			return pi;
 		} catch (IOException e) {
 			throw new StatusException("5001", "上传出错", e);
 		}

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

@@ -10,6 +10,7 @@ import cn.com.qmth.examcloud.web.filestorage.FileStoragePathEnvInfo;
 import cn.com.qmth.examcloud.web.filestorage.FileStorageType;
 import cn.com.qmth.examcloud.web.filestorage.FileStorageUtil;
 import cn.com.qmth.examcloud.web.filestorage.YunHttpRequest;
+import cn.com.qmth.examcloud.web.filestorage.YunPathInfo;
 import cn.com.qmth.examcloud.web.upyun.UpYunClient;
 import cn.com.qmth.examcloud.web.upyun.UpYunHttpRequest;
 import cn.com.qmth.examcloud.web.upyun.UpYunPathInfo;
@@ -25,13 +26,17 @@ public class UpyunFileStorageImpl implements FileStorage {
 	private UpyunService upyunService;
 
 	@Override
-	public String saveFile(File file, String path) {
+	public YunPathInfo saveFile(File file, String path) {
 		String siteId="transPath";
 		UpyunPathEnvironmentInfo env = new UpyunPathEnvironmentInfo();
 		env.setRelativePath(path);
 		String fpath = upyunService.writeFile(siteId, env, file, null).getRelativePath();
 		UpyunSite site=UpyunSiteManager.getUpyunSite(siteId);
-		return FileStorageType.UPYUN+"-"+site.getUpyunId()+"://"+fpath;
+		UpYunClient c=UpyunSiteManager.getUpYunClientByUpyunId(site.getUpyunId());
+		String relativePath=FileStorageType.UPYUN+"-"+site.getUpyunId()+"://"+fpath;
+		String url=FileStorageUtil.getUrl(c.getDomain(), fpath);
+		YunPathInfo pi=new YunPathInfo(url, relativePath);
+		return pi;
 	}
 
 	@Override
@@ -42,10 +47,14 @@ public class UpyunFileStorageImpl implements FileStorage {
 	}
 
 	@Override
-	public String saveFile(String siteId, FileStoragePathEnvInfo env, File file) {
-		UpYunPathInfo pathInfo =upyunService.writeFile(siteId, of(env), file, null);
+	public YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, File file,String md5) {
+		UpYunPathInfo pathInfo =upyunService.writeFile(siteId, of(env), file, md5);
 		UpyunSite site=UpyunSiteManager.getUpyunSite(siteId);
-		return FileStorageType.UPYUN+"-"+site.getUpyunId()+"://"+pathInfo.getRelativePath();
+		String relativePath=FileStorageType.UPYUN+"-"+site.getUpyunId()+"://"+pathInfo.getRelativePath();
+		UpYunClient c=UpyunSiteManager.getUpYunClientByUpyunId(site.getUpyunId());
+		String url=FileStorageUtil.getUrl(c.getDomain(), pathInfo.getRelativePath());
+		YunPathInfo pi=new YunPathInfo(url, relativePath);
+		return pi;
 	}
 	
 	private UpyunPathEnvironmentInfo of(FileStoragePathEnvInfo env) {