/* global downloadMetadata */
const html = require('choo/html');
const archiveTile = require('./archiveTile');
const modal = require('./modal');
const noStreams = require('./noStreams');
const notFound = require('./notFound');
const downloadPassword = require('./downloadPassword');
const downloadCompleted = require('./downloadCompleted');
const BIG_SIZE = 1024 * 1024 * 256;
function createFileInfo(state) {
return {
id: state.params.id,
secretKey: state.params.key,
nonce: downloadMetadata.nonce,
requiresPassword: downloadMetadata.pwd
};
}
function downloading(state, emit) {
return html`
${state.translate('downloadingTitle')}
${archiveTile.downloading(state, emit)}
`;
}
function preview(state, emit) {
if (!state.capabilities.streamDownload && state.fileInfo.size > BIG_SIZE) {
return noStreams(state, emit);
}
return html`
${state.translate('downloadTitle')}
${state.translate('downloadDescription')}
${archiveTile.preview(state, emit)}
`;
}
module.exports = function(state, emit) {
let content = '';
if (!state.fileInfo) {
state.fileInfo = createFileInfo(state);
if (downloadMetadata.status === 404) {
return notFound(state);
}
if (!state.fileInfo.nonce) {
// coming from something like the browser back button
return location.reload();
}
}
if (!state.transfer && !state.fileInfo.requiresPassword) {
emit('getMetadata');
}
if (state.transfer) {
switch (state.transfer.state) {
case 'downloading':
case 'decrypting':
content = downloading(state, emit);
break;
case 'complete':
content = downloadCompleted(state);
break;
default:
content = preview(state, emit);
}
} else if (state.fileInfo.requiresPassword && !state.fileInfo.password) {
content = downloadPassword(state, emit);
}
return html`
${state.modal && modal(state, emit)}
`;
};