2017-08-24 21:54:02 +00:00
|
|
|
import FileSender from './fileSender';
|
|
|
|
import FileReceiver from './fileReceiver';
|
2018-01-24 18:23:13 +00:00
|
|
|
import {
|
|
|
|
copyToClipboard,
|
|
|
|
delay,
|
|
|
|
fadeOut,
|
|
|
|
openLinksInNewTab,
|
|
|
|
percent,
|
|
|
|
saveFile
|
|
|
|
} from './utils';
|
2017-08-24 21:54:02 +00:00
|
|
|
import * as metrics from './metrics';
|
|
|
|
|
|
|
|
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() {
|
|
|
|
const files = state.storage.files;
|
|
|
|
let rerender = false;
|
|
|
|
for (const file of files) {
|
2018-01-23 02:11:48 +00:00
|
|
|
const oldLimit = file.dlimit;
|
|
|
|
const oldTotal = file.dtotal;
|
2018-01-24 18:23:13 +00:00
|
|
|
await file.updateDownloadCount();
|
|
|
|
if (file.dtotal === file.dlimit) {
|
2017-08-24 21:54:02 +00:00
|
|
|
state.storage.remove(file.id);
|
|
|
|
rerender = true;
|
2018-01-24 18:23:13 +00:00
|
|
|
} else if (oldLimit !== file.dlimit || oldTotal !== file.dtotal) {
|
2018-01-23 02:11:48 +00:00
|
|
|
rerender = true;
|
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
|
|
|
|
2018-01-24 18:23:13 +00:00
|
|
|
// emitter.on('navigate', checkFiles);
|
2017-08-24 21:54:02 +00:00
|
|
|
|
|
|
|
emitter.on('render', () => {
|
|
|
|
lastRender = Date.now();
|
|
|
|
});
|
|
|
|
|
2017-11-30 21:41:09 +00:00
|
|
|
emitter.on('changeLimit', async ({ file, value }) => {
|
2018-01-24 18:23:13 +00:00
|
|
|
await file.changeLimit(value);
|
|
|
|
state.storage.writeFile(file);
|
2017-11-30 21:41:09 +00:00
|
|
|
metrics.changedDownloadLimit(file);
|
|
|
|
});
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
emitter.on('cancel', () => {
|
|
|
|
state.transfer.cancel();
|
|
|
|
});
|
|
|
|
|
|
|
|
emitter.on('upload', async ({ file, type }) => {
|
|
|
|
const size = file.size;
|
|
|
|
const sender = new FileSender(file);
|
2017-09-13 19:01:55 +00:00
|
|
|
sender.on('progress', updateProgress);
|
2017-08-24 21:54:02 +00:00
|
|
|
sender.on('encrypting', render);
|
|
|
|
state.transfer = sender;
|
|
|
|
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-01-31 19:12:36 +00:00
|
|
|
const ownedFile = await sender.upload();
|
2018-01-24 18:23:13 +00:00
|
|
|
state.storage.totalUploads += 1;
|
|
|
|
metrics.completedUpload(ownedFile);
|
|
|
|
|
|
|
|
state.storage.addFile(ownedFile);
|
|
|
|
|
2017-10-04 20:34:41 +00:00
|
|
|
document.getElementById('cancel-upload').hidden = 'hidden';
|
2017-08-24 21:54:02 +00:00
|
|
|
await delay(1000);
|
|
|
|
await fadeOut('upload-progress');
|
|
|
|
openLinksInNewTab(links, false);
|
|
|
|
state.transfer = null;
|
2018-01-24 18:23:13 +00:00
|
|
|
|
|
|
|
emitter.emit('pushState', `/share/${ownedFile.id}`);
|
2017-08-24 21:54:02 +00:00
|
|
|
} catch (err) {
|
2017-08-31 16:43:36 +00:00
|
|
|
console.error(err);
|
2017-08-24 21:54:02 +00:00
|
|
|
state.transfer = null;
|
|
|
|
if (err.message === '0') {
|
|
|
|
//cancelled. do nothing
|
|
|
|
metrics.cancelledUpload({ size, type });
|
|
|
|
return render();
|
|
|
|
}
|
|
|
|
state.raven.captureException(err);
|
|
|
|
metrics.stoppedUpload({ size, type, err });
|
2017-08-31 16:43:36 +00:00
|
|
|
emitter.emit('pushState', '/error');
|
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-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-01-24 18:23:13 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
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-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();
|
|
|
|
receiver.on('progress', updateProgress);
|
|
|
|
receiver.on('decrypting', render);
|
|
|
|
state.transfer = receiver;
|
2017-08-31 16:43:36 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (e.message === '401') {
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-24 21:54:02 +00:00
|
|
|
render();
|
2017-08-31 16:43:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
emitter.on('download', async file => {
|
|
|
|
state.transfer.on('progress', render);
|
|
|
|
state.transfer.on('decrypting', render);
|
|
|
|
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-01-24 18:23:13 +00:00
|
|
|
const f = await state.transfer.download();
|
2017-08-24 21:54:02 +00:00
|
|
|
const time = Date.now() - start;
|
|
|
|
const speed = size / (time / 1000);
|
|
|
|
await delay(1000);
|
|
|
|
await fadeOut('download-progress');
|
|
|
|
saveFile(f);
|
|
|
|
state.storage.totalDownloads += 1;
|
2017-10-24 20:25:45 +00:00
|
|
|
state.transfer = null;
|
2017-08-24 21:54:02 +00:00
|
|
|
metrics.completedDownload({ size, time, speed });
|
|
|
|
emitter.emit('pushState', '/completed');
|
|
|
|
} catch (err) {
|
2018-01-24 18:23:13 +00:00
|
|
|
if (err.message === '0') {
|
|
|
|
// download cancelled
|
|
|
|
return render();
|
|
|
|
}
|
2017-08-31 16:43:36 +00:00
|
|
|
console.error(err);
|
2017-08-24 21:54:02 +00:00
|
|
|
const location = err.message === 'notfound' ? '/404' : '/error';
|
|
|
|
if (location === '/error') {
|
|
|
|
state.raven.captureException(err);
|
|
|
|
metrics.stoppedDownload({ size, err });
|
|
|
|
}
|
2017-08-31 16:43:36 +00:00
|
|
|
emitter.emit('pushState', location);
|
2017-08-24 21:54:02 +00:00
|
|
|
} finally {
|
|
|
|
state.transfer = null;
|
|
|
|
openLinksInNewTab(links, false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
emitter.on('copy', ({ url, location }) => {
|
|
|
|
copyToClipboard(url);
|
|
|
|
metrics.copiedLink({ location });
|
|
|
|
});
|
|
|
|
|
2018-01-24 18:23:13 +00:00
|
|
|
setInterval(() => {
|
|
|
|
// poll for updates of the download counts
|
|
|
|
// TODO something for the share page: || state.route === '/share/:id'
|
|
|
|
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);
|
|
|
|
}
|