|
20 | 20 | // you must obtain a commercial license from CoCreate LLC.
|
21 | 21 | // For details, visit <https://cocreate.app/licenses/> or contact us at [email protected].
|
22 | 22 |
|
23 |
| - |
24 |
| -const { URL } = require('url'); |
| 23 | +const { URL } = require("url"); |
25 | 24 |
|
26 | 25 | class CoCreateUrlUploader {
|
27 |
| - constructor(crud) { |
28 |
| - this.crud = crud |
29 |
| - crud.wsManager.on('importUrl', async (data) => { |
30 |
| - this.fetchFileFromURL(data) |
31 |
| - }); |
32 |
| - } |
33 |
| - |
| 26 | + constructor(crud) { |
| 27 | + this.crud = crud; |
| 28 | + crud.wsManager.on("importUrl", async (data) => { |
| 29 | + this.fetchFileFromURL(data); |
| 30 | + }); |
| 31 | + } |
34 | 32 |
|
35 |
| - async fetchFileFromURL(data) { |
36 |
| - try { |
37 |
| - const file = data.file |
38 |
| - const fetch = await import('node-fetch').then(module => module.default); |
39 |
| - const response = await fetch(file.src); |
| 33 | + async fetchFileFromURL(data) { |
| 34 | + try { |
| 35 | + const file = data.file; |
| 36 | + const fetch = await import("node-fetch").then( |
| 37 | + (module) => module.default |
| 38 | + ); |
| 39 | + const response = await fetch(file.src); |
40 | 40 |
|
41 |
| - if (!response.ok) { |
42 |
| - data.error = 'Failed to fetch file: ' + response.statusText |
43 |
| - if (data.socket) |
44 |
| - return this.crud.wsManager.send(data) |
45 |
| - } |
| 41 | + if (!response.ok) { |
| 42 | + data.error = "Failed to fetch file: " + response.statusText; |
| 43 | + if (data.socket) return this.crud.wsManager.send(data); |
| 44 | + } |
46 | 45 |
|
47 |
| - const arrayBuffer = await response.arrayBuffer(); |
48 |
| - file.src = this.arrayBufferToBase64(arrayBuffer) |
| 46 | + const arrayBuffer = await response.arrayBuffer(); |
| 47 | + file.src = this.arrayBufferToBase64(arrayBuffer); |
49 | 48 |
|
50 |
| - file.size = arrayBuffer.byteLength |
51 |
| - file['content-type'] = response.headers.get('content-type') |
| 49 | + file.size = arrayBuffer.byteLength; |
| 50 | + file["content-type"] = response.headers.get("content-type"); |
52 | 51 |
|
53 |
| - if (!file.name) { |
54 |
| - const parsedUrl = new URL(file.src); |
55 |
| - file.name = parsedUrl.pathname.split('/').pop(); |
56 |
| - } |
| 52 | + if (!file.name) { |
| 53 | + const parsedUrl = new URL(file.src); |
| 54 | + file.name = parsedUrl.pathname.split("/").pop(); |
| 55 | + } |
57 | 56 |
|
58 |
| - if (!file.directory) |
59 |
| - file.directory = '/' |
60 |
| - if (!file.path) |
61 |
| - file.path = file.directory |
62 |
| - if (!file.pathname) { |
63 |
| - if (file.path.endsWith('/')) |
64 |
| - file.pathname = file.path + file.name |
65 |
| - else |
66 |
| - file.pathname = file.path + '/' + file.name |
67 |
| - } |
68 |
| - if (data.socket) |
69 |
| - this.crud.wsManager.send(data) |
| 57 | + if (!file.directory) file.directory = "/"; |
| 58 | + if (!file.path) file.path = file.directory; |
| 59 | + if (!file.pathname) { |
| 60 | + if (file.path.endsWith("/")) |
| 61 | + file.pathname = file.path + file.name; |
| 62 | + else file.pathname = file.path + "/" + file.name; |
| 63 | + } |
| 64 | + if (data.socket) this.crud.wsManager.send(data); |
70 | 65 |
|
71 |
| - // return data; |
72 |
| - } catch (error) { |
73 |
| - console.error('Error fetching file:', error); |
74 |
| - throw error; |
75 |
| - } |
76 |
| - } |
| 66 | + // return data; |
| 67 | + } catch (error) { |
| 68 | + console.error("Error fetching file:", error); |
| 69 | + throw error; |
| 70 | + } |
| 71 | + } |
77 | 72 |
|
78 |
| - arrayBufferToBase64(buffer) { |
79 |
| - let binary = ''; |
80 |
| - const bytes = new Uint8Array(buffer); |
81 |
| - const len = bytes.byteLength; |
82 |
| - for (let i = 0; i < len; i++) { |
83 |
| - binary += String.fromCharCode(bytes[i]); |
84 |
| - } |
85 |
| - return Buffer.from(binary, 'binary').toString('base64'); |
86 |
| - } |
| 73 | + arrayBufferToBase64(buffer) { |
| 74 | + let binary = ""; |
| 75 | + const bytes = new Uint8Array(buffer); |
| 76 | + const len = bytes.byteLength; |
| 77 | + for (let i = 0; i < len; i++) { |
| 78 | + binary += String.fromCharCode(bytes[i]); |
| 79 | + } |
| 80 | + return Buffer.from(binary, "binary").toString("base64"); |
| 81 | + } |
87 | 82 | }
|
88 | 83 |
|
89 | 84 | module.exports = CoCreateUrlUploader;
|
0 commit comments