* refactor(misc): Harden request handling, tighten defaults, and pin CI dependencies * feat(auth): normalize usernames for login rate limiting * refactor(epub): fix XML document builder creation * feat(http): enhance image download with redirect handling and DNS rebinding protection * chore: upgrade Node.js version to 24 and add frontend dependency audit step * feat(auth): refactor username normalization for login rate limiting and enhance image download security * fix(metadata): simplify JAXB validation event handling by using direct import * chore(pipeline): remove unnecessary --force flag from npm ci and format audit step * chore(pipeline): streamline frontend dependency audits and add validation step * fix(image): improve error handling for image processing and download, enforce pixel limits * test(image): update tests * fix(icon): update DOMPurify import and replace deprecated theme package * fix(imports): update theme package imports and format code for consistency * fix(cors): update allowed origins configuration to support wildcard and trim whitespace
141 lines
4.2 KiB
TypeScript
141 lines
4.2 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[];
|
|
}
|
|
|
|
type IconContentMap = Record<string, string>;
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class IconService {
|
|
|
|
private readonly baseUrl = `${API_CONFIG.BASE_URL}/api/v1/icons`;
|
|
private requestCache = new Map<string, Observable<string>>();
|
|
private preloadCache$: Observable<void> | null = null;
|
|
|
|
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']
|
|
});
|
|
}
|
|
|
|
preloadAllIcons(): Observable<void> {
|
|
if (this.preloadCache$) {
|
|
return this.preloadCache$;
|
|
}
|
|
|
|
this.preloadCache$ = this.http.get<IconContentMap>(`${this.baseUrl}/all/content`).pipe(
|
|
tap((iconsMap) => {
|
|
Object.entries(iconsMap).forEach(([iconName, content]) => {
|
|
const sanitized = this.sanitizer.bypassSecurityTrustHtml(this.sanitizeSvgContent(content));
|
|
this.iconCache.cacheIcon(iconName, content, sanitized);
|
|
});
|
|
}),
|
|
map(() => void 0),
|
|
shareReplay({ bufferSize: 1, refCount: false }),
|
|
finalize(() => this.preloadCache$ = null)
|
|
);
|
|
|
|
return this.preloadCache$;
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
});
|
|
})
|
|
);
|
|
}
|
|
}
|