hsu-utils 0.0.52 → 0.0.54

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  *
3
- * hsu-utils v0.0.51
3
+ * hsu-utils v0.0.53
4
4
  *
5
5
  * some front-end utils
6
6
  *
@@ -1 +1,8 @@
1
+ type ResponseLike = Response | {
2
+ headers: Record<string, string> | {
3
+ get(name: string): string | null;
4
+ };
5
+ };
6
+ export declare function getFileNameFromHeader(response: ResponseLike): string | null;
1
7
  export default function downloadFile(file: ArrayBuffer | Blob | string, fileName?: string, signal?: AbortSignal): Promise<void>;
8
+ export {};
@@ -8,12 +8,38 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { Typeof } from '..';
11
+ export function getFileNameFromHeader(response) {
12
+ let contentDisposition = null;
13
+ if (response.headers && typeof response.headers.get === 'function') {
14
+ contentDisposition = response.headers.get('Content-Disposition');
15
+ }
16
+ else if (response.headers && typeof response.headers === 'object') {
17
+ const headers = response.headers;
18
+ contentDisposition = headers['content-disposition'] || headers['Content-Disposition'] || null;
19
+ }
20
+ if (!contentDisposition) {
21
+ return null;
22
+ }
23
+ const filenameStarMatch = contentDisposition.match(/filename\*=UTF-8''([^;]+)/i);
24
+ if (filenameStarMatch) {
25
+ return decodeURIComponent(filenameStarMatch[1]);
26
+ }
27
+ const filenameMatch = contentDisposition.match(/filename=["']?([^;"']+)["']?/i);
28
+ if (filenameMatch) {
29
+ return decodeURIComponent(filenameMatch[1]);
30
+ }
31
+ return null;
32
+ }
11
33
  function downloadFileByUrl(url, fileName, signal) {
12
34
  return __awaiter(this, void 0, void 0, function* () {
13
35
  try {
14
36
  const response = yield fetch(url, { signal });
37
+ let finalFileName = fileName;
38
+ if (!finalFileName) {
39
+ finalFileName = getFileNameFromHeader(response) || undefined;
40
+ }
15
41
  const arrayBuffer = yield response.arrayBuffer();
16
- downloadFile(arrayBuffer, fileName);
42
+ downloadFile(arrayBuffer, finalFileName);
17
43
  }
18
44
  catch (error) {
19
45
  if ((error === null || error === void 0 ? void 0 : error.name) !== 'AbortError') {
@@ -1 +1,8 @@
1
+ type ResponseLike = Response | {
2
+ headers: Record<string, string> | {
3
+ get(name: string): string | null;
4
+ };
5
+ };
6
+ export declare function getFileNameFromHeader(response: ResponseLike): string | null;
1
7
  export default function downloadFile(file: ArrayBuffer | Blob | string, fileName?: string, signal?: AbortSignal): Promise<void>;
8
+ export {};
@@ -36,10 +36,34 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
36
36
  }
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.getFileNameFromHeader = void 0;
39
40
  var __1 = require("..");
41
+ function getFileNameFromHeader(response) {
42
+ var contentDisposition = null;
43
+ if (response.headers && typeof response.headers.get === 'function') {
44
+ contentDisposition = response.headers.get('Content-Disposition');
45
+ }
46
+ else if (response.headers && typeof response.headers === 'object') {
47
+ var headers = response.headers;
48
+ contentDisposition = headers['content-disposition'] || headers['Content-Disposition'] || null;
49
+ }
50
+ if (!contentDisposition) {
51
+ return null;
52
+ }
53
+ var filenameStarMatch = contentDisposition.match(/filename\*=UTF-8''([^;]+)/i);
54
+ if (filenameStarMatch) {
55
+ return decodeURIComponent(filenameStarMatch[1]);
56
+ }
57
+ var filenameMatch = contentDisposition.match(/filename=["']?([^;"']+)["']?/i);
58
+ if (filenameMatch) {
59
+ return decodeURIComponent(filenameMatch[1]);
60
+ }
61
+ return null;
62
+ }
63
+ exports.getFileNameFromHeader = getFileNameFromHeader;
40
64
  function downloadFileByUrl(url, fileName, signal) {
41
65
  return __awaiter(this, void 0, void 0, function () {
42
- var response, arrayBuffer, error_1, downloadElement;
66
+ var response, finalFileName, arrayBuffer, error_1, downloadElement;
43
67
  return __generator(this, function (_a) {
44
68
  switch (_a.label) {
45
69
  case 0:
@@ -47,10 +71,14 @@ function downloadFileByUrl(url, fileName, signal) {
47
71
  return [4, fetch(url, { signal: signal })];
48
72
  case 1:
49
73
  response = _a.sent();
74
+ finalFileName = fileName;
75
+ if (!finalFileName) {
76
+ finalFileName = getFileNameFromHeader(response) || undefined;
77
+ }
50
78
  return [4, response.arrayBuffer()];
51
79
  case 2:
52
80
  arrayBuffer = _a.sent();
53
- downloadFile(arrayBuffer, fileName);
81
+ downloadFile(arrayBuffer, finalFileName);
54
82
  return [3, 4];
55
83
  case 3:
56
84
  error_1 = _a.sent();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hsu-utils",
3
- "version": "0.0.52",
3
+ "version": "0.0.54",
4
4
  "description": "some front-end utils",
5
5
  "repository": "git@github.com:VitaTsui/hsu-utils.git",
6
6
  "author": "VitaHsu <vitahsu7@gmail.com>",