download.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* global downloadMetadata */
  2. const html = require('choo/html');
  3. const archiveTile = require('./archiveTile');
  4. const modal = require('./modal');
  5. const noStreams = require('./noStreams');
  6. const notFound = require('./notFound');
  7. const downloadPassword = require('./downloadPassword');
  8. const downloadCompleted = require('./downloadCompleted');
  9. const BIG_SIZE = 1024 * 1024 * 256;
  10. function createFileInfo(state) {
  11. return {
  12. id: state.params.id,
  13. secretKey: state.params.key,
  14. nonce: downloadMetadata.nonce,
  15. requiresPassword: downloadMetadata.pwd
  16. };
  17. }
  18. function downloading(state, emit) {
  19. return html`
  20. <div
  21. class="flex flex-col w-full h-full items-center md:justify-center md:-mt-8"
  22. >
  23. <h1 class="text-3xl font-bold mb-4">
  24. ${state.translate('downloadingTitle')}
  25. </h1>
  26. ${archiveTile.downloading(state, emit)}
  27. </div>
  28. `;
  29. }
  30. function preview(state, emit) {
  31. if (!state.capabilities.streamDownload && state.fileInfo.size > BIG_SIZE) {
  32. return noStreams(state, emit);
  33. }
  34. return html`
  35. <div
  36. class="flex flex-col w-full max-w-md h-full mx-auto items-center justify-center"
  37. >
  38. <h1 class="text-3xl font-bold mb-4">
  39. ${state.translate('downloadTitle')}
  40. </h1>
  41. <p
  42. class="w-full text-grey-80 text-center leading-normal dark:text-grey-40"
  43. >
  44. ${state.translate('downloadDescription')}
  45. </p>
  46. ${archiveTile.preview(state, emit)}
  47. </div>
  48. `;
  49. }
  50. module.exports = function(state, emit) {
  51. let content = '';
  52. if (!state.fileInfo) {
  53. state.fileInfo = createFileInfo(state);
  54. if (!state.fileInfo.nonce) {
  55. return notFound(state);
  56. }
  57. }
  58. if (!state.transfer && !state.fileInfo.requiresPassword) {
  59. emit('getMetadata');
  60. }
  61. if (state.transfer) {
  62. switch (state.transfer.state) {
  63. case 'downloading':
  64. case 'decrypting':
  65. content = downloading(state, emit);
  66. break;
  67. case 'complete':
  68. content = downloadCompleted(state);
  69. break;
  70. default:
  71. content = preview(state, emit);
  72. }
  73. } else if (state.fileInfo.requiresPassword && !state.fileInfo.password) {
  74. content = downloadPassword(state, emit);
  75. }
  76. return html`
  77. <main class="main">
  78. ${state.modal && modal(state, emit)}
  79. <section
  80. class="relative h-full w-full p-6 md:p-8 md:rounded-xl md:shadow-big"
  81. >
  82. ${content}
  83. </section>
  84. </main>
  85. `;
  86. };