refactor(api): improve file resource handling in book download and FB2 metadata extraction (#2018)
Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
+3
-3
@@ -12,7 +12,7 @@ import com.adityachandel.booklore.util.FileUtils;
|
|||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.core.io.InputStreamResource;
|
import org.springframework.core.io.FileSystemResource;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
@@ -56,8 +56,8 @@ public class BookDownloadService {
|
|||||||
throw ApiError.FAILED_TO_DOWNLOAD_FILE.createException(bookId);
|
throw ApiError.FAILED_TO_DOWNLOAD_FILE.createException(bookId);
|
||||||
}
|
}
|
||||||
|
|
||||||
InputStream inputStream = new FileInputStream(bookFile);
|
// Use FileSystemResource which properly handles file resources and closing
|
||||||
InputStreamResource resource = new InputStreamResource(inputStream);
|
Resource resource = new FileSystemResource(bookFile);
|
||||||
|
|
||||||
String encodedFilename = URLEncoder.encode(file.getFileName().toString(), StandardCharsets.UTF_8)
|
String encodedFilename = URLEncoder.encode(file.getFileName().toString(), StandardCharsets.UTF_8)
|
||||||
.replace("+", "%20");
|
.replace("+", "%20");
|
||||||
|
|||||||
+3
-3
@@ -354,14 +354,14 @@ public class Fb2MetadataExtractor implements FileMetadataExtractor {
|
|||||||
|
|
||||||
private InputStream getInputStream(File file) throws Exception {
|
private InputStream getInputStream(File file) throws Exception {
|
||||||
FileInputStream fis = new FileInputStream(file);
|
FileInputStream fis = new FileInputStream(file);
|
||||||
try {
|
|
||||||
if (file.getName().toLowerCase().endsWith(".gz")) {
|
if (file.getName().toLowerCase().endsWith(".gz")) {
|
||||||
|
try {
|
||||||
return new GZIPInputStream(fis);
|
return new GZIPInputStream(fis);
|
||||||
}
|
|
||||||
return fis;
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
fis.close();
|
fis.close();
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return fis;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user