fix(文件中心): 增加文件访问过期时间从1小时变为24小时,替换url,取消url后面的参数

This commit is contained in:
2025-07-14 15:26:44 +08:00
parent d8c3239a63
commit 04df758543
2 changed files with 32 additions and 15 deletions
@@ -163,7 +163,9 @@ public class OrderController {
for (OrderFile orderFile : list){ for (OrderFile orderFile : list){
Map<String,String> imgMap = new HashMap<>(); Map<String,String> imgMap = new HashMap<>();
String url = fileCenterService.getViewUrlByPath(orderFile.getFilePath()); String url = fileCenterService.getViewUrlByPath(orderFile.getFilePath());
imgMap.put("url", url.replace(cdnOssUrl,cdnUrl)); // 去掉URL中的参数并替换域名
String cleanUrl = removeUrlParams(url.replace(cdnOssUrl, cdnUrl));
imgMap.put("url", cleanUrl);
imgMap.put("contentType", orderFile.getFileType()); imgMap.put("contentType", orderFile.getFileType());
fileMap.put("path",orderFile.getFilePath()); fileMap.put("path",orderFile.getFilePath());
imgMap.put("viewUrl", fileCenterService.getViewUrlByPathStyle(fileMap)); imgMap.put("viewUrl", fileCenterService.getViewUrlByPathStyle(fileMap));
@@ -584,7 +586,9 @@ public class OrderController {
for (OrderFile orderFile : orderFiles) { for (OrderFile orderFile : orderFiles) {
Map<String, String> fileInfo = new HashMap<>(); Map<String, String> fileInfo = new HashMap<>();
String url = fileCenterService.getViewUrlByPath(orderFile.getFilePath()); String url = fileCenterService.getViewUrlByPath(orderFile.getFilePath());
fileInfo.put("url", url.replace(cdnOssUrl, cdnUrl)); // 去掉URL中的参数并替换域名
String cleanUrl = removeUrlParams(url.replace(cdnOssUrl, cdnUrl));
fileInfo.put("url", cleanUrl);
fileInfo.put("contentType", orderFile.getFileType()); fileInfo.put("contentType", orderFile.getFileType());
fileMap.put("path", orderFile.getFilePath()); fileMap.put("path", orderFile.getFilePath());
fileInfo.put("viewUrl", fileCenterService.getViewUrlByPathStyle(fileMap)); fileInfo.put("viewUrl", fileCenterService.getViewUrlByPathStyle(fileMap));
@@ -662,7 +666,9 @@ public class OrderController {
for (String orderFile : list){ for (String orderFile : list){
Map<String,String> imgMap = new HashMap<>(); Map<String,String> imgMap = new HashMap<>();
String url = fileCenterService.getViewUrlByPath(orderFile); String url = fileCenterService.getViewUrlByPath(orderFile);
imgMap.put("url", url.replace(cdnOssUrl,cdnUrl)); // 去掉URL中的参数并替换域名
String cleanUrl = removeUrlParams(url.replace(cdnOssUrl, cdnUrl));
imgMap.put("url", cleanUrl);
if (orderFile.endsWith(".jpeg") || orderFile.endsWith(".png")){ if (orderFile.endsWith(".jpeg") || orderFile.endsWith(".png")){
imgMap.put("contentType", "image/jpeg"); imgMap.put("contentType", "image/jpeg");
} }
@@ -681,4 +687,20 @@ public class OrderController {
} }
return new PageResult<>(); return new PageResult<>();
} }
/**
* 移除URL中的参数
* @param url 原始URL
* @return 移除参数后的URL
*/
private String removeUrlParams(String url) {
if (url == null || url.isEmpty()) {
return url;
}
int questionMarkIndex = url.indexOf('?');
if (questionMarkIndex != -1) {
return url.substring(0, questionMarkIndex);
}
return url;
}
} }
@@ -3,12 +3,7 @@ package com.central.oss.template;
import com.aliyun.oss.HttpMethod; import com.aliyun.oss.HttpMethod;
import com.aliyun.oss.OSS; import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GeneratePresignedUrlRequest; import com.aliyun.oss.model.*;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.ObjectListing;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.PutObjectRequest;
import com.central.oss.model.ObjectInfo; import com.central.oss.model.ObjectInfo;
import com.central.oss.properties.FileServerProperties; import com.central.oss.properties.FileServerProperties;
import lombok.SneakyThrows; import lombok.SneakyThrows;
@@ -38,7 +33,7 @@ public class OssTemplate implements InitializingBean {
private static final String DEF_CONTEXT_TYPE = "application/octet-stream"; private static final String DEF_CONTEXT_TYPE = "application/octet-stream";
private static final String PATH_SPLIT = "/"; private static final String PATH_SPLIT = "/";
private static final int DEFAULT_EXPIRE_TYPE = 60; private static final int DEFAULT_EXPIRE_TYPE = 60 * 24;
@Autowired @Autowired
private FileServerProperties fileProperties; private FileServerProperties fileProperties;