-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1988 from liuliaozhong/3.3.x_issue812
fix: fast_transfer_file API 分发文件,如果源文件中的文件名包含空格,会报错 #812
- Loading branch information
Showing
7 changed files
with
143 additions
and
107 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
...mmons/common-utils/src/main/java/com/tencent/bk/job/common/util/FilePathValidateUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.tencent.bk.job.common.util; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* 文件路径合法性校验工具类 | ||
*/ | ||
public class FilePathValidateUtil { | ||
// 传统DOS正则表达式 | ||
private static final String CONVENTIONAL_DOS_PATH_REGEX = "(^[A-Za-z]:\\\\([^\\\\])(([^\\\\/:*?\"<>|])*\\\\?)*)|" + | ||
"(^[A-Za-z]:[\\\\])"; | ||
// Linux路径正则表达式 | ||
private static final String LINUX_PATH_REGEX = "^/(((../)*|(./)*)|(\\.?[^.].*/{0,1}))+"; | ||
|
||
// 传统DOS Pattern | ||
private static final Pattern CONVENTIONAL_DOS_PATH_PATTERN = Pattern.compile(CONVENTIONAL_DOS_PATH_REGEX); | ||
// Linux路径Pattern | ||
private static final Pattern LINUX_PATH_PATTERN = Pattern.compile(LINUX_PATH_REGEX); | ||
|
||
/** | ||
* 验证文件系统绝对路径的合法性 | ||
* @param path 绝对路径 | ||
* @return boolean true合法,false非法 | ||
*/ | ||
public static boolean validateFileSystemAbsolutePath(String path) { | ||
if (StringUtils.isBlank(path)) { | ||
return false; | ||
} | ||
if (isLinuxAbsolutePath(path)) { | ||
return validateLinuxFileSystemAbsolutePath(path); | ||
} else { | ||
return validateWindowsFileSystemAbsolutePath(path); | ||
} | ||
} | ||
|
||
/** | ||
* 判断是否Linux绝对路径 | ||
* | ||
* @param path 文件路径 | ||
* @return boolean | ||
*/ | ||
private static boolean isLinuxAbsolutePath(String path) { | ||
if (path.startsWith("/")) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* 1 传统DOS路径 | ||
* 标准的DOS路径可由以下三部分组成: | ||
* 1)卷号或驱动器号,后跟卷分隔符(:)。 | ||
* 2)目录名称。目录分隔符用来分隔嵌套目录层次结构中的子目录。 | ||
* 3)文件名。目录分隔符用来分隔文件路径和文件名。 | ||
* @param path | ||
* @return boolean | ||
*/ | ||
private static boolean validateWindowsFileSystemAbsolutePath(String path) { | ||
// 传统DOS | ||
if (CONVENTIONAL_DOS_PATH_PATTERN.matcher(path).matches()) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* 文件或目录名,除了/以外,所有的字符都合法 | ||
* | ||
* @param path | ||
* @return boolean | ||
*/ | ||
private static boolean validateLinuxFileSystemAbsolutePath(String path) { | ||
if (LINUX_PATH_PATTERN.matcher(path).matches()) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...s/common-utils/src/test/java/com/tencent/bk/job/common/util/FilePathValidateUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.tencent.bk.job.common.util; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
public class FilePathValidateUtilTest { | ||
@Test | ||
void testFileSystemAbsolutePath(){ | ||
// 传统DOS路径 | ||
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\Documents\\abc.txt")).isTrue(); | ||
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("c:\\Documents\\abc.txt")).isTrue(); | ||
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\Documents\\嘉 abc.txt")).isTrue(); | ||
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath(":\\abc.txt")).isFalse(); | ||
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:")).isFalse(); | ||
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\\\")).isFalse(); | ||
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\")).isTrue(); | ||
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\logs\\..\\access.log")).isTrue(); | ||
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\.config\\conf")).isTrue(); | ||
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\user\\abc>a")).isFalse(); | ||
|
||
// linux路径 | ||
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/data/test_2022-04-12.apk")).isTrue(); | ||
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/data/test_2022 04 12.apk")).isTrue(); | ||
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/")).isTrue(); | ||
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/tmp/")).isTrue(); | ||
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/tmp/.conf/abc")).isTrue(); | ||
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/tmp/test/../test.log")).isTrue(); | ||
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("data/test_2022-04-12.apk")).isFalse(); | ||
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("///")).isTrue(); // 根目录 | ||
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/tmp////")).isTrue(); // /tmp/ | ||
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/tmp//test/")).isTrue();// /tmp/test/ | ||
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("///")).isTrue(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters