channel-worker 1.0.14 → 1.0.16
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.
- package/lib/command-poller.js +31 -0
- package/lib/nst-manager.js +12 -4
- package/package.json +1 -1
package/lib/command-poller.js
CHANGED
|
@@ -46,6 +46,9 @@ class CommandPoller {
|
|
|
46
46
|
case 'close_profile':
|
|
47
47
|
await this.handleCloseProfile(command);
|
|
48
48
|
break;
|
|
49
|
+
case 'save_file':
|
|
50
|
+
await this.handleSaveFile(command);
|
|
51
|
+
break;
|
|
49
52
|
default:
|
|
50
53
|
// Other commands (scan_facebook_pages, etc.) handled by extension
|
|
51
54
|
console.log(`[commands] Skipping ${command.type} — handled by extension`);
|
|
@@ -141,6 +144,34 @@ class CommandPoller {
|
|
|
141
144
|
}
|
|
142
145
|
}
|
|
143
146
|
|
|
147
|
+
async handleSaveFile(command) {
|
|
148
|
+
const { url, save_path } = command.payload || {};
|
|
149
|
+
console.log(`[commands] Saving file: ${url} → ${save_path}`);
|
|
150
|
+
try {
|
|
151
|
+
const fs = require('fs');
|
|
152
|
+
const path = require('path');
|
|
153
|
+
|
|
154
|
+
// Ensure directory exists
|
|
155
|
+
const dir = path.dirname(save_path);
|
|
156
|
+
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
|
|
157
|
+
|
|
158
|
+
// Download and save
|
|
159
|
+
const res = await fetch(url);
|
|
160
|
+
if (!res.ok) throw new Error(`Download failed: ${res.status}`);
|
|
161
|
+
const buffer = Buffer.from(await res.arrayBuffer());
|
|
162
|
+
fs.writeFileSync(save_path, buffer);
|
|
163
|
+
|
|
164
|
+
console.log(`[commands] File saved: ${save_path} (${buffer.length} bytes)`);
|
|
165
|
+
await this.api.updateCommand(command._id, {
|
|
166
|
+
status: 'done',
|
|
167
|
+
result: { file_path: save_path, size: buffer.length },
|
|
168
|
+
});
|
|
169
|
+
} catch (err) {
|
|
170
|
+
console.error(`[commands] Save file failed: ${err.message}`);
|
|
171
|
+
await this.api.updateCommand(command._id, { status: 'failed', error: err.message });
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
144
175
|
async handleCloseProfile(command) {
|
|
145
176
|
const { profile_id } = command.payload || {};
|
|
146
177
|
console.log(`[commands] Closing profile: ${profile_id}`);
|
package/lib/nst-manager.js
CHANGED
|
@@ -37,8 +37,8 @@ class NstManager {
|
|
|
37
37
|
// Find profile by name, return profileId
|
|
38
38
|
async findProfile(name) {
|
|
39
39
|
try {
|
|
40
|
+
// Try SDK first
|
|
40
41
|
const res = await this.client.profiles().getProfiles({ keyword: name });
|
|
41
|
-
// Response: { data: { docs: [...], totalDocs, ... } }
|
|
42
42
|
let profiles = [];
|
|
43
43
|
if (res?.data?.docs && Array.isArray(res.data.docs)) {
|
|
44
44
|
profiles = res.data.docs;
|
|
@@ -47,9 +47,17 @@ class NstManager {
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
const match = profiles.find(p => p.name === name);
|
|
50
|
-
if (match)
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
if (match) return match.profileId || match._id;
|
|
51
|
+
|
|
52
|
+
// Fallback: fetch all profiles via local API
|
|
53
|
+
const rawRes = await fetch('http://localhost:8848/api/v2/profiles/', {
|
|
54
|
+
headers: { 'x-api-key': this.apiKey },
|
|
55
|
+
});
|
|
56
|
+
const rawData = await rawRes.json();
|
|
57
|
+
const allProfiles = rawData?.data?.docs || rawData?.data || [];
|
|
58
|
+
const fallback = allProfiles.find(p => p.name === name);
|
|
59
|
+
if (fallback) return fallback.profileId || fallback._id;
|
|
60
|
+
|
|
53
61
|
return null;
|
|
54
62
|
} catch (err) {
|
|
55
63
|
console.error(`[nst] Error finding profile "${name}":`, err.message);
|