New hashing
Some checks are pending
/ build (push) Waiting to run

Signed-off-by: Minecon724 <git@m724.eu>
This commit is contained in:
Minecon724 2025-03-04 16:24:35 +01:00
parent fff71d1140
commit d03f90b7f6
Signed by untrusted user who does not match committer: Minecon724
GPG key ID: 3CCC4D267742C8E8
2 changed files with 31 additions and 64 deletions

View file

@ -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<String, String>();
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
});

View file

@ -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<String, String> copyTree(Path srcDir, Path destDir) throws IOException {
public static Map<String, String> copyTree(Path srcDir, Path destDir) throws IOException {
var map = new HashMap<String, String>();
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());
}
}