refactor: pull kepubify & ffprobe during build (#50)

* build: pull kepubify & ffprobe during build

* refactor: use local ffprobe / kepubify
This commit is contained in:
James Ward
2026-03-20 17:20:18 -05:00
committed by James Cox
parent 4d82cb7188
commit 1c15629a10
6 changed files with 48 additions and 256 deletions
+26
View File
@@ -33,6 +33,30 @@ RUN set -eux; \
FROM linuxserver/unrar:7.1.10 AS unrar-layer
FROM mwader/static-ffmpeg:8.1 AS ffprobe-layer
FROM scratch AS kepubify-layer-amd64
ARG KEPUBIFY_VERSION="4.0.4"
ARG KEPUBIFY_AMD64_CHECKSUM="sha256:37d7628d26c5c906f607f24b36f781f306075e7073a6fe7820a751bb60431fc5"
ADD \
--checksum="${KEPUBIFY_AMD64_CHECKSUM}" \
--chmod=755 \
https://github.com/pgaskin/kepubify/releases/download/v${KEPUBIFY_VERSION}/kepubify-linux-64bit /kepubify
FROM scratch AS kepubify-layer-arm64
ARG KEPUBIFY_VERSION="4.0.4"
ARG KEPUBIFY_ARM64_CHECKSUM="sha256:5a15b8f6f6a96216c69330601bca29638cfee50f7bf48712795cff88ae2d03a3"
ADD \
--checksum="${KEPUBIFY_ARM64_CHECKSUM}" \
--chmod=755 \
https://github.com/pgaskin/kepubify/releases/download/v${KEPUBIFY_VERSION}/kepubify-linux-arm64 /kepubify
FROM kepubify-layer-${TARGETARCH} AS kepubify-layer
FROM eclipse-temurin:25-jre-alpine
ENV JAVA_TOOL_OPTIONS="-XX:+UseG1GC -XX:+UseCompactObjectHeaders -XX:+UseStringDeduplication -XX:MaxRAMPercentage=75.0 -XX:+ExitOnOutOfMemoryError"
@@ -43,6 +67,8 @@ COPY packaging/docker/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh
COPY --from=unrar-layer /usr/bin/unrar-alpine /usr/local/bin/unrar
COPY --from=ffprobe-layer /ffprobe /usr/local/bin/ffprobe
COPY --from=kepubify-layer /kepubify /usr/local/bin/kepubify
COPY --from=backend-build /workspace/booklore-api/app.jar /app/app.jar
@@ -6,10 +6,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.stream.Collectors;
@Slf4j
@@ -19,19 +16,15 @@ public class KepubConversionService {
@Autowired
private FileService fileService;
private static final String KEPUBIFY_GITHUB_BASE_URL = "https://github.com/booklore-app/booklore-tools/raw/main/kepubify/";
private static final String BIN_DARWIN_ARM64 = "kepubify-darwin-arm64";
private static final String BIN_DARWIN_X64 = "kepubify-darwin-64bit";
private static final String BIN_LINUX_X64 = "kepubify-linux-64bit";
private static final String BIN_LINUX_X86 = "kepubify-linux-32bit";
private static final String BIN_LINUX_ARM = "kepubify-linux-arm";
private static final String BIN_LINUX_ARM64 = "kepubify-linux-arm64";
public File convertEpubToKepub(File epubFile, File tempDir, boolean forceEnableHyphenation) throws IOException, InterruptedException {
validateInputs(epubFile);
Path kepubifyBinary = setupKepubifyBinary();
Path kepubifyBinary = fileService.findSystemFile("kepubify");
if (kepubifyBinary == null) {
throw new IOException("Kepubify conversion failed: could not find kepubify binary");
}
File outputFile = executeKepubifyConversion(epubFile, tempDir, kepubifyBinary, forceEnableHyphenation);
log.info("Successfully converted {} to {} (size: {} bytes)", epubFile.getName(), outputFile.getName(), outputFile.length());
@@ -44,60 +37,6 @@ public class KepubConversionService {
}
}
private Path setupKepubifyBinary() throws IOException {
String binaryName = getKepubifyBinaryName();
String toolsDirPath = fileService.getToolsKepubifyPath();
Path toolsDir = Paths.get(toolsDirPath);
if (!Files.exists(toolsDir)) {
Files.createDirectories(toolsDir);
}
Path binaryPath = toolsDir.resolve(binaryName);
if (!Files.exists(binaryPath)) {
String downloadUrl = KEPUBIFY_GITHUB_BASE_URL + binaryName;
log.info("Downloading kepubify binary '{}' from {}", binaryName, downloadUrl);
try (InputStream in = java.net.URI.create(downloadUrl).toURL().openStream()) {
Files.copy(in, binaryPath, StandardCopyOption.REPLACE_EXISTING);
}
if (!binaryPath.toFile().setExecutable(true)) {
log.warn("Failed to set executable permission for '{}'", binaryPath.toAbsolutePath());
}
log.info("Downloaded kepubify binary to {}", binaryPath.toAbsolutePath());
} else {
if (!binaryPath.toFile().setExecutable(true)) {
log.warn("Failed to set executable permission for '{}'", binaryPath.toAbsolutePath());
}
log.debug("Using existing kepubify binary at {}", binaryPath.toAbsolutePath());
}
return binaryPath;
}
private String getKepubifyBinaryName() {
String osName = System.getProperty("os.name").toLowerCase();
String osArch = System.getProperty("os.arch").toLowerCase();
log.debug("Detected OS: {} ({})", osName, osArch);
if (osName.contains("mac") || osName.contains("darwin")) {
if (osArch.contains("arm") || osArch.contains("aarch64")) {
return BIN_DARWIN_ARM64;
} else {
return BIN_DARWIN_X64;
}
} else if (osName.contains("linux")) {
if (osArch.contains("arm64") || osArch.contains("aarch64")) {
return BIN_LINUX_ARM64;
} else if (osArch.contains("arm")) {
return BIN_LINUX_ARM;
} else if (osArch.contains("64")) {
return BIN_LINUX_X64;
} else if (osArch.contains("86")) {
return BIN_LINUX_X86;
}
}
throw new IllegalStateException("Unsupported operating system or architecture: " + osName + " / " + osArch);
}
private File executeKepubifyConversion(File epubFile, File tempDir, Path kepubifyBinary, boolean forceEnableHyphenation) throws IOException, InterruptedException {
ProcessBuilder pb;
@@ -5,81 +5,16 @@ import lombok.extern.slf4j.Slf4j;
import org.booklore.util.FileService;
import org.springframework.stereotype.Service;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
@Slf4j
@Service
@AllArgsConstructor
public class FfprobeService {
private static final String FFPROBE_GITHUB_BASE_URL = "https://github.com/booklore-app/booklore-tools/raw/main/ffprobe/";
private static final String BIN_DARWIN_ARM64 = "ffprobe-darwin-arm64";
private static final String BIN_DARWIN_X64 = "ffprobe-darwin-64";
private static final String BIN_LINUX_X64 = "ffprobe-linux-64";
private static final String BIN_LINUX_ARM64 = "ffprobe-linux-arm64";
private final FileService fileService;
public Path getFfprobeBinary() {
try {
return setupFfprobeBinary();
} catch (Exception e) {
log.warn("Failed to set up ffprobe binary: {}", e.getMessage());
return null;
}
}
private Path setupFfprobeBinary() throws Exception {
String binaryName = getFfprobeBinaryName();
Path toolsDir = Paths.get(fileService.getToolsFfprobePath());
if (!Files.exists(toolsDir)) {
Files.createDirectories(toolsDir);
}
Path binaryPath = toolsDir.resolve(binaryName);
if (!Files.exists(binaryPath)) {
String downloadUrl = FFPROBE_GITHUB_BASE_URL + binaryName;
log.info("Downloading ffprobe binary '{}' from {}", binaryName, downloadUrl);
try (InputStream in = java.net.URI.create(downloadUrl).toURL().openStream()) {
Files.copy(in, binaryPath, StandardCopyOption.REPLACE_EXISTING);
}
if (!binaryPath.toFile().setExecutable(true)) {
log.warn("Failed to set executable permission for '{}'", binaryPath.toAbsolutePath());
}
log.info("Downloaded ffprobe binary to {}", binaryPath.toAbsolutePath());
} else {
if (!binaryPath.toFile().setExecutable(true)) {
log.warn("Failed to set executable permission for '{}'", binaryPath.toAbsolutePath());
}
log.debug("Using existing ffprobe binary at {}", binaryPath.toAbsolutePath());
}
return binaryPath;
}
private String getFfprobeBinaryName() {
String osName = System.getProperty("os.name").toLowerCase();
String osArch = System.getProperty("os.arch").toLowerCase();
log.debug("Detected OS: {} ({})", osName, osArch);
if (osName.contains("mac") || osName.contains("darwin")) {
if (osArch.contains("arm") || osArch.contains("aarch64")) {
return BIN_DARWIN_ARM64;
} else {
return BIN_DARWIN_X64;
}
} else if (osName.contains("linux")) {
if (osArch.contains("arm64") || osArch.contains("aarch64")) {
return BIN_LINUX_ARM64;
} else if (osArch.contains("64")) {
return BIN_LINUX_X64;
}
}
throw new IllegalStateException("Unsupported operating system or architecture: " + osName + " / " + osArch);
return fileService.findSystemFile("ffprobe");
}
}
@@ -148,12 +148,22 @@ public class FileService {
return Paths.get(appProperties.getPathConfig(), "bookdrop_temp", bookdropFileId + ".jpg").toString();
}
public String getToolsKepubifyPath() {
return Paths.get(appProperties.getPathConfig(), "tools", "kepubify").toString();
}
public Path findSystemFile(String filename) {
// Search first in the application folder's "local" `tools` / `bin` folders
// If not found in those, then search the system $PATH.
String systemSearchPath = System.getenv("PATH");
String searchPath = systemSearchPath == null ? "bin:tools" : "bin:tools:".concat(systemSearchPath);
String[] searchPaths = searchPath.split(":");
public String getToolsFfprobePath() {
return Paths.get(appProperties.getPathConfig(), "tools", "ffprobe").toString();
for (String path : searchPaths) {
Path possiblePath = Paths.get(path).resolve(filename).toAbsolutePath();
if (Files.isRegularFile(possiblePath)) {
return possiblePath;
}
}
return null;
}
@@ -1,108 +0,0 @@
package org.booklore.service.reader;
import org.booklore.util.FileService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class FfprobeServiceTest {
@Mock
FileService fileService;
@InjectMocks
FfprobeService ffprobeService;
@TempDir
Path tempDir;
@BeforeEach
void setUp() {
when(fileService.getToolsFfprobePath()).thenReturn(tempDir.resolve("tools/ffprobe").toString());
}
@Test
void getFfprobeBinary_returnsExistingBinary() throws IOException {
Path toolsDir = tempDir.resolve("tools/ffprobe");
Files.createDirectories(toolsDir);
String expectedName = getExpectedBinaryName();
Path binaryPath = toolsDir.resolve(expectedName);
Files.writeString(binaryPath, "fake-binary");
Path result = ffprobeService.getFfprobeBinary();
assertThat(result).isNotNull();
assertThat(result.getFileName().toString()).isEqualTo(expectedName);
assertThat(result).exists();
}
@Test
void getFfprobeBinary_createsDirectoryIfMissing() throws IOException {
Path toolsDir = tempDir.resolve("tools/ffprobe");
String expectedName = getExpectedBinaryName();
Path binaryPath = toolsDir.resolve(expectedName);
Files.createDirectories(toolsDir);
Files.writeString(binaryPath, "fake-binary");
Path result = ffprobeService.getFfprobeBinary();
assertThat(result).isNotNull();
assertThat(toolsDir).exists();
}
@Test
void getFfprobeBinary_returnsNullOnFailure() {
when(fileService.getToolsFfprobePath()).thenReturn("/nonexistent\0path");
Path result = ffprobeService.getFfprobeBinary();
assertThat(result).isNull();
}
@Test
void getFfprobeBinary_setsExecutablePermission() throws IOException {
Path toolsDir = tempDir.resolve("tools/ffprobe");
Files.createDirectories(toolsDir);
String expectedName = getExpectedBinaryName();
Path binaryPath = toolsDir.resolve(expectedName);
Files.writeString(binaryPath, "fake-binary");
Path result = ffprobeService.getFfprobeBinary();
assertThat(result).isNotNull();
assertThat(result.toFile().canExecute()).isTrue();
}
private String getExpectedBinaryName() {
String osName = System.getProperty("os.name").toLowerCase();
String osArch = System.getProperty("os.arch").toLowerCase();
if (osName.contains("mac") || osName.contains("darwin")) {
if (osArch.contains("arm") || osArch.contains("aarch64")) {
return "ffprobe-darwin-arm64";
}
return "ffprobe-darwin-64";
} else if (osName.contains("linux")) {
if (osArch.contains("arm64") || osArch.contains("aarch64")) {
return "ffprobe-linux-arm64";
}
return "ffprobe-linux-64";
}
throw new IllegalStateException("Unsupported OS for test: " + osName);
}
}
@@ -333,16 +333,6 @@ class FileServiceTest {
() -> assertTrue(result.endsWith("555.jpg"))
);
}
@Test
void getToolsKepubifyPath_returnsCorrectPath() {
String result = fileService.getToolsKepubifyPath();
assertAll(
() -> assertTrue(result.contains("tools")),
() -> assertTrue(result.contains("kepubify"))
);
}
}
}