refactor: pull kepubify & ffprobe during build (#50)
* build: pull kepubify & ffprobe during build * refactor: use local ffprobe / kepubify
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user