@@ -4,8 +4,9 @@ import fs from 'fs';
4
4
import { executeBashCommand } from './bash.js' ;
5
5
import { clearInterval , setInterval } from 'timers' ;
6
6
import readLastLines from 'read-last-lines' ;
7
+ import path from 'path' ;
7
8
8
- export const rcloneCopyWithProgress = async ( path , rcloneConfig , destinationPath ) => {
9
+ export const rcloneCopyWithProgress = async ( path : string , rcloneConfig : string , destinationPath : string ) => {
9
10
const intervalId = setInterval ( ( ) => {
10
11
logWithTimestamp ( "Copying files, check rclone.log for status..." ) ;
11
12
// Add something useful here, e.g. giving percentage complete, transfer speed, time remaining
@@ -18,6 +19,57 @@ export const rcloneCopyWithProgress = async (path, rcloneConfig, destinationPath
18
19
}
19
20
} ;
20
21
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
+
21
73
22
74
export const getRcloneConfig = ( ) => {
23
75
const configPath = '/root/.config/rclone/rclone.conf' ;
@@ -30,7 +82,7 @@ export const getRcloneConfig = () => {
30
82
return config [ 'node-teslausb' ] ;
31
83
}
32
84
33
- export const rcloneCopy = async ( sourcePath , rcloneConfig , destinationPath ) => {
85
+ export const rcloneCopy = async ( sourcePath : string , rcloneConfig : string , destinationPath : string ) => {
34
86
if ( fs . existsSync ( sourcePath ) === false ) {
35
87
logWithTimestamp ( `Skipping rclone copy for ${ sourcePath } (does not exist)` )
36
88
return
@@ -40,7 +92,7 @@ export const rcloneCopy = async (sourcePath, rcloneConfig, destinationPath) => {
40
92
41
93
}
42
94
43
- export const getLastLineAsObject = async ( filePath ) => {
95
+ export const getLastLineAsObject = async ( filePath : string ) => {
44
96
try {
45
97
const lastLine = await readLastLines . read ( filePath , 1 ) ;
46
98
return JSON . parse ( lastLine ) ;
0 commit comments