From 24681c81c09022f584a57006f2534b5f74723414 Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期二, 30 六月 2026 09:27:31 +0800
Subject: [PATCH] 初始化项目
---
yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java | 39 +++++++++++++++++++++++++++++++++------
1 files changed, 33 insertions(+), 6 deletions(-)
diff --git a/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java b/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java
index 639c689..7fe36cd 100644
--- a/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java
+++ b/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java
@@ -15,6 +15,7 @@
import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO;
import cn.iocoder.yudao.module.infra.dal.mysql.file.FileMapper;
import cn.iocoder.yudao.module.infra.framework.file.core.client.FileClient;
+import cn.iocoder.yudao.module.infra.framework.file.core.utils.FilePathUtils;
import cn.iocoder.yudao.module.infra.framework.file.core.utils.FileTypeUtils;
import com.google.common.annotations.VisibleForTesting;
import jakarta.annotation.Resource;
@@ -70,11 +71,14 @@
@Override
@SneakyThrows
public String createFile(byte[] content, String name, String directory, String type) {
- // 1.1 澶勭悊 type 涓虹┖鐨勬儏鍐�
+ // 1.1 澶勭悊 name 鐨勫悎娉曟�э紝绂佹鎼哄甫鐩綍璺緞
+ name = FilePathUtils.validateFileName(name);
+
+ // 1.2.1 澶勭悊 type 涓虹┖鐨勬儏鍐�
if (StrUtil.isEmpty(type)) {
type = FileTypeUtils.getMineType(content, name);
}
- // 1.2 澶勭悊 name 涓虹┖鐨勬儏鍐�
+ // 1.2.2 澶勭悊 name 涓虹┖鐨勬儏鍐�
if (StrUtil.isEmpty(name)) {
name = DigestUtil.sha256Hex(content);
}
@@ -102,7 +106,11 @@
@VisibleForTesting
String generateUploadPath(String name, String directory) {
- // 1. 鐢熸垚鍓嶇紑銆佸悗缂�
+ // 1.1 澶勭悊 name 鍜� directory 鐨勫悎娉曟��
+ name = FilePathUtils.validateFileName(name);
+ FilePathUtils.validatePath(name);
+ FilePathUtils.validateDirectory(directory);
+ // 1.2 鐢熸垚鍓嶇紑銆佸悗缂�
String prefix = null;
if (PATH_PREFIX_DATE_ENABLE) {
prefix = LocalDateTimeUtil.format(LocalDateTimeUtil.now(), PURE_DATE_PATTERN);
@@ -159,7 +167,13 @@
@Override
public Long createFile(FileCreateReqVO createReqVO) {
+ // 1.1 鏍¢獙鍙傛暟鐨勫悎娉曟��
+ FilePathUtils.validatePath(createReqVO.getPath());
+ createReqVO.setName(FilePathUtils.validateFileName(createReqVO.getName()));
+ // 1.2 澶勭悊 URL 鐨勫悎娉曟�э紝绉婚櫎 URL 涓殑鏌ヨ鍙傛暟锛堜緥濡傜鍚嶅弬鏁帮級锛屼繚璇� URL 鐨勫敮涓�鎬�
createReqVO.setUrl(HttpUtils.removeUrlQuery(createReqVO.getUrl())); // 鐩殑锛氱Щ闄ょ鏈夋《鎯呭喌涓嬶紝URL 鐨勭鍚嶅弬鏁�
+
+ // 2. 淇濆瓨鍒版暟鎹簱
FileDO file = BeanUtils.toBean(createReqVO, FileDO.class);
fileMapper.insert(file);
return file.getId();
@@ -172,15 +186,17 @@
@Override
public void deleteFile(Long id) throws Exception {
- // 鏍¢獙瀛樺湪
+ // 1.1 鏍¢獙瀛樺湪
FileDO file = validateFileExists(id);
+ // 1.2 鏍¢獙璺緞鍚堟硶鎬э紝閬垮厤璇垹鏂囦欢瀛樺偍鍣ㄤ腑鐨勫叾浠栨枃浠�
+ FilePathUtils.validatePath(file.getPath());
- // 浠庢枃浠跺瓨鍌ㄥ櫒涓垹闄�
+ // 2.1 浠庢枃浠跺瓨鍌ㄥ櫒涓垹闄�
FileClient client = fileConfigService.getFileClient(file.getConfigId());
Assert.notNull(client, "瀹㈡埛绔�({}) 涓嶈兘涓虹┖", file.getConfigId());
client.delete(file.getPath());
- // 鍒犻櫎璁板綍
+ // 2.2 鍒犻櫎璁板綍
fileMapper.deleteById(id);
}
@@ -190,6 +206,7 @@
// 鍒犻櫎鏂囦欢
List<FileDO> files = fileMapper.selectByIds(ids);
for (FileDO file : files) {
+ FilePathUtils.validatePath(file.getPath());
// 鑾峰彇瀹㈡埛绔�
FileClient client = fileConfigService.getFileClient(file.getConfigId());
Assert.notNull(client, "瀹㈡埛绔�({}) 涓嶈兘涓虹┖", file.getPath());
@@ -211,9 +228,19 @@
@Override
public byte[] getFileContent(Long configId, String path) throws Exception {
+ // 1. 鏍¢獙璺緞鍚堟硶鎬�
+ FilePathUtils.validatePath(path);
+
+ // 2.1 鑾峰彇瀹㈡埛绔�
FileClient client = fileConfigService.getFileClient(configId);
Assert.notNull(client, "瀹㈡埛绔�({}) 涓嶈兘涓虹┖", configId);
+ // 2.2 鑾峰彇鏂囦欢鍐呭
return client.getContent(path);
}
+ @Override
+ public FileDO getFileByConfigIdAndPath(Long configId, String path) {
+ return fileMapper.selectLatestByConfigIdAndPath(configId, path);
+ }
+
}
--
Gitblit v1.9.3