forked from PhotoboothProject/photobooth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremotebuzzer-server.js
More file actions
1161 lines (1009 loc) · 41 KB
/
remotebuzzer-server.js
File metadata and controls
1161 lines (1009 loc) · 41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const fs = require('fs');
const path = require('path');
/* VARIABLES */
// eslint-disable-next-line no-unused-vars
let collageInProgress = false,
triggerArmed = true,
copySuccess = false,
rearmTimer = null;
// Sync destination: photos are synced directly to the USB stick root (no subdirectory)
const { execSync, spawnSync } = require('child_process');
const { pid: PID, platform: PLATFORM } = process;
const REARM_TIMEOUT_MS = 60000; // Fallback to re-arm trigger if no completion arrives
const shellQuote = (s) => '\'' + String(s).replace(/'/g, '\'\\\'\'') + '\'';
/* LOGGING FUNCTION */
const log = function (...optionalParams) {
const currentDate = new Date();
const formattedDate = currentDate.toISOString().replace(/T/, ' ').replace(/\..+/, '');
console.log('[' + formattedDate + `][remotebuzzer][DEBUG] ${PID}:`, ...optionalParams);
};
/* SOURCE PHOTOBOOTH CONFIG */
/*const {execSync} = require('child_process');*/
const cmdConfig = 'bin/photobooth photobooth:config:list json';
const stdoutConfig = execSync(cmdConfig).toString();
const config = JSON.parse(stdoutConfig);
const cmdEnvironmentg = 'bin/photobooth photobooth:environment:list json';
const stdoutEnvironment = execSync(cmdEnvironmentg).toString();
const environment = JSON.parse(stdoutEnvironment);
/* USB INPUT LISTENER CONFIG */
const inputDevicePath = config.remotebuzzer.input_device;
/* WRITE PROCESS PID FILE */
const writePIDFile = (filename) => {
try {
fs.writeFileSync(filename, parseInt(PID, 10).toString(), { flag: 'w' });
log(`PID file created [${filename}]`);
} catch (err) {
throw new Error(`Unable to write PID file [${filename}] - ${err.message}`);
}
};
const pidFilename = path.join(environment.absoluteFolders.var, 'run/remotebuzzer.pid');
writePIDFile(pidFilename);
/* HANDLE EXCEPTIONS */
process.on('uncaughtException', function (err) {
log('Error: ', err.message);
fs.unlink(pidFilename, function (error) {
if (error) {
log('Error deleting PID file ', error.message);
}
});
log('Exiting');
/* got to exit now and here - can not recover from error */
process.exit();
});
/* START HTTP & WEBSOCKET SERVER */
const baseUrl = 'http://' + config.remotebuzzer.serverip + ':' + config.remotebuzzer.port;
log('Server starting on ' + baseUrl);
function triggerPictureFromInput() {
if (!config.remotebuzzer.usebuttons || !config.remotebuzzer.picturebutton) {
log('USB input ignored, hardware buttons or picture button disabled in config');
return;
}
if (!triggerArmed) {
log('USB input ignored, trigger not armed');
return;
}
if (!config.picture.enabled) {
log('USB input ignored, taking pictures disabled');
return;
}
photoboothAction('picture');
}
function triggerActionFromInput(action) {
switch (action) {
case 'picture':
triggerPictureFromInput();
break;
case 'collage':
if (!config.remotebuzzer.usebuttons || !config.remotebuzzer.collagebutton) {
log('USB input ignored, hardware buttons or collage button disabled in config');
} else if (!triggerArmed) {
log('USB input ignored, trigger not armed');
} else if (!config.collage.enabled) {
log('USB input ignored, collage disabled');
} else {
photoboothAction('collage');
}
break;
case 'custom':
if (!config.remotebuzzer.usebuttons || !config.remotebuzzer.custombutton) {
log('USB input ignored, hardware buttons or custom button disabled in config');
} else if (!triggerArmed) {
log('USB input ignored, trigger not armed');
} else if (!config.custom.enabled) {
log('USB input ignored, custom action disabled');
} else {
photoboothAction('custom');
}
break;
case 'video':
if (!config.remotebuzzer.usebuttons || !config.remotebuzzer.videobutton) {
log('USB input ignored, hardware buttons or video button disabled in config');
} else if (!triggerArmed) {
log('USB input ignored, trigger not armed');
} else if (!config.video.enabled) {
log('USB input ignored, video disabled');
} else {
photoboothAction('video');
}
break;
case 'print':
if (!config.remotebuzzer.usebuttons || !config.remotebuzzer.printbutton) {
log('USB input ignored, hardware buttons or print button disabled in config');
} else if (!triggerArmed) {
log('USB input ignored, trigger not armed');
} else {
photoboothAction('print');
}
break;
default:
log(`USB input ignored, unsupported action [${action}]`);
}
}
function startUsbInputListener() {
const bindings = [];
const keyToInt = (key) => (key ? parseInt(key, 10) : 0);
const pushBinding = (code, action) => {
if (code && !Number.isNaN(code)) {
bindings.push({ code, action });
}
};
pushBinding(keyToInt(config.picture.key), 'picture');
pushBinding(keyToInt(config.collage.key), 'collage');
pushBinding(keyToInt(config.custom.key), 'custom');
pushBinding(keyToInt(config.video.key), 'video');
pushBinding(keyToInt(config.print.key), 'print');
if (!inputDevicePath || bindings.length === 0) {
return;
}
const EVENT_SIZE = 24; // timeval (16 bytes) + type (2) + code (2) + value (4)
const stream = fs.createReadStream(inputDevicePath, { highWaterMark: EVENT_SIZE * 8 });
stream.on('data', (chunk) => {
for (let offset = 0; offset + EVENT_SIZE <= chunk.length; offset += EVENT_SIZE) {
const type = chunk.readUInt16LE(offset + 16);
const code = chunk.readUInt16LE(offset + 18);
const value = chunk.readInt32LE(offset + 20);
// EV_KEY press events use type 1, value 1 for key down, 0 for up
if (type === 1 && value === 1) {
const binding = bindings.find((b) => b.code === code);
if (binding) {
log(`USB input matched keycode ${code}, triggering action [${binding.action}]`);
triggerActionFromInput(binding.action);
}
}
}
});
stream.on('error', (err) => {
log(`USB input listener error on [${inputDevicePath}]: ${err.message}`);
});
const codes = bindings.map((b) => b.code).join(', ');
log(`Listening for USB input on [${inputDevicePath}] keycodes [${codes}]`);
}
if (inputDevicePath) {
startUsbInputListener();
}
function armTrigger() {
triggerArmed = true;
if (rearmTimer) {
clearTimeout(rearmTimer);
rearmTimer = null;
}
}
function disarmTrigger() {
triggerArmed = false;
if (rearmTimer) {
clearTimeout(rearmTimer);
}
rearmTimer = setTimeout(() => {
triggerArmed = true;
log(`Re-arming trigger after timeout (${REARM_TIMEOUT_MS}ms)`);
rearmTimer = null;
}, REARM_TIMEOUT_MS);
}
function photoboothAction(type) {
switch (type) {
case 'picture':
disarmTrigger();
collageInProgress = false;
log('Photobooth trigger PICTURE : [ photobooth-socket ] => [ All Clients ]: command [ picture ]');
ioServer.emit('photobooth-socket', 'start-picture');
break;
case 'custom':
disarmTrigger();
collageInProgress = false;
log('Photobooth trigger CUSTOM : [ photobooth-socket ] => [ All Clients ]: command [ custom ]');
ioServer.emit('photobooth-socket', 'start-custom');
break;
case 'video':
disarmTrigger();
collageInProgress = false;
log('Photobooth trigger VIDEO : [ photobooth-socket ] => [ All Clients ]: command [ video ]');
ioServer.emit('photobooth-socket', 'start-video');
break;
case 'move2usb':
disarmTrigger();
collageInProgress = false;
log('Photobooth trigger MOVE2USB : [ photobooth-socket ] => [ All Clients ]: command [ move2usb ]');
move2usbAction();
break;
case 'collage':
disarmTrigger();
collageInProgress = true;
log('Photobooth trigger COLLAGE : [ photobooth-socket ] => [ All Clients ]: command [ collage ]');
ioServer.emit('photobooth-socket', 'start-collage');
break;
case 'collage-next':
log('Photobooth COLLAGE : [ photobooth-socket ] => [ All Clients ]: command [ collage-next ]');
ioServer.emit('photobooth-socket', 'collage-next');
break;
case 'completed':
armTrigger();
collageInProgress = false;
log('Photobooth activity completed : [ photobooth-socket ] => [ All Clients ]: command [ completed ]');
ioServer.emit('photobooth-socket', 'completed');
break;
case 'print':
disarmTrigger();
log('Photobooth trigger PRINT : [ photobooth-socket ] => [ All Clients ]: command [ print ]');
ioServer.emit('photobooth-socket', 'print');
break;
case 'rotary-cw':
ioServer.emit('photobooth-socket', 'rotary-cw');
break;
case 'rotary-ccw':
ioServer.emit('photobooth-socket', 'rotary-ccw');
break;
case 'rotary-btn-press':
ioServer.emit('photobooth-socket', 'rotary-btn-press');
break;
case 'reset':
photoboothAction('completed');
break;
default:
log('Photobooth action [', type, '] not implemented - ignoring');
break;
}
}
/* CONFIGURE HTTP ENDPOINTS */
const requestListener = function (req, res) {
function sendText(content, contentType) {
res.setHeader('Content-Type', contentType || 'text/plain');
res.setHeader('Access-Control-Allow-Origin', '*');
res.writeHead(200);
res.end(content);
}
const urlObj = new URL(req.url, 'http://' + config.webserver.ip);
const queryParams = urlObj.searchParams;
switch (urlObj.pathname) {
case '/':
log('http: GET /');
sendText(
`<h1>Trigger Endpoints</h1>
<ul>
<li>Trigger photo: <a href="${baseUrl}/commands/start-picture" target="_blank">${baseUrl}/commands/start-picture</a></li>
<li>Trigger collage: <a href="${baseUrl}/commands/start-collage" target="_blank">${baseUrl}/commands/start-collage</a></li>
<li>Trigger custom: <a href="${baseUrl}/commands/start-custom" target="_blank">${baseUrl}/commands/start-custom</a></li>
<li>Trigger print: <a href="${baseUrl}/commands/start-print" target="_blank">${baseUrl}/commands/start-print</a></li>
<li>Trigger video: <a href="${baseUrl}/commands/start-video" target="_blank">${baseUrl}/commands/start-video</a></li>
<li>Trigger picture move to USB: <a href="${baseUrl}/commands/start-move2usb" target="_blank">${baseUrl}/commands/start-move2usb</a></li>
<li>Increase the printlimit by i <a href="${baseUrl}/commands/increase-print-limit?i=1" target="_blank">${baseUrl}/commands/increase-print-limit?i=1</a></li>
</ul>
<h1>Rotary Endpoints</h1>
<ul>
<li>Focus next: <a href="${baseUrl}/commands/rotary-cw" target="_blank">${baseUrl}/commands/rotary-cw</a></li>
<li>Focus previous: <a href="${baseUrl}/commands/rotary-ccw" target="_blank">${baseUrl}/commands/rotary-ccw</a></li>
<li>Click: <a href="${baseUrl}/commands/rotary-btn-press" target="_blank">${baseUrl}/commands/rotary-btn-press</a></li>
</ul>
<h1>Power</h1>
<ul>
<li>Shutdwon now: <a href="${baseUrl}/commands/shutdown-now" target="_blank">${baseUrl}/commands/shutdown-now</a></li>
<li>Reboot now: <a href="${baseUrl}/commands/reboot-now" target="_blank">${baseUrl}/commands/reboot-now</a></li>
</ul>`,
'text/html'
);
break;
case '/commands/increase-print-limit':
log('http: GET /commands/increase-print-limit');
if (config.remotebuzzer.usebuttons) {
if (config.print.from_result || config.print.from_gallery) {
let i = 1;
let j = parseInt(queryParams.get('i'), 10);
if (j) {
i = j;
}
http.get(config.webserver.url + 'api/printLimit.php?increaseCount=' + i);
sendText(`Increased print limit by ${i}`);
} else {
sendText('Please enable print from results screen or print from gallery.');
}
} else {
sendText('Please enable Hardware Button support!');
}
break;
case '/commands/start-picture':
log('http: GET /commands/start-picture');
if (config.remotebuzzer.usebuttons && config.remotebuzzer.picturebutton) {
if (triggerArmed) {
if (config.picture.enabled) {
photoboothAction('picture');
sendText('TAKE PHOTO TRIGGERED.');
} else {
sendText('PHOTO DISABLED.');
}
} else {
sendText('ALREADY TRIGGERED AN ACTION');
}
} else {
sendText('Please enable Hardware Button support and Picture Button!');
}
break;
case '/commands/start-collage':
log('http: GET /commands/start-collage');
if (config.remotebuzzer.usebuttons && config.remotebuzzer.collagebutton) {
if (triggerArmed) {
if (config.collage.enabled) {
photoboothAction('collage');
sendText('TAKE COLLAGE TRIGGERED');
} else {
sendText('COLLAGE DISABLED.');
}
} else {
sendText('ALREADY TRIGGERED AN ACTION');
}
} else {
sendText('Please enable Hardware Button support and Collage Button!');
}
break;
case '/commands/start-custom':
log('http: GET /commands/start-custom');
if (config.remotebuzzer.usebuttons && config.remotebuzzer.custombutton) {
if (triggerArmed) {
if (config.custom.enabled) {
photoboothAction('custom');
sendText('TAKE CUSTOM TRIGGERED');
} else {
sendText('CUSTOM DISABLED.');
}
} else {
sendText('ALREADY TRIGGERED AN ACTION');
}
} else {
sendText('Please enable Hardware Button support and Custom Button!');
}
break;
case '/commands/start-print':
log('http: GET /commands/start-print');
if (config.remotebuzzer.usebuttons && config.remotebuzzer.printbutton) {
if (triggerArmed) {
photoboothAction('print');
sendText('PRINT TRIGGERED');
} else {
sendText('ALREADY TRIGGERED AN ACTION');
}
} else {
sendText('Please enable Hardware Button support and Print Button!');
}
break;
case '/commands/start-move2usb':
log('http: GET /commands/start-move2usb');
if (config.remotebuzzer.usebuttons && config.remotebuzzer.move2usb != 'disabled') {
if (triggerArmed) {
photoboothAction('move2usb');
sendText('MOVE2USB TRIGGERED');
} else {
sendText('ALREADY TRIGGERED AN ACTION');
}
} else {
sendText('Please enable Hardware Button support and Move2USB Button!');
}
break;
case '/commands/start-video':
log('http: GET /commands/start-video');
if (config.remotebuzzer.usebuttons && config.remotebuzzer.videobutton) {
if (triggerArmed) {
if (config.video.enabled) {
photoboothAction('video');
sendText('TAKE VIDEO TRIGGERED');
} else {
sendText('VIDEO DISABLED.');
}
} else {
sendText('ALREADY TRIGGERED AN ACTION');
}
} else {
sendText('Please enable Hardware Button support and Video Button!');
}
break;
case '/commands/rotary-cw':
log('http: GET /commands/rotary-cw');
if (config.remotebuzzer.userotary) {
photoboothAction('rotary-cw');
sendText('FOCUS NEXT ELEMENT');
} else {
sendText('Please enable rotary Controller support!');
}
break;
case '/commands/rotary-ccw':
log('http: GET /commands/rotary-ccw');
if (config.remotebuzzer.userotary) {
photoboothAction('rotary-ccw');
sendText('FOCUS PREVIOUS ELEMENT');
} else {
sendText('Please enable rotary Controller support!');
}
break;
case '/commands/rotary-btn-press':
log('http: GET /commands/rotary-btn-press');
if (config.remotebuzzer.userotary) {
photoboothAction('rotary-btn-press');
sendText('CLICK ELEMENT');
} else {
sendText('Please enable rotary Controller support!');
}
break;
case '/commands/shutdown-now':
log('http: GET /commands/shutdown-now');
if (config.remotebuzzer.usebuttons && config.remotebuzzer.shutdownbutton) {
sendText('SHUTTING DOWN');
/* Initiate system shutdown */
const cmd = 'sudo ' + config.commands.shutdown;
execSync(cmd);
} else {
sendText('Please enable Hardware Button support and Shutdown Button!');
}
break;
case '/commands/reboot-now':
log('http: GET /commands/reboot-now');
if (config.remotebuzzer.usebuttons && config.remotebuzzer.rebootbutton) {
sendText('REBOOTING NOW');
/* Initiate system shutdown */
const cmd = 'sudo ' + config.commands.reboot;
execSync(cmd);
} else {
sendText('Please enable Hardware Button support and Reboot Button!');
}
break;
default:
res.writeHead(404);
res.end();
}
};
const http = require('http');
const server = new http.Server(requestListener);
/* CONFIGURE WEBSOCKET SERVER */
const ioServer = require('socket.io')(server, {
cors: {
origin: '*',
methods: ['GET', 'POST']
}
});
/* NEW CLIENT CONNECTED */
ioServer.on('connection', function (client) {
log('New client connected - ID', client.id);
client.on('photobooth-socket', function (data) {
log('Data from client ID ', client.id, ': [ photobooth-socket ] => [' + data + ']');
/* CLIENT COMMANDS RECEIVED */
switch (data) {
case 'completed':
photoboothAction('completed');
break;
case 'in-progress':
triggerArmed = false;
break;
case 'start-picture':
photoboothAction('picture');
break;
case 'start-collage':
photoboothAction('collage');
break;
case 'start-custom':
photoboothAction('custom');
break;
case 'start-video':
photoboothAction('video');
break;
case 'start-move2usb':
photoboothAction('move2usb');
break;
case 'collage-wait-for-next':
armTrigger();
break;
default:
log('Received unknown command [', data, '] - ignoring');
break;
}
});
/* CLIENT DISCONNECTED */
client.on('disconnect', function () {
log('Client disconnected - ID ', client.id);
if (ioServer.engine.clientsCount == 0) {
log('No more clients connected - removing lock and arming trigger');
triggerArmed = true;
collageInProgress = false;
}
});
});
/* STARTUP COMPLETED */
server.listen(config.remotebuzzer.port, () => {
log('socket.io server started');
});
function move2usbAction() {
const parseConfig = () => {
try {
return {
dataAbsPath: environment.absoluteFolders.data,
drive: config.synctodrive.target,
dbName: config.database.file
};
} catch (err) {
log('ERROR: unable to parse sync-to-drive config', err);
}
return null;
};
/* PARSE PHOTOBOOTH CONFIG */
const parsedConfig = parseConfig();
if (!parsedConfig) {
log('ERROR: Could not parse config, aborting move2usb');
photoboothAction('completed');
return;
}
copySuccess = false;
log('USB target ', parsedConfig.drive);
const getDriveInfo = ({ drive }) => {
let json = null;
const requiredColumns = 'NAME,KNAME,PATH,LABEL,MOUNTPOINT,MOUNTPOINTS,SUBSYSTEMS,TYPE';
drive = drive.toLowerCase();
try {
// Try -ablJO first (all columns), fall back to explicit column list
let output;
try {
output = execSync('LC_ALL=C lsblk -ablJO 2>/dev/null').toString();
} catch {
log('lsblk -O not supported, falling back to explicit columns');
output = execSync(`LC_ALL=C lsblk -ablJ -o ${requiredColumns} 2>/dev/null`).toString();
}
json = JSON.parse(output);
} catch (err) {
log(
'ERROR: Could not parse the output of lsblk! Please make sure its installed and that it offers JSON output!',
err.message
);
return null;
}
if (!json || !json.blockdevices) {
log('ERROR: The output of lsblk was malformed!');
return null;
}
const device = json.blockdevices.find(
(blk) =>
blk.subsystems &&
blk.subsystems.includes('usb') &&
((blk.name && drive === blk.name.toLowerCase()) ||
(blk.kname && drive === blk.kname.toLowerCase()) ||
(blk.path && drive === blk.path.toLowerCase()) ||
(blk.label && drive === blk.label.toLowerCase()))
);
if (device) {
// Normalize mountpoints (array) to mountpoint (string) for compatibility
// Newer lsblk versions use "mountpoints" array instead of "mountpoint" string
if (!device.mountpoint && Array.isArray(device.mountpoints)) {
device.mountpoint = device.mountpoints.find((mp) => mp !== null) || null;
}
return device;
}
// Fallback: lsblk may not report labels (e.g. in LXC containers or after USB reconnect).
// Use blkid which reads directly from the block device and always works.
log('lsblk did not find device "' + drive + '" by label, trying blkid fallback...');
return findDeviceByBlkid(drive, json.blockdevices);
};
const findDeviceByBlkid = (drive, lsblkDevices) => {
let blkidOutput;
try {
blkidOutput = execSync('LC_ALL=C blkid 2>/dev/null').toString();
} catch (err) {
log('blkid fallback failed: ' + err.message);
log('ERROR: Device ' + drive + ' was not detected (blkid fallback also failed)');
return null;
}
const lines = blkidOutput.split('\n').filter((line) => line.trim());
for (const line of lines) {
const labelMatch = line.match(/\bLABEL="([^"]+)"/);
if (!labelMatch || labelMatch[1].toLowerCase() !== drive) {
continue;
}
const pathMatch = line.match(/^(\/dev\/[^:]+):/);
if (!pathMatch) {
continue;
}
const devicePath = pathMatch[1];
log('blkid found device with label "' + drive + '" at ' + devicePath);
// Cross-reference with lsblk to verify it is a USB device
const lsblkEntry = lsblkDevices.find((blk) => blk.path === devicePath);
if (lsblkEntry && lsblkEntry.subsystems && !lsblkEntry.subsystems.includes('usb')) {
log(
'Device ' +
devicePath +
' is not a USB device (subsystems: ' +
lsblkEntry.subsystems +
'), skipping'
);
continue;
}
// Determine current mountpoint
let mountpoint = null;
if (lsblkEntry && lsblkEntry.mountpoint) {
mountpoint = lsblkEntry.mountpoint;
} else if (lsblkEntry && Array.isArray(lsblkEntry.mountpoints)) {
mountpoint = lsblkEntry.mountpoints.find((mp) => mp !== null) || null;
} else {
try {
mountpoint =
execSync('findmnt -n -o TARGET ' + devicePath + ' 2>/dev/null')
.toString()
.trim() || null;
// eslint-disable-next-line no-unused-vars
} catch (e) {
// Device is not currently mounted
}
}
const name = path.basename(devicePath);
const device = {
name: name,
kname: name,
path: devicePath,
label: labelMatch[1],
mountpoint: mountpoint,
subsystems: lsblkEntry && lsblkEntry.subsystems ? lsblkEntry.subsystems : 'block:scsi:usb:pci'
};
log('blkid fallback found device: ' + JSON.stringify(device));
return device;
}
log('ERROR: Device ' + drive + ' was not detected');
return null;
};
const isWritable = (directory) => {
try {
fs.accessSync(directory, fs.constants.W_OK);
return true;
// eslint-disable-next-line no-unused-vars
} catch (err) {
return false;
}
};
/**
* Find the current mountpoint of a device using findmnt (most reliable).
* Returns the mountpoint string or null.
*/
const findCurrentMountpoint = (drive) => {
// First check the mountpoint from lsblk data
if (drive.mountpoint) {
try {
execSync(`mountpoint -q '${drive.mountpoint.replace(/'/g, '\'\\\'\'')}'`, { stdio: 'ignore' });
return drive.mountpoint;
// eslint-disable-next-line no-unused-vars
} catch (err) {
// lsblk mountpoint is stale
}
}
// Use findmnt as the authoritative source
try {
const mp = execSync('findmnt -n -o TARGET ' + drive.path + ' 2>/dev/null')
.toString()
.trim();
return mp || null;
// eslint-disable-next-line no-unused-vars
} catch (err) {
return null;
}
};
/**
* Get mount options for FAT/exFAT filesystems that ensure all users can read/write.
* Returns null for non-FAT filesystems.
*/
const getFATMountOptions = (devicePath) => {
try {
const fstype = execSync('lsblk -n -o FSTYPE ' + devicePath + ' 2>/dev/null')
.toString()
.trim();
if (fstype === 'vfat' || fstype === 'exfat') {
return 'umask=0000,uid=' + process.getuid() + ',gid=' + process.getgid();
}
// eslint-disable-next-line no-unused-vars
} catch (err) {
// ignore
}
return null;
};
const mountDrive = (drive) => {
if (!drive || !drive.path) {
log('ERROR: No drive or drive path provided for mounting');
return null;
}
// Check if already mounted (via lsblk data or findmnt)
const currentMountpoint = findCurrentMountpoint(drive);
if (currentMountpoint) {
drive.mountpoint = currentMountpoint;
if (isWritable(currentMountpoint)) {
log('Device ' + drive.path + ' is already mounted at ' + currentMountpoint + ' and writable');
return drive;
}
// Mounted but not writable (e.g. FAT32 mounted by another user with their uid/gid)
log('Device ' + drive.path + ' is mounted at ' + currentMountpoint + ' but not writable, unmounting...');
unmountDrive(drive);
}
// Try udisksctl first
try {
const mountCmd = `LC_ALL=C udisksctl mount -b ${drive.path}`;
log('Mounting device ' + drive.path + ' via udisksctl');
const mountRes = execSync(mountCmd, { timeout: 30000 }).toString();
log('udisksctl output: ' + mountRes.trim());
const mountMatch = mountRes.match(/Mounted\s+\S+\s+at\s+(.+)/);
if (mountMatch) {
const mountPoint = mountMatch[1].trim().replace(/[\n.]+$/gu, '');
if (mountPoint) {
drive.mountpoint = mountPoint;
log('Mounted via udisksctl at ' + drive.mountpoint);
return drive;
}
}
log('udisksctl mount returned unexpected output: ' + mountRes.trim());
} catch (udisksErr) {
log('udisksctl mount failed: ' + udisksErr.message);
}
// udisksctl may have succeeded but output was not parseable.
// Check with findmnt if the device actually got mounted.
try {
const findmntRes = execSync('findmnt -n -o TARGET ' + drive.path + ' 2>/dev/null')
.toString()
.trim();
if (findmntRes) {
if (isWritable(findmntRes)) {
drive.mountpoint = findmntRes;
log('Device already mounted (detected via findmnt) at ' + drive.mountpoint);
return drive;
}
// Mounted but not writable, unmount and continue to fallback
log('Device mounted at ' + findmntRes + ' but not writable, unmounting...');
drive.mountpoint = findmntRes;
unmountDrive(drive);
}
// eslint-disable-next-line no-unused-vars
} catch (findmntErr) {
// Device is not mounted
}
// Ensure the device is fully unmounted before attempting fallback mount
try {
const remainingMount = execSync('findmnt -n -o TARGET ' + drive.path + ' 2>/dev/null')
.toString()
.trim();
if (remainingMount) {
log('Device still mounted at ' + remainingMount + ' (automount?), forcing unmount...');
try {
execSync('sudo umount ' + shellQuote(drive.path), { timeout: 30000 });
log('Forced unmount successful');
} catch (forceErr) {
log('Forced unmount failed: ' + forceErr.message);
}
}
// eslint-disable-next-line no-unused-vars
} catch (err) {
// not mounted, good
}
// Fallback: direct mount command with sudo
const label = drive.label || path.basename(drive.path);
const fallbackMountpoint = path.join('/media', label);
try {
if (!fs.existsSync(fallbackMountpoint)) {
execSync('sudo mkdir -p ' + shellQuote(fallbackMountpoint));
}
// Detect filesystem type to apply appropriate mount options
const mountOpts = getFATMountOptions(drive.path);
const mountCmd = mountOpts
? `sudo mount -o ${mountOpts} ${shellQuote(drive.path)} ${shellQuote(fallbackMountpoint)}`
: `sudo mount ${shellQuote(drive.path)} ${shellQuote(fallbackMountpoint)}`;
log('Trying fallback: ' + mountCmd);
execSync(mountCmd, { timeout: 30000 });
drive.mountpoint = fallbackMountpoint;
log('Mounted via direct mount at ' + drive.mountpoint);
return drive;
} catch (mountErr) {
log('Direct mount also failed: ' + mountErr.message);
}
log('ERROR: Unable to mount device ' + drive.path);
return null;
};
const startSync = ({ dataAbsPath, drive }) => {
if (!fs.existsSync(dataAbsPath)) {
log(`ERROR: Folder [${dataAbsPath}] does not exist!`);
return;
}
log('Starting sync to USB drive ...');
log(`Source data folder [${dataAbsPath}]`);
log(`Syncing to drive [${drive.path}] -> [${drive.mountpoint}]`);
fs.writeFileSync(path.join(dataAbsPath, 'copy.chk'), '', { flag: 'w' });
const usbCheckfile = path.join(drive.mountpoint, 'copy.chk');
if (fs.existsSync(usbCheckfile)) {
log(' ');
log('[WARNING] Move2USB: stale copy.chk already on USB before rsync:', usbCheckfile);
log(' ');
}
const cmd = (() => {
switch (process.platform) {
case 'win32':
return null;
case 'linux':
// prettier-ignore
return [
'rsync',
'-a',
'--delete-before',
'-b',
`--backup-dir=${path.join(drive.mountpoint, 'deleted')}`,
'--ignore-existing',
'--include=\'*.\'{jpg,chk,gif,mp4}',
'--include=\'*/\'',
'--exclude=\'*\'',
'--prune-empty-dirs',
dataAbsPath + '/',
drive.mountpoint
].join(' ');
default:
return null;
}
})();
if (!cmd) {
log('ERROR: No command for syncing!');
return;
}
log('Executing command: <', cmd, '>');
const rsyncResult = spawnSync(cmd, {
shell: '/bin/bash',
stdio: 'ignore'
});
if (rsyncResult.error) {
log('ERROR: Could not start rsync:', rsyncResult.error.toString());
return;
}
if (rsyncResult.status !== 0) {
log('ERROR: rsync exited with code ' + rsyncResult.status);
return;
}
log('Sync completed');
if (fs.existsSync(usbCheckfile)) {
copySuccess = true;
} else {
log(' ');
log(
'[ERROR] Move2USB: sync verification failed (copy.chk missing on USB after rsync, expected at):',
usbCheckfile
);
log(' ');
copySuccess = false;
return;
}
try {
fs.unlinkSync(usbCheckfile);
} catch (err) {
log('Warning: Could not remove checkfile from USB: ' + err.message);
}
try {
fs.unlinkSync(path.join(dataAbsPath, 'copy.chk'));
} catch (err) {
log('Warning: Could not remove local checkfile: ' + err.message);
}
};
const unmountDrive = (drive) => {
if (!drive || !drive.path) {
log('Nothing to unmount');