2018-08-07 22:40:17 +00:00
|
|
|
/* global DEFAULTS LIMITS */
|
2017-08-24 21:54:02 +00:00
|
|
|
import FileSender from './fileSender';
|
|
|
|
import FileReceiver from './fileReceiver';
|
2018-07-31 18:09:18 +00:00
|
|
|
import { copyToClipboard, delay, openLinksInNewTab, percent } from './utils';
|
2017-08-24 21:54:02 +00:00
|
|
|
import * as metrics from './metrics';
|
2018-08-03 19:24:41 +00:00
|
|
|
import { bytes } from './utils';
|
2018-10-25 02:32:53 +00:00
|
|
|
import okDialog from './ui/okDialog';
|
2018-10-26 01:55:11 +00:00
|
|
|
import copyDialog from './ui/copyDialog';
|
2018-12-21 19:40:52 +00:00
|
|
|
import signupDialog from './ui/signupDialog';
|
2017-08-24 21:54:02 +00:00
|
|
|
|
|
|
|
export default function(state, emitter) {
|
|
|
|
let lastRender = 0;
|
2017-09-13 19:01:55 +00:00
|
|
|
let updateTitle = false;
|
2017-08-24 21:54:02 +00:00
|
|
|
|
|
|
|
function render() {
|
|
|
|
emitter.emit('render');
|
|
|
|
}
|
|
|
|
|
|
|
|
async function checkFiles() {
|
2018-08-07 22:40:17 +00:00
|
|
|
const changes = await state.user.syncFileList();
|
|
|
|
const rerender = changes.incoming || changes.downloadCount;
|
2017-08-24 21:54:02 +00:00
|
|
|
if (rerender) {
|
|
|
|
render();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 19:01:55 +00: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;
|
|
|
|
emitter.emit('DOMTitleChange', 'Firefox Send');
|
|
|
|
});
|
|
|
|
checkFiles();
|
|
|
|
});
|
2017-08-24 21:54:02 +00:00
|
|
|
|
|
|
|
emitter.on('render', () => {
|
|
|
|
lastRender = Date.now();
|
|
|
|
});
|
|
|
|
|
2018-09-24 19:01:39 +00:00
|
|
|
emitter.on('login', email => {
|
|
|
|
state.user.login(email);
|
2018-08-07 22:40:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
emitter.on('logout', () => {
|
|
|
|
state.user.logout();
|
2018-09-24 22:25:08 +00:00
|
|
|
state.timeLimit = DEFAULTS.EXPIRE_SECONDS;
|
|
|
|
state.downloadCount = 1;
|
2018-08-31 21:20:15 +00:00
|
|
|
emitter.emit('pushState', '/');
|
2018-08-07 22:40:17 +00:00
|
|
|
});
|
|
|
|
|
2017-11-30 21:41:09 +00:00
|
|
|
emitter.on('changeLimit', async ({ file, value }) => {
|
2018-08-31 17:59:26 +00:00
|
|
|
const ok = await file.changeLimit(value, state.user);
|
|
|
|
if (!ok) {
|
|
|
|
return;
|
|
|
|
}
|
2018-01-24 18:23:13 +00:00
|
|
|
state.storage.writeFile(file);
|
2017-11-30 21:41:09 +00:00
|
|
|
metrics.changedDownloadLimit(file);
|
|
|
|
});
|
|
|
|
|
2018-10-25 02:07:10 +00:00
|
|
|
emitter.on('removeUpload', file => {
|
|
|
|
state.archive.remove(file);
|
2018-08-03 19:24:41 +00:00
|
|
|
render();
|
2018-07-31 18:09:18 +00:00
|
|
|
});
|
|
|
|
|
2017-08-24 21:54:02 +00:00
|
|
|
emitter.on('delete', async ({ file, location }) => {
|
|
|
|
try {
|
|
|
|
metrics.deletedUpload({
|
|
|
|
size: file.size,
|
|
|
|
time: file.time,
|
|
|
|
speed: file.speed,
|
|
|
|
type: file.type,
|
|
|
|
ttl: file.expiresAt - Date.now(),
|
|
|
|
location
|
|
|
|
});
|
|
|
|
state.storage.remove(file.id);
|
2018-01-24 18:23:13 +00:00
|
|
|
await file.del();
|
2017-08-24 21:54:02 +00:00
|
|
|
} catch (e) {
|
|
|
|
state.raven.captureException(e);
|
|
|
|
}
|
2018-10-25 02:07:10 +00:00
|
|
|
render();
|
2017-08-24 21:54:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
emitter.on('cancel', () => {
|
|
|
|
state.transfer.cancel();
|
|
|
|
});
|
|
|
|
|
2018-07-31 18:09:18 +00:00
|
|
|
emitter.on('addFiles', async ({ files }) => {
|
2018-11-19 18:48:52 +00:00
|
|
|
if (files.length < 1) {
|
|
|
|
return;
|
|
|
|
}
|
2018-08-07 22:40:17 +00:00
|
|
|
const maxSize = state.user.maxSize;
|
|
|
|
try {
|
|
|
|
state.archive.addFiles(files, maxSize);
|
|
|
|
} catch (e) {
|
2018-12-21 19:40:52 +00:00
|
|
|
if (e.message === 'fileTooBig' && maxSize < LIMITS.MAX_FILE_SIZE) {
|
|
|
|
state.modal = signupDialog();
|
|
|
|
} else {
|
|
|
|
state.modal = okDialog(
|
|
|
|
state.translate(e.message, {
|
|
|
|
size: bytes(maxSize),
|
|
|
|
count: LIMITS.MAX_FILES_PER_ARCHIVE
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2018-07-31 18:09:18 +00:00
|
|
|
}
|
|
|
|
render();
|
|
|
|
});
|
|
|
|
|
2018-08-31 21:20:15 +00:00
|
|
|
emitter.on('upload', async ({ type, dlimit, password }) => {
|
2018-08-07 22:40:17 +00:00
|
|
|
if (state.storage.files.length >= LIMITS.MAX_ARCHIVES_PER_USER) {
|
2018-09-07 17:53:40 +00:00
|
|
|
state.modal = okDialog(
|
2018-08-07 22:40:17 +00:00
|
|
|
state.translate('tooManyArchives', {
|
|
|
|
count: LIMITS.MAX_ARCHIVES_PER_USER
|
|
|
|
})
|
|
|
|
);
|
2018-09-07 17:53:40 +00:00
|
|
|
return render();
|
2018-08-07 22:40:17 +00:00
|
|
|
}
|
2018-08-03 19:24:41 +00:00
|
|
|
const size = state.archive.size;
|
2018-08-07 22:40:17 +00:00
|
|
|
if (!state.timeLimit) state.timeLimit = DEFAULTS.EXPIRE_SECONDS;
|
2018-08-31 21:20:15 +00:00
|
|
|
const sender = new FileSender();
|
2018-08-08 18:07:09 +00:00
|
|
|
|
2017-09-13 19:01:55 +00:00
|
|
|
sender.on('progress', updateProgress);
|
2017-08-24 21:54:02 +00:00
|
|
|
sender.on('encrypting', render);
|
2018-07-31 18:09:18 +00:00
|
|
|
sender.on('complete', render);
|
2017-08-24 21:54:02 +00:00
|
|
|
state.transfer = sender;
|
2018-01-31 23:47:34 +00:00
|
|
|
state.uploading = true;
|
2017-08-24 21:54:02 +00:00
|
|
|
render();
|
2018-01-24 18:23:13 +00:00
|
|
|
|
2017-08-24 21:54:02 +00:00
|
|
|
const links = openLinksInNewTab();
|
|
|
|
await delay(200);
|
|
|
|
try {
|
|
|
|
metrics.startedUpload({ size, type });
|
2018-07-31 18:09:18 +00:00
|
|
|
|
2018-08-31 21:20:15 +00:00
|
|
|
const ownedFile = await sender.upload(
|
|
|
|
state.archive,
|
|
|
|
state.timeLimit,
|
|
|
|
dlimit,
|
|
|
|
state.user.bearerToken
|
|
|
|
);
|
2018-02-02 18:15:17 +00:00
|
|
|
ownedFile.type = type;
|
2018-01-24 18:23:13 +00:00
|
|
|
state.storage.totalUploads += 1;
|
|
|
|
metrics.completedUpload(ownedFile);
|
|
|
|
|
|
|
|
state.storage.addFile(ownedFile);
|
2018-08-31 21:20:15 +00:00
|
|
|
// TODO integrate password into /upload request
|
2018-07-31 18:09:18 +00:00
|
|
|
if (password) {
|
|
|
|
emitter.emit('password', { password, file: ownedFile });
|
|
|
|
}
|
2018-10-29 16:52:24 +00:00
|
|
|
state.modal = copyDialog(ownedFile.name, ownedFile.url);
|
2017-08-24 21:54:02 +00:00
|
|
|
} catch (err) {
|
|
|
|
if (err.message === '0') {
|
|
|
|
//cancelled. do nothing
|
|
|
|
metrics.cancelledUpload({ size, type });
|
2018-03-12 17:15:11 +00:00
|
|
|
render();
|
|
|
|
} else {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(err);
|
|
|
|
state.raven.captureException(err);
|
|
|
|
metrics.stoppedUpload({ size, type, err });
|
|
|
|
emitter.emit('pushState', '/error');
|
2017-08-24 21:54:02 +00:00
|
|
|
}
|
2018-01-31 23:47:34 +00:00
|
|
|
} finally {
|
2018-03-12 17:15:11 +00:00
|
|
|
openLinksInNewTab(links, false);
|
2018-12-21 18:27:46 +00:00
|
|
|
state.archive.clear();
|
2018-07-31 18:09:18 +00:00
|
|
|
state.password = '';
|
2018-01-31 23:47:34 +00:00
|
|
|
state.uploading = false;
|
|
|
|
state.transfer = null;
|
2018-12-21 18:27:46 +00:00
|
|
|
await state.user.syncFileList();
|
2018-10-25 02:07:10 +00:00
|
|
|
render();
|
2017-08-24 21:54:02 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-01-24 18:23:13 +00:00
|
|
|
emitter.on('password', async ({ password, file }) => {
|
2017-08-31 16:43:36 +00:00
|
|
|
try {
|
2018-02-16 20:56:53 +00:00
|
|
|
state.settingPassword = true;
|
|
|
|
render();
|
2018-01-24 18:23:13 +00:00
|
|
|
await file.setPassword(password);
|
|
|
|
state.storage.writeFile(file);
|
2017-08-31 16:43:36 +00:00
|
|
|
metrics.addedPassword({ size: file.size });
|
2018-02-16 20:56:53 +00:00
|
|
|
await delay(1000);
|
2018-01-24 18:23:13 +00:00
|
|
|
} catch (err) {
|
2018-03-02 05:36:45 +00:00
|
|
|
// eslint-disable-next-line no-console
|
2018-01-24 18:23:13 +00:00
|
|
|
console.error(err);
|
2018-02-21 20:35:52 +00:00
|
|
|
state.passwordSetError = err;
|
|
|
|
} finally {
|
|
|
|
state.settingPassword = false;
|
2017-08-31 16:43:36 +00:00
|
|
|
}
|
|
|
|
render();
|
|
|
|
});
|
|
|
|
|
2018-01-24 18:23:13 +00:00
|
|
|
emitter.on('getMetadata', async () => {
|
2017-08-31 16:43:36 +00:00
|
|
|
const file = state.fileInfo;
|
2018-07-06 22:49:50 +00:00
|
|
|
|
2018-01-24 18:23:13 +00:00
|
|
|
const receiver = new FileReceiver(file);
|
2017-08-31 16:43:36 +00:00
|
|
|
try {
|
2018-01-24 18:23:13 +00:00
|
|
|
await receiver.getMetadata();
|
|
|
|
state.transfer = receiver;
|
2017-08-31 16:43:36 +00:00
|
|
|
} catch (e) {
|
2018-08-07 22:40:17 +00:00
|
|
|
if (e.message === '401' || e.message === '404') {
|
2017-08-31 16:43:36 +00:00
|
|
|
file.password = null;
|
2018-01-24 18:23:13 +00:00
|
|
|
if (!file.requiresPassword) {
|
2017-08-31 16:43:36 +00:00
|
|
|
return emitter.emit('pushState', '/404');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-05 19:40:49 +00:00
|
|
|
|
2017-08-24 21:54:02 +00:00
|
|
|
render();
|
2017-08-31 16:43:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
emitter.on('download', async file => {
|
2018-02-24 19:24:12 +00:00
|
|
|
state.transfer.on('progress', updateProgress);
|
2017-08-31 16:43:36 +00:00
|
|
|
state.transfer.on('decrypting', render);
|
2018-07-31 18:09:18 +00:00
|
|
|
state.transfer.on('complete', render);
|
2017-08-31 16:43:36 +00:00
|
|
|
const links = openLinksInNewTab();
|
|
|
|
const size = file.size;
|
2017-08-24 21:54:02 +00:00
|
|
|
try {
|
|
|
|
const start = Date.now();
|
|
|
|
metrics.startedDownload({ size: file.size, ttl: file.ttl });
|
2018-07-31 18:29:26 +00:00
|
|
|
const dl = state.transfer.download({
|
|
|
|
stream: state.capabilities.streamDownload
|
|
|
|
});
|
2018-02-24 19:24:12 +00:00
|
|
|
render();
|
|
|
|
await dl;
|
2017-08-24 21:54:02 +00:00
|
|
|
const time = Date.now() - start;
|
|
|
|
const speed = size / (time / 1000);
|
|
|
|
state.storage.totalDownloads += 1;
|
|
|
|
metrics.completedDownload({ size, time, speed });
|
|
|
|
} catch (err) {
|
2018-01-24 18:23:13 +00:00
|
|
|
if (err.message === '0') {
|
|
|
|
// download cancelled
|
2018-02-05 17:11:42 +00:00
|
|
|
state.transfer.reset();
|
2018-03-12 17:15:11 +00:00
|
|
|
render();
|
|
|
|
} else {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(err);
|
|
|
|
state.transfer = null;
|
|
|
|
const location = err.message === '404' ? '/404' : '/error';
|
|
|
|
if (location === '/error') {
|
|
|
|
state.raven.captureException(err);
|
|
|
|
metrics.stoppedDownload({ size, err });
|
|
|
|
}
|
|
|
|
emitter.emit('pushState', location);
|
2017-08-24 21:54:02 +00:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
openLinksInNewTab(links, false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
emitter.on('copy', ({ url, location }) => {
|
|
|
|
copyToClipboard(url);
|
|
|
|
metrics.copiedLink({ location });
|
|
|
|
});
|
|
|
|
|
2018-01-24 18:23:13 +00:00
|
|
|
setInterval(() => {
|
2018-12-12 18:14:06 +00:00
|
|
|
// poll for updates of the upload list
|
2018-01-24 18:23:13 +00:00
|
|
|
if (state.route === '/') {
|
|
|
|
checkFiles();
|
|
|
|
}
|
|
|
|
}, 2 * 60 * 1000);
|
|
|
|
|
2017-08-24 21:54:02 +00:00
|
|
|
setInterval(() => {
|
|
|
|
// poll for rerendering the file list countdown timers
|
|
|
|
if (
|
|
|
|
state.route === '/' &&
|
|
|
|
state.storage.files.length > 0 &&
|
|
|
|
Date.now() - lastRender > 30000
|
|
|
|
) {
|
|
|
|
render();
|
|
|
|
}
|
|
|
|
}, 60000);
|
|
|
|
}
|