@@ -203,29 +203,6 @@ class onedrive {
203203 throw new Error ( 'Upload did not complete properly' ) ;
204204 }
205205
206- async getMetadata ( { accessToken, itemPath } ) {
207- const targetPath = itemPath === 'root' ? 'root' : `root:/${ itemPath } ` ;
208- const url = `https://graph.microsoft.com/v1.0/me/drive/${ targetPath } ` ;
209-
210- const response = await got . get ( url , {
211- headers : { Authorization : `Bearer ${ accessToken } ` } ,
212- responseType : 'json'
213- } ) ;
214-
215- return response . body ;
216- }
217-
218- async listChildren ( { accessToken, itemId } ) {
219- const url = `https://graph.microsoft.com/v1.0/me/drive/items/${ itemId } /children` ;
220-
221- const response = await got . get ( url , {
222- headers : { Authorization : `Bearer ${ accessToken } ` } ,
223- responseType : 'json'
224- } ) ;
225-
226- return response . body ;
227- }
228-
229206 async downloadFile ( { accessToken, dir, onlyFileName, downloadPath, log } ) {
230207 try {
231208 const meta = await this . getMetadata ( { accessToken, itemPath : dir } ) ;
@@ -305,6 +282,79 @@ class onedrive {
305282
306283 return response ;
307284 }
285+
286+ async getMetadata ( { accessToken, itemPath } ) {
287+ const targetPath = itemPath === 'root' ? 'root' : `root:/${ itemPath } ` ;
288+ const url = `https://graph.microsoft.com/v1.0/me/drive/${ targetPath } ` ;
289+
290+ const response = await got . get ( url , {
291+ headers : { Authorization : `Bearer ${ accessToken } ` } ,
292+ responseType : 'json'
293+ } ) ;
294+
295+ return response . body ;
296+ }
297+
298+ async listChildren ( { accessToken, itemId } ) {
299+ const url = `https://graph.microsoft.com/v1.0/me/drive/items/${ itemId } /children` ;
300+
301+ const response = await got . get ( url , {
302+ headers : { Authorization : `Bearer ${ accessToken } ` } ,
303+ responseType : 'json'
304+ } ) ;
305+
306+ return response . body ;
307+ }
308+
309+ async listBackups ( { accessToken, dir, types, log } ) {
310+ try {
311+ // Normalize path
312+ let normalizedDir = ( dir || '' ) . replace ( / \\ / g, '/' ) ;
313+ if ( ! normalizedDir || normalizedDir === '/' ) {
314+ normalizedDir = 'root' ;
315+ } else {
316+ if ( normalizedDir . startsWith ( '/' ) ) {
317+ normalizedDir = normalizedDir . slice ( 1 ) ;
318+ }
319+ }
320+
321+ const metadata = await this . getMetadata ( { accessToken, itemPath : normalizedDir } ) ;
322+ if ( ! metadata ?. id ) {
323+ throw new Error ( 'Could not retrieve metadata' ) ;
324+ }
325+
326+ const childrenRes = await this . listChildren ( { accessToken, itemId : metadata . id } ) ;
327+ let children = childrenRes ?. value ;
328+ if ( ! children ) return null ;
329+
330+ // Filter and transform children
331+ children = children
332+ . map ( file => ( {
333+ path : file . name ,
334+ name : file . name ,
335+ size : file . size ,
336+ id : file . id
337+ } ) )
338+ . filter ( file =>
339+ ( types . includes ( file . name . split ( '_' ) [ 0 ] ) || types . includes ( file . name . split ( '.' ) [ 0 ] ) ) &&
340+ file . name . endsWith ( '.gz' )
341+ ) ;
342+
343+ // Group by type
344+ const files = { } ;
345+ for ( const file of children ) {
346+ const type = file . name . split ( '_' ) [ 0 ] ;
347+ if ( ! files [ type ] ) files [ type ] = [ ] ;
348+ files [ type ] . push ( file ) ;
349+ }
350+
351+ return files ;
352+
353+ } catch ( error ) {
354+ log . error ( `listBackups error: ${ error . message } ` ) ;
355+ throw error ;
356+ }
357+ }
308358}
309359
310360module . exports = onedrive ;
0 commit comments