Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class FileUploadReqVO {
@AssertTrue(message = "文件目录不正确")
@JsonIgnore
public boolean isDirectoryValid() {
return !StrUtil.containsAny(directory, "..", "/", "\\");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果是这样,改成 startwith

.. 不能包含
/ 和 \ 不能 startWith

简洁一点。好理解一些。

return !StrUtil.containsAny(directory, "..", "\\");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class AppFileUploadReqVO {
@AssertTrue(message = "文件目录不正确")
@JsonIgnore
public boolean isDirectoryValid() {
return !StrUtil.containsAny(directory, "..", "/", "\\");
return !StrUtil.containsAny(directory, "..", "\\");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePresignedUrlRespVO;
import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO;
import jakarta.validation.constraints.NotEmpty;
import org.jspecify.annotations.Nullable;

import java.util.List;

Expand Down Expand Up @@ -34,7 +35,7 @@ public interface FileService {
* @return 文件路径
*/
String createFile(@NotEmpty(message = "文件内容不能为空") byte[] content,
String name, String directory, String type);
String name, @Nullable String directory, String type);

/**
* 生成文件预签名地址信息,用于上传
Expand All @@ -45,10 +46,11 @@ String createFile(@NotEmpty(message = "文件内容不能为空") byte[] content
*/
FilePresignedUrlRespVO presignPutUrl(@NotEmpty(message = "文件名不能为空") String name,
String directory);

/**
* 生成文件预签名地址信息,用于读取
*
* @param url 完整的文件访问地址
* @param url 完整的文件访问地址
* @param expirationSeconds 访问有效期,单位秒
* @return 文件预签名地址
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.common.annotations.VisibleForTesting;
import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import org.jspecify.annotations.Nullable;
import org.springframework.stereotype.Service;

import java.util.List;
Expand All @@ -36,13 +37,13 @@ public class FileServiceImpl implements FileService {

/**
* 上传文件的前缀,是否包含日期(yyyyMMdd)
*
* <p>
* 目的:按照日期,进行分目录
*/
static boolean PATH_PREFIX_DATE_ENABLE = true;
/**
* 上传文件的后缀,是否包含时间戳
*
* <p>
* 目的:保证文件的唯一性,避免覆盖
* 定制:可按需调整成 UUID、或者其他方式
*/
Expand All @@ -61,7 +62,7 @@ public PageResult<FileDO> getFilePage(FilePageReqVO pageReqVO) {

@Override
@SneakyThrows
public String createFile(byte[] content, String name, String directory, String type) {
public String createFile(byte[] content, String name, @Nullable String directory, String type) {
// 1.1 处理 type 为空的情况
if (StrUtil.isEmpty(type)) {
type = FileTypeUtils.getMineType(content, name);
Expand Down Expand Up @@ -93,7 +94,7 @@ public String createFile(byte[] content, String name, String directory, String t
}

@VisibleForTesting
String generateUploadPath(String name, String directory) {
String generateUploadPath(String name, @Nullable String directory) {
// 1. 生成前缀、后缀
String prefix = null;
if (PATH_PREFIX_DATE_ENABLE) {
Expand All @@ -117,6 +118,11 @@ String generateUploadPath(String name, String directory) {
if (StrUtil.isNotEmpty(prefix)) {
name = prefix + StrUtil.SLASH + name;
}
if (directory != null) {
directory = directory.replaceAll("/+", "/");
if (directory.startsWith("/")) directory = directory.substring(1);
if (directory.endsWith("/")) directory = directory.substring(0, directory.length() - 1);
}
// 2.3 最后拼接 directory 目录
if (StrUtil.isNotEmpty(directory)) {
name = directory + StrUtil.SLASH + name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public void testGetFileContent() throws Exception {
public void testGenerateUploadPath_AllEnabled() {
// 准备参数
String name = "test.jpg";
String directory = "avatar";
String directory = "/avatar";
FileServiceImpl.PATH_PREFIX_DATE_ENABLE = true;
FileServiceImpl.PATH_SUFFIX_TIMESTAMP_ENABLE = true;

Expand All @@ -201,16 +201,16 @@ public void testGenerateUploadPath_AllEnabled() {

// 断言
// 格式为:avatar/yyyyMMdd/test_timestamp.jpg
assertTrue(path.startsWith(directory + "/"));
assertTrue(path.startsWith("avatar/"));
// 包含日期格式:8 位数字,如 20240517
assertTrue(path.matches(directory + "/\\d{8}/test_\\d+\\.jpg"));
assertTrue(path.matches("avatar/\\d{8}/test_\\d+\\.jpg"));
}

@Test
public void testGenerateUploadPath_PrefixEnabled_SuffixDisabled() {
// 准备参数
String name = "test.jpg";
String directory = "avatar";
String directory = "avatar//"; // 模拟前端填写路径时出现失误
FileServiceImpl.PATH_PREFIX_DATE_ENABLE = true;
FileServiceImpl.PATH_SUFFIX_TIMESTAMP_ENABLE = false;

Expand All @@ -219,9 +219,9 @@ public void testGenerateUploadPath_PrefixEnabled_SuffixDisabled() {

// 断言
// 格式为:avatar/yyyyMMdd/test.jpg
assertTrue(path.startsWith(directory + "/"));
assertTrue(path.startsWith("avatar/"));
// 包含日期格式:8 位数字,如 20240517
assertTrue(path.matches(directory + "/\\d{8}/test\\.jpg"));
assertTrue(path.matches("avatar/\\d{8}/test\\.jpg"));
}

@Test
Expand Down