basefilereader.js

  1. import { parseURL, text2buffer } from './util';
  2. /**
  3. * Abstract class that should be extended
  4. */
  5. class BaseFileReader {
  6. /**
  7. * Constructs a file reader for specific file types
  8. *
  9. * @param {BlueFileHeader|MatFileHeader} header_class
  10. * @param {object} options
  11. * @returns {BaseFileReader}
  12. */
  13. constructor(header_class, options) {
  14. this.header_class = header_class;
  15. this.options = options;
  16. }
  17. /**
  18. * Internal method that will read a file or stop at the header
  19. *
  20. * @private
  21. * @memberof BaseFileReader
  22. * @param {File} theFile - a File object for the Bluefile or Matfile
  23. * @param {function} onload - callback when the header has been read
  24. * @param {boolean} justHeader - Whether or not to only read the header
  25. */
  26. _read(theFile, onload, justHeader) {
  27. const that = this;
  28. const reader = new FileReader();
  29. // Note: webkitSlice is Chrome specific and deprecated now.
  30. // For some backwards-compatibility, we'll continue to support it.
  31. // In a future version, this will just use `File.slice`.
  32. const sliceMethod =
  33. theFile.webkitSlice === undefined ? 'slice' : 'webkitSlice';
  34. const blob = justHeader ? theFile[sliceMethod](0, 512) : theFile;
  35. // Closure to capture the file information.
  36. reader.onloadend = ((theFile) => {
  37. return function (e) {
  38. if (e.target.error) {
  39. onload(null);
  40. return;
  41. }
  42. const raw = reader.result;
  43. const hdr = new that.header_class(raw, that.options);
  44. hdr.file = theFile;
  45. hdr.file_name = theFile.name;
  46. onload(hdr);
  47. };
  48. })(theFile);
  49. reader.readAsArrayBuffer(blob);
  50. }
  51. /**
  52. * Read only the header from a local {Blue,Mat}file.
  53. *
  54. * @memberof BaseFileReader
  55. * @param {File} theFile - a File object for the Bluefile or Matfile
  56. * @param {function} onload - callback when the header has been read
  57. */
  58. readheader(theFile, onload) {
  59. this._read(theFile, onload, true);
  60. }
  61. /**
  62. * Read a local Bluefile or Matfile on disk.
  63. *
  64. * @memberof BaseFileReader
  65. * @param {File} theFile - a File object for the Bluefile or Matfile
  66. * @param {function} onload - callback when the file has been read
  67. */
  68. read(theFile, onload) {
  69. this._read(theFile, onload, false);
  70. }
  71. /**
  72. * Read a Bluefile or Matfile from a URL
  73. *
  74. * @memberof BaseFileReader
  75. * @param {string} href - the URL for the Bluefile or Matfile
  76. * @param {function} onload - callback when the header has been read
  77. */
  78. read_http(href, onload) {
  79. const that = this;
  80. const oReq = new XMLHttpRequest();
  81. oReq.open('GET', href, true);
  82. oReq.responseType = 'arraybuffer';
  83. oReq.overrideMimeType('text/plain; charset=x-user-defined');
  84. oReq.onload = function (_oEvent) {
  85. if (oReq.readyState === 4) {
  86. if (oReq.status === 200 || oReq.status === 0) {
  87. // status = 0 is necessary for file URL
  88. let arrayBuffer = null; // Note: not oReq.responseText
  89. if (oReq.response) {
  90. arrayBuffer = oReq.response;
  91. const hdr = new that.header_class(arrayBuffer, that.options);
  92. const fileUrl = parseURL(href);
  93. hdr.file_name = fileUrl.file;
  94. onload(hdr);
  95. } else if (oReq.responseText) {
  96. text2buffer(oReq.responseText, function (arrayBuffer) {
  97. const hdr = new that.header_class(arrayBuffer, that.options);
  98. const fileUrl = parseURL(href);
  99. hdr.file_name = fileUrl.file;
  100. onload(hdr);
  101. });
  102. }
  103. return;
  104. }
  105. }
  106. onload(null);
  107. };
  108. oReq.onerror = function (_oEvent) {
  109. onload(null);
  110. };
  111. oReq.send(null);
  112. return oReq;
  113. }
  114. }
  115. export { BaseFileReader };