Allow uploading multiple SVG icons and render them directly as inline SVGs instead of using <img> tags (#1796)
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {SafeHtml} from '@angular/platform-browser';
|
||||
import {Observable, BehaviorSubject} from 'rxjs';
|
||||
|
||||
interface CachedIcon {
|
||||
content: string;
|
||||
sanitized: SafeHtml;
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class IconCacheService {
|
||||
private cache = new Map<string, CachedIcon>();
|
||||
private readonly CACHE_DURATION_MS = 1000 * 60 * 60;
|
||||
|
||||
private cacheUpdate$ = new BehaviorSubject<string | null>(null);
|
||||
|
||||
getCachedContent(iconName: string): string | null {
|
||||
const cached = this.cache.get(iconName);
|
||||
|
||||
if (!cached) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (Date.now() - cached.timestamp > this.CACHE_DURATION_MS) {
|
||||
this.cache.delete(iconName);
|
||||
return null;
|
||||
}
|
||||
|
||||
return cached.content;
|
||||
}
|
||||
|
||||
getCachedSanitized(iconName: string): SafeHtml | null {
|
||||
const cached = this.cache.get(iconName);
|
||||
|
||||
if (!cached) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (Date.now() - cached.timestamp > this.CACHE_DURATION_MS) {
|
||||
this.cache.delete(iconName);
|
||||
return null;
|
||||
}
|
||||
|
||||
return cached.sanitized;
|
||||
}
|
||||
|
||||
cacheIcon(iconName: string, content: string, sanitized: SafeHtml): void {
|
||||
this.cache.set(iconName, {
|
||||
content,
|
||||
sanitized,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
this.cacheUpdate$.next(iconName);
|
||||
}
|
||||
|
||||
isCached(iconName: string): boolean {
|
||||
const cached = this.cache.get(iconName);
|
||||
|
||||
if (!cached) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Date.now() - cached.timestamp > this.CACHE_DURATION_MS) {
|
||||
this.cache.delete(iconName);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
invalidate(iconName: string): void {
|
||||
this.cache.delete(iconName);
|
||||
this.cacheUpdate$.next(null);
|
||||
}
|
||||
|
||||
invalidateMultiple(iconNames: string[]): void {
|
||||
iconNames.forEach(name => this.cache.delete(name));
|
||||
this.cacheUpdate$.next(null);
|
||||
}
|
||||
|
||||
clearCache(): void {
|
||||
this.cache.clear();
|
||||
this.cacheUpdate$.next(null);
|
||||
}
|
||||
|
||||
getCacheUpdates(): Observable<string | null> {
|
||||
return this.cacheUpdate$.asObservable();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import {inject, Injectable} from '@angular/core';
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import {Observable} from 'rxjs';
|
||||
import {Observable, of} from 'rxjs';
|
||||
import {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';
|
||||
|
||||
interface PageResponse<T> {
|
||||
content: T[];
|
||||
@@ -11,6 +14,24 @@ interface PageResponse<T> {
|
||||
totalPages: number;
|
||||
}
|
||||
|
||||
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'
|
||||
})
|
||||
@@ -19,6 +40,8 @@ export class IconService {
|
||||
private readonly baseUrl = `${API_CONFIG.BASE_URL}/api/v1/icons`;
|
||||
|
||||
private http = inject(HttpClient);
|
||||
private iconCache = inject(IconCacheService);
|
||||
private sanitizer = inject(DomSanitizer);
|
||||
|
||||
saveSvgIcon(svgContent: string, svgName: string): Observable<any> {
|
||||
return this.http.post(this.baseUrl, {
|
||||
@@ -29,11 +52,63 @@ export class IconService {
|
||||
|
||||
getIconNames(page: number = 0, size: number = 50): Observable<PageResponse<string>> {
|
||||
return this.http.get<PageResponse<string>>(this.baseUrl, {
|
||||
params: { page: page.toString(), size: size.toString() }
|
||||
params: {page: page.toString(), size: size.toString()}
|
||||
});
|
||||
}
|
||||
|
||||
getSvgIconContent(iconName: string): Observable<string> {
|
||||
const cached = this.iconCache.getCachedContent(iconName);
|
||||
if (cached) {
|
||||
return of(cached);
|
||||
}
|
||||
|
||||
return this.http.get(`${this.baseUrl}/${encodeURIComponent(iconName)}/content`, {
|
||||
responseType: 'text'
|
||||
}).pipe(
|
||||
tap(content => {
|
||||
const sanitized = this.sanitizer.bypassSecurityTrustHtml(content);
|
||||
this.iconCache.cacheIcon(iconName, content, sanitized);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
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<any> {
|
||||
return this.http.delete(`${this.baseUrl}/${encodeURIComponent(svgName)}`);
|
||||
return this.http.delete(`${this.baseUrl}/${encodeURIComponent(svgName)}`).pipe(
|
||||
tap(() => {
|
||||
this.iconCache.invalidate(svgName);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
saveBatchSvgIcons(icons: SvgIconData[]): Observable<SvgIconBatchResponse> {
|
||||
return this.http.post<SvgIconBatchResponse>(`${this.baseUrl}/batch`, {icons}).pipe(
|
||||
tap((response) => {
|
||||
const successfulIcons = response.results
|
||||
.filter(result => result.success)
|
||||
.map(result => result.iconName);
|
||||
|
||||
this.iconCache.invalidateMultiple(successfulIcons);
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user