Files
grimmory/booklore-ui/src/app/features/book/service/book.service.ts
T
ACXandacx10 3f334202b6 Introduce multi-format book support, including audiobooks (#2486)
* fix: support reading alternative book formats using bookType query param

* feat: add streaming reader support for alternative EPUB formats

* refactor: extract tabs section into MetadataTabsComponent

* feat: implement per-file reading progress tracking

* feat: add configurable format priority with cascading fallback

* refactor: redesign library creator with single-page layout

* refactor: unify reading progress into ReadingProgressService

* refactor: improve delete options with clearer file format and book deletion

* refactor: improve book format matching with normalization and fuzzy fallback

* feat: add folder-based audiobook support with ZIP downloads and preparing state

* fix: display unique format types in metadata viewer

* refactor: auto-attach files to single book in folder during rescan

* feat: implement folder-centric book file grouping with fuzzy matching

* refactor: consolidate book grouping with organization mode support

* fix: use folder hash for folder-based audiobooks in auto-attach

* fix: delete only BookFileEntity on file removal, not entire book

* fix: set folderBased flag and correct file size for auto-attached audiobook folders

* fix: use count query to avoid lazy initialization in file delete handler

* fix: preserve book metadata when file is moved by detecting hash match

* fix: skip books with no files in restoration to avoid NPE

* fix: round all progress percentages to one decimal place before sending to UI

* style: improve library creator dialog spacing and layout

* fix: migrate FOLDER_AS_BOOK to BOOK_PER_FOLDER for backwards compatibility

* fix: FileMoveService now handles multi-file BookEntity properly

* style: redesign metadata-picker and file-mover UI components

* style: redesign file-naming-pattern UI and improve theme contrast

* style: relax spacing in library-creator component

* style: tighten directory-picker layout for consistency

* fix: PathPatternResolver now respects folderBased flag for audiobooks

* style: move checkmark column to left side and add pending status in file-mover

* refactor: consolidate BookFileType into BookType and add audiobook styling

* feat: show 'Continue Reading' button for in-progress books

* fix: preserve dots in folder names during grouping and prevent duplicate attachments

* style: reduce gap between download-all button and primary file section

* style: shorten 'Continue Reading' button to 'Read' on mobile

* feat: add DISK_TYPE config to disable file moves on network storage

* style: standardize settings pages with shared styles and consistent spacing

* style: revamp settings pages with modern card-based layouts

* style: revamp OPDS, auth, and task management settings layouts

* fix: update FileMoveService tests to include AppProperties parameter

* fix: derive BookFileType from extension when uploading additional book files

* feat: show format type and size in download button with dropdown for alternatives

* feat: add audiobook streaming player with HTTP Range support

* feat: add M4B chapter extraction and chapter navigation

* feat: add audiobook bookmarks and sleep timer

* refactor: split AudiobookReaderService into focused services

* feat: add Media Session API for background audio playback

* feat: display file extensions in format tags with distinct colors and add audiobook upload support

* feat: require bookFileId for viewer settings API

* fix: show split button for audiobooks with readable alternatives

* feat: improve reading sessions table with book type badges and better formatting

* feat: add audiobook play button in files tab

* feat: use file extension for format display and meaningful tab values

* feat: add extension field to BookFile DTO

* feat: use extension field for book card format pill

* fix: use mixed orientation for format priority drag-drop to support multi-line layout

* feat: add attach book files feature to consolidate single-file books

* fix: increase drag handle size for format priority chips

* fix: add missing BookFileRepository mock to BookServiceDeleteTests

* feat: add delete buttons to files tab and move shelf to other items menu

* feat: add physical books support for cataloging books without digital files

* refactor: redesign icon-picker component with improved styling

* refactor: redesign metadata-picker component with improved layout and alignment

* refactor: move Add Physical Book to top of library menu

* refactor: use p-autoComplete for authors/categories in add-physical-book dialog

* feat: add square cover search and display support for audiobooks

* fix: navigate to first page when opening unread ebooks

* fix: exclude API requests from service worker interception

* feat: support fileless books with automatic file attachment

* fix: bypass service worker for audiobook streaming requests

* refactor: remove alphabetical filter sorting, default to count, limit filters to 100

---------

Co-authored-by: acx10 <acx10@users.noreply.github.com>
2026-01-27 10:12:50 -07:00

778 lines
27 KiB
TypeScript

import {inject, Injectable} from '@angular/core';
import {first, Observable, of, throwError} from 'rxjs';
import {HttpClient, HttpParams} from '@angular/common/http';
import {catchError, distinctUntilChanged, filter, finalize, map, shareReplay, tap} from 'rxjs/operators';
import {AdditionalFile, AdditionalFileType, Book, BookDeletionResponse, BookMetadata, BookRecommendation, BookSetting, BookType, BulkMetadataUpdateRequest, CreatePhysicalBookRequest, MetadataUpdateWrapper, ReadStatus} from '../model/book.model';
import {BookState} from '../model/state/book-state.model';
import {API_CONFIG} from '../../../core/config/api-config';
import {MessageService} from 'primeng/api';
import {ResetProgressType} from '../../../shared/constants/reset-progress-type';
import {AuthService} from '../../../shared/service/auth.service';
import {FileDownloadService} from '../../../shared/service/file-download.service';
import {Router} from '@angular/router';
import {BookStateService} from './book-state.service';
import {BookSocketService} from './book-socket.service';
import {BookPatchService} from './book-patch.service';
export interface BookStatusUpdateResponse {
bookId: number;
readStatus: ReadStatus;
readStatusModifiedTime: string;
dateFinished?: string;
}
export interface PersonalRatingUpdateResponse {
bookId: number;
personalRating?: number;
}
@Injectable({
providedIn: 'root',
})
export class BookService {
private readonly url = `${API_CONFIG.BASE_URL}/api/v1/books`;
private http = inject(HttpClient);
private messageService = inject(MessageService);
private authService = inject(AuthService);
private fileDownloadService = inject(FileDownloadService);
private router = inject(Router);
private bookStateService = inject(BookStateService);
private bookSocketService = inject(BookSocketService);
private bookPatchService = inject(BookPatchService);
private loading$: Observable<Book[]> | null = null;
constructor() {
this.authService.token$.pipe(
distinctUntilChanged()
).subscribe(token => {
if (token === null) {
this.bookStateService.resetBookState();
this.loading$ = null;
} else {
const current = this.bookStateService.getCurrentBookState();
if (current.loaded && !current.books) {
this.bookStateService.updateBookState({
books: null,
loaded: false,
error: null,
});
this.loading$ = null;
}
}
});
}
/*------------------ State Management ------------------*/
bookState$ = this.bookStateService.bookState$.pipe(
tap(state => {
if (!state.loaded && !state.error && !this.loading$) {
this.loading$ = this.fetchBooks().pipe(
shareReplay(1),
finalize(() => (this.loading$ = null))
);
this.loading$.subscribe();
}
})
);
getCurrentBookState(): BookState {
return this.bookStateService.getCurrentBookState();
}
private fetchBooks(): Observable<Book[]> {
return this.http.get<Book[]>(this.url).pipe(
tap(books => {
this.bookStateService.updateBookState({
books: books || [],
loaded: true,
error: null,
});
}),
catchError(error => {
const curr = this.bookStateService.getCurrentBookState();
this.bookStateService.updateBookState({
books: curr.books,
loaded: true,
error: error.message,
});
throw error;
})
);
}
refreshBooks(): void {
this.http.get<Book[]>(this.url).pipe(
tap(books => {
this.bookStateService.updateBookState({
books: books || [],
loaded: true,
error: null,
});
}),
catchError(error => {
this.bookStateService.updateBookState({
books: null,
loaded: true,
error: error.message,
});
return of(null);
})
).subscribe();
}
removeBooksByLibraryId(libraryId: number): void {
const currentState = this.bookStateService.getCurrentBookState();
const currentBooks = currentState.books || [];
const filteredBooks = currentBooks.filter(book => book.libraryId !== libraryId);
this.bookStateService.updateBookState({...currentState, books: filteredBooks});
}
removeBooksFromShelf(shelfId: number): void {
const currentState = this.bookStateService.getCurrentBookState();
const currentBooks = currentState.books || [];
const updatedBooks = currentBooks.map(book => ({
...book,
shelves: book.shelves?.filter(shelf => shelf.id !== shelfId),
}));
this.bookStateService.updateBookState({...currentState, books: updatedBooks});
}
/*------------------ Book Retrieval ------------------*/
getBookByIdFromState(bookId: number): Book | undefined {
const currentState = this.bookStateService.getCurrentBookState();
return currentState.books?.find(book => +book.id === +bookId);
}
getBooksByIdsFromState(bookIds: number[]): Book[] {
const currentState = this.bookStateService.getCurrentBookState();
if (!currentState.books || bookIds.length === 0) return [];
const idSet = new Set(bookIds.map(id => +id));
return currentState.books.filter(book => idSet.has(+book.id));
}
getBookByIdFromAPI(bookId: number, withDescription: boolean): Observable<Book> {
return this.http.get<Book>(`${this.url}/${bookId}`, {
params: {
withDescription: withDescription.toString()
}
});
}
getBooksInSeries(bookId: number): Observable<Book[]> {
return this.bookStateService.bookState$.pipe(
filter(state => state.loaded),
first(),
map(state => {
const allBooks = state.books || [];
const currentBook = allBooks.find(b => b.id === bookId);
if (!currentBook || !currentBook.metadata?.seriesName) {
return [];
}
const seriesName = currentBook.metadata.seriesName.toLowerCase();
return allBooks.filter(b => b.metadata?.seriesName?.toLowerCase() === seriesName);
})
);
}
getBookRecommendations(bookId: number, limit: number = 20): Observable<BookRecommendation[]> {
return this.http.get<BookRecommendation[]>(`${this.url}/${bookId}/recommendations`, {
params: {limit: limit.toString()}
});
}
/*------------------ Book Operations ------------------*/
deleteBooks(ids: Set<number>): Observable<BookDeletionResponse> {
const idList = Array.from(ids);
const params = new HttpParams().set('ids', idList.join(','));
return this.http.delete<BookDeletionResponse>(this.url, {params}).pipe(
tap(response => {
const currentState = this.bookStateService.getCurrentBookState();
const remainingBooks = (currentState.books || []).filter(
book => !ids.has(book.id)
);
this.bookStateService.updateBookState({
books: remainingBooks,
loaded: true,
error: null,
});
if (response.failedFileDeletions?.length > 0) {
this.messageService.add({
severity: 'warn',
summary: 'Some files could not be deleted',
detail: `Books: ${response.failedFileDeletions.join(', ')}`,
});
} else {
this.messageService.add({
severity: 'success',
summary: 'Books Deleted',
detail: `${idList.length} book(s) deleted successfully.`,
});
}
}),
catchError(error => {
this.messageService.add({
severity: 'error',
summary: 'Delete Failed',
detail: error?.error?.message || error?.message || 'An error occurred while deleting books.',
});
return throwError(() => error);
})
);
}
updateBookShelves(bookIds: Set<number | undefined>, shelvesToAssign: Set<number | null | undefined>, shelvesToUnassign: Set<number | null | undefined>): Observable<Book[]> {
return this.bookPatchService.updateBookShelves(bookIds, shelvesToAssign, shelvesToUnassign).pipe(
catchError(error => {
const currentState = this.bookStateService.getCurrentBookState();
this.bookStateService.updateBookState({...currentState, error: error.message});
throw error;
})
);
}
createPhysicalBook(request: CreatePhysicalBookRequest): Observable<Book> {
return this.http.post<Book>(`${this.url}/physical`, request).pipe(
tap(newBook => {
const currentState = this.bookStateService.getCurrentBookState();
const updatedBooks = [...(currentState.books || []), newBook];
this.bookStateService.updateBookState({
...currentState,
books: updatedBooks
});
this.messageService.add({
severity: 'success',
summary: 'Physical Book Created',
detail: `"${newBook.metadata?.title || 'Book'}" has been added to your library.`
});
}),
catchError(error => {
this.messageService.add({
severity: 'error',
summary: 'Creation Failed',
detail: error?.error?.message || error?.message || 'An error occurred while creating the physical book.'
});
return throwError(() => error);
})
);
}
/*------------------ Reading & Viewer Settings ------------------*/
readBook(bookId: number, reader?: 'pdf-streaming' | 'epub-streaming', explicitBookType?: BookType): void {
const book = this.bookStateService
.getCurrentBookState()
.books?.find(b => b.id === bookId);
if (!book) {
console.error('Book not found');
return;
}
// Determine the book type - use explicit type if provided, otherwise use primary
const bookType: BookType | undefined = explicitBookType ?? book.primaryFile?.bookType;
const isAlternativeFormat = explicitBookType && explicitBookType !== book.primaryFile?.bookType;
let baseUrl: string | null = null;
let queryParams: Record<string, any> = {};
switch (bookType) {
case 'PDF':
baseUrl = reader === 'pdf-streaming' ? 'cbx-reader' : 'pdf-reader';
break;
case 'EPUB':
baseUrl = 'ebook-reader';
if (reader === 'epub-streaming') {
queryParams['streaming'] = true;
}
break;
case 'FB2':
case 'MOBI':
case 'AZW3':
baseUrl = 'ebook-reader';
break;
case 'CBX':
baseUrl = 'cbx-reader';
break;
case 'AUDIOBOOK':
baseUrl = 'audiobook-reader';
break;
}
if (!baseUrl) {
console.error('Unsupported book type:', bookType);
return;
}
// Add bookType to query params if reading an alternative format
if (isAlternativeFormat) {
queryParams['bookType'] = bookType;
}
const hasQueryParams = Object.keys(queryParams).length > 0;
this.router.navigate([`/${baseUrl}/book/${book.id}`], hasQueryParams ? { queryParams } : undefined);
this.updateLastReadTime(book.id);
}
getBookSetting(bookId: number, bookFileId: number): Observable<BookSetting> {
return this.http.get<BookSetting>(`${this.url}/${bookId}/viewer-setting?bookFileId=${bookFileId}`);
}
updateViewerSetting(bookSetting: BookSetting, bookId: number): Observable<void> {
return this.http.put<void>(`${this.url}/${bookId}/viewer-setting`, bookSetting);
}
/*------------------ File Operations ------------------*/
getFileContent(bookId: number, bookType?: string): Observable<Blob> {
let url = `${this.url}/${bookId}/content`;
if (bookType) {
url += `?bookType=${bookType}`;
}
return this.http.get<Blob>(url, {responseType: 'blob' as 'json'});
}
downloadFile(book: Book): void {
const downloadUrl = `${this.url}/${book.id}/download`;
this.fileDownloadService.downloadFile(downloadUrl, book.primaryFile?.fileName ?? 'book');
}
downloadAllFiles(book: Book): void {
const downloadUrl = `${this.url}/${book.id}/download-all`;
const fileName = book.metadata?.title
? `${book.metadata.title.replace(/[^a-zA-Z0-9\-_]/g, '_')}.zip`
: `book-${book.id}.zip`;
this.fileDownloadService.downloadFile(downloadUrl, fileName);
}
deleteAdditionalFile(bookId: number, fileId: number): Observable<void> {
const deleteUrl = `${this.url}/${bookId}/files/${fileId}`;
return this.http.delete<void>(deleteUrl).pipe(
tap(() => {
const currentState = this.bookStateService.getCurrentBookState();
const updatedBooks = (currentState.books || []).map(book => {
if (book.id === bookId) {
return {
...book,
alternativeFormats: book.alternativeFormats?.filter(file => file.id !== fileId),
supplementaryFiles: book.supplementaryFiles?.filter(file => file.id !== fileId)
};
}
return book;
});
this.bookStateService.updateBookState({
...currentState,
books: updatedBooks
});
this.messageService.add({
severity: 'success',
summary: 'File Deleted',
detail: 'Additional file deleted successfully.'
});
}),
catchError(error => {
this.messageService.add({
severity: 'error',
summary: 'Delete Failed',
detail: error?.error?.message || error?.message || 'An error occurred while deleting the file.'
});
return throwError(() => error);
})
);
}
deleteBookFile(bookId: number, fileId: number, isPrimary: boolean): Observable<void> {
const deleteUrl = `${this.url}/${bookId}/files/${fileId}`;
return this.http.delete<void>(deleteUrl).pipe(
tap(() => {
const currentState = this.bookStateService.getCurrentBookState();
const updatedBooks = (currentState.books || []).map(book => {
if (book.id === bookId) {
if (isPrimary) {
// Primary file was deleted - promote first alternative to primary, or set null
const remainingAlternatives = book.alternativeFormats?.filter(file => file.id !== fileId) || [];
if (remainingAlternatives.length > 0) {
const [newPrimary, ...restAlternatives] = remainingAlternatives;
return {
...book,
primaryFile: newPrimary,
alternativeFormats: restAlternatives
};
} else {
return {
...book,
primaryFile: undefined,
alternativeFormats: []
};
}
} else {
// Alternative file was deleted
return {
...book,
alternativeFormats: book.alternativeFormats?.filter(file => file.id !== fileId)
};
}
}
return book;
});
this.bookStateService.updateBookState({
...currentState,
books: updatedBooks
});
this.messageService.add({
severity: 'success',
summary: 'File Deleted',
detail: 'Book file deleted successfully.'
});
}),
catchError(error => {
this.messageService.add({
severity: 'error',
summary: 'Delete Failed',
detail: error?.error?.message || error?.message || 'An error occurred while deleting the file.'
});
return throwError(() => error);
})
);
}
uploadAdditionalFile(bookId: number, file: File, fileType: AdditionalFileType, description?: string): Observable<AdditionalFile> {
const formData = new FormData();
formData.append('file', file);
const isBook = fileType === AdditionalFileType.ALTERNATIVE_FORMAT;
formData.append('isBook', String(isBook));
if (isBook) {
const lower = (file?.name || '').toLowerCase();
const ext = lower.includes('.') ? lower.substring(lower.lastIndexOf('.') + 1) : '';
const bookType = ext === 'pdf'
? 'PDF'
: ext === 'epub'
? 'EPUB'
: (ext === 'cbz' || ext === 'cbr' || ext === 'cb7' || ext === 'cbt')
? 'CBX'
: (ext === 'm4b' || ext === 'm4a' || ext === 'mp3')
? 'AUDIOBOOK'
: null;
if (bookType) {
formData.append('bookType', bookType);
}
}
if (description) {
formData.append('description', description);
}
return this.http.post<AdditionalFile>(`${this.url}/${bookId}/files`, formData).pipe(
tap((newFile) => {
const currentState = this.bookStateService.getCurrentBookState();
const updatedBooks = (currentState.books || []).map(book => {
if (book.id === bookId) {
const updatedBook = {...book};
if (fileType === AdditionalFileType.ALTERNATIVE_FORMAT) {
updatedBook.alternativeFormats = [...(book.alternativeFormats || []), newFile];
} else {
updatedBook.supplementaryFiles = [...(book.supplementaryFiles || []), newFile];
}
return updatedBook;
}
return book;
});
this.bookStateService.updateBookState({
...currentState,
books: updatedBooks
});
this.messageService.add({
severity: 'success',
summary: 'File Uploaded',
detail: 'Additional file uploaded successfully.'
});
}),
catchError(error => {
this.messageService.add({
severity: 'error',
summary: 'Upload Failed',
detail: error?.error?.message || error?.message || 'An error occurred while uploading the file.'
});
return throwError(() => error);
})
);
}
downloadAdditionalFile(book: Book, fileId: number): void {
const additionalFile = [
...(book.alternativeFormats || []),
...(book.supplementaryFiles || [])
].find((f: AdditionalFile) => f.id === fileId);
const downloadUrl = `${this.url}/${book!.id}/files/${fileId}/download`;
this.fileDownloadService.downloadFile(downloadUrl, additionalFile!.fileName!);
}
/*------------------ Progress & Status Tracking ------------------*/
updateLastReadTime(bookId: number): void {
this.bookPatchService.updateLastReadTime(bookId);
}
savePdfProgress(bookId: number, page: number, percentage: number, bookFileId?: number): Observable<void> {
return this.bookPatchService.savePdfProgress(bookId, page, percentage, bookFileId);
}
/*saveEpubProgress(bookId: number, cfi: string, href: string, percentage: number): Observable<void> {
return this.bookPatchService.saveEpubProgress(bookId, cfi, href, percentage);
}*/
saveCbxProgress(bookId: number, page: number, percentage: number, bookFileId?: number): Observable<void> {
return this.bookPatchService.saveCbxProgress(bookId, page, percentage, bookFileId);
}
updateDateFinished(bookId: number, dateFinished: string | null): Observable<void> {
return this.bookPatchService.updateDateFinished(bookId, dateFinished);
}
resetProgress(bookIds: number | number[], type: ResetProgressType): Observable<BookStatusUpdateResponse[]> {
return this.bookPatchService.resetProgress(bookIds, type);
}
updateBookReadStatus(bookIds: number | number[], status: ReadStatus): Observable<BookStatusUpdateResponse[]> {
return this.bookPatchService.updateBookReadStatus(bookIds, status);
}
/*------------------ Personal Rating ------------------*/
resetPersonalRating(bookIds: number | number[]): Observable<PersonalRatingUpdateResponse[]> {
return this.bookPatchService.resetPersonalRating(bookIds);
}
updatePersonalRating(bookIds: number | number[], rating: number): Observable<PersonalRatingUpdateResponse[]> {
return this.bookPatchService.updatePersonalRating(bookIds, rating);
}
/*------------------ Metadata Operations ------------------*/
updateBookMetadata(bookId: number | undefined, wrapper: MetadataUpdateWrapper, mergeCategories: boolean): Observable<BookMetadata> {
const params = new HttpParams().set('mergeCategories', mergeCategories.toString());
return this.http.put<BookMetadata>(`${this.url}/${bookId}/metadata`, wrapper, {params}).pipe(
map(updatedMetadata => {
this.handleBookMetadataUpdate(bookId!, updatedMetadata);
return updatedMetadata;
})
);
}
updateBooksMetadata(request: BulkMetadataUpdateRequest): Observable<void> {
return this.http.put(`${this.url}/bulk-edit-metadata`, request).pipe(
map(() => void 0)
);
}
toggleAllLock(bookIds: Set<number>, lock: string): Observable<void> {
const requestBody = {
bookIds: Array.from(bookIds),
lock: lock
};
return this.http.put<BookMetadata[]>(`${this.url}/metadata/toggle-all-lock`, requestBody).pipe(
tap((updatedMetadataList) => {
const currentState = this.bookStateService.getCurrentBookState();
const updatedBooks = (currentState.books || []).map(book => {
const updatedMetadata = updatedMetadataList.find(meta => meta.bookId === book.id);
return updatedMetadata ? {...book, metadata: updatedMetadata} : book;
});
this.bookStateService.updateBookState({...currentState, books: updatedBooks});
}),
map(() => void 0),
catchError((error) => {
throw error;
})
);
}
toggleFieldLocks(bookIds: number[] | Set<number>, fieldActions: Record<string, 'LOCK' | 'UNLOCK'>): Observable<void> {
const bookIdSet = bookIds instanceof Set ? bookIds : new Set(bookIds);
const requestBody = {
bookIds: Array.from(bookIdSet),
fieldActions
};
return this.http.put<void>(`${this.url}/metadata/toggle-field-locks`, requestBody).pipe(
tap(() => {
const currentState = this.bookStateService.getCurrentBookState();
const updatedBooks = (currentState.books || []).map(book => {
if (!bookIdSet.has(book.id)) return book;
const updatedMetadata = {...book.metadata};
for (const [field, action] of Object.entries(fieldActions)) {
const lockField = field.endsWith('Locked') ? field : `${field}Locked`;
if (lockField in updatedMetadata) {
(updatedMetadata as Record<string, unknown>)[lockField] = action === 'LOCK';
}
}
return {
...book,
metadata: updatedMetadata
};
});
this.bookStateService.updateBookState({
...currentState,
books: updatedBooks as Book[]
});
}),
catchError(error => {
this.messageService.add({
severity: 'error',
summary: 'Field Lock Update Failed',
detail: 'Failed to update metadata field locks. Please try again.',
});
throw error;
})
);
}
consolidateMetadata(metadataType: 'authors' | 'categories' | 'moods' | 'tags' | 'series' | 'publishers' | 'languages', targetValues: string[], valuesToMerge: string[]): Observable<unknown> {
const payload = {metadataType, targetValues, valuesToMerge};
return this.http.post(`${this.url}/metadata/manage/consolidate`, payload).pipe(
tap(() => {
this.refreshBooks();
})
);
}
deleteMetadata(metadataType: 'authors' | 'categories' | 'moods' | 'tags' | 'series' | 'publishers' | 'languages', valuesToDelete: string[]): Observable<unknown> {
const payload = {metadataType, valuesToDelete};
return this.http.post(`${this.url}/metadata/manage/delete`, payload).pipe(
tap(() => {
this.refreshBooks();
})
);
}
/*------------------ Cover Operations ------------------*/
getUploadCoverUrl(bookId: number): string {
return this.url + '/' + bookId + "/metadata/cover/upload"
}
uploadCoverFromUrl(bookId: number, url: string): Observable<BookMetadata> {
return this.http.post<BookMetadata>(`${this.url}/${bookId}/metadata/cover/from-url`, {url});
}
regenerateCovers(): Observable<void> {
return this.http.post<void>(`${this.url}/regenerate-covers`, {});
}
regenerateCover(bookId: number): Observable<void> {
return this.http.post<void>(`${this.url}/${bookId}/regenerate-cover`, {});
}
generateCustomCover(bookId: number): Observable<void> {
return this.http.post<void>(`${this.url}/${bookId}/generate-custom-cover`, {});
}
generateCustomCoversForBooks(bookIds: number[]): Observable<void> {
return this.http.post<void>(`${this.url}/bulk-generate-custom-covers`, {bookIds});
}
regenerateCoversForBooks(bookIds: number[]): Observable<void> {
return this.http.post<void>(`${this.url}/bulk-regenerate-covers`, {bookIds});
}
bulkUploadCover(bookIds: number[], file: File): Observable<void> {
const formData = new FormData();
formData.append('file', file);
formData.append('bookIds', bookIds.join(','));
return this.http.post<void>(`${this.url}/bulk-upload-cover`, formData);
}
/*------------------ Websocket Handlers ------------------*/
handleNewlyCreatedBook(book: Book): void {
this.bookSocketService.handleNewlyCreatedBook(book);
}
handleRemovedBookIds(removedBookIds: number[]): void {
this.bookSocketService.handleRemovedBookIds(removedBookIds);
}
handleBookUpdate(updatedBook: Book): void {
this.bookSocketService.handleBookUpdate(updatedBook);
}
handleMultipleBookUpdates(updatedBooks: Book[]): void {
this.bookSocketService.handleMultipleBookUpdates(updatedBooks);
}
handleBookMetadataUpdate(bookId: number, updatedMetadata: BookMetadata): void {
this.bookSocketService.handleBookMetadataUpdate(bookId, updatedMetadata);
}
handleMultipleBookCoverPatches(patches: { id: number; coverUpdatedOn: string }[]): void {
this.bookSocketService.handleMultipleBookCoverPatches(patches);
}
/*------------------ Book File Attachment ------------------*/
attachBookFiles(targetBookId: number, sourceBookIds: number[], deleteSourceBooks: boolean): Observable<Book> {
return this.http.post<Book>(`${this.url}/${targetBookId}/attach-file`, {
sourceBookIds,
deleteSourceBooks
}).pipe(
tap(updatedBook => {
const currentState = this.bookStateService.getCurrentBookState();
let updatedBooks = (currentState.books || []).map(book =>
book.id === targetBookId ? updatedBook : book
);
// If deleteSourceBooks, remove source books from state
if (deleteSourceBooks) {
const sourceIdSet = new Set(sourceBookIds);
updatedBooks = updatedBooks.filter(book => !sourceIdSet.has(book.id));
}
this.bookStateService.updateBookState({
...currentState,
books: updatedBooks
});
const fileCount = sourceBookIds.length;
this.messageService.add({
severity: 'success',
summary: 'Files Attached',
detail: `${fileCount} book file${fileCount > 1 ? 's have' : ' has'} been attached successfully.`
});
}),
catchError(error => {
this.messageService.add({
severity: 'error',
summary: 'Attachment Failed',
detail: error?.error?.message || error?.message || 'An error occurred while attaching the files.'
});
return throwError(() => error);
})
);
}
}