Skip to content

Commit 7c04042

Browse files
author
3aa49ec6bfc910647fa1c5a013e48eef
committed
Started to build function with more granular copy controls
1 parent 985ec04 commit 7c04042

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

worker/package-lock.json

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

worker/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"sqlite3": "^5.1.6"
2121
},
2222
"devDependencies": {
23+
"@types/ini": "^4.1.0",
2324
"@types/node": "^20.10.5",
2425
"@types/ping": "^0.4.4",
2526
"tsup": "^8.0.1",

worker/src/modules/rclone.ts

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import fs from 'fs';
44
import { executeBashCommand } from './bash.js';
55
import { clearInterval, setInterval } from 'timers';
66
import readLastLines from 'read-last-lines';
7+
import path from 'path';
78

8-
export const rcloneCopyWithProgress = async (path, rcloneConfig, destinationPath) => {
9+
export const rcloneCopyWithProgress = async (path: string, rcloneConfig: string, destinationPath: string) => {
910
const intervalId = setInterval(() => {
1011
logWithTimestamp("Copying files, check rclone.log for status...");
1112
// Add something useful here, e.g. giving percentage complete, transfer speed, time remaining
@@ -18,6 +19,57 @@ export const rcloneCopyWithProgress = async (path, rcloneConfig, destinationPath
1819
}
1920
};
2021

22+
// TODO: Move over to this new function (not tested yet)
23+
24+
export const rCloneCopyWithProgressByFolder = async (basePath: string, rcloneConfig: string, destinationPath: string) => {
25+
// Function to list subfolders
26+
const listSubfolders = async (dir: string): Promise<string[]> => {
27+
const subdirs = await fs.promises.readdir(dir);
28+
const folders = await Promise.all(subdirs.map(async (subdir) => {
29+
const res = path.resolve(dir, subdir);
30+
return (await fs.promises.stat(res)).isDirectory() ? res : '';
31+
}));
32+
return folders.filter(Boolean);
33+
};
34+
35+
// Function to get folder information (number of files and total size)
36+
const getFolderInfo = async (folderPath: string): Promise<{ fileCount: number, totalSize: number }> => {
37+
const files = await fs.promises.readdir(folderPath);
38+
let totalSize = 0;
39+
for (const file of files) {
40+
const filePath = path.join(folderPath, file);
41+
const stat = await fs.promises.stat(filePath);
42+
if (stat.isFile()) totalSize += stat.size;
43+
}
44+
return { fileCount: files.length, totalSize };
45+
};
46+
47+
// List and sort subfolders
48+
const folders = await listSubfolders(basePath);
49+
folders.sort().reverse();
50+
51+
for (const folder of folders) {
52+
// Get folder information
53+
const { fileCount, totalSize } = await getFolderInfo(folder);
54+
55+
// Log event
56+
console.log(`Copying folder: ${folder}, Files: ${fileCount}, Total Size: ${Math.round(totalSize / 1024 / 1024)} bytes`);
57+
58+
const intervalId = setInterval(() => {
59+
logWithTimestamp(`Still copying folder: ${folder}. Check rclone.log for status...`);
60+
// Add something useful here, e.g. giving percentage complete, transfer speed, time remaining
61+
}, 60000); // 60000 ms = 1 minute
62+
63+
try {
64+
await rcloneCopy(folder, rcloneConfig, path.join(destinationPath, path.basename(folder)));
65+
} finally {
66+
clearInterval(intervalId);
67+
}
68+
69+
}
70+
};
71+
72+
2173

2274
export const getRcloneConfig = () => {
2375
const configPath = '/root/.config/rclone/rclone.conf';
@@ -30,7 +82,7 @@ export const getRcloneConfig = () => {
3082
return config['node-teslausb'];
3183
}
3284

33-
export const rcloneCopy = async (sourcePath, rcloneConfig, destinationPath) => {
85+
export const rcloneCopy = async (sourcePath: string, rcloneConfig: string, destinationPath: string) => {
3486
if (fs.existsSync(sourcePath) === false) {
3587
logWithTimestamp(`Skipping rclone copy for ${sourcePath} (does not exist)`)
3688
return
@@ -40,7 +92,7 @@ export const rcloneCopy = async (sourcePath, rcloneConfig, destinationPath) => {
4092

4193
}
4294

43-
export const getLastLineAsObject = async (filePath) => {
95+
export const getLastLineAsObject = async (filePath: string) => {
4496
try {
4597
const lastLine = await readLastLines.read(filePath, 1);
4698
return JSON.parse(lastLine);

0 commit comments

Comments
 (0)