@tagsamurai/fats-api-services 1.0.3-alpha.34 → 1.0.3-alpha.36

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.
@@ -34,8 +34,30 @@ const fetchBlobFile = async (url, token) => {
34
34
  type: res.headers.get("Content-Type") || "image/webp"
35
35
  });
36
36
  };
37
- const downloadFile = async (fileUrl) => {
38
- return createBlobURL(fileUrl);
37
+ const downloadFile = async (fileUrl, fileName, immediateRevoke = true) => {
38
+ const token = getAuthToken();
39
+ const blob = await fetchBlobFile(fileUrl, token);
40
+ let objectUrl = "";
41
+ const isViewable = /^(image|application\/pdf)/i.test(blob.type);
42
+ if (isViewable) {
43
+ const file = new File([blob], fileName, {
44
+ type: blob.type
45
+ });
46
+ objectUrl = URL.createObjectURL(file);
47
+ window.open(objectUrl, "_blank");
48
+ } else {
49
+ objectUrl = URL.createObjectURL(blob);
50
+ const a = document.createElement("a");
51
+ a.href = objectUrl;
52
+ a.download = fileName;
53
+ document.body.appendChild(a);
54
+ a.click();
55
+ a.remove();
56
+ }
57
+ if (immediateRevoke) {
58
+ URL.revokeObjectURL(objectUrl);
59
+ }
60
+ return objectUrl;
39
61
  };
40
62
  const createBlobURL = async (rawFileUrl) => {
41
63
  try {
@@ -41,8 +41,30 @@ System.register(["axios"], function(exports, module) {
41
41
  type: res.headers.get("Content-Type") || "image/webp"
42
42
  });
43
43
  });
44
- const downloadFile = exports("downloadFile", async (fileUrl) => {
45
- return createBlobURL(fileUrl);
44
+ const downloadFile = exports("downloadFile", async (fileUrl, fileName, immediateRevoke = true) => {
45
+ const token = getAuthToken();
46
+ const blob = await fetchBlobFile(fileUrl, token);
47
+ let objectUrl = "";
48
+ const isViewable = /^(image|application\/pdf)/i.test(blob.type);
49
+ if (isViewable) {
50
+ const file = new File([blob], fileName, {
51
+ type: blob.type
52
+ });
53
+ objectUrl = URL.createObjectURL(file);
54
+ window.open(objectUrl, "_blank");
55
+ } else {
56
+ objectUrl = URL.createObjectURL(blob);
57
+ const a = document.createElement("a");
58
+ a.href = objectUrl;
59
+ a.download = fileName;
60
+ document.body.appendChild(a);
61
+ a.click();
62
+ a.remove();
63
+ }
64
+ if (immediateRevoke) {
65
+ URL.revokeObjectURL(objectUrl);
66
+ }
67
+ return objectUrl;
46
68
  });
47
69
  const createBlobURL = async (rawFileUrl) => {
48
70
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tagsamurai/fats-api-services",
3
- "version": "1.0.3-alpha.34",
3
+ "version": "1.0.3-alpha.36",
4
4
  "author": "developer.tagsamurai",
5
5
  "description": "Fixed Asset Tag Samurai Services Library",
6
6
  "module": "./api-services.es.js",
@@ -2,8 +2,10 @@ export declare const buildFileURL: (name: string, width?: number, height?: numbe
2
2
  export declare const fetchBlobFile: (url: string, token: string) => Promise<Blob>;
3
3
  /**
4
4
  *
5
- * @param fileUrl Full URL of the file to download
5
+ * @param fileUrl The full URL
6
+ * @param fileName Custom File name
7
+ * @param immediateRevoke Immediately revoke the object URL after download - default to true
6
8
  * @returns
7
9
  */
8
- export declare const downloadFile: (fileUrl: string) => Promise<string>;
10
+ export declare const downloadFile: (fileUrl: string, fileName: string, immediateRevoke?: boolean) => Promise<string>;
9
11
  export declare const getImageURL: (name?: string | null, width?: number, height?: number, returnURLOnly?: boolean) => Promise<string | undefined>;