Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from downloadjs to multi-download #174

Merged
merged 15 commits into from
Oct 28, 2024
Merged
1 change: 0 additions & 1 deletion .github/FUNDING.yml

This file was deleted.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ Blaze app can now be accessed at port `8080` :tada:
## Privacy and Analytics
- Blaze server does not track or record the files that are being shared both by WebSockets and WebTorrent.
- Any user related data like nickname, room names are always stored on device, and are only shared with the server when the user joins a room for file sharing.
- Blaze client uses Google Analytics to record the following:
- [Basic visit data](https://developers.google.com/analytics/devguides/collection/analyticsjs#what_data_does_the_google_analytics_tag_capture) as recorded by [Google Analytics](https://support.google.com/analytics/answer/6004245?ref_topic=2919631)
- Blaze client uses Google Analytics 4 to record the following:
- Part of [Basic visit data](https://support.google.com/analytics/answer/9234069?hl=en) - page views, scrolls and outbound clicks, rest are disabled.
- If Blaze PWA is installed on the device, and whether files are shared using share targets.

## Contributing
Expand Down
6 changes: 0 additions & 6 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"classnames": "^2.3.2",
"copy-to-clipboard": "^3.3.3",
"date-fns": "^2.29.3",
"downloadjs": "^1.4.7",
"nanoid": "^3.3.4",
"preact": "^10.3.2",
"preact-feather": "^4.2.1",
Expand Down
17 changes: 5 additions & 12 deletions client/src/routes/App/FileTransfer/FileTransfer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { h, createRef } from 'preact';
import download from 'downloadjs';
import { route } from 'preact-router';
import { PureComponent, forwardRef, memo } from 'preact/compat';
import { ArrowLeft, CheckCircle, Home, Plus, Image, Film, Box, Music, File, Zap, Share2, Send } from 'preact-feather';
Expand All @@ -17,6 +16,7 @@ import Visualizer from '../../../utils/visualizer';
import formatSize from '../../../utils/formatSize';
import pluralize from '../../../utils/pluralize';
import urls from '../../../utils/urls';
import { multiDownload } from '../../../utils/download';
import constants from '../../../../../common/constants';
import roomsDispatch from '../../../reducers/rooms';

Expand Down Expand Up @@ -292,17 +292,10 @@ class FileTransfer extends PureComponent {
isSelectorEnabled: false,
});
},
onDone: (file, meta) => {
if (file !== undefined) {
if (Array.isArray(file)) {
file.forEach(file => {
file.getBlob((err, blob) => download(blob, file.name));
});
}
else {
download(file, meta.name, meta.type);
}
}
onDone: (files) => {
if (files === undefined) return;

multiDownload(files);
this.resetState();
},
ddelange marked this conversation as resolved.
Show resolved Hide resolved
});
Expand Down
6 changes: 2 additions & 4 deletions client/src/routes/App/layouts/AppLanding/AppLanding.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { h } from 'preact';
import { memo } from 'preact/compat';
import { ArrowDownCircle, Gift, Grid, PieChart, Settings } from 'preact-feather';
import { ArrowDownCircle, Gift, Grid, Settings } from 'preact-feather';
import { Link } from 'preact-router/match';
import { useContext } from 'preact/hooks';
import { PWAInstall } from '../../contexts/PWAInstall';
Expand Down Expand Up @@ -36,9 +36,7 @@ function AppLanding({ children, title, subtitle }) {
deferredPrompt.prompt();
const { outcome } = await deferredPrompt.userChoice;

window.ga('send', 'event', {
eventCategory: 'pwa-install',
eventAction: 'promo-shown',
window.gtag('event', 'pwa-install-prompt', {
outcome,
});
};
Expand Down
10 changes: 5 additions & 5 deletions client/src/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
<meta name="msapplication-TileColor" content="#0D1322" />
<meta name="msapplication-TileImage" content="/assets/images/mstile-144x144.png" />

<script async src="https://www.googletagmanager.com/gtag/js?id=G-K0HSN5M77L"></script>
<script>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', 'UA-82138003-3', 'auto');
ga('set', 'transport', 'beacon');
ga('send', 'pageview');
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-K0HSN5M77L');
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>


<% preact.headEnd %>
Expand Down
41 changes: 41 additions & 0 deletions client/src/utils/download.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Adapted from https://github.com/sindresorhus/multi-download/blob/v4.0.0/index.js
// to take File as input https://developer.mozilla.org/en-US/docs/Web/API/File

/**
* Creates a promise that resolves after the specified number of milliseconds
*/
const delay = milliseconds => new Promise(resolve => {
setTimeout(resolve, milliseconds);
});

/**
* Downloads a single file
* @param file - An instance of the File type representing the file to download
*/
const download = async (file) => {
const a = document.createElement('a');
const url = URL.createObjectURL(file);
a.download = file.name;
a.href = url;
a.style.display = 'none';
document.body.append(a);
a.click();
await delay(100); // for Chrome
a.remove();
URL.revokeObjectURL(url);
};

/**
* Initiates multiple file downloads with a constant delay between each one
* @param files - An array of instances of the File type representing the files to download
*/
export const multiDownload = async (files) => {
if (!files) {
throw new Error('`files` required');
};

for (const file of files) {
download(file);
await delay(1000);
}
};
22 changes: 18 additions & 4 deletions client/src/utils/fileShare.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ class FileShare {
this.socket.listen(constants.FILE_INIT, (data) => {
if (data.end) {
if (fileParts.length) {
onDone(new Blob(fileParts), metaData.meta[0]);
onDone([
new File(fileParts, metaData.meta[0].name, {type: metaData.meta[0].type})
]);
fileParts = [];
size = 0;
statProg = 0.25;
Expand Down Expand Up @@ -106,8 +108,20 @@ class FileShare {

torrent.on('upload', update);
torrent.on('download', update);
torrent.on('done', () => {
onDone(torrent.files);
torrent.on('done', async () => {
const files = await Promise.all(
torrent.files.map(
(file) =>
new Promise((resolve, reject) => {
// make regular File from webtorrent File https://github.com/webtorrent/webtorrent/blob/v1.9.7/lib/file.js#L13
file.getBlob((err, blob) => {
if (err) reject(err);
resolve(new File([blob], file.name, { type: file.type }));
});
})
)
);
onDone(files);
});
}

Expand Down Expand Up @@ -227,4 +241,4 @@ class FileShare {

}

export default FileShare;
export default FileShare;
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
context: .
dockerfile: ./server/Dockerfile
environment:
TRUST_PROXY: true
TRUST_PROXY: 1
ports:
- 3030:3030

Expand Down