mirror of
https://github.com/szimek/sharedrop.git
synced 2026-06-28 21:00:35 +08:00
89 lines
2.3 KiB
JavaScript
89 lines
2.3 KiB
JavaScript
/* jshint -W030 */
|
|
import $ from 'jquery';
|
|
import { Promise } from 'rsvp';
|
|
import config from 'share-drop/config/environment';
|
|
|
|
import FileSystem from '../services/file';
|
|
import Analytics from '../services/analytics';
|
|
|
|
export function initialize(application) {
|
|
function checkWebRTCSupport() {
|
|
return new Promise((resolve, reject) => {
|
|
// window.util is a part of PeerJS library
|
|
if (window.util.supports.sctp) {
|
|
resolve();
|
|
} else {
|
|
// eslint-disable-next-line prefer-promise-reject-errors
|
|
reject('browser-unsupported');
|
|
}
|
|
});
|
|
}
|
|
|
|
function clearFileSystem() {
|
|
return new Promise((resolve, reject) => {
|
|
// TODO: change File into a service and require it here
|
|
FileSystem.removeAll()
|
|
.then(() => {
|
|
resolve();
|
|
})
|
|
.catch(() => {
|
|
// eslint-disable-next-line prefer-promise-reject-errors
|
|
reject('filesystem-unavailable');
|
|
});
|
|
});
|
|
}
|
|
|
|
function authenticateToFirebase() {
|
|
return new Promise((resolve, reject) => {
|
|
const xhr = $.getJSON('/auth');
|
|
xhr.then((data) => {
|
|
const ref = new window.Firebase(config.FIREBASE_URL);
|
|
// eslint-disable-next-line no-param-reassign
|
|
application.ref = ref;
|
|
// eslint-disable-next-line no-param-reassign
|
|
application.userId = data.id;
|
|
// eslint-disable-next-line no-param-reassign
|
|
application.publicIp = data.public_ip;
|
|
|
|
ref.authWithCustomToken(data.token, (error) => {
|
|
if (error) {
|
|
reject(error);
|
|
} else {
|
|
resolve();
|
|
}
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
// TODO: move it to a separate initializer
|
|
function trackSizeOfReceivedFiles() {
|
|
$.subscribe('file_received.p2p', (event, data) => {
|
|
Analytics.trackEvent('received', {
|
|
event_category: 'file',
|
|
event_label: 'size',
|
|
value: Math.round(data.info.size / 1000),
|
|
});
|
|
});
|
|
}
|
|
|
|
application.deferReadiness();
|
|
|
|
checkWebRTCSupport()
|
|
.then(clearFileSystem)
|
|
.catch((error) => {
|
|
// eslint-disable-next-line no-param-reassign
|
|
application.error = error;
|
|
})
|
|
.then(authenticateToFirebase)
|
|
.then(trackSizeOfReceivedFiles)
|
|
.then(() => {
|
|
application.advanceReadiness();
|
|
});
|
|
}
|
|
|
|
export default {
|
|
name: 'prerequisites',
|
|
initialize,
|
|
};
|