fileReceiver.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import Nanobus from 'nanobus';
  2. import Keychain from './keychain';
  3. import { bytes } from './utils';
  4. import { metadata, downloadFile } from './api';
  5. export default class FileReceiver extends Nanobus {
  6. constructor(fileInfo) {
  7. super('FileReceiver');
  8. this.keychain = new Keychain(fileInfo.secretKey, fileInfo.nonce);
  9. if (fileInfo.requiresPassword) {
  10. this.keychain.setPassword(fileInfo.password, fileInfo.url);
  11. }
  12. this.fileInfo = fileInfo;
  13. this.reset();
  14. }
  15. get progressRatio() {
  16. return this.progress[0] / this.progress[1];
  17. }
  18. get progressIndefinite() {
  19. return this.state !== 'downloading';
  20. }
  21. get sizes() {
  22. return {
  23. partialSize: bytes(this.progress[0]),
  24. totalSize: bytes(this.progress[1])
  25. };
  26. }
  27. cancel() {
  28. if (this.downloadRequest) {
  29. this.downloadRequest.cancel();
  30. }
  31. }
  32. reset() {
  33. this.msg = 'fileSizeProgress';
  34. this.state = 'initialized';
  35. this.progress = [0, 1];
  36. }
  37. async getMetadata() {
  38. const meta = await metadata(this.fileInfo.id, this.keychain);
  39. this.keychain.setIV(meta.iv);
  40. this.fileInfo.name = meta.name;
  41. this.fileInfo.type = meta.type;
  42. this.fileInfo.iv = meta.iv;
  43. this.fileInfo.size = meta.size;
  44. this.state = 'ready';
  45. }
  46. async download(noSave = false) {
  47. this.state = 'downloading';
  48. this.downloadRequest = await downloadFile(
  49. this.fileInfo.id,
  50. this.keychain,
  51. p => {
  52. this.progress = p;
  53. this.emit('progress');
  54. }
  55. );
  56. try {
  57. const ciphertext = await this.downloadRequest.result;
  58. this.downloadRequest = null;
  59. this.msg = 'decryptingFile';
  60. this.state = 'decrypting';
  61. this.emit('decrypting');
  62. const plaintext = await this.keychain.decryptFile(ciphertext);
  63. if (!noSave) {
  64. await saveFile({
  65. plaintext,
  66. name: decodeURIComponent(this.fileInfo.name),
  67. type: this.fileInfo.type
  68. });
  69. }
  70. this.msg = 'downloadFinish';
  71. this.state = 'complete';
  72. } catch (e) {
  73. this.downloadRequest = null;
  74. throw e;
  75. }
  76. }
  77. }
  78. async function saveFile(file) {
  79. return new Promise(function(resolve, reject) {
  80. const dataView = new DataView(file.plaintext);
  81. const blob = new Blob([dataView], { type: file.type });
  82. if (navigator.msSaveBlob) {
  83. navigator.msSaveBlob(blob, file.name);
  84. return resolve();
  85. } else if (/iPhone|fxios/i.test(navigator.userAgent)) {
  86. // This method is much slower but createObjectURL
  87. // is buggy on iOS
  88. const reader = new FileReader();
  89. reader.addEventListener('loadend', function() {
  90. if (reader.error) {
  91. return reject(reader.error);
  92. }
  93. if (reader.result) {
  94. const a = document.createElement('a');
  95. a.href = reader.result;
  96. a.download = file.name;
  97. document.body.appendChild(a);
  98. a.click();
  99. }
  100. resolve();
  101. });
  102. reader.readAsDataURL(blob);
  103. } else {
  104. const downloadUrl = URL.createObjectURL(blob);
  105. const a = document.createElement('a');
  106. a.href = downloadUrl;
  107. a.download = file.name;
  108. document.body.appendChild(a);
  109. a.click();
  110. URL.revokeObjectURL(downloadUrl);
  111. setTimeout(resolve, 100);
  112. }
  113. });
  114. }