WIP: ePub support

This commit is contained in:
aditya.chandel
2025-01-14 11:29:15 -07:00
parent b0cd19189b
commit ceb344677c
10 changed files with 633 additions and 10 deletions
+2
View File
@@ -55,6 +55,8 @@ dependencies {
// MapStruct
implementation 'org.mapstruct:mapstruct:1.6.3'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.6.3'
implementation 'io.documentnode:epub4j-core:4.2.2'
}
hibernate {
@@ -0,0 +1,29 @@
package com.adityachandel.booklore.controller;
import com.adityachandel.booklore.service.EpubService;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
@RestController
@RequestMapping("/api/epub")
public class EpubController {
private final EpubService epubService;
public EpubController(EpubService epubService) {
this.epubService = epubService;
}
// New endpoint to serve the entire EPUB file
@GetMapping("/{bookId}/download")
public ResponseEntity<ByteArrayResource> downloadEpub(@PathVariable Long bookId) throws IOException {
ByteArrayResource epubFile = epubService.getEpubFile(bookId);
return ResponseEntity.ok()
.header("Content-Type", "application/epub+zip")
.header("Content-Disposition", "attachment; filename=\"book.epub\"")
.body(epubFile);
}
}
@@ -0,0 +1,27 @@
package com.adityachandel.booklore.service;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
@Service
public class EpubService {
// Method to retrieve the EPUB file as a ByteArrayResource
public ByteArrayResource getEpubFile(Long bookId) throws IOException {
String bookPath = "/Users/aditya.chandel/Downloads/Harry1.epub"; // Example path
File file = new File(bookPath);
if (!file.exists()) {
throw new IOException("EPUB file not found for book id: " + bookId);
}
// Convert the EPUB file to a byte array
try (FileInputStream inputStream = new FileInputStream(file)) {
byte[] fileContent = inputStream.readAllBytes();
return new ByteArrayResource(fileContent);
}
}
}