channel-worker 1.0.15 → 1.0.17

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.
@@ -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`);
@@ -82,7 +85,38 @@ class CommandPoller {
82
85
  } catch { /* ignore */ }
83
86
  }
84
87
 
85
- const extensionPath = this.config.extension_path || '';
88
+ const baseExtPath = this.config.extension_path || '';
89
+ let extensionPath = baseExtPath;
90
+
91
+ // Create per-profile extension dir with config.json containing profileId
92
+ if (baseExtPath && profile_id) {
93
+ const fs = require('fs');
94
+ const path = require('path');
95
+ const profileExtPath = baseExtPath + '-' + profile_id;
96
+ try {
97
+ // Copy extension files to per-profile dir (if not exists or outdated)
98
+ if (!fs.existsSync(profileExtPath)) fs.mkdirSync(profileExtPath, { recursive: true });
99
+ const files = fs.readdirSync(baseExtPath);
100
+ for (const f of files) {
101
+ const src = path.join(baseExtPath, f);
102
+ const dst = path.join(profileExtPath, f);
103
+ if (fs.statSync(src).isFile()) {
104
+ // Copy if newer
105
+ const srcMtime = fs.statSync(src).mtimeMs;
106
+ const dstMtime = fs.existsSync(dst) ? fs.statSync(dst).mtimeMs : 0;
107
+ if (srcMtime > dstMtime) fs.copyFileSync(src, dst);
108
+ }
109
+ }
110
+ // Write profile-specific config.json
111
+ const config = { channelManagerApi: 'https://api.channel.tunasm.art', profileId: profile_id };
112
+ fs.writeFileSync(path.join(profileExtPath, 'config.json'), JSON.stringify(config));
113
+ extensionPath = profileExtPath;
114
+ console.log(`[commands] Extension dir: ${profileExtPath} (profile: ${profile_id})`);
115
+ } catch (e) {
116
+ console.warn(`[commands] Per-profile ext dir failed: ${e.message}, using base`);
117
+ }
118
+ }
119
+
86
120
  const result = await this.nst.launchProfile(profile_id, { proxy, extensionPath });
87
121
  console.log(`[commands] Profile ${profile_id} launched${extensionPath ? ' (ext)' : ''}`);
88
122
 
@@ -141,6 +175,34 @@ class CommandPoller {
141
175
  }
142
176
  }
143
177
 
178
+ async handleSaveFile(command) {
179
+ const { url, save_path } = command.payload || {};
180
+ console.log(`[commands] Saving file: ${url} → ${save_path}`);
181
+ try {
182
+ const fs = require('fs');
183
+ const path = require('path');
184
+
185
+ // Ensure directory exists
186
+ const dir = path.dirname(save_path);
187
+ if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
188
+
189
+ // Download and save
190
+ const res = await fetch(url);
191
+ if (!res.ok) throw new Error(`Download failed: ${res.status}`);
192
+ const buffer = Buffer.from(await res.arrayBuffer());
193
+ fs.writeFileSync(save_path, buffer);
194
+
195
+ console.log(`[commands] File saved: ${save_path} (${buffer.length} bytes)`);
196
+ await this.api.updateCommand(command._id, {
197
+ status: 'done',
198
+ result: { file_path: save_path, size: buffer.length },
199
+ });
200
+ } catch (err) {
201
+ console.error(`[commands] Save file failed: ${err.message}`);
202
+ await this.api.updateCommand(command._id, { status: 'failed', error: err.message });
203
+ }
204
+ }
205
+
144
206
  async handleCloseProfile(command) {
145
207
  const { profile_id } = command.payload || {};
146
208
  console.log(`[commands] Closing profile: ${profile_id}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "channel-worker",
3
- "version": "1.0.15",
3
+ "version": "1.0.17",
4
4
  "description": "Channel Manager worker daemon — runs on remote machines to execute video pipeline jobs",
5
5
  "main": "lib/daemon.js",
6
6
  "bin": {