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