Save epub progress

This commit is contained in:
aditya.chandel
2025-01-14 17:25:09 -07:00
parent e9a42ac473
commit 6e42548fbb
5 changed files with 61 additions and 27 deletions
@@ -2,9 +2,9 @@ package com.adityachandel.booklore.controller;
import com.adityachandel.booklore.model.dto.Book; import com.adityachandel.booklore.model.dto.Book;
import com.adityachandel.booklore.model.dto.BookViewerSetting; import com.adityachandel.booklore.model.dto.BookViewerSetting;
import com.adityachandel.booklore.model.dto.request.ReadProgressRequest;
import com.adityachandel.booklore.model.dto.request.ShelvesAssignmentRequest; import com.adityachandel.booklore.model.dto.request.ShelvesAssignmentRequest;
import com.adityachandel.booklore.service.BooksService; import com.adityachandel.booklore.service.BooksService;
import com.adityachandel.booklore.service.metadata.BookMetadataService;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
@@ -20,7 +20,6 @@ import java.util.List;
public class BookController { public class BookController {
private BooksService booksService; private BooksService booksService;
private BookMetadataService bookMetadataService;
@GetMapping("/{bookId}") @GetMapping("/{bookId}")
public ResponseEntity<Book> getBook(@PathVariable long bookId, @RequestParam(required = false, defaultValue = "false") boolean withDescription) { public ResponseEntity<Book> getBook(@PathVariable long bookId, @RequestParam(required = false, defaultValue = "false") boolean withDescription) {
@@ -68,4 +67,10 @@ public class BookController {
public ResponseEntity<List<Book>> addBookToShelf(@RequestBody @Valid ShelvesAssignmentRequest request) { public ResponseEntity<List<Book>> addBookToShelf(@RequestBody @Valid ShelvesAssignmentRequest request) {
return ResponseEntity.ok(booksService.assignShelvesToBooks(request.getBookIds(), request.getShelvesToAssign(), request.getShelvesToUnassign())); return ResponseEntity.ok(booksService.assignShelvesToBooks(request.getBookIds(), request.getShelvesToAssign(), request.getShelvesToUnassign()));
} }
@PostMapping("/progress")
public ResponseEntity<Void> addBookToProgress(@RequestBody @Valid ReadProgressRequest request) {
booksService.updateReadProgress(request);
return ResponseEntity.noContent().build();
}
} }
@@ -0,0 +1,18 @@
package com.adityachandel.booklore.model.dto.request;
import jakarta.validation.constraints.AssertTrue;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
@Data
public class ReadProgressRequest {
@NotNull
private Long bookId;
private String epubProgress;
private Integer pdfProgress;
@AssertTrue(message = "Either epubProgress or pdfProgress must be provided")
public boolean isProgressValid() {
return epubProgress != null || pdfProgress != null;
}
}
@@ -6,14 +6,17 @@ import com.adityachandel.booklore.mapper.BookViewerSettingMapper;
import com.adityachandel.booklore.model.dto.Book; import com.adityachandel.booklore.model.dto.Book;
import com.adityachandel.booklore.model.dto.BookViewerSetting; import com.adityachandel.booklore.model.dto.BookViewerSetting;
import com.adityachandel.booklore.model.dto.BookWithNeighbors; import com.adityachandel.booklore.model.dto.BookWithNeighbors;
import com.adityachandel.booklore.model.dto.request.ReadProgressRequest;
import com.adityachandel.booklore.model.entity.BookEntity; import com.adityachandel.booklore.model.entity.BookEntity;
import com.adityachandel.booklore.model.entity.BookViewerSettingEntity; import com.adityachandel.booklore.model.entity.BookViewerSettingEntity;
import com.adityachandel.booklore.model.entity.ShelfEntity; import com.adityachandel.booklore.model.entity.ShelfEntity;
import com.adityachandel.booklore.model.enums.BookFileType;
import com.adityachandel.booklore.repository.BookRepository; import com.adityachandel.booklore.repository.BookRepository;
import com.adityachandel.booklore.repository.BookViewerSettingRepository; import com.adityachandel.booklore.repository.BookViewerSettingRepository;
import com.adityachandel.booklore.repository.LibraryRepository; import com.adityachandel.booklore.repository.LibraryRepository;
import com.adityachandel.booklore.repository.ShelfRepository; import com.adityachandel.booklore.repository.ShelfRepository;
import com.adityachandel.booklore.util.FileService; import com.adityachandel.booklore.util.FileService;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
@@ -131,4 +134,15 @@ public class BooksService {
BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId)); BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
return fileService.getBookCover(bookEntity.getMetadata().getThumbnail()); return fileService.getBookCover(bookEntity.getMetadata().getThumbnail());
} }
public void updateReadProgress(ReadProgressRequest request) {
BookEntity book = bookRepository.findById(request.getBookId()).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(request.getBookId()));
book.setLastReadTime(Instant.now());
if (book.getBookType() == BookFileType.EPUB) {
book.setEpubProgress(request.getEpubProgress());
} else if (book.getBookType() == BookFileType.PDF) {
book.setPdfProgress(request.getPdfProgress());
}
bookRepository.save(book);
}
} }
@@ -159,6 +159,9 @@ export class BookService {
}); });
} }
saveEpubProgress(bookId: number, progress: string): Observable<void> {
return this.http.post<void>(`${this.url}/progress`, { bookId: bookId, epubProgress: progress });
}
/*------------------ All the websocket handlers go below ------------------*/ /*------------------ All the websocket handlers go below ------------------*/
@@ -10,6 +10,7 @@ import {DropdownModule} from 'primeng/dropdown';
import {ActivatedRoute} from '@angular/router'; import {ActivatedRoute} from '@angular/router';
import {Book} from '../../book/model/book.model'; import {Book} from '../../book/model/book.model';
import {BookService} from '../../book/service/book.service'; import {BookService} from '../../book/service/book.service';
import {forkJoin} from 'rxjs';
@Component({ @Component({
selector: 'app-epub-viewer', selector: 'app-epub-viewer',
@@ -28,7 +29,8 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
private keyListener: (e: KeyboardEvent) => void = () => { private keyListener: (e: KeyboardEvent) => void = () => {
}; };
fontSize: number = 120; fontSize: number = 120;
bookLoreBook: Book | undefined
epub!: Book;
fontTypes: any[] = [ fontTypes: any[] = [
{label: 'Serif', value: 'serif'}, {label: 'Serif', value: 'serif'},
@@ -44,20 +46,11 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
ngOnInit(): void { ngOnInit(): void {
this.route.paramMap.subscribe((params) => { this.route.paramMap.subscribe((params) => {
this.loadBook(+params.get('bookId')!) let book$ = this.bookService.getBookByIdFromAPI(+params.get('bookId')!, false);
this.loadEpub(+params.get('bookId')!); let epub$ = this.epubService.downloadEpub(+params.get('bookId')!);
}); forkJoin([book$, epub$]).subscribe(results => {
} this.epub = results[0];
private loadBook(bookId: number): void {
this.bookService.getBookById(bookId).subscribe((book) => {
this.bookLoreBook = book;
});
}
loadEpub(bookId: number): void {
this.epubService.downloadEpub(bookId).subscribe(
(data: Blob) => {
const fileReader = new FileReader(); const fileReader = new FileReader();
fileReader.onload = () => { fileReader.onload = () => {
const epubData = fileReader.result as ArrayBuffer; const epubData = fileReader.result as ArrayBuffer;
@@ -77,8 +70,8 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
allowScriptedContent: true, allowScriptedContent: true,
}); });
if(this.bookLoreBook?.epubProgress) { if (this.epub?.epubProgress) {
this.rendition.display(this.bookLoreBook?.epubProgress); this.rendition.display(this.epub.epubProgress);
} else { } else {
this.rendition.display(); this.rendition.display();
} }
@@ -87,12 +80,10 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
this.updateFontSize(); this.updateFontSize();
this.trackProgress(); this.trackProgress();
}; };
fileReader.readAsArrayBuffer(data); fileReader.readAsArrayBuffer(results[1]);
}, })
(error) => {
console.error('Failed to load the EPUB:', error); });
}
);
} }
ngOnDestroy(): void { ngOnDestroy(): void {
@@ -105,9 +96,12 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
private trackProgress(): void { private trackProgress(): void {
if (this.rendition) { if (this.rendition) {
this.rendition.on('relocated', (location: any) => { this.rendition.on('relocated', (location: any) => {
const percentage = location.start.percentage.toFixed(2); this.bookService.saveEpubProgress(this.epub.id, location.start.cfi).subscribe({
console.log(`Reading progress: ${percentage * 100}%`); next: () => {
console.log('Current location:', location); },
error: () => {
}
})
}); });
} }
} }