const debug = require ( "debug" ) . debug ( "UploadFolderCommand" ) ; import Client , { File , FileSystemFolder , IFileNameFormats , UploadFilesCommand , UploadFilesCommandOptions , SourceTargetFileNames , } from "../client" ; import Command , { CommandStatus } from "./command" ; /**
 * options to create a upload folder command
 */ export interface UploadFolderCommandOptions { /**
     * The name of the source folder with the file structure to be uploaded
     */ folderName : string ; param { SourceTargetFileNames } fileNames getTargetFileNameBeforeUpload ? : ( fileNames : SourceTargetFileNames ) => string ; processFileAfterUpload ? : ( file : File ) => Promise < void > ; } /**
 * Command to upload the contents of a folder from local file system to nextcloud recursively
 */ export default class UploadFolderCommand extends Command { private folderName : string ; private processFileAfterUpload ? : ( file : File ) => Promise < void > ; private getTargetFileNameBeforeUpload : ( fileNames : SourceTargetFileNames ) => string ; private bytesUploaded : number ; param { Client } client param { ISourceTargetFileNames [ ] } files param { ( file : File ) => void } processAfterUpload constructor ( client , options ) { super ( client ) ; this . folderName = options . folderName ; this . processFileAfterUpload = options . processFileAfterUpload ; if ( options . getTargetFileNameBeforeUpload ) { this . getTargetFileNameBeforeUpload = options . getTargetFileNameBeforeUpload ; } else { this . getTargetFileNameBeforeUpload = ( fileNames ) : string => { return fileNames . targetFileName ; } ; } this . bytesUploaded = 0 ; } async returns { Promise < void > } protected async onExecute ( ) { this . status = CommandStatus . running ; let fileNames = [ ] ; const fsf = new FileSystemFolder ( this . folderName ) ; try { fileNames = await fsf . getFileNames ( ) ; } catch ( e ) { this . resultMetaData . errors . push ( e ) ; this . status = CommandStatus . failed ; this . percentCompleted = 100 ; this . bytesUploaded = 0 ; this . resultMetaData . timeElapsed = 0 ; return ; } const files = [ ] ; for ( const fileNameFormat of fileNames ) { const targetFileName = this . getTargetFileNameBeforeUpload ( { sourceFileName : fileNameFormat . absolute , targetFileName : fileNameFormat . relative } ) ; if ( targetFileName !== "" ) { files . push ( { sourceFileName : fileNameFormat . absolute , targetFileName } ) ; } } const options = { files , processFileAfterUpload : this . processFileAfterUpload } ; const uc = new UploadFilesCommand ( this . client , options ) ; uc . execute ( ) ; while ( uc . isFinished ( ) !== true ) { this . status = uc . getStatus ( ) ; this . percentCompleted = uc . getPercentCompleted ( ) ; this . resultMetaData = uc . getResultMetaData ( ) ; this . bytesUploaded = uc . getBytesUploaded ( ) ; await ( async ( ) => { return new Promise ( resolve => setTimeout ( resolve , 1000 ) ) ; } ) ( ) ; } this . status = uc . getStatus ( ) ; this . percentCompleted = uc . getPercentCompleted ( ) ; this . resultMetaData = uc . getResultMetaData ( ) ; this . bytesUploaded = uc . getBytesUploaded ( ) ; return ; } ; public getBytesUploaded ( ) { return this . bytesUploaded ; } }