Ensure temporary files are properly cleaned up after metadata updates (#1226)
This commit is contained in:
+68
-28
@@ -3,7 +3,11 @@ package com.adityachandel.booklore.service.metadata.writer;
|
|||||||
import com.adityachandel.booklore.model.MetadataClearFlags;
|
import com.adityachandel.booklore.model.MetadataClearFlags;
|
||||||
import com.adityachandel.booklore.model.entity.BookMetadataEntity;
|
import com.adityachandel.booklore.model.entity.BookMetadataEntity;
|
||||||
import com.adityachandel.booklore.model.enums.BookFileType;
|
import com.adityachandel.booklore.model.enums.BookFileType;
|
||||||
|
import com.github.junrar.Archive;
|
||||||
|
import com.github.junrar.rarfile.FileHeader;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
|
||||||
|
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
import org.w3c.dom.Element;
|
import org.w3c.dom.Element;
|
||||||
@@ -17,26 +21,19 @@ import javax.xml.transform.Transformer;
|
|||||||
import javax.xml.transform.TransformerFactory;
|
import javax.xml.transform.TransformerFactory;
|
||||||
import javax.xml.transform.dom.DOMSource;
|
import javax.xml.transform.dom.DOMSource;
|
||||||
import javax.xml.transform.stream.StreamResult;
|
import javax.xml.transform.stream.StreamResult;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.*;
|
||||||
import java.io.File;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.LinkOption;
|
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.StandardCopyOption;
|
import java.nio.file.StandardCopyOption;
|
||||||
import java.nio.file.StandardOpenOption;
|
import java.nio.file.StandardOpenOption;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
import java.util.Comparator;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipFile;
|
import java.util.zip.ZipFile;
|
||||||
import java.util.zip.ZipOutputStream;
|
import java.util.zip.ZipOutputStream;
|
||||||
import com.github.junrar.Archive;
|
|
||||||
import com.github.junrar.rarfile.FileHeader;
|
|
||||||
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
|
|
||||||
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
@@ -45,6 +42,8 @@ public class CbxMetadataWriter implements MetadataWriter {
|
|||||||
@Override
|
@Override
|
||||||
public void writeMetadataToFile(File file, BookMetadataEntity metadata, String thumbnailUrl, boolean restoreMode, MetadataClearFlags clearFlags) {
|
public void writeMetadataToFile(File file, BookMetadataEntity metadata, String thumbnailUrl, boolean restoreMode, MetadataClearFlags clearFlags) {
|
||||||
Path backup = null;
|
Path backup = null;
|
||||||
|
Path tempDir = null;
|
||||||
|
Path tempFile = null;
|
||||||
boolean writeSucceeded = false;
|
boolean writeSucceeded = false;
|
||||||
try {
|
try {
|
||||||
// Create a backup next to the source file (temp name, safe to delete later)
|
// Create a backup next to the source file (temp name, safe to delete later)
|
||||||
@@ -82,7 +81,8 @@ public class CbxMetadataWriter implements MetadataWriter {
|
|||||||
SevenZArchiveEntry existing = null;
|
SevenZArchiveEntry existing = null;
|
||||||
for (SevenZArchiveEntry e : sevenZ.getEntries()) {
|
for (SevenZArchiveEntry e : sevenZ.getEntries()) {
|
||||||
if (e != null && !e.isDirectory() && isComicInfoName(e.getName())) {
|
if (e != null && !e.isDirectory() && isComicInfoName(e.getName())) {
|
||||||
existing = e; break;
|
existing = e;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (existing != null) {
|
if (existing != null) {
|
||||||
@@ -147,18 +147,19 @@ public class CbxMetadataWriter implements MetadataWriter {
|
|||||||
|
|
||||||
// Repack depending on container type; always write to a temp target then atomic move
|
// Repack depending on container type; always write to a temp target then atomic move
|
||||||
if (isCbz) {
|
if (isCbz) {
|
||||||
Path temp = Files.createTempFile("cbx_edit", ".cbz");
|
tempFile = Files.createTempFile("cbx_edit", ".cbz");
|
||||||
repackZipReplacingComicInfo(file.toPath(), temp, xmlBytes);
|
repackZipReplacingComicInfo(file.toPath(), tempFile, xmlBytes);
|
||||||
atomicReplace(temp, file.toPath());
|
atomicReplace(tempFile, file.toPath());
|
||||||
|
tempFile = null; // Successfully moved, don't delete in finally
|
||||||
writeSucceeded = true;
|
writeSucceeded = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isCb7) {
|
if (isCb7) {
|
||||||
// Convert to CBZ with updated ComicInfo.xml
|
// Convert to CBZ with updated ComicInfo.xml
|
||||||
Path tempZip = Files.createTempFile("cbx_edit", ".cbz");
|
tempFile = Files.createTempFile("cbx_edit", ".cbz");
|
||||||
try (SevenZFile sevenZ = new SevenZFile(file);
|
try (SevenZFile sevenZ = new SevenZFile(file);
|
||||||
ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(tempZip))) {
|
ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(tempFile))) {
|
||||||
for (SevenZArchiveEntry e : sevenZ.getEntries()) {
|
for (SevenZArchiveEntry e : sevenZ.getEntries()) {
|
||||||
if (e.isDirectory()) continue;
|
if (e.isDirectory()) continue;
|
||||||
String entryName = e.getName();
|
String entryName = e.getName();
|
||||||
@@ -178,8 +179,12 @@ public class CbxMetadataWriter implements MetadataWriter {
|
|||||||
zos.closeEntry();
|
zos.closeEntry();
|
||||||
}
|
}
|
||||||
Path target = file.toPath().resolveSibling(stripExtension(file.getName()) + ".cbz");
|
Path target = file.toPath().resolveSibling(stripExtension(file.getName()) + ".cbz");
|
||||||
atomicReplace(tempZip, target);
|
atomicReplace(tempFile, target);
|
||||||
try { Files.deleteIfExists(file.toPath()); } catch (Exception ignored) {}
|
tempFile = null; // Successfully moved, don't delete in finally
|
||||||
|
try {
|
||||||
|
Files.deleteIfExists(file.toPath());
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
}
|
||||||
writeSucceeded = true;
|
writeSucceeded = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -189,7 +194,7 @@ public class CbxMetadataWriter implements MetadataWriter {
|
|||||||
boolean rarAvailable = isRarAvailable(rarBin);
|
boolean rarAvailable = isRarAvailable(rarBin);
|
||||||
|
|
||||||
if (rarAvailable) {
|
if (rarAvailable) {
|
||||||
Path tempDir = Files.createTempDirectory("cbx_rar_");
|
tempDir = Files.createTempDirectory("cbx_rar_");
|
||||||
try {
|
try {
|
||||||
// Extract entire RAR into a temp directory
|
// Extract entire RAR into a temp directory
|
||||||
try (Archive archive = new Archive(file)) {
|
try (Archive archive = new Archive(file)) {
|
||||||
@@ -234,20 +239,16 @@ public class CbxMetadataWriter implements MetadataWriter {
|
|||||||
log.warn("RAR creation failed with exit code {}. Falling back to CBZ conversion for {}", code, file.getName());
|
log.warn("RAR creation failed with exit code {}. Falling back to CBZ conversion for {}", code, file.getName());
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
try { // cleanup temp dir
|
// tempDir cleanup will be handled in outer finally block
|
||||||
java.nio.file.Files.walk(tempDir)
|
|
||||||
.sorted(java.util.Comparator.reverseOrder())
|
|
||||||
.forEach(path -> { try { Files.deleteIfExists(path); } catch (Exception ignore) {} });
|
|
||||||
} catch (Exception ignore) {}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.warn("`rar` binary not found. Falling back to CBZ conversion for {}", file.getName());
|
log.warn("`rar` binary not found. Falling back to CBZ conversion for {}", file.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback: convert the CBR to CBZ containing updated ComicInfo.xml
|
// Fallback: convert the CBR to CBZ containing updated ComicInfo.xml
|
||||||
Path tempZip = Files.createTempFile("cbx_edit", ".cbz");
|
tempFile = Files.createTempFile("cbx_edit", ".cbz");
|
||||||
try (Archive archive = new Archive(file);
|
try (Archive archive = new Archive(file);
|
||||||
ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(tempZip))) {
|
ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(tempFile))) {
|
||||||
for (FileHeader fh : archive.getFileHeaders()) {
|
for (FileHeader fh : archive.getFileHeaders()) {
|
||||||
if (fh.isDirectory()) continue;
|
if (fh.isDirectory()) continue;
|
||||||
String entryName = fh.getFileName();
|
String entryName = fh.getFileName();
|
||||||
@@ -265,8 +266,12 @@ public class CbxMetadataWriter implements MetadataWriter {
|
|||||||
zos.closeEntry();
|
zos.closeEntry();
|
||||||
}
|
}
|
||||||
Path target = file.toPath().resolveSibling(stripExtension(file.getName()) + ".cbz");
|
Path target = file.toPath().resolveSibling(stripExtension(file.getName()) + ".cbz");
|
||||||
atomicReplace(tempZip, target);
|
atomicReplace(tempFile, target);
|
||||||
try { Files.deleteIfExists(file.toPath()); } catch (Exception ignored) {}
|
tempFile = null; // Successfully moved, don't delete in finally
|
||||||
|
try {
|
||||||
|
Files.deleteIfExists(file.toPath());
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
}
|
||||||
writeSucceeded = true;
|
writeSucceeded = true;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// Attempt to restore the original file from backup
|
// Attempt to restore the original file from backup
|
||||||
@@ -280,8 +285,27 @@ public class CbxMetadataWriter implements MetadataWriter {
|
|||||||
}
|
}
|
||||||
log.warn("Failed to write metadata for {}: {}", file.getName(), e.getMessage(), e);
|
log.warn("Failed to write metadata for {}: {}", file.getName(), e.getMessage(), e);
|
||||||
} finally {
|
} finally {
|
||||||
|
// Clean up temporary file if it wasn't successfully moved
|
||||||
|
if (tempFile != null) {
|
||||||
|
try {
|
||||||
|
Files.deleteIfExists(tempFile);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("Failed to delete temp file: {}", tempFile, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean up temporary directory if it was created
|
||||||
|
if (tempDir != null) {
|
||||||
|
deleteDirectoryRecursively(tempDir);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean up backup file if write succeeded
|
||||||
if (writeSucceeded && backup != null) {
|
if (writeSucceeded && backup != null) {
|
||||||
try { Files.deleteIfExists(backup); } catch (Exception ignore) {}
|
try {
|
||||||
|
Files.deleteIfExists(backup);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("Failed to delete backup file: {}", backup, e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -451,4 +475,20 @@ public class CbxMetadataWriter implements MetadataWriter {
|
|||||||
public BookFileType getSupportedBookType() {
|
public BookFileType getSupportedBookType() {
|
||||||
return BookFileType.CBX;
|
return BookFileType.CBX;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void deleteDirectoryRecursively(Path dir) {
|
||||||
|
try (var pathStream = Files.walk(dir)) {
|
||||||
|
pathStream
|
||||||
|
.sorted(Comparator.reverseOrder())
|
||||||
|
.forEach(path -> {
|
||||||
|
try {
|
||||||
|
Files.delete(path);
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.warn("Failed to delete temp file/directory: {}", path, e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.warn("Failed to clean up temporary directory: {}", dir, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+30
-13
@@ -32,6 +32,7 @@ import java.net.URL;
|
|||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.StandardCopyOption;
|
import java.nio.file.StandardCopyOption;
|
||||||
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
@@ -51,7 +52,7 @@ public class EpubMetadataWriter implements MetadataWriter {
|
|||||||
log.warn("Failed to create backup of EPUB {}: {}", epubFile.getName(), ex.getMessage());
|
log.warn("Failed to create backup of EPUB {}: {}", epubFile.getName(), ex.getMessage());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Path tempDir;
|
Path tempDir = null;
|
||||||
try {
|
try {
|
||||||
tempDir = Files.createTempDirectory("epub_edit_" + UUID.randomUUID());
|
tempDir = Files.createTempDirectory("epub_edit_" + UUID.randomUUID());
|
||||||
ZipFile zipFile = new ZipFile(epubFile);
|
ZipFile zipFile = new ZipFile(epubFile);
|
||||||
@@ -190,6 +191,9 @@ public class EpubMetadataWriter implements MetadataWriter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
|
if (tempDir != null) {
|
||||||
|
deleteDirectoryRecursively(tempDir);
|
||||||
|
}
|
||||||
if (backupFile.exists()) {
|
if (backupFile.exists()) {
|
||||||
try {
|
try {
|
||||||
Files.delete(backupFile.toPath());
|
Files.delete(backupFile.toPath());
|
||||||
@@ -252,9 +256,10 @@ public class EpubMetadataWriter implements MetadataWriter {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Path tempDir = null;
|
||||||
try {
|
try {
|
||||||
File epubFile = new File(bookEntity.getFullFilePath().toUri());
|
File epubFile = new File(bookEntity.getFullFilePath().toUri());
|
||||||
Path tempDir = Files.createTempDirectory("epub_cover_" + UUID.randomUUID());
|
tempDir = Files.createTempDirectory("epub_cover_" + UUID.randomUUID());
|
||||||
new ZipFile(epubFile).extractAll(tempDir.toString());
|
new ZipFile(epubFile).extractAll(tempDir.toString());
|
||||||
|
|
||||||
File opfFile = findOpfFile(tempDir.toFile());
|
File opfFile = findOpfFile(tempDir.toFile());
|
||||||
@@ -286,6 +291,10 @@ public class EpubMetadataWriter implements MetadataWriter {
|
|||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("Failed to update EPUB with uploaded cover image: {}", e.getMessage(), e);
|
log.warn("Failed to update EPUB with uploaded cover image: {}", e.getMessage(), e);
|
||||||
|
} finally {
|
||||||
|
if (tempDir != null) {
|
||||||
|
deleteDirectoryRecursively(tempDir);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -295,9 +304,10 @@ public class EpubMetadataWriter implements MetadataWriter {
|
|||||||
log.warn("Cover update via URL failed: empty or null URL.");
|
log.warn("Cover update via URL failed: empty or null URL.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Path tempDir = null;
|
||||||
try {
|
try {
|
||||||
File epubFile = new File(bookEntity.getFullFilePath().toUri());
|
File epubFile = new File(bookEntity.getFullFilePath().toUri());
|
||||||
Path tempDir = Files.createTempDirectory("epub_cover_url_" + UUID.randomUUID());
|
tempDir = Files.createTempDirectory("epub_cover_url_" + UUID.randomUUID());
|
||||||
new ZipFile(epubFile).extractAll(tempDir.toString());
|
new ZipFile(epubFile).extractAll(tempDir.toString());
|
||||||
|
|
||||||
File opfFile = findOpfFile(tempDir.toFile());
|
File opfFile = findOpfFile(tempDir.toFile());
|
||||||
@@ -333,6 +343,10 @@ public class EpubMetadataWriter implements MetadataWriter {
|
|||||||
log.info("Cover image updated in EPUB via URL: {}", epubFile.getName());
|
log.info("Cover image updated in EPUB via URL: {}", epubFile.getName());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("Failed to update EPUB with cover from URL: {}", e.getMessage(), e);
|
log.warn("Failed to update EPUB with cover from URL: {}", e.getMessage(), e);
|
||||||
|
} finally {
|
||||||
|
if (tempDir != null) {
|
||||||
|
deleteDirectoryRecursively(tempDir);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -524,16 +538,19 @@ public class EpubMetadataWriter implements MetadataWriter {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getIdentifierByScheme(Element metadataElement, String scheme) {
|
private void deleteDirectoryRecursively(Path dir) {
|
||||||
NodeList identifiers = metadataElement.getElementsByTagNameNS("*", "identifier");
|
try (var pathStream = Files.walk(dir)) {
|
||||||
for (int i = 0; i < identifiers.getLength(); i++) {
|
pathStream
|
||||||
Element idElement = (Element) identifiers.item(i);
|
.sorted(Comparator.reverseOrder())
|
||||||
String schemeAttr = idElement.getAttributeNS(OPF_NS, "scheme");
|
.forEach(path -> {
|
||||||
if (scheme.equalsIgnoreCase(schemeAttr)) {
|
try {
|
||||||
return idElement.getTextContent();
|
Files.delete(path);
|
||||||
}
|
} catch (IOException e) {
|
||||||
|
log.warn("Failed to delete temp file/directory: {}", path, e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.warn("Failed to clean up temporary directory: {}", dir, e);
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user