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 java.awt.image.BufferedImage;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Paths;
|
||||
@@ -33,7 +34,10 @@ public class BackgroundUploadService {
|
||||
String extension = getFileExtension(originalFilename);
|
||||
String filename = "1." + extension;
|
||||
|
||||
BufferedImage originalImage = ImageIO.read(file.getInputStream());
|
||||
BufferedImage originalImage;
|
||||
try (InputStream inputStream = file.getInputStream()) {
|
||||
originalImage = ImageIO.read(inputStream);
|
||||
}
|
||||
if (originalImage == null) {
|
||||
throw new IllegalArgumentException("Invalid image file");
|
||||
}
|
||||
|
||||
+4
-1
@@ -63,7 +63,10 @@ public class EpubProcessor extends AbstractFileProcessor implements BookFileProc
|
||||
public boolean generateCover(BookEntity bookEntity) {
|
||||
try {
|
||||
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();
|
||||
|
||||
if (coverImage == null) {
|
||||
|
||||
+6
-3
@@ -121,9 +121,12 @@ public class KepubConversionService {
|
||||
}
|
||||
|
||||
private String readProcessOutput(InputStream inputStream) {
|
||||
return new BufferedReader(new InputStreamReader(inputStream))
|
||||
.lines()
|
||||
.collect(Collectors.joining("\n"));
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
|
||||
return reader.lines().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) {
|
||||
|
||||
+2
-2
@@ -30,8 +30,8 @@ public class EpubMetadataExtractor implements FileMetadataExtractor {
|
||||
|
||||
@Override
|
||||
public byte[] extractCover(File epubFile) {
|
||||
try {
|
||||
Book epub = new EpubReader().readEpub(new FileInputStream(epubFile));
|
||||
try (FileInputStream fis = new FileInputStream(epubFile)) {
|
||||
Book epub = new EpubReader().readEpub(fis);
|
||||
io.documentnode.epub4j.domain.Resource coverImage = epub.getCoverImage();
|
||||
|
||||
if (coverImage == null) {
|
||||
|
||||
@@ -31,6 +31,9 @@ import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
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.Path;
|
||||
import java.nio.file.Paths;
|
||||
@@ -197,7 +200,10 @@ public class FileService {
|
||||
public void createThumbnailFromFile(long bookId, MultipartFile file) {
|
||||
try {
|
||||
validateCoverFile(file);
|
||||
BufferedImage originalImage = ImageIO.read(file.getInputStream());
|
||||
BufferedImage originalImage;
|
||||
try (InputStream inputStream = file.getInputStream()) {
|
||||
originalImage = ImageIO.read(inputStream);
|
||||
}
|
||||
if (originalImage == null) {
|
||||
throw ApiError.IMAGE_NOT_FOUND.createException();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user