|
@@ -23,9 +23,12 @@ 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.support.SpringContextHolder;
|
|
|
+import cn.com.qmth.examcloud.web.upyun.UpYunClient;
|
|
|
import cn.com.qmth.examcloud.web.upyun.UpyunSiteManager;
|
|
|
|
|
|
-/**文件存储服务接口工具类
|
|
|
+/**
|
|
|
+ * 文件存储服务接口工具类
|
|
|
+ *
|
|
|
* @author 86182
|
|
|
*
|
|
|
*/
|
|
@@ -37,64 +40,65 @@ public class FileStorageUtil {
|
|
|
|
|
|
private static String beanSuff = "FileStorage";
|
|
|
|
|
|
-
|
|
|
-
|
|
|
/**
|
|
|
* 根据当前配置存储类型保存文件到存储服务器
|
|
|
*
|
|
|
* @param siteId
|
|
|
* @param env
|
|
|
- * @param in 文件流
|
|
|
- * @param md5 文件MD5 可为空
|
|
|
+ * @param in 文件流
|
|
|
+ * @param md5 文件MD5 可为空
|
|
|
* @return 返回包含协议名的地址,数据库直接存储用 如:upyun-1://student_photo/001.jpg
|
|
|
*/
|
|
|
- public static YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, InputStream in,String md5) {
|
|
|
+ public static YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, InputStream in, String md5) {
|
|
|
FileStorageType fsType = getFileStorageType();
|
|
|
- return saveFile(siteId, env, in, fsType,md5);
|
|
|
+ return saveFile(siteId, env, in, fsType, md5);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 根据当前配置存储类型保存文件到存储服务器
|
|
|
*
|
|
|
* @param siteId
|
|
|
* @param env
|
|
|
- * @param file 文件
|
|
|
+ * @param file 文件
|
|
|
* @param withMd5 是否校验MD5
|
|
|
* @return 返回包含协议名的地址,数据库直接存储用 如:upyun-1://student_photo/001.jpg
|
|
|
*/
|
|
|
- public static YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, File file,boolean withMd5) {
|
|
|
+ public static YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, File file, boolean withMd5) {
|
|
|
FileStorageType fsType = getFileStorageType();
|
|
|
String md5 = null;
|
|
|
if (withMd5) {
|
|
|
md5 = MD5.md5Hex(file);
|
|
|
}
|
|
|
- return saveFile(siteId, env, file, fsType,md5);
|
|
|
+ return saveFile(siteId, env, file, fsType, md5);
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 根据当前配置存储类型保存文件到存储服务器
|
|
|
*
|
|
|
* @param siteId
|
|
|
* @param env
|
|
|
* @param file 文件
|
|
|
- * @param md5 文件MD5 可为空
|
|
|
+ * @param md5 文件MD5 可为空
|
|
|
* @return 返回包含协议名的地址,数据库直接存储用 如:upyun-1://student_photo/001.jpg
|
|
|
*/
|
|
|
- public static YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, File file,String md5) {
|
|
|
+ public static YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, File file, String md5) {
|
|
|
FileStorageType fsType = getFileStorageType();
|
|
|
- return saveFile(siteId, env, file, fsType,md5);
|
|
|
+ return saveFile(siteId, env, file, fsType, md5);
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 根据存储类型保存文件到存储服务器
|
|
|
*
|
|
|
* @param siteId
|
|
|
* @param env
|
|
|
- * @param in 文件流
|
|
|
+ * @param in 文件流
|
|
|
* @param fsType 存储类型
|
|
|
- * @param md5 文件MD5 可为空
|
|
|
+ * @param md5 文件MD5 可为空
|
|
|
* @return 返回包含协议名的地址,数据库直接存储用 如:upyun-1://student_photo/001.jpg
|
|
|
*/
|
|
|
- private static YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, InputStream in, FileStorageType fsType,String md5) {
|
|
|
-
|
|
|
+ private static YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, InputStream in,
|
|
|
+ FileStorageType fsType, String md5) {
|
|
|
+
|
|
|
if (siteId == null) {
|
|
|
throw new StatusException("1000", "siteId是空");
|
|
|
}
|
|
@@ -106,8 +110,9 @@ public class FileStorageUtil {
|
|
|
}
|
|
|
env.setTimeMillis(String.valueOf(System.currentTimeMillis()));
|
|
|
FileStorage fs = SpringContextHolder.getBean(fsType.name().toLowerCase() + beanSuff, FileStorage.class);
|
|
|
- return fs.saveFile(siteId, env, in,md5);
|
|
|
+ return fs.saveFile(siteId, env, in, md5);
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 根据存储类型保存文件到存储服务器
|
|
|
*
|
|
@@ -115,10 +120,11 @@ public class FileStorageUtil {
|
|
|
* @param env
|
|
|
* @param file 文件
|
|
|
* @param fsType 存储类型
|
|
|
- * @param md5 文件MD5 可为空
|
|
|
+ * @param md5 文件MD5 可为空
|
|
|
* @return 返回包含协议名的地址,数据库直接存储用 如:upyun-1://student_photo/001.jpg
|
|
|
*/
|
|
|
- private static YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, File file, FileStorageType fsType,String md5) {
|
|
|
+ private static YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, File file, FileStorageType fsType,
|
|
|
+ String md5) {
|
|
|
if (siteId == null) {
|
|
|
throw new StatusException("2000", "siteId是空");
|
|
|
}
|
|
@@ -130,7 +136,7 @@ public class FileStorageUtil {
|
|
|
}
|
|
|
env.setTimeMillis(String.valueOf(System.currentTimeMillis()));
|
|
|
FileStorage fs = SpringContextHolder.getBean(fsType.name().toLowerCase() + beanSuff, FileStorage.class);
|
|
|
- return fs.saveFile(siteId, env, file,md5);
|
|
|
+ return fs.saveFile(siteId, env, file, md5);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -140,7 +146,7 @@ public class FileStorageUtil {
|
|
|
* @return 可直接访问的文件地址
|
|
|
*/
|
|
|
public static String realPath(String path) {
|
|
|
- if(StringUtils.isBlank(path)) {
|
|
|
+ if (StringUtils.isBlank(path)) {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
@@ -151,10 +157,11 @@ public class FileStorageUtil {
|
|
|
path = diposeOldPath(path);
|
|
|
// 如果是全路径直接返回
|
|
|
if (path.startsWith("http") || path.startsWith("https")) {
|
|
|
- return path;
|
|
|
+ return replaceUpyunDomain(path);
|
|
|
}
|
|
|
// 根据路径头获取对应的处理类
|
|
|
- FileStorage fs = SpringContextHolder.getBean(FileStorageHelper.getHead(path).toLowerCase() + beanSuff, FileStorage.class);
|
|
|
+ FileStorage fs = SpringContextHolder.getBean(FileStorageHelper.getHead(path).toLowerCase() + beanSuff,
|
|
|
+ FileStorage.class);
|
|
|
// 返回处理类处理结果
|
|
|
return fs.realPath(path);
|
|
|
|
|
@@ -224,7 +231,7 @@ public class FileStorageUtil {
|
|
|
* @return 处理后的路径。补全路径头。
|
|
|
*/
|
|
|
private static String diposeOldPath(String path) {
|
|
|
- if(StringUtils.isBlank(path)) {
|
|
|
+ if (StringUtils.isBlank(path)) {
|
|
|
return null;
|
|
|
}
|
|
|
// 如果是全路径直接返回
|
|
@@ -242,7 +249,6 @@ public class FileStorageUtil {
|
|
|
return path;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 将网络文件保存到本地
|
|
|
*
|
|
@@ -273,40 +279,47 @@ public class FileStorageUtil {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- /**将网络文件保存到本地
|
|
|
- * @param fileUrl 网络文件URL
|
|
|
- * @param localFile 本地文件对象
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将网络文件保存到本地
|
|
|
+ *
|
|
|
+ * @param fileUrl 网络文件URL
|
|
|
+ * @param localFile 本地文件对象
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
- public static void saveUrlAs(String fileUrl, File localFile) {
|
|
|
+ public static void saveUrlAs(String fileUrl, File localFile) {
|
|
|
try {
|
|
|
saveUrlAs(fileUrl, localFile.getAbsolutePath());
|
|
|
} catch (IOException e) {
|
|
|
- throw new StatusException("5001", "下载文件出错",e);
|
|
|
+ throw new StatusException("5001", "下载文件出错", e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**获取指定云存储的签名
|
|
|
+ /**
|
|
|
+ * 获取指定云存储的签名
|
|
|
+ *
|
|
|
* @param siteId
|
|
|
* @param env
|
|
|
* @param md5
|
|
|
* @return
|
|
|
*/
|
|
|
- public static YunHttpRequest getSignature(FileStorageType fsType,String siteId, FileStoragePathEnvInfo env, String md5) {
|
|
|
+ public static YunHttpRequest getSignature(FileStorageType fsType, String siteId, FileStoragePathEnvInfo env,
|
|
|
+ String md5) {
|
|
|
env.setTimeMillis(String.valueOf(System.currentTimeMillis()));
|
|
|
// 获取对应的处理类
|
|
|
FileStorage fs = SpringContextHolder.getBean(fsType.name().toLowerCase() + beanSuff, FileStorage.class);
|
|
|
return fs.getSignature(siteId, env, md5);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- /**获取完整的不带域名的路径
|
|
|
+ /**
|
|
|
+ * 获取完整的不带域名的路径
|
|
|
+ *
|
|
|
* @param prefixPath 路径前缀
|
|
|
* @param sourcePath 数据库存储的路径
|
|
|
* @return 处理之后的路径
|
|
|
*/
|
|
|
- public static String getIntactPath(String prefixPath,String sourcePath) {
|
|
|
- if(StringUtils.isBlank(sourcePath)) {
|
|
|
+ public static String getIntactPath(String prefixPath, String sourcePath) {
|
|
|
+ if (StringUtils.isBlank(sourcePath)) {
|
|
|
return null;
|
|
|
}
|
|
|
// 如果是全路径直接返回
|
|
@@ -319,31 +332,33 @@ public class FileStorageUtil {
|
|
|
sourcePath = sourcePath.substring(1);
|
|
|
}
|
|
|
if (prefixPath.endsWith("/")) {
|
|
|
- prefixPath = prefixPath.substring(0,prefixPath.length());
|
|
|
+ prefixPath = prefixPath.substring(0, prefixPath.length());
|
|
|
}
|
|
|
- return prefixPath+"/"+sourcePath;
|
|
|
+ return prefixPath + "/" + sourcePath;
|
|
|
}
|
|
|
// 新协议无需处理
|
|
|
return sourcePath;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
- /**删除云存储文件
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除云存储文件
|
|
|
+ *
|
|
|
* @param path 全路径,包含根目录,含协议名
|
|
|
*/
|
|
|
public static void deleteFile(String path) {
|
|
|
- if(StringUtils.isBlank(path)) {
|
|
|
+ if (StringUtils.isBlank(path)) {
|
|
|
return;
|
|
|
}
|
|
|
- path=diposeOldPath(path);
|
|
|
+ path = diposeOldPath(path);
|
|
|
// 如果是全路径直接返回
|
|
|
if (path.startsWith("http") || path.startsWith("https")) {
|
|
|
return;
|
|
|
}
|
|
|
- FileStorage fs = SpringContextHolder.getBean(FileStorageHelper.getHead(path).toLowerCase() + beanSuff, FileStorage.class);
|
|
|
+ FileStorage fs = SpringContextHolder.getBean(FileStorageHelper.getHead(path).toLowerCase() + beanSuff,
|
|
|
+ FileStorage.class);
|
|
|
fs.deleteFile(path);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 获取文件访问路径(备用域名地址)
|
|
|
*
|
|
@@ -351,7 +366,7 @@ public class FileStorageUtil {
|
|
|
* @return 可直接访问的文件地址,如果没有配置备用地址,则返回主域名地址
|
|
|
*/
|
|
|
public static String realPathBackup(String path) {
|
|
|
- if(StringUtils.isBlank(path)) {
|
|
|
+ if (StringUtils.isBlank(path)) {
|
|
|
return null;
|
|
|
}
|
|
|
if (StringUtils.isBlank(path)) {
|
|
@@ -361,37 +376,53 @@ public class FileStorageUtil {
|
|
|
path = diposeOldPath(path);
|
|
|
// 如果是全路径直接返回
|
|
|
if (path.startsWith("http") || path.startsWith("https")) {
|
|
|
- return path;
|
|
|
+ return replaceUpyunDomain(path);
|
|
|
}
|
|
|
// 根据路径头获取对应的处理类
|
|
|
- FileStorage fs = SpringContextHolder.getBean(FileStorageHelper.getHead(path).toLowerCase() + beanSuff, FileStorage.class);
|
|
|
+ FileStorage fs = SpringContextHolder.getBean(FileStorageHelper.getHead(path).toLowerCase() + beanSuff,
|
|
|
+ FileStorage.class);
|
|
|
// 返回处理类处理结果
|
|
|
return fs.realPathBackup(path);
|
|
|
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
public static void initYunClient() {
|
|
|
UpyunSiteManager.initClient();
|
|
|
AliyunSiteManager.initClient();
|
|
|
}
|
|
|
+
|
|
|
public static void initYunSite() {
|
|
|
UpyunSiteManager.initSite();
|
|
|
AliyunSiteManager.initSite();
|
|
|
}
|
|
|
-
|
|
|
|
|
|
public static FileStorageType getFileStorageType() {
|
|
|
SysPropertyCacheBean spc = CacheHelper.getSysProperty("filestorage.type");
|
|
|
- if (spc == null || spc.getValue() == null) {
|
|
|
- throw new StatusException("10001", "未配置文件存储服务类型");
|
|
|
- }
|
|
|
- String type=(String)spc.getValue();
|
|
|
- try {
|
|
|
- return FileStorageType.valueOf(type);
|
|
|
+ if (spc == null || spc.getValue() == null) {
|
|
|
+ throw new StatusException("10001", "未配置文件存储服务类型");
|
|
|
+ }
|
|
|
+ String type = (String) spc.getValue();
|
|
|
+ try {
|
|
|
+ return FileStorageType.valueOf(type);
|
|
|
} catch (Exception e) {
|
|
|
throw new StatusException("10002", "配置文件存储服务类型错误");
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ private static String replaceUpyunDomain(String path) {
|
|
|
+ String oldUpyunDomain = "static.qmth.com.cn";
|
|
|
+ String oldUpyunBackupDomain = "static-backup.qmth.com.cn";
|
|
|
+ String replaceStr = null;
|
|
|
+ UpYunClient c = UpyunSiteManager.getUpYunClientByUpyunId("1");
|
|
|
+ if (path.startsWith(oldUpyunDomain)) {
|
|
|
+ replaceStr = c.getDomain();
|
|
|
+ }
|
|
|
+ if (path.startsWith(oldUpyunBackupDomain)) {
|
|
|
+ replaceStr = c.getDomainBackup();
|
|
|
+ }
|
|
|
+ path = path.replace("https://", "");
|
|
|
+ path = path.replace("http://", "");
|
|
|
+ path=path.substring(path.indexOf("/")+1);
|
|
|
+ return FileStorageHelper.getUrl(replaceStr, path);
|
|
|
+ }
|
|
|
}
|