2017-08-24 23:54:02 +02:00
|
|
|
import FileSender from './fileSender';
|
|
|
|
import FileReceiver from './fileReceiver';
|
2018-07-31 20:09:18 +02:00
|
|
|
import { copyToClipboard, delay, openLinksInNewTab, percent } from './utils';
|
2017-08-24 23:54:02 +02:00
|
|
|
import * as metrics from './metrics';
|
2019-04-26 22:30:33 +02:00
|
|
|
import { bytes, locale } from './utils';
|
2018-10-25 04:32:53 +02:00
|
|
|
import okDialog from './ui/okDialog';
|
2018-10-26 03:55:11 +02:00
|
|
|
import copyDialog from './ui/copyDialog';
|
2019-03-10 02:59:06 +01:00
|
|
|
import shareDialog from './ui/shareDialog';
|
2018-12-21 20:40:52 +01:00
|
|
|
import signupDialog from './ui/signupDialog';
|
2019-04-26 22:30:33 +02:00
|
|
|
import surveyDialog from './ui/surveyDialog';
|
2017-08-24 23:54:02 +02:00
|
|
|
|
|
|
|
export default function(state, emitter) {
|
|
|
|
let lastRender = 0;
|
2017-09-13 21:01:55 +02:00
|
|
|
let updateTitle = false;
|
2017-08-24 23:54:02 +02:00
|
|
|
|
|
|
|
function render() {
|
|
|
|
emitter.emit('render');
|
|
|
|
}
|
|
|
|
|
|
|
|
async function checkFiles() {
|
2018-08-08 00:40:17 +02:00
|
|
|
const changes = await state.user.syncFileList();
|
|
|
|
const rerender = changes.incoming || changes.downloadCount;
|
2017-08-24 23:54:02 +02:00
|
|
|
if (rerender) {
|
|
|
|
render();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 21:01:55 +02:00
|
|
|
function updateProgress() {
|
|
|
|
if (updateTitle) {
|
|
|
|
emitter.emit('DOMTitleChange', percent(state.transfer.progressRatio));
|
|
|
|
}
|
|
|
|
render();
|
|
|
|
}
|
|
|
|
|
|
|
|
emitter.on('DOMContentLoaded', () => {
|
|
|
|
document.addEventListener('blur', () => (updateTitle = true));
|
|
|
|
document.addEventListener('focus', () => {
|
|
|
|
updateTitle = false;
|
2020-10-15 18:48:54 +02:00
|
|
|
emitter.emit('DOMTitleChange', 'Send');
|
2017-09-13 21:01:55 +02:00
|
|
|
});
|
|
|
|
checkFiles();
|
|
|
|
});
|
2017-08-24 23:54:02 +02:00
|
|
|
|
|
|
|
emitter.on('render', () => {
|
|
|
|
lastRender = Date.now();
|
|
|
|
});
|
|
|
|
|
2018-09-24 21:01:39 +02:00
|
|
|
emitter.on('login', email => {
|
|
|
|
state.user.login(email);
|
2018-08-08 00:40:17 +02:00
|
|
|
});
|
|
|
|
|
2020-07-25 03:11:50 +02:00
|
|
|
emitter.on('logout', async () => {
|
|
|
|
await state.user.logout();
|
2019-02-12 20:50:06 +01:00
|
|
|
metrics.loggedOut({ trigger: 'button' });
|
2018-08-31 23:20:15 +02:00
|
|
|
emitter.emit('pushState', '/');
|
2018-08-08 00:40:17 +02:00
|
|
|
});
|
|
|
|
|
2018-10-25 04:07:10 +02:00
|
|
|
emitter.on('removeUpload', file => {
|
|
|
|
state.archive.remove(file);
|
2019-03-06 19:43:58 +01:00
|
|
|
if (state.archive.numFiles === 0) {
|
|
|
|
state.archive.clear();
|
|
|
|
}
|
2018-08-03 21:24:41 +02:00
|
|
|
render();
|
2018-07-31 20:09:18 +02:00
|
|
|
});
|
|
|
|
|
2019-02-12 20:50:06 +01:00
|
|
|
emitter.on('delete', async ownedFile => {
|
2017-08-24 23:54:02 +02:00
|
|
|
try {
|
|
|
|
metrics.deletedUpload({
|
2019-02-12 20:50:06 +01:00
|
|
|
size: ownedFile.size,
|
|
|
|
time: ownedFile.time,
|
|
|
|
speed: ownedFile.speed,
|
|
|
|
type: ownedFile.type,
|
|
|
|
ttl: ownedFile.expiresAt - Date.now(),
|
2017-08-24 23:54:02 +02:00
|
|
|
location
|
|
|
|
});
|
2019-02-12 20:50:06 +01:00
|
|
|
state.storage.remove(ownedFile.id);
|
|
|
|
await ownedFile.del();
|
2017-08-24 23:54:02 +02:00
|
|
|
} catch (e) {
|
2019-08-09 20:06:21 +02:00
|
|
|
state.sentry.captureException(e);
|
2017-08-24 23:54:02 +02:00
|
|
|
}
|
2018-10-25 04:07:10 +02:00
|
|
|
render();
|
2017-08-24 23:54:02 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
emitter.on('cancel', () => {
|
|
|
|
state.transfer.cancel();
|
|
|
|
});
|
|
|
|
|
2018-07-31 20:09:18 +02:00
|
|
|
emitter.on('addFiles', async ({ files }) => {
|
2018-11-19 19:48:52 +01:00
|
|
|
if (files.length < 1) {
|
|
|
|
return;
|
|
|
|
}
|
2018-08-08 00:40:17 +02:00
|
|
|
const maxSize = state.user.maxSize;
|
|
|
|
try {
|
2019-02-26 19:39:50 +01:00
|
|
|
state.archive.addFiles(
|
|
|
|
files,
|
|
|
|
maxSize,
|
|
|
|
state.LIMITS.MAX_FILES_PER_ARCHIVE
|
|
|
|
);
|
2018-08-08 00:40:17 +02:00
|
|
|
} catch (e) {
|
2019-02-26 19:39:50 +01:00
|
|
|
if (e.message === 'fileTooBig' && maxSize < state.LIMITS.MAX_FILE_SIZE) {
|
2019-02-12 20:50:06 +01:00
|
|
|
return emitter.emit('signup-cta', 'size');
|
2018-12-21 20:40:52 +01:00
|
|
|
}
|
2019-02-12 20:50:06 +01:00
|
|
|
state.modal = okDialog(
|
|
|
|
state.translate(e.message, {
|
|
|
|
size: bytes(maxSize),
|
2019-02-26 19:39:50 +01:00
|
|
|
count: state.LIMITS.MAX_FILES_PER_ARCHIVE
|
2019-02-12 20:50:06 +01:00
|
|
|
})
|
|
|
|
);
|
2018-07-31 20:09:18 +02:00
|
|
|
}
|
|
|
|
render();
|
|
|
|
});
|
|
|
|
|
2019-02-12 20:50:06 +01:00
|
|
|
emitter.on('signup-cta', source => {
|
2019-03-05 21:58:40 +01:00
|
|
|
const query = state.query;
|
|
|
|
state.user.startAuthFlow(source, {
|
|
|
|
campaign: query.utm_campaign,
|
|
|
|
content: query.utm_content,
|
|
|
|
medium: query.utm_medium,
|
|
|
|
source: query.utm_source,
|
|
|
|
term: query.utm_term
|
|
|
|
});
|
2019-02-12 20:50:06 +01:00
|
|
|
state.modal = signupDialog(source);
|
|
|
|
render();
|
|
|
|
});
|
|
|
|
|
|
|
|
emitter.on('authenticate', async (code, oauthState) => {
|
|
|
|
try {
|
|
|
|
await state.user.finishLogin(code, oauthState);
|
|
|
|
await state.user.syncFileList();
|
|
|
|
emitter.emit('replaceState', '/');
|
|
|
|
} catch (e) {
|
|
|
|
emitter.emit('replaceState', '/error');
|
|
|
|
setTimeout(render);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
emitter.on('upload', async () => {
|
2019-02-26 19:39:50 +01:00
|
|
|
if (state.storage.files.length >= state.LIMITS.MAX_ARCHIVES_PER_USER) {
|
2018-09-07 19:53:40 +02:00
|
|
|
state.modal = okDialog(
|
2018-08-08 00:40:17 +02:00
|
|
|
state.translate('tooManyArchives', {
|
2019-02-26 19:39:50 +01:00
|
|
|
count: state.LIMITS.MAX_ARCHIVES_PER_USER
|
2018-08-08 00:40:17 +02:00
|
|
|
})
|
|
|
|
);
|
2018-09-07 19:53:40 +02:00
|
|
|
return render();
|
2018-08-08 00:40:17 +02:00
|
|
|
}
|
2019-02-12 20:50:06 +01:00
|
|
|
const archive = state.archive;
|
2018-08-31 23:20:15 +02:00
|
|
|
const sender = new FileSender();
|
2018-08-08 20:07:09 +02:00
|
|
|
|
2017-09-13 21:01:55 +02:00
|
|
|
sender.on('progress', updateProgress);
|
2017-08-24 23:54:02 +02:00
|
|
|
sender.on('encrypting', render);
|
2018-07-31 20:09:18 +02:00
|
|
|
sender.on('complete', render);
|
2017-08-24 23:54:02 +02:00
|
|
|
state.transfer = sender;
|
2018-02-01 00:47:34 +01:00
|
|
|
state.uploading = true;
|
2017-08-24 23:54:02 +02:00
|
|
|
render();
|
2018-01-24 19:23:13 +01:00
|
|
|
|
2017-08-24 23:54:02 +02:00
|
|
|
const links = openLinksInNewTab();
|
|
|
|
await delay(200);
|
2019-02-12 20:50:06 +01:00
|
|
|
const start = Date.now();
|
2017-08-24 23:54:02 +02:00
|
|
|
try {
|
2019-02-12 20:50:06 +01:00
|
|
|
const ownedFile = await sender.upload(archive, state.user.bearerToken);
|
2018-01-24 19:23:13 +01:00
|
|
|
state.storage.totalUploads += 1;
|
2019-02-12 20:50:06 +01:00
|
|
|
const duration = Date.now() - start;
|
|
|
|
metrics.completedUpload(archive, duration);
|
2018-01-24 19:23:13 +01:00
|
|
|
|
|
|
|
state.storage.addFile(ownedFile);
|
2018-08-31 23:20:15 +02:00
|
|
|
// TODO integrate password into /upload request
|
2019-02-12 20:50:06 +01:00
|
|
|
if (archive.password) {
|
|
|
|
emitter.emit('password', {
|
|
|
|
password: archive.password,
|
|
|
|
file: ownedFile
|
|
|
|
});
|
2018-07-31 20:09:18 +02:00
|
|
|
}
|
2019-03-10 02:59:06 +01:00
|
|
|
state.modal = state.capabilities.share
|
|
|
|
? shareDialog(ownedFile.name, ownedFile.url)
|
|
|
|
: copyDialog(ownedFile.name, ownedFile.url);
|
2017-08-24 23:54:02 +02:00
|
|
|
} catch (err) {
|
|
|
|
if (err.message === '0') {
|
|
|
|
//cancelled. do nothing
|
2019-08-08 18:52:22 +02:00
|
|
|
metrics.cancelledUpload(archive, err.duration);
|
2018-03-12 18:15:11 +01:00
|
|
|
render();
|
2020-07-25 03:11:50 +02:00
|
|
|
} else if (err.message === '401') {
|
|
|
|
const refreshed = await state.user.refresh();
|
|
|
|
if (refreshed) {
|
|
|
|
return emitter.emit('upload');
|
|
|
|
}
|
|
|
|
emitter.emit('pushState', '/error');
|
2018-03-12 18:15:11 +01:00
|
|
|
} else {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(err);
|
2019-08-09 20:06:21 +02:00
|
|
|
state.sentry.withScope(scope => {
|
|
|
|
scope.setExtra('duration', err.duration);
|
|
|
|
scope.setExtra('size', err.size);
|
|
|
|
state.sentry.captureException(err);
|
2019-08-09 19:09:34 +02:00
|
|
|
});
|
2019-08-08 18:52:22 +02:00
|
|
|
metrics.stoppedUpload(archive, err.duration);
|
2018-03-12 18:15:11 +01:00
|
|
|
emitter.emit('pushState', '/error');
|
2017-08-24 23:54:02 +02:00
|
|
|
}
|
2018-02-01 00:47:34 +01:00
|
|
|
} finally {
|
2018-03-12 18:15:11 +01:00
|
|
|
openLinksInNewTab(links, false);
|
2019-02-12 20:50:06 +01:00
|
|
|
archive.clear();
|
2018-02-01 00:47:34 +01:00
|
|
|
state.uploading = false;
|
|
|
|
state.transfer = null;
|
2018-12-21 19:27:46 +01:00
|
|
|
await state.user.syncFileList();
|
2018-10-25 04:07:10 +02:00
|
|
|
render();
|
2017-08-24 23:54:02 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-01-24 19:23:13 +01:00
|
|
|
emitter.on('password', async ({ password, file }) => {
|
2017-08-31 18:43:36 +02:00
|
|
|
try {
|
2018-02-16 21:56:53 +01:00
|
|
|
state.settingPassword = true;
|
|
|
|
render();
|
2018-01-24 19:23:13 +01:00
|
|
|
await file.setPassword(password);
|
|
|
|
state.storage.writeFile(file);
|
2018-02-16 21:56:53 +01:00
|
|
|
await delay(1000);
|
2018-01-24 19:23:13 +01:00
|
|
|
} catch (err) {
|
2018-03-02 06:36:45 +01:00
|
|
|
// eslint-disable-next-line no-console
|
2018-01-24 19:23:13 +01:00
|
|
|
console.error(err);
|
2018-02-21 21:35:52 +01:00
|
|
|
state.passwordSetError = err;
|
|
|
|
} finally {
|
|
|
|
state.settingPassword = false;
|
2017-08-31 18:43:36 +02:00
|
|
|
}
|
|
|
|
render();
|
|
|
|
});
|
|
|
|
|
2018-01-24 19:23:13 +01:00
|
|
|
emitter.on('getMetadata', async () => {
|
2017-08-31 18:43:36 +02:00
|
|
|
const file = state.fileInfo;
|
2018-07-07 00:49:50 +02:00
|
|
|
|
2018-01-24 19:23:13 +01:00
|
|
|
const receiver = new FileReceiver(file);
|
2017-08-31 18:43:36 +02:00
|
|
|
try {
|
2018-01-24 19:23:13 +01:00
|
|
|
await receiver.getMetadata();
|
|
|
|
state.transfer = receiver;
|
2017-08-31 18:43:36 +02:00
|
|
|
} catch (e) {
|
2018-08-08 00:40:17 +02:00
|
|
|
if (e.message === '401' || e.message === '404') {
|
2017-08-31 18:43:36 +02:00
|
|
|
file.password = null;
|
2018-01-24 19:23:13 +01:00
|
|
|
if (!file.requiresPassword) {
|
2017-08-31 18:43:36 +02:00
|
|
|
return emitter.emit('pushState', '/404');
|
|
|
|
}
|
2020-07-30 03:52:55 +02:00
|
|
|
} else {
|
|
|
|
console.error(e);
|
|
|
|
return emitter.emit('pushState', '/error');
|
2017-08-31 18:43:36 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-05 21:40:49 +02:00
|
|
|
|
2017-08-24 23:54:02 +02:00
|
|
|
render();
|
2017-08-31 18:43:36 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
emitter.on('download', async file => {
|
2018-02-24 20:24:12 +01:00
|
|
|
state.transfer.on('progress', updateProgress);
|
2017-08-31 18:43:36 +02:00
|
|
|
state.transfer.on('decrypting', render);
|
2018-07-31 20:09:18 +02:00
|
|
|
state.transfer.on('complete', render);
|
2017-08-31 18:43:36 +02:00
|
|
|
const links = openLinksInNewTab();
|
|
|
|
const size = file.size;
|
2019-02-12 20:50:06 +01:00
|
|
|
const start = Date.now();
|
2017-08-24 23:54:02 +02:00
|
|
|
try {
|
2018-07-31 20:29:26 +02:00
|
|
|
const dl = state.transfer.download({
|
|
|
|
stream: state.capabilities.streamDownload
|
|
|
|
});
|
2018-02-24 20:24:12 +01:00
|
|
|
render();
|
|
|
|
await dl;
|
2017-08-24 23:54:02 +02:00
|
|
|
state.storage.totalDownloads += 1;
|
2019-02-12 20:50:06 +01:00
|
|
|
const duration = Date.now() - start;
|
|
|
|
metrics.completedDownload({
|
|
|
|
size,
|
|
|
|
duration,
|
|
|
|
password_protected: file.requiresPassword
|
|
|
|
});
|
2017-08-24 23:54:02 +02:00
|
|
|
} catch (err) {
|
2018-01-24 19:23:13 +01:00
|
|
|
if (err.message === '0') {
|
|
|
|
// download cancelled
|
2018-02-05 18:11:42 +01:00
|
|
|
state.transfer.reset();
|
2018-03-12 18:15:11 +01:00
|
|
|
render();
|
|
|
|
} else {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
state.transfer = null;
|
|
|
|
const location = err.message === '404' ? '/404' : '/error';
|
|
|
|
if (location === '/error') {
|
2019-08-09 20:06:21 +02:00
|
|
|
state.sentry.withScope(scope => {
|
|
|
|
scope.setExtra('duration', err.duration);
|
|
|
|
scope.setExtra('size', err.size);
|
|
|
|
scope.setExtra('progress', err.progress);
|
|
|
|
state.sentry.captureException(err);
|
2019-08-09 19:09:34 +02:00
|
|
|
});
|
2019-02-12 20:50:06 +01:00
|
|
|
const duration = Date.now() - start;
|
|
|
|
metrics.stoppedDownload({
|
|
|
|
size,
|
|
|
|
duration,
|
|
|
|
password_protected: file.requiresPassword
|
|
|
|
});
|
2018-03-12 18:15:11 +01:00
|
|
|
}
|
|
|
|
emitter.emit('pushState', location);
|
2017-08-24 23:54:02 +02:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
openLinksInNewTab(links, false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-02-12 20:50:06 +01:00
|
|
|
emitter.on('copy', ({ url }) => {
|
2017-08-24 23:54:02 +02:00
|
|
|
copyToClipboard(url);
|
2019-02-12 20:50:06 +01:00
|
|
|
// metrics.copiedLink({ location });
|
2017-08-24 23:54:02 +02:00
|
|
|
});
|
|
|
|
|
2019-04-26 22:30:33 +02:00
|
|
|
emitter.on('closeModal', () => {
|
|
|
|
if (
|
|
|
|
state.PREFS.surveyUrl &&
|
|
|
|
['copy', 'share'].includes(state.modal.type) &&
|
|
|
|
locale().startsWith('en') &&
|
|
|
|
(state.storage.totalUploads > 1 || state.storage.totalDownloads > 0) &&
|
|
|
|
!state.user.surveyed
|
|
|
|
) {
|
|
|
|
state.user.surveyed = true;
|
|
|
|
state.modal = surveyDialog();
|
|
|
|
} else {
|
|
|
|
state.modal = null;
|
|
|
|
}
|
|
|
|
render();
|
|
|
|
});
|
|
|
|
|
2018-01-24 19:23:13 +01:00
|
|
|
setInterval(() => {
|
2018-12-12 19:14:06 +01:00
|
|
|
// poll for updates of the upload list
|
2019-03-01 23:12:23 +01:00
|
|
|
if (!state.modal && state.route === '/') {
|
2018-01-24 19:23:13 +01:00
|
|
|
checkFiles();
|
|
|
|
}
|
|
|
|
}, 2 * 60 * 1000);
|
|
|
|
|
2017-08-24 23:54:02 +02:00
|
|
|
setInterval(() => {
|
|
|
|
// poll for rerendering the file list countdown timers
|
|
|
|
if (
|
2019-03-01 23:12:23 +01:00
|
|
|
!state.modal &&
|
2017-08-24 23:54:02 +02:00
|
|
|
state.route === '/' &&
|
|
|
|
state.storage.files.length > 0 &&
|
|
|
|
Date.now() - lastRender > 30000
|
|
|
|
) {
|
|
|
|
render();
|
|
|
|
}
|
|
|
|
}, 60000);
|
|
|
|
}
|