refactor: use try-with-resources for better resource management in file processing (#1607)
Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> Co-authored-by: Aditya Chandel <8075870+adityachandelgit@users.noreply.github.com>
This commit is contained in:
co-authored by
Aditya Chandel
parent
57199004ca
commit
3f3b72248a
+5
-1
@@ -9,6 +9,7 @@ import org.springframework.web.multipart.MultipartFile;
|
|||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
@@ -33,7 +34,10 @@ public class BackgroundUploadService {
|
|||||||
String extension = getFileExtension(originalFilename);
|
String extension = getFileExtension(originalFilename);
|
||||||
String filename = "1." + extension;
|
String filename = "1." + extension;
|
||||||
|
|
||||||
BufferedImage originalImage = ImageIO.read(file.getInputStream());
|
BufferedImage originalImage;
|
||||||
|
try (InputStream inputStream = file.getInputStream()) {
|
||||||
|
originalImage = ImageIO.read(inputStream);
|
||||||
|
}
|
||||||
if (originalImage == null) {
|
if (originalImage == null) {
|
||||||
throw new IllegalArgumentException("Invalid image file");
|
throw new IllegalArgumentException("Invalid image file");
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-1
@@ -63,7 +63,10 @@ public class EpubProcessor extends AbstractFileProcessor implements BookFileProc
|
|||||||
public boolean generateCover(BookEntity bookEntity) {
|
public boolean generateCover(BookEntity bookEntity) {
|
||||||
try {
|
try {
|
||||||
File epubFile = new File(FileUtils.getBookFullPath(bookEntity));
|
File epubFile = new File(FileUtils.getBookFullPath(bookEntity));
|
||||||
io.documentnode.epub4j.domain.Book epub = new EpubReader().readEpub(new FileInputStream(epubFile));
|
io.documentnode.epub4j.domain.Book epub;
|
||||||
|
try (FileInputStream fis = new FileInputStream(epubFile)) {
|
||||||
|
epub = new EpubReader().readEpub(fis);
|
||||||
|
}
|
||||||
Resource coverImage = epub.getCoverImage();
|
Resource coverImage = epub.getCoverImage();
|
||||||
|
|
||||||
if (coverImage == null) {
|
if (coverImage == null) {
|
||||||
|
|||||||
+6
-3
@@ -121,9 +121,12 @@ public class KepubConversionService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String readProcessOutput(InputStream inputStream) {
|
private String readProcessOutput(InputStream inputStream) {
|
||||||
return new BufferedReader(new InputStreamReader(inputStream))
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
|
||||||
.lines()
|
return reader.lines().collect(Collectors.joining("\n"));
|
||||||
.collect(Collectors.joining("\n"));
|
} catch (Exception e) {
|
||||||
|
log.warn("Error reading process output: {}", e.getMessage());
|
||||||
|
return "";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void logProcessResults(int exitCode, String output, String error) {
|
private void logProcessResults(int exitCode, String output, String error) {
|
||||||
|
|||||||
+2
-2
@@ -30,8 +30,8 @@ public class EpubMetadataExtractor implements FileMetadataExtractor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] extractCover(File epubFile) {
|
public byte[] extractCover(File epubFile) {
|
||||||
try {
|
try (FileInputStream fis = new FileInputStream(epubFile)) {
|
||||||
Book epub = new EpubReader().readEpub(new FileInputStream(epubFile));
|
Book epub = new EpubReader().readEpub(fis);
|
||||||
io.documentnode.epub4j.domain.Resource coverImage = epub.getCoverImage();
|
io.documentnode.epub4j.domain.Resource coverImage = epub.getCoverImage();
|
||||||
|
|
||||||
if (coverImage == null) {
|
if (coverImage == null) {
|
||||||
|
|||||||
@@ -31,6 +31,9 @@ import java.awt.image.BufferedImage;
|
|||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.net.URI;
|
||||||
|
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.Paths;
|
import java.nio.file.Paths;
|
||||||
@@ -197,7 +200,10 @@ public class FileService {
|
|||||||
public void createThumbnailFromFile(long bookId, MultipartFile file) {
|
public void createThumbnailFromFile(long bookId, MultipartFile file) {
|
||||||
try {
|
try {
|
||||||
validateCoverFile(file);
|
validateCoverFile(file);
|
||||||
BufferedImage originalImage = ImageIO.read(file.getInputStream());
|
BufferedImage originalImage;
|
||||||
|
try (InputStream inputStream = file.getInputStream()) {
|
||||||
|
originalImage = ImageIO.read(inputStream);
|
||||||
|
}
|
||||||
if (originalImage == null) {
|
if (originalImage == null) {
|
||||||
throw ApiError.IMAGE_NOT_FOUND.createException();
|
throw ApiError.IMAGE_NOT_FOUND.createException();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user