mirror of https://github.com/halo-dev/halo
Fix the problem of duplicate path separator in windows system (#1812)
* fix: duplicate path separator in windows system * fix: code stylepull/1813/head
parent
6a2602eef2
commit
1de5799f82
|
@ -151,7 +151,7 @@ public final class FilePathDescriptor {
|
||||||
sb.append(first);
|
sb.append(first);
|
||||||
for (String segment : more) {
|
for (String segment : more) {
|
||||||
if (StringUtils.isNotBlank(segment)) {
|
if (StringUtils.isNotBlank(segment)) {
|
||||||
if (sb.length() > 0) {
|
if (sb.length() > 0 && !endsWith(sb, separator)) {
|
||||||
sb.append(separator);
|
sb.append(separator);
|
||||||
}
|
}
|
||||||
sb.append(segment);
|
sb.append(segment);
|
||||||
|
@ -162,6 +162,14 @@ public final class FilePathDescriptor {
|
||||||
return path;
|
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.
|
* build file path object.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package run.halo.app.handler.file;
|
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.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||||
import static run.halo.app.model.support.HaloConst.FILE_SEPARATOR;
|
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("/home/halo/2021/10/1.4.9.png", descriptor.getFullPath());
|
||||||
assertEquals("2021/10/1.4.9.png", descriptor.getRelativePath());
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue