* refactor(icon): fix SVG icon loading straight to memory * refactor(icon): loading with improved error handling * refactor(icon): add flag for SVG icons loading state * chore(release): 2.3.0 [skip ci] ## [2.3.0](https://github.com/grimmory-tools/grimmory/compare/v2.2.6...v2.3.0) (2026-03-21) ### Features * **release:** document develop-based stable release previews ([930e526](https://github.com/grimmory-tools/grimmory/commit/930e5262285540b3b65b9ef4f7be328a05dfb5b4)) ### Bug Fixes * **api:** fix potential memory leaks in file processing ([031e8ae](https://github.com/grimmory-tools/grimmory/commit/031e8ae257c3354cacce2d17c8c6bc35ce80badb)) * **ci:** correct artifact download action pin ([37ca101](https://github.com/grimmory-tools/grimmory/commit/37ca101dd4bd08ccfe6d4d2395ecc71298cc323d)) * **ci:** publish PR test results from workflow_run ([11a76bf](https://github.com/grimmory-tools/grimmory/commit/11a76bffe12f80d0271e3626291cfbd275346727)) * **ci:** repair release preview and test result publishing ([afa5b81](https://github.com/grimmory-tools/grimmory/commit/afa5b818ebe612e66acc819aa398ac0c6184d21b)) * drop telemetry from app ([#52](https://github.com/grimmory-tools/grimmory/issues/52)) ([4d82cb7](https://github.com/grimmory-tools/grimmory/commit/4d82cb718833a2a4e08ee2f18b2ff3ab9b043dd6)) * **ui:** repair frontend compile after rebrand ([fea1ec6](https://github.com/grimmory-tools/grimmory/commit/fea1ec6930ae64445c6d7e7b38bdc4e7925b51c1)) ### Refactors * **build:** rename frontend dist output to grimmory ([ecf388f](https://github.com/grimmory-tools/grimmory/commit/ecf388f7a2086fbd1d8737a972926d70ec3190d4)) * **i18n:** rename booklore translation keys to grimmory ([eb94afa](https://github.com/grimmory-tools/grimmory/commit/eb94afa7ad600eaa2535802583f21937bd1ae2c0)) * **metadata:** move default parser from Amazon to Goodreads ([e252122](https://github.com/grimmory-tools/grimmory/commit/e252122bc5a89c75af85ddbaae66a14d368479f9)) * pull kepubify & ffprobe during build ([#50](https://github.com/grimmory-tools/grimmory/issues/50)) ([1c15629](https://github.com/grimmory-tools/grimmory/commit/1c15629a10ba2e2ad78455f118e8c937b585b157)) * **ui:** rebrand frontend surfaces to grimmory ([d786dd8](https://github.com/grimmory-tools/grimmory/commit/d786dd8ccbebaa360385dce834816bcb6aaf3b2f)) ### Chores * **api:** remove the custom startup banner ([98c9b1a](https://github.com/grimmory-tools/grimmory/commit/98c9b1ae653c2140b7b2ab86b5511be3376bfe43)) * **deps:** bump flatted from 3.4.1 to 3.4.2 in /booklore-ui ([#73](https://github.com/grimmory-tools/grimmory/issues/73)) ([c4bd0c7](https://github.com/grimmory-tools/grimmory/commit/c4bd0c779ae398968dbdea6d566693ed57eab3eb)) * **funding:** point support links at opencollective ([55c0ac0](https://github.com/grimmory-tools/grimmory/commit/55c0ac089966b8586e7d7bdef1fc469049903d08)) * **release:** 2.2.7 [skip ci] ([0b5e24c](https://github.com/grimmory-tools/grimmory/commit/0b5e24c23848c9e97b04f4221303d0b4bf0b2dd7)) * remove old verbose PR template, replace with temporary more low-key one. ([#84](https://github.com/grimmory-tools/grimmory/issues/84)) ([b868526](https://github.com/grimmory-tools/grimmory/commit/b8685268a9a9730416a209201a70c284cee590c5)) * **ui:** drop financial support dialog ([#21](https://github.com/grimmory-tools/grimmory/issues/21)) ([62be6b1](https://github.com/grimmory-tools/grimmory/commit/62be6b152cbd42910620fdeae1f35783ea258b27)) ### Documentation * updated supported file formats in README.md ([#68](https://github.com/grimmory-tools/grimmory/issues/68)) ([f912e80](https://github.com/grimmory-tools/grimmory/commit/f912e802f97263206308817acd5bab84a5321dcf)) ### Style * **i18n:** normalize translation json formatting ([#89](https://github.com/grimmory-tools/grimmory/issues/89)) ([857290d](https://github.com/grimmory-tools/grimmory/commit/857290d215b518e61c9b3a058ae5d40d5e214672)) * **ui:** simplify the topbar logo branding ([0416d48](https://github.com/grimmory-tools/grimmory/commit/0416d48a6c441792b4c9607b3e472159c879d439)) * fix(book-browser): prevent memory leaks by unsubscribing from observables (#80) * test(icon): remove redundant initialization in IconServiceTest --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Alex Fair <20632147+afairgiant@users.noreply.github.com>
125 lines
3.7 KiB
TypeScript
125 lines
3.7 KiB
TypeScript
import { inject, Injectable } from '@angular/core';
|
|
import { HttpClient } from '@angular/common/http';
|
|
import { Observable, of } from 'rxjs';
|
|
import { finalize, map, shareReplay, tap } from 'rxjs/operators';
|
|
import { API_CONFIG } from '../../core/config/api-config';
|
|
import { IconCacheService } from './icon-cache.service';
|
|
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
|
|
import DOMPurify from 'dompurify';
|
|
|
|
interface SvgIconData {
|
|
svgName: string;
|
|
svgData: string;
|
|
}
|
|
|
|
interface IconSaveResult {
|
|
iconName: string;
|
|
success: boolean;
|
|
errorMessage: string;
|
|
}
|
|
|
|
interface SvgIconBatchResponse {
|
|
totalRequested: number;
|
|
successCount: number;
|
|
failureCount: number;
|
|
results: IconSaveResult[];
|
|
}
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class IconService {
|
|
|
|
private readonly baseUrl = `${API_CONFIG.BASE_URL}/api/v1/icons`;
|
|
private requestCache = new Map<string, Observable<string>>();
|
|
|
|
private http = inject(HttpClient);
|
|
private iconCache = inject(IconCacheService);
|
|
private sanitizer = inject(DomSanitizer);
|
|
|
|
/**
|
|
* Sanitizes SVG content using DOMPurify.
|
|
*/
|
|
private sanitizeSvgContent(content: string): string {
|
|
return DOMPurify.sanitize(content, {
|
|
USE_PROFILES: { svg: true },
|
|
FORBID_TAGS: ['script', 'style', 'foreignObject']
|
|
});
|
|
}
|
|
|
|
getIconNames(page: number = 0, size: number = 1000): Observable<string[]> {
|
|
return this.http.get<{ content: string[] }>(`${this.baseUrl}?page=${page}&size=${size}`).pipe(
|
|
map(response => response.content)
|
|
);
|
|
}
|
|
|
|
getSvgIconContent(iconName: string): Observable<string> {
|
|
const cached = this.iconCache.getCachedSanitized(iconName);
|
|
if (cached) {
|
|
return of('');
|
|
}
|
|
|
|
if (!this.requestCache.has(iconName)) {
|
|
const request$ = this.http.get(`${this.baseUrl}/${encodeURIComponent(iconName)}/content`, {
|
|
responseType: 'text'
|
|
}).pipe(
|
|
tap(content => {
|
|
const sanitized = this.sanitizer.bypassSecurityTrustHtml(this.sanitizeSvgContent(content));
|
|
this.iconCache.cacheIcon(iconName, content, sanitized);
|
|
}),
|
|
shareReplay({ bufferSize: 1, refCount: true }),
|
|
finalize(() => this.requestCache.delete(iconName))
|
|
);
|
|
|
|
this.requestCache.set(iconName, request$);
|
|
}
|
|
|
|
return this.requestCache.get(iconName)!;
|
|
}
|
|
|
|
getSanitizedSvgContent(iconName: string): Observable<SafeHtml> {
|
|
const cached = this.iconCache.getCachedSanitized(iconName);
|
|
if (cached) {
|
|
return of(cached);
|
|
}
|
|
|
|
return new Observable<SafeHtml>(observer => {
|
|
this.getSvgIconContent(iconName).subscribe({
|
|
next: () => {
|
|
const sanitized = this.iconCache.getCachedSanitized(iconName);
|
|
if (sanitized) {
|
|
observer.next(sanitized);
|
|
observer.complete();
|
|
}
|
|
},
|
|
error: (err) => observer.error(err)
|
|
});
|
|
});
|
|
}
|
|
|
|
deleteSvgIcon(svgName: string): Observable<unknown> {
|
|
return this.http.delete(`${this.baseUrl}/${encodeURIComponent(svgName)}`).pipe(
|
|
tap(() => {
|
|
this.iconCache.removeIcon(svgName);
|
|
this.requestCache.delete(svgName);
|
|
})
|
|
);
|
|
}
|
|
|
|
saveBatchSvgIcons(icons: SvgIconData[]): Observable<SvgIconBatchResponse> {
|
|
return this.http.post<SvgIconBatchResponse>(`${this.baseUrl}/batch`, { icons }).pipe(
|
|
tap((response) => {
|
|
response.results.forEach(result => {
|
|
if (result.success) {
|
|
const iconData = icons.find(icon => icon.svgName === result.iconName);
|
|
if (iconData) {
|
|
const sanitized = this.sanitizer.bypassSecurityTrustHtml(this.sanitizeSvgContent(iconData.svgData));
|
|
this.iconCache.cacheIcon(iconData.svgName, iconData.svgData, sanitized);
|
|
}
|
|
}
|
|
});
|
|
})
|
|
);
|
|
}
|
|
}
|