Browse Source

fix: detect the type of resources from Content-Type when the URL does not tell it

loadResource took the type of a resource from its URL path only, so URLs like
Google Fonts' /icon?family=Material+Icons failed with "Unable to detect file
type". When the path does not tell the type, it now comes from the
Content-Type of a HEAD response: text/css or a JavaScript MIME type. This works
for same-origin URLs and servers that allow cross-origin requests.
pull/16309/head
Igor Kulikov 1 month ago
parent
commit
55a277153d
  1. 37
      ui-ngx/src/app/core/services/resources.service.ts

37
ui-ngx/src/app/core/services/resources.service.ts

@ -17,7 +17,7 @@ import {
DOCUMENT
} from '@angular/core';
import { forkJoin, from, Observable, ReplaySubject, throwError } from 'rxjs';
import { forkJoin, from, Observable, of, ReplaySubject, throwError } from 'rxjs';
import { HttpClient } from '@angular/common/http';
import { IModulesMap } from '@modules/common/modules-map.models';
import { TbResourceId } from '@shared/models/id/tb-resource-id';
@ -26,7 +26,7 @@ import { AuthService } from '@core/auth/auth.service';
import { select, Store } from '@ngrx/store';
import { selectIsAuthenticated } from '@core/auth/auth.selectors';
import { AppState } from '@core/core.state';
import { map, tap } from 'rxjs/operators';
import { catchError, map, switchMap, tap } from 'rxjs/operators';
import { RequestConfig } from '@core/http/http-utils';
import { isJSResource, removeTbResourcePrefix } from '@shared/models/resource.models';
@ -96,6 +96,22 @@ const resourceFileType = (url: string, baseUrl: string): string => {
return /^(css|js)\d*$/.exec(segment)?.[1];
};
// The file type of a resource served under a URL that does not tell it, as Google Fonts' /icon, from the Content-Type
// of a HEAD response. Works only for same-origin URLs and servers that allow cross-origin requests.
const resourceFileTypeFromContentType = (url: string): Observable<string> =>
from(fetch(url, {method: 'HEAD'})).pipe(
map(response => {
const mimeType = response.headers.get('content-type')?.split(';')[0].trim().toLowerCase();
if (mimeType === 'text/css') {
return 'css';
} else if (/^(text|application)\/(x-)?(javascript|ecmascript)$/.test(mimeType)) {
return 'js';
}
return undefined;
}),
catchError(() => of(undefined))
);
const matchesSelector = (selectors: ɵCssSelectorList, selector: string) =>
selectors.some(s => s.some(s1 => typeof s1 === 'string' && s1 === selector));
@ -160,12 +176,17 @@ export class ResourcesService {
}
const fileType = resourceFileType(url, this.document.baseURI);
if (!fileType) {
return throwError(() => new Error(`Unable to detect file type from url: ${url}`));
} else if (fileType !== 'css' && fileType !== 'js') {
return throwError(() => new Error(`Unsupported file type: ${fileType}`));
}
return this.loadResourceByType(fileType, url);
const fileType$ = fileType ? of(fileType) : resourceFileTypeFromContentType(url);
return fileType$.pipe(
switchMap(type => {
if (!type) {
return throwError(() => new Error(`Unable to detect file type from url: ${url}`));
} else if (type !== 'css' && type !== 'js') {
return throwError(() => new Error(`Unsupported file type: ${type}`));
}
return this.loadedResources[url] ? this.loadedResources[url].asObservable() : this.loadResourceByType(type, url);
})
);
}
public downloadResource(downloadUrl: string, config?: RequestConfig): Observable<any> {

Loading…
Cancel
Save