diff --git a/src/main/java/eu/m724/blog/BlogBuilder.java b/src/main/java/eu/m724/blog/BlogBuilder.java index 33fd76b..5f6cc3a 100644 --- a/src/main/java/eu/m724/blog/BlogBuilder.java +++ b/src/main/java/eu/m724/blog/BlogBuilder.java @@ -18,7 +18,6 @@ import java.nio.file.FileAlreadyExistsException; import java.nio.file.Files; import java.nio.file.Path; import java.util.*; -import java.util.concurrent.ThreadLocalRandom; import java.util.stream.Collectors; /** @@ -204,13 +203,8 @@ public class BlogBuilder { var fileHashes = new HashMap(); if (renderOptions.remapAssets()) { - var remapper = StaticCacheRemapper.fromGitRepository(workingDirectory); - if (remapper == null) { - LOGGER.warn("Site should be a Git directory"); // TODO like it isn't? - remapper = new StaticCacheRemapper(Integer.toHexString(ThreadLocalRandom.current().nextInt())); - } + var assetHashes = StaticCacheRemapper.copyTree(workingDirectory.resolve("assets"), outputDirectory.resolve("assets")); - var assetHashes = remapper.copyTree(workingDirectory.resolve("assets"), outputDirectory.resolve("assets")); assetHashes.forEach((k, v) -> { fileHashes.put("assets/" + k, v); // TODO this seems like a hack }); @@ -219,12 +213,8 @@ public class BlogBuilder { } if (renderOptions.remapTemplateStatic()) { - var remapper = StaticCacheRemapper.fromGitRepository(templateDirectory); - if (remapper == null) { - LOGGER.warn("Template should be a Git directory"); - remapper = new StaticCacheRemapper(Integer.toHexString(ThreadLocalRandom.current().nextInt())); - } - var templateStaticHashes = remapper.copyTree(templateDirectory.resolve("static"), outputDirectory.resolve("static")); + var templateStaticHashes = StaticCacheRemapper.copyTree(templateDirectory.resolve("static"), outputDirectory.resolve("static")); + templateStaticHashes.forEach((k, v) -> { fileHashes.put("static/" + k, v); // TODO this seems like a hack }); diff --git a/src/main/java/eu/m724/blog/StaticCacheRemapper.java b/src/main/java/eu/m724/blog/StaticCacheRemapper.java index db95a84..1c6b988 100644 --- a/src/main/java/eu/m724/blog/StaticCacheRemapper.java +++ b/src/main/java/eu/m724/blog/StaticCacheRemapper.java @@ -1,61 +1,16 @@ package eu.m724.blog; -import org.eclipse.jgit.api.Git; -import org.eclipse.jgit.api.errors.GitAPIException; -import org.eclipse.jgit.lib.RepositoryBuilder; - import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.HashMap; import java.util.Map; import java.util.stream.Collectors; +import java.util.zip.CRC32C; public class StaticCacheRemapper { - private final String revision; - public StaticCacheRemapper(String revision) { - this.revision = revision; - } - - public String getRevision() { - return revision; - } - - /** - * Creates a {@link StaticCacheRemapper} from a static files folder - * @param path the {@link Path} that points to the folder with the static files - * @return null if no Git repository at {@code path} - */ - public static StaticCacheRemapper fromGitRepository(Path path) { - path = path.toAbsolutePath(); - - var builder = new RepositoryBuilder() - .findGitDir(path.toFile()); - - if (builder.getGitDir() != null) { - var relativePath = builder.getGitDir().toPath().getParent().relativize(path); - - try ( - var repository = builder.build(); - var git = new Git(repository) - ) { - var log = git.log().addPath(relativePath.toString()).call(); - var commit = log.iterator().next(); - - if (commit != null) { - var commitIdShort = commit.getId().getName().substring(0, 10); // TODO maybe less than 10 is ok - return new StaticCacheRemapper(commitIdShort); - } - } catch (GitAPIException | IOException e) { - // TODO do something about it - } - } - - return null; - } - - public Map copyTree(Path srcDir, Path destDir) throws IOException { + public static Map copyTree(Path srcDir, Path destDir) throws IOException { var map = new HashMap(); try (var walk = Files.walk(srcDir)) { @@ -70,12 +25,13 @@ public class StaticCacheRemapper { Files.createDirectories(parent); } - var fileName = dest.getFileName().toString(); - fileName = insertHashInPath(fileName, revision); - dest = dest.resolveSibling(fileName); + var hash = hashFile(src); + + var filename = insertHashInPath(dest.getFileName().toString(), hash); + dest = dest.resolveSibling(filename); Files.copy(src, dest); - map.put(rel.toString(), revision); + map.put(rel.toString(), hash); } } } @@ -109,4 +65,25 @@ public class StaticCacheRemapper { return basePath + "." + hash + "." + extension; } + + /** + * CRC32C hashes a file. + * + * @param path the path with the file + * @return the hex-encoded hash + * @throws IOException couldn't open input stream + */ + private static String hashFile(Path path) throws IOException { + var crc = new CRC32C(); + + var buf = new byte[16384]; + try (var is = Files.newInputStream(path)) { + int read; + while ((read = is.read(buf)) != -1) { + crc.update(buf, 0, read); + } + } + + return Long.toHexString(crc.getValue()); + } }