Skip to content

Commit

Permalink
fix: file name decoding from response headers (DEV-4401) (#1996)
Browse files Browse the repository at this point in the history
  • Loading branch information
irmastnt authored Dec 10, 2024
1 parent ffecefd commit 07b003f
Showing 1 changed file with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,10 @@ export class RepresentationService {
const ingestFileUrl = this.getIngestFileUrl(attachedProject.shortcode, assetId);
this.downloadFile(ingestFileUrl, this.userCanView(fileValue));
}

private downloadFile(url: string, userCanView = true) {
let headers = {};
const isLoggedIn = this._store.selectSnapshot(UserSelectors.isLoggedIn);
const withCredentials = isLoggedIn && userCanView;
if (withCredentials) {
if (isLoggedIn && userCanView) {
const authToken = this._accessTokenService.getAccessToken();
headers = new HttpHeaders({
Authorization: `Bearer ${authToken}`,
Expand All @@ -109,10 +107,23 @@ export class RepresentationService {
.pipe(take(1))
.subscribe((res: HttpResponse<Blob>) => {
const contentDisposition = res.headers.get('content-disposition');
const fileNameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
const matches = fileNameRegex.exec(contentDisposition || '');
const fileName =
contentDisposition && matches != null && matches[1] ? matches[1].replace(/['"]/g, '') : url.split('/').pop()!;
let fileName: string | null = null;
if (contentDisposition) {
const fileNameRegex = /filename\*=(?:([a-zA-Z0-9-]+)''([^;'\n]*))|filename=([^;'\n]*)/;
const matches = fileNameRegex.exec(contentDisposition);
fileName =
matches && matches[2]
? decodeURIComponent(matches[2].replace(/\+/g, ' '))
: matches && matches[3]
? matches[3].replace(/['"]/g, '').replace(/\+/g, ' ')
: null;
}

// Fallback to extracting filename from URL if not found
if (!fileName) {
fileName = url.split('/').pop()!;
}

const a = document.createElement('a');
a.href = window.URL.createObjectURL(res.body!);
a.download = fileName;
Expand Down

0 comments on commit 07b003f

Please sign in to comment.