Skip to content

Commit 2f34d71

Browse files
committed
Build dist files
1 parent 61cee45 commit 2f34d71

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/LiveComponent/assets/dist/Backend/BackendResponse.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export default class {
33
private body;
44
constructor(response: Response);
55
getBody(): Promise<string>;
6+
getBlob(): Promise<Blob>;
67
}

src/LiveComponent/assets/dist/live_controller.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ class BackendResponse {
111111
}
112112
return this.body;
113113
}
114+
async getBlob() {
115+
return await this.response.blob();
116+
}
114117
}
115118

116119
function getElementAsTagText(element) {
@@ -2119,11 +2122,43 @@ class Component {
21192122
this.isRequestPending = false;
21202123
this.backendRequest.promise.then(async (response) => {
21212124
const backendResponse = new BackendResponse(response);
2122-
const html = await backendResponse.getBody();
21232125
for (const input of Object.values(this.pendingFiles)) {
21242126
input.value = '';
21252127
}
21262128
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+
}
2143+
const blob = await backendResponse.getBlob();
2144+
const link = Object.assign(window.document.createElement('a'), {
2145+
target: '_blank',
2146+
style: 'display: none',
2147+
href: window.URL.createObjectURL(blob),
2148+
download: fileName,
2149+
});
2150+
this.element.appendChild(link);
2151+
link.click();
2152+
this.element.removeChild(link);
2153+
this.backendRequest = null;
2154+
thisPromiseResolve(backendResponse);
2155+
if (this.isRequestPending) {
2156+
this.isRequestPending = false;
2157+
this.performRequest();
2158+
}
2159+
return response;
2160+
}
2161+
const html = await backendResponse.getBody();
21272162
if (!headers.get('Content-Type')?.includes('application/vnd.live-component+html') &&
21282163
!headers.get('X-Live-Redirect')) {
21292164
const controls = { displayError: true };

0 commit comments

Comments
 (0)