Skip to content

Commit 32cd824

Browse files
committed
Simplify header checks
1 parent 36143e6 commit 32cd824

File tree

2 files changed

+22
-51
lines changed

2 files changed

+22
-51
lines changed

src/LiveComponent/assets/dist/live_controller.js

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class BackendResponse {
112112
return this.body;
113113
}
114114
async getBlob() {
115-
return await this.response.blob();
115+
return this.response.blob();
116116
}
117117
}
118118

@@ -2122,34 +2122,23 @@ class Component {
21222122
this.isRequestPending = false;
21232123
this.backendRequest.promise.then(async (response) => {
21242124
const backendResponse = new BackendResponse(response);
2125+
const headers = backendResponse.response.headers;
21252126
for (const input of Object.values(this.pendingFiles)) {
21262127
input.value = '';
21272128
}
2128-
const headers = backendResponse.response.headers;
2129-
if (headers.get('X-Live-Download')) {
2130-
if (!(headers.get('Content-Disposition')?.includes('attachment') ||
2131-
headers.get('Content-Disposition')?.includes('inline')) ||
2132-
!headers.get('Content-Disposition')?.includes('filename=')) {
2133-
throw new Error('Invalid LiveDownload response');
2134-
}
2135-
const fileSize = Number.parseInt(headers.get('Content-Length') || '0');
2136-
if (fileSize > 10000000) {
2137-
throw new Error('File is too large to download (10MB limit)');
2138-
}
2139-
const fileName = headers.get('Content-Disposition')?.split('filename=')[1];
2140-
if (!fileName) {
2141-
throw new Error('No filename found in Content-Disposition header');
2142-
}
2129+
const contentDisposition = headers.get('Content-Disposition');
2130+
const fileResponse = contentDisposition?.match(/^(attachment|inline).*filename="?([^;]+)"?/);
2131+
if (fileResponse) {
21432132
const blob = await backendResponse.getBlob();
2144-
const link = Object.assign(window.document.createElement('a'), {
2145-
target: '_blank',
2133+
const link = Object.assign(document.createElement('a'), {
2134+
href: URL.createObjectURL(blob),
2135+
download: fileResponse[2],
21462136
style: 'display: none',
2147-
href: window.URL.createObjectURL(blob),
2148-
download: fileName,
2137+
target: '_blank',
21492138
});
2150-
this.element.appendChild(link);
2139+
document.body.appendChild(link);
21512140
link.click();
2152-
this.element.removeChild(link);
2141+
setTimeout(() => document.body.removeChild(link), 75);
21532142
this.backendRequest = null;
21542143
thisPromiseResolve(backendResponse);
21552144
if (this.isRequestPending) {

src/LiveComponent/assets/src/Component/index.ts

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -300,48 +300,30 @@ export default class Component {
300300

301301
this.backendRequest.promise.then(async (response) => {
302302
const backendResponse = new BackendResponse(response);
303+
const headers = backendResponse.response.headers;
303304

304305
// clear sent files inputs
305306
for (const input of Object.values(this.pendingFiles)) {
306307
input.value = '';
307308
}
308309

309-
const headers = backendResponse.response.headers;
310-
if (headers.get('X-Live-Download')) {
311-
const headerContentDisposition = headers.get('Content-Disposition');
312-
if (
313-
!headerContentDisposition
314-
|| !(headerContentDisposition?.includes('attachment') || headerContentDisposition?.includes('inline'))
315-
|| !headerContentDisposition?.includes('filename=')
316-
) {
317-
throw new Error('Invalid LiveDownload response');
318-
}
319-
320-
const fileSize = Number.parseInt(headers.get('Content-Length') || '0');
321-
if (fileSize > 10000000) {
322-
throw new Error('File is too large to download (10MB limit)');
323-
}
324-
325-
const fileName = headerContentDisposition.split('filename=')[1];
326-
if (!fileName) {
327-
throw new Error('No filename found in Content-Disposition header');
328-
}
329-
310+
// File Download
311+
const contentDisposition = headers.get('Content-Disposition');
312+
const fileResponse = contentDisposition?.match(/^(attachment|inline).*filename="?([^;]+)"?/);
313+
if (fileResponse) {
330314
const blob = await backendResponse.getBlob();
331-
const link = Object.assign(window.document.createElement('a'), {
332-
target: '_blank',
315+
const link = Object.assign(document.createElement('a'), {
316+
href: URL.createObjectURL(blob),
317+
download: fileResponse[2],
333318
style: 'display: none',
334-
href: window.URL.createObjectURL(blob),
335-
download: fileName,
319+
target: '_blank',
336320
});
337-
this.element.appendChild(link);
321+
document.body.appendChild(link);
338322
link.click();
339-
this.element.removeChild(link);
323+
setTimeout(() => document.body.removeChild(link), 75);
340324

341325
this.backendRequest = null;
342326
thisPromiseResolve(backendResponse);
343-
344-
// do we already have another request pending?
345327
if (this.isRequestPending) {
346328
this.isRequestPending = false;
347329
this.performRequest();

0 commit comments

Comments
 (0)