download.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 (downloadMetadata.status === 404) {
  55. return notFound(state);
  56. }
  57. if (!state.fileInfo.nonce) {
  58. // coming from something like the browser back button
  59. return location.reload();
  60. }
  61. }
  62. if (!state.transfer && !state.fileInfo.requiresPassword) {
  63. emit('getMetadata');
  64. }
  65. if (state.transfer) {
  66. switch (state.transfer.state) {
  67. case 'downloading':
  68. case 'decrypting':
  69. content = downloading(state, emit);
  70. break;
  71. case 'complete':
  72. content = downloadCompleted(state);
  73. break;
  74. default:
  75. content = preview(state, emit);
  76. }
  77. } else if (state.fileInfo.requiresPassword && !state.fileInfo.password) {
  78. content = downloadPassword(state, emit);
  79. }
  80. return html`
  81. <main class="main">
  82. ${state.modal && modal(state, emit)}
  83. <section
  84. class="relative h-full w-full p-6 md:p-8 md:rounded-xl md:shadow-big"
  85. >
  86. ${content}
  87. </section>
  88. </main>
  89. `;
  90. };