Skip to content

Commit 8947e0b

Browse files
committed
Merge branch 'main' into lukas/update-datastreams-api
2 parents a39109f + 6aab185 commit 8947e0b

File tree

1 file changed

+59
-8
lines changed

1 file changed

+59
-8
lines changed

examples/demo/demo.ts

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const appActions = {
8080
const file = ($('file') as HTMLInputElement).files?.[0]!;
8181
currentRoom?.localParticipant.sendFile(file, {
8282
mimeType: file.type,
83-
topic: 'welcome',
83+
topic: 'files',
8484
onProgress: (progress) => console.log('sending file, progress', Math.ceil(progress * 100)),
8585
});
8686
},
@@ -277,17 +277,68 @@ const appActions = {
277277
const info = reader.info;
278278

279279
appendLog(`started to receive a file called "${info.name}" from ${participant?.identity}`);
280+
281+
const progressContainer = document.createElement('div');
282+
progressContainer.style.margin = '10px 0';
283+
const progressLabel = document.createElement('div');
284+
progressLabel.innerText = `Receiving "${info.name}" from ${participant?.identity}...`;
285+
const progressBar = document.createElement('progress');
286+
progressBar.max = 100;
287+
progressBar.value = 0;
288+
progressBar.style.width = '100%';
289+
290+
progressContainer.appendChild(progressLabel);
291+
progressContainer.appendChild(progressBar);
292+
$('chat-area').after(progressContainer);
293+
294+
appendLog(`Started receiving file "${info.name}" from ${participant?.identity}`);
295+
280296
reader.onProgress = (progress) => {
281297
console.log(`"progress ${progress ? (progress * 100).toFixed(0) : 'undefined'}%`);
298+
299+
if (progress) {
300+
progressBar.value = progress * 100;
301+
progressLabel.innerText = `Receiving "${info.name}" from ${participant?.identity} (${(progress * 100).toFixed(0)}%)`;
302+
}
282303
};
304+
283305
const result = new Blob(await reader.readAll(), { type: info.mimeType });
284-
appendLog(`completely received file called "${info.name}" from ${participant?.identity}`);
285-
const downloadLink = URL.createObjectURL(result);
286-
const linkEl = document.createElement('a');
287-
linkEl.href = downloadLink;
288-
linkEl.innerText = info.name;
289-
linkEl.setAttribute('download', info.name);
290-
document.body.append(linkEl);
306+
appendLog(`Completely received file "${info.name}" from ${participant?.identity}`);
307+
308+
progressContainer.remove();
309+
310+
if (info.mimeType.startsWith('image/')) {
311+
// Embed images directly in HTML
312+
const imgContainer = document.createElement('div');
313+
imgContainer.style.margin = '10px 0';
314+
imgContainer.style.padding = '10px';
315+
316+
const img = document.createElement('img');
317+
img.style.maxWidth = '300px';
318+
img.style.maxHeight = '300px';
319+
img.src = URL.createObjectURL(result);
320+
321+
const downloadLink = document.createElement('a');
322+
downloadLink.href = img.src;
323+
downloadLink.innerText = `Download ${info.name}`;
324+
downloadLink.setAttribute('download', info.name);
325+
downloadLink.style.display = 'block';
326+
downloadLink.style.marginTop = '5px';
327+
328+
imgContainer.appendChild(img);
329+
imgContainer.appendChild(downloadLink);
330+
$('chat-area').after(imgContainer);
331+
} else {
332+
// Non-images get a text download link instead
333+
const downloadLink = document.createElement('a');
334+
downloadLink.href = URL.createObjectURL(result);
335+
downloadLink.innerText = `Download ${info.name}`;
336+
downloadLink.setAttribute('download', info.name);
337+
downloadLink.style.margin = '10px';
338+
downloadLink.style.padding = '5px';
339+
downloadLink.style.display = 'block';
340+
$('chat-area').after(downloadLink);
341+
}
291342
});
292343

293344
try {

0 commit comments

Comments
 (0)