Pārlūkot izejas kodu

文件上传新增缓存参数

xiatian 3 gadi atpakaļ
vecāks
revīzija
3519432915

+ 22 - 1
examcloud-support/src/main/java/cn/com/qmth/examcloud/support/filestorage/FileStorageUtil.java

@@ -69,12 +69,17 @@ public class FileStorageUtil {
     public static YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, File file, String md5) {
         return saveFile(siteId, env, file, md5, false);
     }
+    
+    public static YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, File file, String md5,Long cacheAge) {
+        FileStorageType fsType = getFileStorageType();
+        return saveFile(siteId, env, file, fsType, md5, false,cacheAge);
+    }
 
     public static YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, File file, String md5, boolean refreshCDN) {
         FileStorageType fsType = getFileStorageType();
         return saveFile(siteId, env, file, fsType, md5, refreshCDN);
     }
-
+    
     /**
      * 保存文件
      *
@@ -155,6 +160,22 @@ public class FileStorageUtil {
         FileStorage fs = SpringContextHolder.getBean(fsType.name().toLowerCase() + beanSuff, FileStorage.class);
         return fs.saveFile(siteId, env, file, md5, refreshCDN);
     }
+    
+    private static YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, File file, FileStorageType fsType,
+            String md5, boolean refreshCDN,Long cacheAge) {
+		if (siteId == null) {
+		throw new StatusException("2000", "siteId是空");
+		}
+		if (file == null) {
+		throw new StatusException("2001", "文件是空");
+		}
+		if (env == null) {
+		throw new StatusException("2002", "文件上传路径信息是空");
+		}
+		env.setTimeMillis(String.valueOf(System.currentTimeMillis()));
+		FileStorage fs = SpringContextHolder.getBean(fsType.name().toLowerCase() + beanSuff, FileStorage.class);
+		return fs.saveFile(siteId, env, file, md5, refreshCDN,cacheAge);
+	}
 
     /**
      * 获取文件访问路径

+ 2 - 0
examcloud-web/src/main/java/cn/com/qmth/examcloud/web/filestorage/FileStorage.java

@@ -28,6 +28,8 @@ public interface FileStorage {
      * @return 返回路径
      */
     YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, File file, String md5, boolean refreshCDN);
+    
+    YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, File file, String md5, boolean refreshCDN,Long cacheAge);
 
     /**
      * 保存文件到存储服务

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

@@ -14,8 +14,6 @@ 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.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
 
 import java.io.*;
@@ -26,8 +24,6 @@ import java.util.Map;
 @Service(value = "aliyunFileStorage")
 public class AliyunFileStorageImpl implements FileStorage {
 
-    private static final Logger LOG = LoggerFactory.getLogger(AliyunFileStorageImpl.class);
-
     // 文件最大大小(byte)
     private static int maxFileSize = 100 * 1024 * 1024;
 
@@ -494,4 +490,55 @@ public class AliyunFileStorageImpl implements FileStorage {
         }
     }
 
+	@Override
+	public YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, File file, String md5, boolean refreshCDN,
+			Long cacheAge) {
+		try (InputStream in = new FileInputStream(file);) {
+            return saveFile(siteId, env, in, md5, refreshCDN,cacheAge);
+        } catch (Exception e) {
+            throw new StatusException("5001", "上传出错", e);
+        }
+	}
+	
+    private YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, InputStream in, String md5, boolean refreshCDN,Long cacheAge) {
+        try {
+            String relativePath = uploadObject(siteId, env, in, md5,cacheAge);
+            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);
+        }
+    }
+    
+    private String uploadObject(String siteId, FileStoragePathEnvInfo env, InputStream in, String md5,Long cacheAge) 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);
+            if(cacheAge!=null) {
+            	meta.setCacheControl("max-age="+cacheAge);
+            }
+            oss.putObject(bucket, path, in, meta);
+        } else {
+            oss.putObject(bucket, path, in);
+        }
+
+        return path;
+    }
+
 }

+ 10 - 0
examcloud-web/src/main/java/cn/com/qmth/examcloud/web/filestorage/impl/UpyunFileStorageImpl.java

@@ -113,4 +113,14 @@ public class UpyunFileStorageImpl implements FileStorage {
         return path;
     }
 
+	@Override
+	public YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, File file, String md5, boolean refreshCDN,
+			Long cacheAge) {
+		try (InputStream in = new FileInputStream(file);) {
+            return saveFile(siteId, env, in, md5, refreshCDN);
+        } catch (Exception e) {
+            throw new StatusException("1001", "上传出错", e);
+        }
+	}
+
 }