@trap_stevo/filetide 0.0.27 → 0.0.29

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.
@@ -50,14 +50,15 @@ class FileMessager {
50
50
  }
51
51
  return client;
52
52
  }
53
- requestFromClient(senderID, clientID, filePath = process.cwd(), destinationPath) {
54
- const client = this.getClientFromID(clientID);
53
+ requestFromClient(requesterID, senderID, filePath = process.cwd(), destinationPath) {
54
+ const senderConnectionID = FileNetClientManager.getOnlineClient(senderID).id;
55
+ const client = this.getClientFromID(senderConnectionID);
55
56
  if (!client) {
56
57
  return;
57
58
  }
58
59
  this.fileNet.emitToTide(client.tideID, "transfer-requested", {
59
60
  senderID,
60
- clientID,
61
+ requesterID,
61
62
  path: filePath,
62
63
  destinationPath
63
64
  });
@@ -107,12 +108,12 @@ class FileMessager {
107
108
  }
108
109
  handleRequestTransferFromClient(transferData) {
109
110
  const {
111
+ requesterID,
110
112
  senderID,
111
- recipientId,
112
113
  filePath,
113
114
  destinationPath
114
115
  } = transferData;
115
- this.requestFromClient(senderID, recipientId, filePath, destinationPath);
116
+ this.requestFromClient(requesterID, senderID, filePath, destinationPath);
116
117
  }
117
118
  handleClientToClientTransfer(transferData) {
118
119
  const {
@@ -5,6 +5,11 @@ const {
5
5
  } = require("@trap_stevo/iotide-client");
6
6
  const chalk = require("chalk");
7
7
  const path = require("path");
8
+ const {
9
+ FileNetUtilityManager,
10
+ significantMessageColors,
11
+ errorMessageColors
12
+ } = require("./HUDManagers/FileNetUtilityManager");
8
13
  const {
9
14
  FileMessagerConfigManager
10
15
  } = require("./HUDManagers/FileMessagerConfigManager");
@@ -33,7 +38,7 @@ class FileMessagerClient {
33
38
  transports: ["websocket"],
34
39
  ...options
35
40
  });
36
- console.log(`\n[FileTide ~ File Messager] ~ Created client with ID ${clientID} and socket ${clientID}!`);
41
+ FileNetUtilityManager.outputGradient(`\n[FileTide ~ File Messager] ~ Created client with ID ${clientID} and socket ${clientID}!`, significantMessageColors);
37
42
  return clientID;
38
43
  }
39
44
  joinRoom(userID, roomName, options = {}) {
@@ -41,12 +46,21 @@ class FileMessagerClient {
41
46
  url: this.clientOptions.url
42
47
  }, options);
43
48
  this.clientTide.joinChannel(socketName, roomName, userID, () => {
44
- console.log(`\n[FileTide ~ File Messager] ~ Client ~ ${userID} joined room ${roomName} successfully!`);
49
+ FileNetUtilityManager.outputGradient(`\n[FileTide ~ File Messager] ~ Client ~ ${userID} joined room ${roomName} successfully!`, significantMessageColors);
45
50
  this.clients.set(userID, {
46
51
  roomName
47
52
  });
48
53
  });
49
54
  }
55
+ requestTransfer(clientID, senderID, filePath, destinationPath) {
56
+ this.clientTide.emitEvent(clientID, "request-transfer-from-client", {
57
+ requesterID: clientID,
58
+ senderID,
59
+ destinationPath,
60
+ filePath
61
+ });
62
+ return;
63
+ }
50
64
 
51
65
  /**
52
66
  * Sends all files in a directory in parallel using the sendFile method.
@@ -115,7 +129,6 @@ class FileMessagerClient {
115
129
  });
116
130
  },
117
131
  onProgress: progress => {
118
- //console.log(`\n[FileTide ~ File Messager] ~ ${progress.toFixed(2)}% |>>| ${fileName}`);
119
132
  this.progressBarManager.updateTaskProgress(fileName, 512 * 1024);
120
133
  },
121
134
  originDetails: {
@@ -124,34 +137,10 @@ class FileMessagerClient {
124
137
  },
125
138
  fileDetails: {
126
139
  name: fileName,
127
- path: filePath
140
+ path: filePath,
141
+ size: fileSize
128
142
  }
129
- }).then(() => console.log(`[FileTide ~ File Messager] ~ File ~ ${fileName} transfer complete!`)).catch(error => console.error("Refused file transfer: ", error));
130
- }
131
- onFileChunkReceived(userID) {
132
- this.clientTide.onEvent(userID, "transfer-progress", data => {
133
- console.log(`\n[FileTide ~ File Messager] ~ Receiving file chunk for client ~ ${userID}: `, data.fileChunk);
134
- console.log("\n[FileTide ~ File Messager] ~ Progress: ", data.progress);
135
- FileUtilityManager.saveChunk(userID, data.fileChunk, data.path, data.name);
136
- });
137
- }
138
- onFileTransferComplete(userID) {
139
- this.clientTide.onEvent(userID, "transfer-complete", data => {
140
- console.log(`\n[FileTide ~ File Messager] ~ File transfer completed for user ${userID}.`);
141
- FileUtilityManager.assembleFile(userID);
142
- });
143
- }
144
- handleIncomingFile(userID) {
145
- this.clientTide.onEvent(userID, "incoming-file", data => {
146
- console.log(`\n[FileTide ~ File Messager] ~ Received file for user ${userID}: ${data.fileName}.`);
147
- FileUtilityManager.saveFile(data.fileName, data.fileData, data.path, data.name);
148
- });
149
- }
150
- handleMultipleClientsTransfer() {
151
- this.clients.forEach((userID, roomData) => {
152
- this.onFileChunkReceived(userID);
153
- this.onFileTransferComplete(userID);
154
- });
143
+ }).then(() => FileNetUtilityManager.outputGradient(`[FileTide ~ File Messager] ~ File ~ ${fileName} transfer complete!`, significantMessageColors)).catch(error => FileNetUtilityManager.outputGradient(`Refused file transfer: ${error}`, errorMessageColors));
155
144
  }
156
145
  }
157
146
  ;
@@ -1,11 +1,17 @@
1
1
  "use strict";
2
2
 
3
+ var _FileTide;
3
4
  function _classPrivateMethodInitSpec(e, a) { _checkPrivateRedeclaration(e, a), a.add(e); }
4
5
  function _checkPrivateRedeclaration(e, t) { if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }
5
6
  function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); }
6
7
  const chalk = require("chalk");
7
8
  const path = require("path");
8
9
  const fs = require("fs");
10
+ const {
11
+ FileNetUtilityManager,
12
+ significantMessageColors,
13
+ errorMessageColors
14
+ } = require("./HUDManagers/FileNetUtilityManager.js");
9
15
  const {
10
16
  FileNetConfigManager
11
17
  } = require("./HUDManagers/FileNetConfigManager.js");
@@ -28,6 +34,11 @@ class FileTide {
28
34
  completionMessageColor: chalk.blue.bold
29
35
  });
30
36
  this.clients = new Map();
37
+ this.currentUserID = "";
38
+ return;
39
+ }
40
+ static outputGradient(text, colors) {
41
+ FileNetUtilityManager.outputGradient(text, colors);
31
42
  return;
32
43
  }
33
44
 
@@ -39,7 +50,7 @@ class FileTide {
39
50
  launchFileTide(serverOptions = {}, onLaunch) {
40
51
  this.fileNet = new FileMessager(serverOptions);
41
52
  this.fileNet.start();
42
- console.log("[FileTide] ~ FileNet launched successfully!");
53
+ FileTide.outputGradient("[FileTide] ~ FileNet launched successfully!", significantMessageColors);
43
54
  if (onLaunch) {
44
55
  onLaunch(this.fileNet);
45
56
  }
@@ -60,9 +71,8 @@ class FileTide {
60
71
  }
61
72
  const newClient = new FileMessagerClient(clientOptions);
62
73
  newClient.joinRoom(userID, roomName, connectionOptions);
63
- newClient.handleMultipleClientsTransfer();
64
74
  this.clients.set(userID, newClient);
65
- console.log(`[FileTide] ~ Messager launched successfully for client ~ ${userID}!`);
75
+ FileTide.outputGradient(`[FileTide] ~ Messager launched successfully for client ~ ${userID}!`, significantMessageColors);
66
76
  _assertClassBrand(_FileTide_brand, this, _setupFileEventListeners).call(this, userID, newClient, onIncomingFile);
67
77
  if (onLaunch) {
68
78
  onLaunch(newClient);
@@ -83,69 +93,34 @@ class FileTide {
83
93
  }
84
94
 
85
95
  /**
86
- * Send a file directly to a device without it needing to create a client.
87
- * @param {String} senderID - The ID of the client device that sent the file
88
- * @param {String} userID - The ID of the client device to send the file to
89
- * @param {Buffer} fileData - The file data to send
90
- * @param {String} fileName - The name of the file
96
+ * Send files / directories directly to a device.
97
+ * @param {String} recipientID - The ID of the client device to send the data to
98
+ * @param {Buffer} fileData - The path of the data to send
99
+ * @param {String} destinationPath - The destination path on the device
91
100
  */
92
- sendFileToDevice(senderID, userID, fileData, filePath, fileName) {
93
- if (this.fileNet.onlineClients.has(userID)) {
94
- const senderClient = this.clients[senderID];
95
- const currentClient = this.clients[userID];
96
- if (!senderClient) {
97
- console.log(`[FileTide] ~ Client ~ ${senderID} not authorized.`);
98
- return;
99
- }
100
- if (!currentClient) {
101
- console.log(`[FileTide] ~ Client ~ ${userID} not authorized.`);
102
- return;
103
- }
104
- console.log(`[FileTide] ~ Sending file to client ~ ${userID}...`);
105
- this.fileNet.transporter.sendFile(fileData, {
106
- onSendChunk: (transferId, chunkIndex, chunkData, totalChunks) => {
107
- return new Promise((resolve, reject) => {
108
- this.clientTide.emitEvent(senderID, "client-to-client-transfer", {
109
- recipientId: userID,
110
- senderID,
111
- fileName,
112
- fileChunk: chunkData,
113
- totalChunks,
114
- chunkIndex,
115
- filePath
116
- }, ack => {
117
- if (ack.success) {
118
- console.log(`Chunk ${chunkIndex} sent successfully!`);
119
- resolve();
120
- } else {
121
- console.error(`Did not send chunk ${chunkIndex}`);
122
- reject(new Error('Chunk failed.'));
123
- }
124
- });
125
- });
126
- },
127
- onComplete: transferId => {
128
- return new Promise((resolve, reject) => {
129
- this.clientTide.emitEvent(senderID, "transfer-complete", {
130
- recipientId: userID,
131
- senderID,
132
- fileName,
133
- filePath
134
- });
135
- resolve();
136
- });
137
- },
138
- onProgress: progress => {
139
- console.log(`[FileTide ~ File Messager] ~ Progress: ${progress.toFixed(2)}%`);
140
- },
141
- fileDetails: {
142
- name: fileName,
143
- path: filePath
144
- }
145
- }).then(() => console.log("File transfer complete!")).catch(error => console.error("File transfer incomplete: ", error));
101
+ sendToDevice(recipientID, filePath, destinationPath) {
102
+ const fileDetails = this.getPathData(filePath);
103
+ const clientID = this.currentUserID;
104
+ const clientMessager = this.clients.get(clientID);
105
+ if (!clientMessager) {
106
+ FileTide.outputGradient(`[FileTide] Client ~ ${clientID} not found.`, ["#F94144", "#F3722C"]);
107
+ return;
108
+ }
109
+ if (!fileDetails) {
110
+ FileTide.outputGradient(`[${clientID}] File at path ${filePath} not found.`, ["#F94144", "#F3722C"]);
146
111
  return;
147
112
  }
148
- console.log(`[FileTide] ~ No active client ~ ${userID} found.`);
113
+ if (fileDetails.type === "file") {
114
+ FileTide.outputGradient(`[${clientID}] Sending file ~ ${fileDetails.content.fileName} to client ~ ${recipientID}...`, ["#00C897", "#00E0FF"]);
115
+ clientMessager.sendFile(clientID, recipientID, fileDetails.content.fileName, fileDetails.content.fileData, destinationPath, fileDetails.content.size);
116
+ return;
117
+ }
118
+ if (fileDetails.type === "directory") {
119
+ FileTide.outputGradient(`[${clientID}] Sending ${fileDetails.content.length} files in directory ~ ${path.basename(filePath)} to client ~ ${recipientID}...`, ["#00C897", "#00E0FF"]);
120
+ clientMessager.sendDirectoryFiles(clientID, recipientID, fileDetails.content, this.getTidePath(filePath), destinationPath);
121
+ return;
122
+ }
123
+ FileTide.outputGradient(`[FileTide] ~ No active client ~ ${recipientID} found.`, errorMessageColors);
149
124
  return;
150
125
  }
151
126
 
@@ -170,9 +145,9 @@ class FileTide {
170
145
  */
171
146
  stopFileTide() {
172
147
  if (this.fileNet) {
173
- console.log("Stopping FileTide...");
148
+ FileTide.outputGradient("Stopping FileTide...", significantMessageColors);
174
149
  this.fileNet = null;
175
- console.log("Stopped FileTide.");
150
+ FileTide.outputGradient("Stopped FileTide.", significantMessageColors);
176
151
  }
177
152
  return;
178
153
  }
@@ -183,12 +158,12 @@ class FileTide {
183
158
  */
184
159
  stopMessager(userID) {
185
160
  if (this.clients.has(userID)) {
186
- console.log(`Stopping messager for user ${userID}...`);
161
+ FileTide.outputGradient(`Stopping messager for user ${userID}...`, significantMessageColors);
187
162
  this.clients.delete(userID);
188
- console.log(`Stopped messager for user ${userID}.`);
163
+ FileTide.outputGradient(`Stopped messager for user ${userID}.`, significantMessageColors);
189
164
  return;
190
165
  }
191
- console.log(`No active messager found for user ${userID}.`);
166
+ FileTide.outputGradient(`No active messager found for user ${userID}.`, errorMessageColors);
192
167
  return;
193
168
  }
194
169
 
@@ -196,12 +171,13 @@ class FileTide {
196
171
  * Stop all active clients.
197
172
  */
198
173
  stopAllMessagers() {
199
- console.log("Stopping all messagers...");
174
+ FileTide.outputGradient("Stopping all messagers...", significantMessageColors);
200
175
  this.clients.clear();
201
- console.log("All messagers stopped.");
176
+ FileTide.outputGradient("All messagers stopped.", significantMessageColors);
202
177
  return;
203
178
  }
204
179
  }
180
+ _FileTide = FileTide;
205
181
  function _setupFileEventListeners(userID, client, onIncomingFile) {
206
182
  const cliTable = new ConsoleTable({
207
183
  padding: 2,
@@ -245,13 +221,13 @@ function _setupFileEventListeners(userID, client, onIncomingFile) {
245
221
  console.log(`\n[FileTide ~ FileNet]\n\n`, cliTable.render(), "\n");
246
222
  });
247
223
  client.clientTide.onEvent(userID, "incoming-transfer", data => {
248
- console.log(`[${userID}] Preparing to receive transfer : ${FileUtilityManager.getTidePath(data.path)}`);
224
+ _FileTide.outputGradient(`[${userID}] Preparing to receive transfer : ${FileUtilityManager.getTidePath(data.path)}`, significantMessageColors);
249
225
  this.progressBarManager.completionMessage = `Successfully received transfer from ${recipientId}!`;
250
226
  this.progressBarManager.displayPage("Page Number:", "Press 'n' for next page, 'p' for previous page.", chalk.blue, chalk.magenta);
251
227
  this.progressBarManager.listenForInput("Page Number:", "Press 'n' for next page, 'p' for previous page.", chalk.blue, chalk.magenta, chalk.yellow);
252
228
  });
253
229
  client.clientTide.onEvent(userID, "incoming-file", data => {
254
- console.log(`[${userID}] Preparing to receive file : ${data.fileName}`);
230
+ _FileTide.outputGradient(`[${userID}] Preparing to receive file : ${data.fileName}`, significantMessageColors);
255
231
  activeTransfers.set(data.fileName, {
256
232
  receivedChunks: [],
257
233
  totalChunks: data.totalChunks,
@@ -266,7 +242,7 @@ function _setupFileEventListeners(userID, client, onIncomingFile) {
266
242
  fs.mkdirSync(saveDir, {
267
243
  recursive: true
268
244
  });
269
- console.log(`[${userID}] Created directory : ${saveDir}`);
245
+ _FileTide.outputGradient(`[${userID}] Created directory : ${saveDir}`, significantMessageColors);
270
246
  }
271
247
  });
272
248
  client.clientTide.onEvent(userID, "transfer-progress", data => {
@@ -277,21 +253,15 @@ function _setupFileEventListeners(userID, client, onIncomingFile) {
277
253
  if (!transferState) {
278
254
  return;
279
255
  }
280
-
281
- //console.log(`[${userID}] Receiving chunk ${data.chunkIndex + 1}/${transferState.totalChunks} for file : ${data.fileName}`);
282
-
283
256
  transferState.receivedChunks[data.chunkIndex] = Buffer.from(data.fileChunk);
284
257
  const progress = (data.chunkIndex + 1) / transferState.totalChunks * 100;
285
-
286
- //console.log(`[${userID}] Progress : ${progress.toFixed(2)}%`);
287
-
288
258
  this.progressBarManager.updateTaskProgress(data.fileName, 512 * 1024);
289
259
  if (onIncomingFile) {
290
260
  onIncomingFile(data);
291
261
  }
292
262
  });
293
263
  client.clientTide.onEvent(userID, "transfer-complete", data => {
294
- console.log(`[${userID}] File transfer complete : ${data.fileName}`);
264
+ _FileTide.outputGradient(`[${userID}] File transfer complete : ${data.fileName}`, significantMessageColors);
295
265
  const transferState = activeTransfers.get(data.fileName);
296
266
  if (!transferState) {
297
267
  return;
@@ -299,12 +269,14 @@ function _setupFileEventListeners(userID, client, onIncomingFile) {
299
269
  const savePath = FileUtilityManager.getTidePath(path.join(data.filePath, data.fileName));
300
270
  const fullFile = Buffer.concat(transferState.receivedChunks);
301
271
  fs.writeFileSync(savePath, fullFile);
302
- console.log(`[${userID}] File saved at : ${savePath}`);
272
+ _FileTide.outputGradient(`[${userID}] File saved at : ${savePath}`, significantMessageColors);
303
273
  activeTransfers.delete(data.fileName);
304
274
  });
305
- client.clientTide.onEvent(userID, "transfer-request", data => {
306
- console.log(`[${userID}] Transfer requested from: ${data.clientID}`);
275
+ client.clientTide.onEvent(userID, "transfer-requested", data => {
276
+ _FileTide.outputGradient(`[${userID}] Transfer requested from: ${data.requesterID}`, significantMessageColors);
277
+ this.sendToDevice(data.requesterID, data.path, data.destinationPath);
307
278
  });
279
+ this.currentUserID = userID;
308
280
  return;
309
281
  }
310
282
  ;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+
3
+ const {
4
+ HUDUtilityManager
5
+ } = require("@trap_stevo/legendarybuilderpronodejs-utilities");
6
+ const chalk = require("chalk");
7
+ const significantMessageColors = ["#00C897", "#00E0FF"];
8
+ const errorMessageColors = ["#F94144", "#F3722C"];
9
+ class FileNetUtilityManager {
10
+ static outputGradient(text, colors) {
11
+ const gradient = HUDUtilityManager.generateGradientColors(colors, text.length);
12
+ let coloredText = "";
13
+ for (let i = 0; i < text.length; i++) {
14
+ const [r, g, b] = gradient[i];
15
+ coloredText += chalk.rgb(r, g, b)(text[i]);
16
+ }
17
+ console.log(coloredText);
18
+ return;
19
+ }
20
+ }
21
+ module.exports = {
22
+ FileNetUtilityManager,
23
+ significantMessageColors,
24
+ errorMessageColors
25
+ };
@@ -35,7 +35,8 @@ function _startTransfer(file, transferId, fileDetails, onSendChunk, onComplete,
35
35
  completedChunks: 0,
36
36
  activeChunks: 0,
37
37
  failedChunks: new Map(),
38
- currentChunk: 0
38
+ currentChunk: 0,
39
+ size: fileDetails.size
39
40
  });
40
41
  const updateProgress = () => {
41
42
  const transferState = this.activeTransfers.get(transferId);
@@ -54,6 +55,7 @@ function _startTransfer(file, transferId, fileDetails, onSendChunk, onComplete,
54
55
  onStart({
55
56
  name: fileDetails.name,
56
57
  path: fileDetails.path,
58
+ size: fileDetails.size,
57
59
  totalChunks: transferState.totalChunks
58
60
  });
59
61
  }
@@ -41,7 +41,6 @@ class FileUtilityManager {
41
41
  ">Temp": platform === "win32" ? path.join("C:", "Windows", "Temp") : "/tmp",
42
42
  ">LogFiles": platform === "win32" ? path.join("C:", "Windows", "Logs") : "/var/log",
43
43
  ">SystemRoot": platform === "win32" ? path.join("C:", "Windows") : "/",
44
- ">Downloads": path.join(homeDir, "Downloads"),
45
44
  ">Cache": platform === "win32" ? path.join(homeDir, "AppData", "Local", "Cache") : path.join(homeDir, ".cache"),
46
45
  ">Config": platform === "win32" ? path.join(homeDir, "AppData", "Local", "Config") : "/etc",
47
46
  ">CurrentDirectory": process.cwd(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trap_stevo/filetide",
3
- "version": "0.0.27",
3
+ "version": "0.0.29",
4
4
  "description": "Revolutionizing real-time file transfer with seamless, instant communication across any device. Deliver files instantly, regardless of platform, and experience unparalleled speed and control in managing transfers. Elevate your file-sharing capabilities with a tool designed for precision, efficiency, and effortless connectivity.",
5
5
  "main": "dist/cjs/FileTide.js",
6
6
  "scripts": {
@@ -31,6 +31,7 @@
31
31
  "dependencies": {
32
32
  "@trap_stevo/iotide": "^0.0.36",
33
33
  "@trap_stevo/iotide-client": "^0.0.15",
34
+ "@trap_stevo/legendarybuilderpronodejs-utilities": "^1.0.40",
34
35
  "chalk": "^4.1.2",
35
36
  "readline": "^1.3.0"
36
37
  },