import Client from "./client" ; import ClientError from "./error" ; import File from "./file" ; import FileSystemElement from "./fileSystemElement" ; import Logger from "./logger" ; const log = new Logger ( ) ; export interface FolderGetFilesOptions { /**
     * callback function to filter files
     */ filterFile ? : ( file : File ) => File | null ; } export default class Folder implements FileSystemElement { private client : Client ; private memento : { baseName : string ; id : number ; deleted : boolean ; lastmod : Date ; name : string ; } ; constructor ( client , name , baseName , lastmod , id = - 1 ) { this . client = client ; this . memento = { baseName , deleted : false , id , lastmod : new Date ( lastmod ) , name , } ; } get name ( ) : string { this . assertExistence ( ) ; return this . memento . name ; } get baseName ( ) : string { this . assertExistence ( ) ; return this . memento . baseName ; } get lastmod ( ) : Date { this . assertExistence ( ) ; return this . memento . lastmod ; } get id ( ) : number { this . assertExistence ( ) ; return this . memento . id ; } returns throws public async getSubFolders ( ) { this . assertExistence ( ) ; return await this . client . getSubFolders ( this . name ) ; } param subFolderBaseName public async hasSubFolder ( subFolderBaseName ) { this . assertExistence ( ) ; const subFolder = await this . client . getFolder ( this . name + "/" + subFolderBaseName ) ; if ( subFolder ) { return true ; } return false ; } returns public async getFiles ( options ? ) { this . assertExistence ( ) ; return await this . client . getFiles ( this . name , options ) ; } param subFolderBaseName public async createSubFolder ( subFolderBaseName ) { this . assertExistence ( ) ; return await this . client . createFolder ( this . name + "/" + subFolderBaseName ) ; } param fileBaseName returns throws public async getFile ( fileBaseName ) { this . assertExistence ( ) ; return this . client . getFile ( this . name + "/" + fileBaseName ) ; } param fileBaseName param data returns throws public async createFile ( fileBaseName , data ) { this . assertExistence ( ) ; log . debug ( "createFile fileBaseName = " , fileBaseName ) ; const invalidChars = [ ":" , "*" , "/" , "\\" , "\"" , "?" , "<" , ">" ] ; for ( const invalidChar of invalidChars ) { if ( fileBaseName . indexOf ( invalidChar ) !== - 1 ) { throw new ClientError ( "Filename contains an invalid character '" + invalidChar + "'" , "ERR_INVALID_CHAR_IN_FILE_NAME" , { fileBaseName } ) ; } } return this . client . createFile ( this . name + "/" + fileBaseName . replace ( /^\/+/g , "" ) , data ) ; } throws public async delete ( ) { log . debug ( "delete" ) ; this . memento . deleted = true ; return await this . client . deleteFolder ( this . memento . name ) ; } param targetFolderName throws public async move ( targetFolderName ) { this . assertExistence ( ) ; const folder = await this . client . moveFolder ( this . name , targetFolderName ) ; this . memento . name = folder . name ; this . memento . baseName = folder . baseName ; this . memento . lastmod = folder . lastmod ; return this ; } returns throws public getUrl ( ) { this . assertExistence ( ) ; return this . client . getLink ( this . name ) ; } returns throws public getUIUrl ( ) { this . assertExistence ( ) ; return this . client . getUILink ( this . id ) ; } returns param fileBaseName throws public async containsFile ( fileBaseName ) { this . assertExistence ( ) ; let file ; file = await this . getFile ( fileBaseName ) ; if ( file ) { return true ; } return false ; } param tagName public async addTag ( tagName ) { return await this . client . addTagToFile ( this . id , tagName ) ; } returns public async getTags ( ) { this . assertExistence ( ) ; const map = await this . client . getTagsOfFile ( this . id ) ; const tagNames = [ ] ; for ( const tagName of map ) { tagNames . push ( tagName [ 0 ] ) ; } return tagNames ; } param tagName public async removeTag ( tagName ) { this . assertExistence ( ) ; const map = await this . client . getTagsOfFile ( this . id ) ; const tagNames = [ ] ; const tagId = map . get ( tagName ) ; if ( tagId ) { await this . client . removeTagOfFile ( this . id , tagId ) ; } } param comment public async addComment ( comment ) { this . assertExistence ( ) ; return await this . client . addCommentToFile ( this . id , comment ) ; } param top param skip returns throws public async getComments ( top ? , skip ? ) { this . assertExistence ( ) ; return await this . client . getFileComments ( this . id , top , skip ) ; } private assertExistence ( ) { if ( this . memento . deleted ) { throw new ClientError ( "Folder does not exist" , "ERR_FOLDER_NOT_EXISTING" ) ; } } }