download.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* global downloadMetadata */
  2. const html = require('choo/html');
  3. const assets = require('../../common/assets');
  4. const archiveTile = require('./archiveTile');
  5. const modal = require('./modal');
  6. const noStreams = require('./noStreams');
  7. const notFound = require('./notFound');
  8. const downloadPassword = require('./downloadPassword');
  9. const downloadCompleted = require('./downloadCompleted');
  10. const BIG_SIZE = 1024 * 1024 * 256;
  11. function createFileInfo(state) {
  12. return {
  13. id: state.params.id,
  14. secretKey: state.params.key,
  15. nonce: downloadMetadata.nonce,
  16. requiresPassword: downloadMetadata.pwd
  17. };
  18. }
  19. function downloading(state, emit) {
  20. return html`
  21. <div
  22. class="flex flex-col w-full h-full items-center md:justify-center md:-mt-8"
  23. >
  24. <h1 class="text-3xl font-bold mb-4">
  25. ${state.translate('downloadingTitle')}
  26. </h1>
  27. ${archiveTile.downloading(state, emit)}
  28. </div>
  29. `;
  30. }
  31. function preview(state, emit) {
  32. if (state.fileInfo.flagged) {
  33. return html`
  34. <div
  35. class="flex flex-col w-full max-w-md h-full mx-auto items-center justify-center"
  36. >
  37. <h1 class="text-xl font-bold">${state.translate('downloadFlagged')}</h1>
  38. </div>
  39. `;
  40. }
  41. if (!state.capabilities.streamDownload && state.fileInfo.size > BIG_SIZE) {
  42. return noStreams(state, emit);
  43. }
  44. return html`
  45. <div class="w-full md:flex md:flex-row items-stretch md:flex-1">
  46. <div
  47. class="px-2 w-full md:px-0 flex-half md:flex md:flex-col mt-12 md:pr-8 pb-4"
  48. >
  49. <h1 class="text-3xl font-bold mb-4 text-center md:text-left">
  50. ${state.translate('downloadTitle')}
  51. </h1>
  52. <p
  53. class="text-grey-80 leading-normal dark:text-grey-40 mb-4 text-center md:text-left"
  54. >
  55. ${state.translate('downloadDescription')}
  56. </p>
  57. <p
  58. class="text-grey-80 leading-normal dark:text-grey-40 font-semibold text-center md:mb-8 md:text-left"
  59. >
  60. ${state.translate('downloadConfirmDescription')}
  61. </p>
  62. <img
  63. class="hidden md:block dl-bg w-full"
  64. src="${assets.get('intro.svg')}"
  65. />
  66. </div>
  67. <div
  68. class="w-full flex-half flex-half md:flex md:flex-col md:justify-center"
  69. >
  70. ${archiveTile.preview(state, emit)}
  71. <a href="/report" class="link-blue mt-4 text-center block"
  72. >${state.translate('reportFile', {
  73. count: state.fileInfo.manifest.files.length
  74. })}</a
  75. >
  76. </div>
  77. </div>
  78. `;
  79. }
  80. module.exports = function(state, emit) {
  81. let content = '';
  82. if (!state.fileInfo) {
  83. state.fileInfo = createFileInfo(state);
  84. if (downloadMetadata.status === 404) {
  85. return notFound(state);
  86. }
  87. if (!state.fileInfo.nonce) {
  88. // coming from something like the browser back button
  89. return location.reload();
  90. }
  91. }
  92. if (state.fileInfo.dead) {
  93. return notFound(state);
  94. }
  95. if (!state.transfer && !state.fileInfo.requiresPassword) {
  96. emit('getMetadata');
  97. }
  98. if (state.transfer) {
  99. switch (state.transfer.state) {
  100. case 'downloading':
  101. case 'decrypting':
  102. content = downloading(state, emit);
  103. break;
  104. case 'complete':
  105. content = downloadCompleted(state);
  106. break;
  107. default:
  108. content = preview(state, emit);
  109. }
  110. } else if (state.fileInfo.requiresPassword && !state.fileInfo.password) {
  111. content = downloadPassword(state, emit);
  112. }
  113. return html`
  114. <main class="main">
  115. ${state.modal && modal(state, emit)}
  116. <section
  117. class="relative overflow-hidden h-full w-full p-6 md:p-8 md:rounded-xl md:shadow-big md:flex md:flex-col"
  118. >
  119. ${content}
  120. </section>
  121. </main>
  122. `;
  123. };