refactor(api): Use try-with-resources for better resource management (#1457)
- Use `try-with-resources` for better resource management in `EpubMetadataWriter` and `AppMigrationService`. - Use a single `HttpClient` instance in `GoogleParser` to improve efficiency. Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
+3
-4
@@ -35,6 +35,7 @@ public class GoogleParser implements BookParser {
|
|||||||
private static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\s+");
|
private static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\s+");
|
||||||
private static final Pattern SPECIAL_CHARACTERS_PATTERN = Pattern.compile("[.,\\-\\[\\]{}()!@#$%^&*_=+|~`<>?/\";:]");
|
private static final Pattern SPECIAL_CHARACTERS_PATTERN = Pattern.compile("[.,\\-\\[\\]{}()!@#$%^&*_=+|~`<>?/\";:]");
|
||||||
private final ObjectMapper objectMapper;
|
private final ObjectMapper objectMapper;
|
||||||
|
private final HttpClient httpClient = HttpClient.newHttpClient();
|
||||||
private static final String GOOGLE_BOOKS_API_URL = "https://www.googleapis.com/books/v1/volumes";
|
private static final String GOOGLE_BOOKS_API_URL = "https://www.googleapis.com/books/v1/volumes";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -61,13 +62,12 @@ public class GoogleParser implements BookParser {
|
|||||||
|
|
||||||
log.info("Google Books API URL (ISBN): {}", uri);
|
log.info("Google Books API URL (ISBN): {}", uri);
|
||||||
|
|
||||||
HttpClient client = HttpClient.newHttpClient();
|
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(uri)
|
.uri(uri)
|
||||||
.GET()
|
.GET()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
if (response.statusCode() == 200) {
|
if (response.statusCode() == 200) {
|
||||||
return parseGoogleBooksApiResponse(response.body());
|
return parseGoogleBooksApiResponse(response.body());
|
||||||
@@ -91,13 +91,12 @@ public class GoogleParser implements BookParser {
|
|||||||
|
|
||||||
log.info("Google Books API URL: {}", uri);
|
log.info("Google Books API URL: {}", uri);
|
||||||
|
|
||||||
HttpClient client = HttpClient.newHttpClient();
|
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(uri)
|
.uri(uri)
|
||||||
.GET()
|
.GET()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
if (response.statusCode() == 200) {
|
if (response.statusCode() == 200) {
|
||||||
return parseGoogleBooksApiResponse(response.body());
|
return parseGoogleBooksApiResponse(response.body());
|
||||||
|
|||||||
+17
-6
@@ -55,8 +55,9 @@ public class EpubMetadataWriter implements MetadataWriter {
|
|||||||
Path tempDir = null;
|
Path tempDir = null;
|
||||||
try {
|
try {
|
||||||
tempDir = Files.createTempDirectory("epub_edit_" + UUID.randomUUID());
|
tempDir = Files.createTempDirectory("epub_edit_" + UUID.randomUUID());
|
||||||
ZipFile zipFile = new ZipFile(epubFile);
|
try (ZipFile zipFile = new ZipFile(epubFile)) {
|
||||||
zipFile.extractAll(tempDir.toString());
|
zipFile.extractAll(tempDir.toString());
|
||||||
|
}
|
||||||
|
|
||||||
File opfFile = findOpfFile(tempDir.toFile());
|
File opfFile = findOpfFile(tempDir.toFile());
|
||||||
if (opfFile == null) {
|
if (opfFile == null) {
|
||||||
@@ -171,7 +172,9 @@ public class EpubMetadataWriter implements MetadataWriter {
|
|||||||
transformer.transform(new DOMSource(opfDoc), new StreamResult(opfFile));
|
transformer.transform(new DOMSource(opfDoc), new StreamResult(opfFile));
|
||||||
|
|
||||||
File tempEpub = new File(epubFile.getParentFile(), epubFile.getName() + ".tmp");
|
File tempEpub = new File(epubFile.getParentFile(), epubFile.getName() + ".tmp");
|
||||||
addFolderContentsToZip(new ZipFile(tempEpub), tempDir.toFile(), tempDir.toFile());
|
try (ZipFile tempZipFile = new ZipFile(tempEpub)) {
|
||||||
|
addFolderContentsToZip(tempZipFile, tempDir.toFile(), tempDir.toFile());
|
||||||
|
}
|
||||||
|
|
||||||
if (!epubFile.delete()) throw new IOException("Could not delete original EPUB");
|
if (!epubFile.delete()) throw new IOException("Could not delete original EPUB");
|
||||||
if (!tempEpub.renameTo(epubFile)) throw new IOException("Could not rename temp EPUB");
|
if (!tempEpub.renameTo(epubFile)) throw new IOException("Could not rename temp EPUB");
|
||||||
@@ -260,7 +263,9 @@ public class EpubMetadataWriter implements MetadataWriter {
|
|||||||
try {
|
try {
|
||||||
File epubFile = new File(bookEntity.getFullFilePath().toUri());
|
File epubFile = new File(bookEntity.getFullFilePath().toUri());
|
||||||
tempDir = Files.createTempDirectory("epub_cover_" + UUID.randomUUID());
|
tempDir = Files.createTempDirectory("epub_cover_" + UUID.randomUUID());
|
||||||
new ZipFile(epubFile).extractAll(tempDir.toString());
|
try (ZipFile zipFile = new ZipFile(epubFile)) {
|
||||||
|
zipFile.extractAll(tempDir.toString());
|
||||||
|
}
|
||||||
|
|
||||||
File opfFile = findOpfFile(tempDir.toFile());
|
File opfFile = findOpfFile(tempDir.toFile());
|
||||||
if (opfFile == null) {
|
if (opfFile == null) {
|
||||||
@@ -282,7 +287,9 @@ public class EpubMetadataWriter implements MetadataWriter {
|
|||||||
transformer.transform(new DOMSource(opfDoc), new StreamResult(opfFile));
|
transformer.transform(new DOMSource(opfDoc), new StreamResult(opfFile));
|
||||||
|
|
||||||
File tempEpub = new File(epubFile.getParentFile(), epubFile.getName() + ".tmp");
|
File tempEpub = new File(epubFile.getParentFile(), epubFile.getName() + ".tmp");
|
||||||
addFolderContentsToZip(new ZipFile(tempEpub), tempDir.toFile(), tempDir.toFile());
|
try (ZipFile tempZipFile = new ZipFile(tempEpub)) {
|
||||||
|
addFolderContentsToZip(tempZipFile, tempDir.toFile(), tempDir.toFile());
|
||||||
|
}
|
||||||
|
|
||||||
if (!epubFile.delete()) throw new IOException("Could not delete original EPUB");
|
if (!epubFile.delete()) throw new IOException("Could not delete original EPUB");
|
||||||
if (!tempEpub.renameTo(epubFile)) throw new IOException("Could not rename temp EPUB");
|
if (!tempEpub.renameTo(epubFile)) throw new IOException("Could not rename temp EPUB");
|
||||||
@@ -308,7 +315,9 @@ public class EpubMetadataWriter implements MetadataWriter {
|
|||||||
try {
|
try {
|
||||||
File epubFile = new File(bookEntity.getFullFilePath().toUri());
|
File epubFile = new File(bookEntity.getFullFilePath().toUri());
|
||||||
tempDir = Files.createTempDirectory("epub_cover_url_" + UUID.randomUUID());
|
tempDir = Files.createTempDirectory("epub_cover_url_" + UUID.randomUUID());
|
||||||
new ZipFile(epubFile).extractAll(tempDir.toString());
|
try (ZipFile zipFile = new ZipFile(epubFile)) {
|
||||||
|
zipFile.extractAll(tempDir.toString());
|
||||||
|
}
|
||||||
|
|
||||||
File opfFile = findOpfFile(tempDir.toFile());
|
File opfFile = findOpfFile(tempDir.toFile());
|
||||||
if (opfFile == null) {
|
if (opfFile == null) {
|
||||||
@@ -335,7 +344,9 @@ public class EpubMetadataWriter implements MetadataWriter {
|
|||||||
transformer.transform(new DOMSource(opfDoc), new StreamResult(opfFile));
|
transformer.transform(new DOMSource(opfDoc), new StreamResult(opfFile));
|
||||||
|
|
||||||
File tempEpub = new File(epubFile.getParentFile(), epubFile.getName() + ".tmp");
|
File tempEpub = new File(epubFile.getParentFile(), epubFile.getName() + ".tmp");
|
||||||
addFolderContentsToZip(new ZipFile(tempEpub), tempDir.toFile(), tempDir.toFile());
|
try (ZipFile tempZipFile = new ZipFile(tempEpub)) {
|
||||||
|
addFolderContentsToZip(tempZipFile, tempDir.toFile(), tempDir.toFile());
|
||||||
|
}
|
||||||
|
|
||||||
if (!epubFile.delete()) throw new IOException("Could not delete original EPUB");
|
if (!epubFile.delete()) throw new IOException("Could not delete original EPUB");
|
||||||
if (!tempEpub.renameTo(epubFile)) throw new IOException("Could not rename temp EPUB");
|
if (!tempEpub.renameTo(epubFile)) throw new IOException("Could not rename temp EPUB");
|
||||||
|
|||||||
+6
-4
@@ -127,8 +127,8 @@ public class AppMigrationService {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
if (Files.exists(thumbsDir)) {
|
if (Files.exists(thumbsDir)) {
|
||||||
Files.walk(thumbsDir)
|
try (var stream = Files.walk(thumbsDir)) {
|
||||||
.filter(Files::isRegularFile)
|
stream.filter(Files::isRegularFile)
|
||||||
.forEach(path -> {
|
.forEach(path -> {
|
||||||
try {
|
try {
|
||||||
// Load original image
|
// Load original image
|
||||||
@@ -160,14 +160,16 @@ public class AppMigrationService {
|
|||||||
throw new UncheckedIOException(e);
|
throw new UncheckedIOException(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Delete old thumbs directory
|
// Delete old thumbs directory
|
||||||
log.info("Deleting old thumbs directory: {}", thumbsDir);
|
log.info("Deleting old thumbs directory: {}", thumbsDir);
|
||||||
Files.walk(thumbsDir)
|
try (var stream = Files.walk(thumbsDir)) {
|
||||||
.sorted(Comparator.reverseOrder())
|
stream.sorted(Comparator.reverseOrder())
|
||||||
.map(Path::toFile)
|
.map(Path::toFile)
|
||||||
.forEach(File::delete);
|
.forEach(File::delete);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error("Error during migration populateCoversAndResizeThumbnails", e);
|
log.error("Error during migration populateCoversAndResizeThumbnails", e);
|
||||||
throw new UncheckedIOException(e);
|
throw new UncheckedIOException(e);
|
||||||
|
|||||||
Reference in New Issue
Block a user