diff --git a/booklore-ui/src/app/shared/components/cover-generator/cover-generator.component.ts b/booklore-ui/src/app/shared/components/cover-generator/cover-generator.component.ts new file mode 100644 index 000000000..113f61fe4 --- /dev/null +++ b/booklore-ui/src/app/shared/components/cover-generator/cover-generator.component.ts @@ -0,0 +1,78 @@ + +import { Component, Input } from '@angular/core'; + +@Component({ + standalone: true, + template: '' +}) +export class CoverGeneratorComponent { + @Input() title: string = ''; + @Input() author: string = ''; + + private wrapText(text: string, maxLineLength: number): string[] { + const words = text.split(' '); + const lines: string[] = []; + let currentLine = ''; + + words.forEach(word => { + if (word.length > maxLineLength) { + if (currentLine.length > 0) { + lines.push(currentLine); + currentLine = ''; + } + lines.push(word); + } else if (currentLine.length + word.length + 1 > maxLineLength) { + lines.push(currentLine); + currentLine = word; + } else { + currentLine += (currentLine.length > 0 ? ' ' : '') + word; + } + }); + + if (currentLine.length > 0) { + lines.push(currentLine); + } + + return lines; + } + + generateCover(): string { + const maxLineLength = 10; + const titleLines = this.wrapText(this.title, maxLineLength); + const authorLines = this.wrapText(this.author, maxLineLength); + + const titleBoxHeight = titleLines.length * 40 + 20; + const titleElements = titleLines.map((line, index) => { + const y = 80 + index * 40; + return `${line}`; + }).join('\n'); + + const titleWithBackground = ` + + ${titleElements} + `; + + const authorElements = authorLines.map((line, index) => { + const y = 364 - (authorLines.length - index - 1) * 36; + return `${line}`; + }).join('\n'); + + const svg = ` + + + + + + + + + + + + ${titleWithBackground} + ${authorElements} + + `; + return `data:image/svg+xml;base64,${btoa(svg)}`; + } +} diff --git a/booklore-ui/src/app/shared/service/url-helper.service.ts b/booklore-ui/src/app/shared/service/url-helper.service.ts index 5ee8245c8..1f76440f6 100644 --- a/booklore-ui/src/app/shared/service/url-helper.service.ts +++ b/booklore-ui/src/app/shared/service/url-helper.service.ts @@ -1,6 +1,8 @@ import {inject, Injectable} from '@angular/core'; import {API_CONFIG} from '../../core/config/api-config'; import {AuthService} from './auth.service'; +import {BookService} from '../../features/book/service/book.service'; +import {CoverGeneratorComponent} from '../components/cover-generator/cover-generator.component'; @Injectable({ providedIn: 'root' @@ -9,6 +11,7 @@ export class UrlHelperService { private readonly baseUrl = API_CONFIG.BASE_URL; private readonly mediaBaseUrl = `${this.baseUrl}/api/v1/media`; private authService = inject(AuthService); + private bookService = inject(BookService); private getToken(): string | null { return this.authService.getOidcAccessToken() || this.authService.getInternalAccessToken(); @@ -20,13 +23,33 @@ export class UrlHelperService { } getThumbnailUrl(bookId: number, coverUpdatedOn?: string): string { - if (!coverUpdatedOn) return 'assets/images/missing-cover.jpg'; + if (!coverUpdatedOn) { + const book = this.bookService.getBookByIdFromState(bookId); + if (book && book.metadata) { + const coverGenerator = new CoverGeneratorComponent(); + coverGenerator.title = book.metadata.title || ''; + coverGenerator.author = (book.metadata.authors || []).join(', '); + return coverGenerator.generateCover(); + } else { + return 'assets/images/missing-cover.jpg'; + } + } const url = `${this.mediaBaseUrl}/book/${bookId}/thumbnail?${coverUpdatedOn}`; return this.appendToken(url); } getCoverUrl(bookId: number, coverUpdatedOn?: string): string { - if (!coverUpdatedOn) return 'assets/images/missing-cover.jpg'; + if (!coverUpdatedOn) { + const book = this.bookService.getBookByIdFromState(bookId); + if (book && book.metadata) { + const coverGenerator = new CoverGeneratorComponent(); + coverGenerator.title = book.metadata.title || ''; + coverGenerator.author = (book.metadata.authors || []).join(', '); + return coverGenerator.generateCover(); + } else { + return 'assets/images/missing-cover.jpg'; + } + } const url = `${this.mediaBaseUrl}/book/${bookId}/cover?${coverUpdatedOn}`; return this.appendToken(url); }