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:
Balázs Szücs
2025-12-29 03:40:12 +01:00
committed by GitHub
parent 01446c2537
commit e65aa47552
2 changed files with 9 additions and 9 deletions

View File

@@ -12,7 +12,7 @@ import com.adityachandel.booklore.util.FileUtils;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
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.http.HttpHeaders;
import org.springframework.http.MediaType;
@@ -56,8 +56,8 @@ public class BookDownloadService {
throw ApiError.FAILED_TO_DOWNLOAD_FILE.createException(bookId);
}
InputStream inputStream = new FileInputStream(bookFile);
InputStreamResource resource = new InputStreamResource(inputStream);
// Use FileSystemResource which properly handles file resources and closing
Resource resource = new FileSystemResource(bookFile);
String encodedFilename = URLEncoder.encode(file.getFileName().toString(), StandardCharsets.UTF_8)
.replace("+", "%20");

View File

@@ -354,14 +354,14 @@ public class Fb2MetadataExtractor implements FileMetadataExtractor {
private InputStream getInputStream(File file) throws Exception {
FileInputStream fis = new FileInputStream(file);
try {
if (file.getName().toLowerCase().endsWith(".gz")) {
if (file.getName().toLowerCase().endsWith(".gz")) {
try {
return new GZIPInputStream(fis);
} catch (Exception e) {
fis.close();
throw e;
}
return fis;
} catch (Exception e) {
fis.close();
throw e;
}
return fis;
}
}