Fix handling root theme folder unexpectedly (#1081)

* Fix handling root theme folder unexpectedly

* Ignore .git folder while finding root path
pull/1083/head
John Niang 2020-09-24 00:16:02 +08:00 committed by GitHub
parent d0568686c0
commit 845fe1114f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 65 additions and 13 deletions

View File

@ -53,7 +53,7 @@ ext {
commonsLangVersion = "3.10" commonsLangVersion = "3.10"
httpclientVersion = "4.5.12" httpclientVersion = "4.5.12"
dataformatYamlVersion = "2.11.0" dataformatYamlVersion = "2.11.0"
jgitVersion = "5.7.0.202003110725-r" jgitVersion = "5.9.0.202009080501-r"
flexmarkVersion = "0.62.2" flexmarkVersion = "0.62.2"
thumbnailatorVersion = "0.4.11" thumbnailatorVersion = "0.4.11"
image4jVersion = "0.7zensight1" image4jVersion = "0.7zensight1"

View File

@ -547,7 +547,10 @@ public class ThemeServiceImpl implements ThemeService {
downloadZipAndUnzip(zipUrl, themeTmpPath); 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) { } catch (IOException e) {
throw new ServiceException("主题拉取失败 " + uri, e); throw new ServiceException("主题拉取失败 " + uri, e);
} finally { } finally {
@ -610,7 +613,7 @@ public class ThemeServiceImpl implements ThemeService {
List<ThemeProperty> themeProperties = new ArrayList<>(); List<ThemeProperty> themeProperties = new ArrayList<>();
if (releases == null) { if (releases == null) {
throw new ServiceException("主题拉取失败"); throw new ServiceException("主题拉取失败!可能原因:当前服务器无法链接到对方服务器或连接超时。");
} }
releases.forEach(tagName -> { releases.forEach(tagName -> {

View File

@ -10,6 +10,7 @@ import run.halo.app.exception.ForbiddenException;
import java.io.*; import java.io.*;
import java.nio.file.*; import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.util.Arrays;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -29,6 +30,11 @@ import java.util.zip.ZipOutputStream;
@Slf4j @Slf4j
public class FileUtils { public class FileUtils {
/**
* Ignored folders while finding root path.
*/
private static final List<String> IGNORED_FOLDERS = Arrays.asList(".git");
private FileUtils() { private FileUtils() {
} }
@ -256,7 +262,12 @@ public class FileUtils {
return Optional.of(rootPath); return Optional.of(rootPath);
} }
// add all folder into queue // 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 // if tests are failed completely

View File

@ -146,7 +146,7 @@ public class GithubUtils {
private HashMap<String, Object> result; private HashMap<String, Object> result;
public GithubRelease(String repoUrl, String tagName) { public GithubRelease(String repoUrl, String tagName) {
this.repoUrl = repoUrl; this.repoUrl = StringUtils.removeEndIgnoreCase(repoUrl, ".git");
this.tagName = tagName; this.tagName = tagName;
result = null; result = null;
} }
@ -206,8 +206,7 @@ public class GithubUtils {
private List<String> result; private List<String> result;
public GithubReleases(String repoUrl) { public GithubReleases(String repoUrl) {
this.repoUrl = repoUrl; this.repoUrl = StringUtils.removeEndIgnoreCase(repoUrl, ".git");
result = null;
} }
@Override @Override
@ -261,7 +260,7 @@ public class GithubUtils {
private HashMap<String, Object> result; private HashMap<String, Object> result;
public GithubLatestRelease(String repoUrl) { public GithubLatestRelease(String repoUrl) {
this.repoUrl = repoUrl; this.repoUrl = StringUtils.removeEndIgnoreCase(repoUrl, ".git");
result = null; result = null;
} }
@ -323,7 +322,7 @@ public class GithubUtils {
private String result; private String result;
public GithubFile(String repoUrl, String branch) { public GithubFile(String repoUrl, String branch) {
this.repoUrl = repoUrl; this.repoUrl = StringUtils.removeEndIgnoreCase(repoUrl, ".git");
this.branch = branch; this.branch = branch;
result = null; result = null;
} }

View File

@ -203,7 +203,7 @@ class FileUtilsTest {
// folder2 // folder2
// file2 // file2
// folder3 // folder3
// file3 // expected_file
// expected: folder2 // expected: folder2
tempDirectory = Files.createTempDirectory("halo-test"); tempDirectory = Files.createTempDirectory("halo-test");
@ -218,13 +218,52 @@ class FileUtilsTest {
Files.createFile(file2); Files.createFile(file2);
Path folder3 = folder2.resolve("folder3"); Path folder3 = folder2.resolve("folder3");
Files.createDirectory(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<Path> 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"); Path file3 = folder3.resolve("file3");
Files.createFile(file3); Files.createFile(file3);
log.info("Prepared test folder structure"); log.info("Prepared test folder structure");
// find the root folder where file3 locates, and we expect folder3 // find the root folder where file3 locates, and we expect folder3
Optional<Path> rootPath = FileUtils.findRootPath(tempDirectory, path -> path.getFileName().toString().equals("file3")); Optional<Path> rootPath = FileUtils.findRootPath(tempDirectory, path -> path.getFileName().toString().equals("expected_file"));
assertTrue(rootPath.isPresent()); assertFalse(rootPath.isPresent());
assertEquals(folder3.toString(), rootPath.get().toString());
} }
} }