const debug = require ( "debug" ) . debug ( "GetFilesRecursivelyCommand" ) ; import Client , { File , Folder , FolderGetFilesOptions } from "../client" ; import Command , { CommandStatus } from "./command" ; export interface GetFilesRecursivelyCommandOptions { /**
     * the source nextcloud folder to start listing the files
     */ sourceFolder : Folder ; /**
     * function to filter files
     */ filterFile ? : ( file : File ) => File | null ; } /**
 * Command to get all files of a nextcloud folder recursively
 */ export default class GetFilesRecursivelyCommand extends Command { private sourceFolder : Folder ; private filterFile ? : ( file : File ) => File | null ; private files : File [ ] ; param { Client } client param { SourceTargetFileNames [ ] } files param { ( file : File ) => void } processAfterUpload constructor ( client , options ) { super ( client ) ; this . sourceFolder = options . sourceFolder ; this . filterFile = options . filterFile ; this . files = [ ] ; } async returns { Promise < void > } protected async onExecute ( ) { this . status = CommandStatus . running ; const startTime = new Date ( ) ; try { this . percentCompleted = 0 ; await this . processFolder ( this . sourceFolder , 100 ) ; this . resultMetaData . messages . push ( `${ this . files . length } files found` ) ; } catch ( e ) { debug ( e . message ) ; this . resultMetaData . errors . push ( e . message ) ; } this . percentCompleted = 100 ; if ( this . resultMetaData . errors . length > 0 ) { this . status = CommandStatus . failed ; } else { this . status = CommandStatus . success ; } this . resultMetaData . timeElapsed = new Date ( ) . getTime ( ) - startTime . getTime ( ) ; return ; } ; public getFiles ( ) { return this . files ; } param { Folder } folder param { number } percentagethe private async processFolder ( folder , percentage ) { const options = { filterFile : this . filterFile } ; const folderFiles = await folder . getFiles ( options ) ; for ( const fi of folderFiles ) { this . files . push ( fi ) ; } const subFolders = await folder . getSubFolders ( ) ; if ( subFolders . length === 0 ) { this . percentCompleted += percentage ; } for ( const subFolder of subFolders ) { await this . processFolder ( subFolder , percentage / subFolders . length ) ; } return ; } }