diff --git a/build.gradle b/build.gradle index 9009d2bdf..b4caf799d 100644 --- a/build.gradle +++ b/build.gradle @@ -53,7 +53,7 @@ ext { commonsLangVersion = "3.10" httpclientVersion = "4.5.12" dataformatYamlVersion = "2.11.0" - jgitVersion = "5.7.0.202003110725-r" + jgitVersion = "5.9.0.202009080501-r" flexmarkVersion = "0.62.2" thumbnailatorVersion = "0.4.11" image4jVersion = "0.7zensight1" diff --git a/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java b/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java index b8de4630e..16fa3dc35 100644 --- a/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java @@ -547,7 +547,10 @@ public class ThemeServiceImpl implements ThemeService { downloadZipAndUnzip(zipUrl, themeTmpPath); - return add(themeTmpPath); + // find root theme folder + Path themeRootPath = getThemeRootPath(themeTmpPath); + log.debug("Got theme root path: [{}]", themeRootPath); + return add(themeRootPath); } catch (IOException e) { throw new ServiceException("主题拉取失败 " + uri, e); } finally { @@ -610,7 +613,7 @@ public class ThemeServiceImpl implements ThemeService { List themeProperties = new ArrayList<>(); if (releases == null) { - throw new ServiceException("主题拉取失败"); + throw new ServiceException("主题拉取失败!可能原因:当前服务器无法链接到对方服务器或连接超时。"); } releases.forEach(tagName -> { diff --git a/src/main/java/run/halo/app/utils/FileUtils.java b/src/main/java/run/halo/app/utils/FileUtils.java index fc7e0e342..9d106aff4 100644 --- a/src/main/java/run/halo/app/utils/FileUtils.java +++ b/src/main/java/run/halo/app/utils/FileUtils.java @@ -10,6 +10,7 @@ import run.halo.app.exception.ForbiddenException; import java.io.*; import java.nio.file.*; import java.nio.file.attribute.BasicFileAttributes; +import java.util.Arrays; import java.util.LinkedList; import java.util.List; import java.util.Optional; @@ -29,6 +30,11 @@ import java.util.zip.ZipOutputStream; @Slf4j public class FileUtils { + /** + * Ignored folders while finding root path. + */ + private static final List IGNORED_FOLDERS = Arrays.asList(".git"); + private FileUtils() { } @@ -256,7 +262,12 @@ public class FileUtils { return Optional.of(rootPath); } // add all folder into queue - subFolders.forEach(queue::push); + subFolders.forEach(e -> { + // if + if (!IGNORED_FOLDERS.contains(e.getFileName().toString())) { + queue.push(e); + } + }); } } // if tests are failed completely diff --git a/src/main/java/run/halo/app/utils/GithubUtils.java b/src/main/java/run/halo/app/utils/GithubUtils.java index 3eb9df8de..7763f170c 100644 --- a/src/main/java/run/halo/app/utils/GithubUtils.java +++ b/src/main/java/run/halo/app/utils/GithubUtils.java @@ -146,7 +146,7 @@ public class GithubUtils { private HashMap result; public GithubRelease(String repoUrl, String tagName) { - this.repoUrl = repoUrl; + this.repoUrl = StringUtils.removeEndIgnoreCase(repoUrl, ".git"); this.tagName = tagName; result = null; } @@ -206,8 +206,7 @@ public class GithubUtils { private List result; public GithubReleases(String repoUrl) { - this.repoUrl = repoUrl; - result = null; + this.repoUrl = StringUtils.removeEndIgnoreCase(repoUrl, ".git"); } @Override @@ -261,7 +260,7 @@ public class GithubUtils { private HashMap result; public GithubLatestRelease(String repoUrl) { - this.repoUrl = repoUrl; + this.repoUrl = StringUtils.removeEndIgnoreCase(repoUrl, ".git"); result = null; } @@ -323,7 +322,7 @@ public class GithubUtils { private String result; public GithubFile(String repoUrl, String branch) { - this.repoUrl = repoUrl; + this.repoUrl = StringUtils.removeEndIgnoreCase(repoUrl, ".git"); this.branch = branch; result = null; } diff --git a/src/test/java/run/halo/app/utils/FileUtilsTest.java b/src/test/java/run/halo/app/utils/FileUtilsTest.java index e2c2883ec..782356332 100644 --- a/src/test/java/run/halo/app/utils/FileUtilsTest.java +++ b/src/test/java/run/halo/app/utils/FileUtilsTest.java @@ -203,7 +203,7 @@ class FileUtilsTest { // folder2 // file2 // folder3 - // file3 + // expected_file // expected: folder2 tempDirectory = Files.createTempDirectory("halo-test"); @@ -218,13 +218,52 @@ class FileUtilsTest { Files.createFile(file2); Path folder3 = folder2.resolve("folder3"); Files.createDirectory(folder3); + Path expectedFile = folder3.resolve("expected_file"); + Files.createFile(expectedFile); + log.info("Prepared test folder structure"); + + // find the root folder where expected file locates, and we expect folder3 + Optional rootPath = FileUtils.findRootPath(tempDirectory, path -> path.getFileName().toString().equals("expected_file")); + assertTrue(rootPath.isPresent()); + assertEquals(folder3.toString(), rootPath.get().toString()); + } + + + @Test + void findRootPathIgnoreTest() throws IOException { + // build folder structure + // folder1 + // .git + // expected_file + // file1 + // folder2 + // file2 + // folder3 + // file3 + // expected: folder2 + tempDirectory = Files.createTempDirectory("halo-test"); + + log.info("Preparing test folder structure"); + Path folder1 = tempDirectory.resolve("folder1"); + Files.createDirectory(folder1); + Path dotGit = tempDirectory.resolve(".git"); + Files.createDirectory(dotGit); + Path expectedFile = dotGit.resolve("expected_file"); + Files.createFile(expectedFile); + Path file1 = tempDirectory.resolve("file1"); + Files.createFile(file1); + Path folder2 = tempDirectory.resolve("folder2"); + Files.createDirectory(folder2); + Path file2 = folder2.resolve("file2"); + Files.createFile(file2); + Path folder3 = folder2.resolve("folder3"); + Files.createDirectory(folder3); Path file3 = folder3.resolve("file3"); Files.createFile(file3); log.info("Prepared test folder structure"); // find the root folder where file3 locates, and we expect folder3 - Optional rootPath = FileUtils.findRootPath(tempDirectory, path -> path.getFileName().toString().equals("file3")); - assertTrue(rootPath.isPresent()); - assertEquals(folder3.toString(), rootPath.get().toString()); + Optional rootPath = FileUtils.findRootPath(tempDirectory, path -> path.getFileName().toString().equals("expected_file")); + assertFalse(rootPath.isPresent()); } } \ No newline at end of file