107 lines
3.8 KiB
JavaScript
107 lines
3.8 KiB
JavaScript
// SyncTool
|
|
// Read or modify this code if you dare, it may be unilicense but you'll loose your sanity
|
|
import { execSync, spawn } from 'node:child_process';
|
|
import { existsSync, readFileSync, writeFileSync, readdirSync, statSync, openAsBlob } from 'node:fs';
|
|
import { isMounted,unmountDrive,mountDrive, driveExists } from './usbkey-lifecycle';
|
|
import { startCopyparty, stopCopyparty } from './services/copyparty'; //Realistically we should just use promise for this to keep things optimized. But, who cares about optimization!
|
|
import { start }
|
|
import data from './config.json' with { type: 'json' }
|
|
import path from 'node:path';
|
|
import { start } from 'node:repl';
|
|
import { runRclone, runRsync } from './services/syncjobs';
|
|
console.log('SyncTool init')
|
|
console.log("Don't blame me if stuff bursts into flames")
|
|
console.log("System info:")
|
|
// Print enabled servers
|
|
const serverConfigMap = {
|
|
'Open Notebook': 'openNotebook',
|
|
'Rclone': 'rclone',
|
|
'Rsync': 'rsync',
|
|
'Copyparty': 'copyparty'
|
|
}
|
|
Object.entries(serverConfigMap).forEach(([server, configKey]) => {
|
|
const isEnabled = data[configKey]?.enabled === true
|
|
console.log(`${isEnabled ? '✓' : '✗'} ${server} ${isEnabled ? '(enabled)' : '(disabled)'}`)
|
|
})
|
|
|
|
|
|
|
|
//Check if the Flash Drive is plugged in
|
|
if (isMounted(config.system.targetUuid, config.system.mountPoint) == true) {
|
|
console.log("Drive looks mounted.")
|
|
var USBKeyMounted = true
|
|
} else {
|
|
if (driveExists != null) {
|
|
var FDDevNode = driveExists()
|
|
var mountAttempts = 0
|
|
console.debug("Mount attempts: ", mountAttempts)
|
|
try {
|
|
mountDrive(FDDevNode, config.mountPoint)
|
|
} catch (error) {
|
|
mountAttempts++
|
|
console.debug("Mount attempts: ", mountAttempts)
|
|
if (mountAttempts >= config.maxFailedMounts) {
|
|
console.error("Failed to mount the drive... Did it disconnect?")
|
|
exit(1)
|
|
}
|
|
} // Otherwise the drive exists and we can move on
|
|
}
|
|
}
|
|
//If RSYNC is on, check if the NAS is mounted and empty:
|
|
if (config.rsync.enabled == true) {
|
|
const nasPath = config.rsync.nasPath
|
|
console.log(`Checking NAS at ${nasPath}:`)
|
|
try {
|
|
const files = readdirSync(nasPath)
|
|
if (files.length === 0) {
|
|
console.warn("NAS is empty!")
|
|
} else {
|
|
console.log(`Found ${files.length} items in NAS`)
|
|
}
|
|
} catch (error) {
|
|
console.error(`Error checking NAS: ${error.message}`)
|
|
}
|
|
}
|
|
// Now it's Loadin time
|
|
// The best part about index.js was when it said "it's loadin time" and loaded all over the place
|
|
var copypartyStarted = false
|
|
var OpenNotebookstarted = false
|
|
var rcloneStarted = false
|
|
var rcloneRuns = 0
|
|
var rSYNcStarted = false
|
|
var rSYNcRuns = 0
|
|
function loadintime() {
|
|
//WELCOME TO if HELL!!!!
|
|
if (config.copyparty.enabled == true) {
|
|
console.debug("Copyparty enabled, starting")
|
|
startCopyparty(config)
|
|
}
|
|
if (config.rclone.enabled == true) {
|
|
// Go ahead and run both once
|
|
rcloneRuns++
|
|
console.debug("Starting first run for Rclone...")
|
|
try {
|
|
await runRclone(config)
|
|
var rcloneStarted = true
|
|
} catch (error) {
|
|
console.warn("Rclone went up in flames: ", error.message)
|
|
var rcloneStarted = false
|
|
}
|
|
}
|
|
if (config.rsync.enabled == true) {
|
|
rSYNcRuns++
|
|
console.debug("Starting first run for Rsync...")
|
|
try {
|
|
await runRsync(config)
|
|
var rSYNcStarted = true
|
|
} catch (error) {
|
|
console.warn("Rsync went up in flames: ", error.message)
|
|
var rSYNcStarted = false
|
|
}
|
|
}
|
|
if (config.openNotebook.enabled == true) {
|
|
//do whatever is needed here
|
|
}
|
|
console.debug("loadin time complete")
|
|
}
|
|
//TODO: Actually do something? idiot
|