Fix the problem of duplicate path separator in windows system (#1812)

* fix: duplicate path separator in windows system

* fix: code style
pull/1813/head
guqing 2022-04-02 18:40:40 +08:00 committed by GitHub
parent 6a2602eef2
commit 1de5799f82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 1 deletions

View File

@ -151,7 +151,7 @@ public final class FilePathDescriptor {
sb.append(first);
for (String segment : more) {
if (StringUtils.isNotBlank(segment)) {
if (sb.length() > 0) {
if (sb.length() > 0 && !endsWith(sb, separator)) {
sb.append(separator);
}
sb.append(segment);
@ -162,6 +162,14 @@ public final class FilePathDescriptor {
return path;
}
static boolean endsWith(StringBuilder sb, String str) {
Assert.notNull(sb, "The stringBuilder must not be null.");
Assert.notNull(str, "The str must not be null.");
int len = sb.length();
int strLen = str.length();
return (len >= strLen && sb.substring(len - strLen).equals(str));
}
/**
* build file path object.
*

View File

@ -1,5 +1,6 @@
package run.halo.app.handler.file;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static run.halo.app.model.support.HaloConst.FILE_SEPARATOR;
@ -87,4 +88,29 @@ public class FilePathDescriptorTest {
assertEquals("/home/halo/2021/10/1.4.9.png", descriptor.getFullPath());
assertEquals("2021/10/1.4.9.png", descriptor.getRelativePath());
}
@Test
public void windowsSystem() {
FilePathDescriptor descriptor = new FilePathDescriptor.Builder()
.setBasePath("C:\\Users\\Halo NiuBi\\.halo\\")
.setSubPath("upload\\2022\\04\\")
.setSeparator("\\")
.setAutomaticRename(false)
.setRenamePredicate(builder -> true)
.setOriginalName("hello.jpg")
.build();
assertThat(descriptor).isNotNull();
assertThat(descriptor.getFullPath()).isEqualTo("C:\\Users\\Halo NiuBi\\"
+ ".halo\\upload\\2022\\04\\hello.jpg");
assertThat(descriptor.getRelativePath()).isEqualTo("upload\\2022\\04\\hello.jpg");
assertThat(descriptor.getBasePath()).isEqualTo("C:\\Users\\Halo NiuBi\\.halo\\");
assertThat(descriptor.getSubPath()).isEqualTo("upload\\2022\\04\\");
assertThat(descriptor.getExtension()).isEqualTo("jpg");
assertThat(descriptor.getName()).isEqualTo("hello");
assertThat(descriptor.getFullName()).isEqualTo("hello.jpg");
}
}