mirror of https://github.com/halo-dev/halo
Fix handling root theme folder unexpectedly (#1081)
* Fix handling root theme folder unexpectedly * Ignore .git folder while finding root pathpull/1083/head
parent
d0568686c0
commit
845fe1114f
|
@ -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"
|
||||
|
|
|
@ -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<ThemeProperty> themeProperties = new ArrayList<>();
|
||||
|
||||
if (releases == null) {
|
||||
throw new ServiceException("主题拉取失败");
|
||||
throw new ServiceException("主题拉取失败!可能原因:当前服务器无法链接到对方服务器或连接超时。");
|
||||
}
|
||||
|
||||
releases.forEach(tagName -> {
|
||||
|
|
|
@ -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<String> 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
|
||||
|
|
|
@ -146,7 +146,7 @@ public class GithubUtils {
|
|||
private HashMap<String, Object> 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<String> 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<String, Object> 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;
|
||||
}
|
||||
|
|
|
@ -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<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");
|
||||
Files.createFile(file3);
|
||||
log.info("Prepared test folder structure");
|
||||
|
||||
// find the root folder where file3 locates, and we expect folder3
|
||||
Optional<Path> rootPath = FileUtils.findRootPath(tempDirectory, path -> path.getFileName().toString().equals("file3"));
|
||||
assertTrue(rootPath.isPresent());
|
||||
assertEquals(folder3.toString(), rootPath.get().toString());
|
||||
Optional<Path> rootPath = FileUtils.findRootPath(tempDirectory, path -> path.getFileName().toString().equals("expected_file"));
|
||||
assertFalse(rootPath.isPresent());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue