From 19517eca731df1edad610149267856b100c0d913 Mon Sep 17 00:00:00 2001 From: guqing <1484563614@qq.com> Date: Sat, 2 Apr 2022 15:41:53 +0800 Subject: [PATCH 1/2] fix: duplicate path separator in windows system --- .../app/handler/file/FilePathDescriptor.java | 10 ++++++- .../handler/file/FilePathDescriptorTest.java | 26 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/main/java/run/halo/app/handler/file/FilePathDescriptor.java b/src/main/java/run/halo/app/handler/file/FilePathDescriptor.java index 74baf73241..44edd5c94d 100644 --- a/src/main/java/run/halo/app/handler/file/FilePathDescriptor.java +++ b/src/main/java/run/halo/app/handler/file/FilePathDescriptor.java @@ -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); @@ -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. * diff --git a/src/test/java/run/halo/app/handler/file/FilePathDescriptorTest.java b/src/test/java/run/halo/app/handler/file/FilePathDescriptorTest.java index 7777b58ddf..a37cde4e8e 100644 --- a/src/test/java/run/halo/app/handler/file/FilePathDescriptorTest.java +++ b/src/test/java/run/halo/app/handler/file/FilePathDescriptorTest.java @@ -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 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"); + } } From b7745a811ab032cf63c4c048516eea7fb000116a Mon Sep 17 00:00:00 2001 From: guqing <1484563614@qq.com> Date: Sat, 2 Apr 2022 15:53:40 +0800 Subject: [PATCH 2/2] fix: code style --- .../run/halo/app/handler/file/FilePathDescriptorTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/run/halo/app/handler/file/FilePathDescriptorTest.java b/src/test/java/run/halo/app/handler/file/FilePathDescriptorTest.java index a37cde4e8e..0da12ff6aa 100644 --- a/src/test/java/run/halo/app/handler/file/FilePathDescriptorTest.java +++ b/src/test/java/run/halo/app/handler/file/FilePathDescriptorTest.java @@ -102,8 +102,8 @@ public void windowsSystem() { assertThat(descriptor).isNotNull(); - assertThat(descriptor.getFullPath()).isEqualTo("C:\\Users\\Halo NiuBi\\" + - ".halo\\upload\\2022\\04\\hello.jpg"); + 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\\");