mirror of
https://github.com/booklore-app/booklore.git
synced 2026-02-18 00:17:53 +01:00
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:
@@ -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");
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user