file-serve.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. function bom(blob: any, opts: any) {
  2. if (typeof opts === "undefined") opts = { autoBom: false };
  3. else if (typeof opts !== "object") {
  4. console.warn("Deprecated: Expected third argument to be a object");
  5. opts = { autoBom: !opts };
  6. }
  7. if (
  8. opts.autoBom &&
  9. /^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(
  10. blob.type
  11. )
  12. ) {
  13. return new Blob([String.fromCharCode(0xfeff), blob], { type: blob.type });
  14. }
  15. return blob;
  16. }
  17. function download(url: string, name: string, opts: any): Promise<void> {
  18. return new Promise((resolve, reject) => {
  19. const xhr = new XMLHttpRequest();
  20. xhr.open("GET", url);
  21. xhr.responseType = "blob";
  22. xhr.onload = function () {
  23. saveAs(xhr.response, name, opts).then(resolve);
  24. };
  25. xhr.onerror = function () {
  26. reject("could not download file");
  27. };
  28. xhr.send();
  29. });
  30. }
  31. function corsEnabled(url: any) {
  32. const xhr = new XMLHttpRequest();
  33. // use sync to avoid popup blocker
  34. xhr.open("HEAD", url, false);
  35. try {
  36. xhr.send();
  37. } catch (e) {}
  38. return xhr.status >= 200 && xhr.status <= 299;
  39. }
  40. function click(node: any) {
  41. return new Promise<void>((resolve) => {
  42. setTimeout(() => {
  43. try {
  44. node.dispatchEvent(new MouseEvent("click"));
  45. } catch (e) {
  46. const evt = document.createEvent("MouseEvents");
  47. evt.initMouseEvent(
  48. "click",
  49. true,
  50. true,
  51. window,
  52. 0,
  53. 0,
  54. 0,
  55. 80,
  56. 20,
  57. false,
  58. false,
  59. false,
  60. false,
  61. 0,
  62. null
  63. );
  64. node.dispatchEvent(evt);
  65. }
  66. resolve();
  67. }, 0);
  68. });
  69. }
  70. const isMacOSWebView =
  71. navigator &&
  72. /Macintosh/.test(navigator.userAgent) &&
  73. /AppleWebKit/.test(navigator.userAgent) &&
  74. !/Safari/.test(navigator.userAgent);
  75. type SaveAs = (
  76. blob: Blob | string,
  77. name?: string,
  78. opts?: { autoBom: boolean }
  79. ) => Promise<void>;
  80. export const saveAs: SaveAs =
  81. "download" in HTMLAnchorElement.prototype && !isMacOSWebView
  82. ? (blob, name = "download", opts) => {
  83. const URL = global.URL || global.webkitURL;
  84. const a = document.createElement("a");
  85. a.download = name;
  86. a.rel = "noopener";
  87. if (typeof blob === "string") {
  88. a.href = blob;
  89. if (a.origin !== location.origin) {
  90. if (corsEnabled(a.href)) {
  91. return download(blob, name, opts);
  92. }
  93. a.target = "_blank";
  94. }
  95. return click(a);
  96. } else {
  97. a.href = URL.createObjectURL(blob);
  98. setTimeout(function () {
  99. URL.revokeObjectURL(a.href);
  100. }, 4e4); // 40s
  101. return click(a);
  102. }
  103. }
  104. : "msSaveOrOpenBlob" in navigator
  105. ? (blob, name = "download", opts) => {
  106. if (typeof blob === "string") {
  107. if (corsEnabled(blob)) {
  108. return download(blob, name, opts);
  109. } else {
  110. const a = document.createElement("a");
  111. a.href = blob;
  112. a.target = "_blank";
  113. return click(a);
  114. }
  115. } else {
  116. return (navigator as any).msSaveOrOpenBlob(bom(blob, opts), name)
  117. ? Promise.resolve()
  118. : Promise.reject("unknown");
  119. }
  120. }
  121. : (blob, name, opts) => {
  122. if (typeof blob === "string") return download(blob, name as string, opts);
  123. const force = blob.type === "application/octet-stream";
  124. const isSafari =
  125. /constructor/i.test(HTMLElement.toString()) || (global as any).safari;
  126. const isChromeIOS = /CriOS\/[\d]+/.test(navigator.userAgent);
  127. if (
  128. (isChromeIOS || (force && isSafari) || isMacOSWebView) &&
  129. typeof FileReader !== "undefined"
  130. ) {
  131. return new Promise<void>((resolve, reject) => {
  132. const reader = new FileReader();
  133. reader.onloadend = function () {
  134. let url = reader.result as string;
  135. url = isChromeIOS
  136. ? url
  137. : url.replace(/^data:[^;]*;/, "data:attachment/file;");
  138. location.href = url;
  139. resolve();
  140. };
  141. reader.onerror = function () {
  142. reject();
  143. };
  144. reader.readAsDataURL(blob);
  145. });
  146. } else {
  147. const URL = global.URL || global.webkitURL;
  148. const url = URL.createObjectURL(blob);
  149. location.href = url;
  150. setTimeout(function () {
  151. URL.revokeObjectURL(url);
  152. }, 4e4); // 40s
  153. return Promise.resolve();
  154. }
  155. };
  156. export default saveAs;