Fix BE to return epub data by book id

This commit is contained in:
aditya.chandel
2025-01-14 15:35:11 -07:00
parent dafa3f89d1
commit 442a095a4c
2 changed files with 10 additions and 11 deletions
@@ -1,25 +1,24 @@
package com.adityachandel.booklore.service; package com.adityachandel.booklore.service;
import com.adityachandel.booklore.exception.ApiError;
import com.adityachandel.booklore.model.entity.BookEntity;
import com.adityachandel.booklore.repository.BookRepository;
import lombok.AllArgsConstructor;
import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.ByteArrayResource;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
@AllArgsConstructor
@Service @Service
public class EpubService { public class EpubService {
// Method to retrieve the EPUB file as a ByteArrayResource private final BookRepository bookRepository;
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 public ByteArrayResource getEpubFile(Long bookId) throws IOException {
try (FileInputStream inputStream = new FileInputStream(file)) { BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
try (FileInputStream inputStream = new FileInputStream(bookEntity.getPath())) {
byte[] fileContent = inputStream.readAllBytes(); byte[] fileContent = inputStream.readAllBytes();
return new ByteArrayResource(fileContent); return new ByteArrayResource(fileContent);
} }
@@ -31,7 +31,7 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
private book: any; private book: any;
private rendition: any; private rendition: any;
private keyListener: (e: KeyboardEvent) => void = () => {}; private keyListener: (e: KeyboardEvent) => void = () => {};
fontSize: number = 100; fontSize: number = 120;
fontTypes: any[] = [ fontTypes: any[] = [
{label: 'Serif', value: 'serif'}, {label: 'Serif', value: 'serif'},