Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Apr 2, 2022
Merged
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
10 changes: 9 additions & 1 deletion src/main/java/run/halo/app/handler/file/FilePathDescriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private String getPath(String first, String... more) {
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);
Expand All @@ -162,6 +162,14 @@ private String getPath(String first, String... more) {
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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -87,4 +88,29 @@ public void otherName() {
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");
}
}