import path from "path" ; import Client , { ClientError } from "./client" ; import FileSystemElement from "./fileSystemElement" ; import Folder from "./folder" ; /**
 * The file class represents a file in nextcloud.
 * It exposes file properties and content handling, commenting and tagging
 */ export default class File implements FileSystemElement { private memento : { baseName : string ; id : number ; deleted : boolean ; lastmod : Date ; mime : string ; name : string ; size : number ; } ; private client : Client ; constructor ( client , name , baseName , lastmod , size , mime , id ) { this . memento = { baseName , deleted : false , id , lastmod : new Date ( lastmod ) , mime , name , size , } ; this . client = client ; } /**
     * The name of the file including the path
     * The name is readonly
     */ get name ( ) : string { this . assertExistence ( ) ; return this . memento . name ; } /**
     * The base name of the file (file name without path)
     * The base name is readonly
     */ get baseName ( ) : string { this . assertExistence ( ) ; return this . memento . baseName ; } /**
     * The timestamp of the last file change
     * readonly
     */ get lastmod ( ) : Date { this . assertExistence ( ) ; return this . memento . lastmod ; } /**
     * The file size in bytes
     * readonly
     */ get size ( ) : number { this . assertExistence ( ) ; return this . memento . size ; } /**
     * The mime type (content type) of the file
     */ get mime ( ) : string { this . assertExistence ( ) ; return this . memento . mime ; } /**
     * The unique id of the file.
     */ get id ( ) : number { this . assertExistence ( ) ; return this . memento . id ; } throws public async delete ( ) { this . memento . deleted = true ; return await this . client . deleteFile ( this . memento . name ) ; } throws returns public async getFolder ( ) { this . assertExistence ( ) ; const folder = await this . client . getFolder ( path . dirname ( ( this . memento . name ) ) ) ; if ( folder ) { return folder ; } throw new ClientError ( "Error, the folder of the file does not exist anymore" , "ERR_FILE_FOLDER_DOES_NOT_EXIST" ) ; } param targetFileName throws public async move ( targetFileName ) { this . assertExistence ( ) ; const file = await this . client . moveFile ( this . name , targetFileName ) ; this . memento . name = file . name ; this . memento . baseName = file . baseName ; this . memento . lastmod = file . lastmod ; this . memento . mime = file . mime ; this . memento . size = file . size ; return this ; } returns throws public async getContent ( ) { this . assertExistence ( ) ; return this . client . getContent ( this . name ) ; } returns throws public getUrl ( ) { this . assertExistence ( ) ; return this . client . getLink ( this . name ) ; } returns throws public getUIUrl ( ) { this . assertExistence ( ) ; return this . client . getUILink ( this . id ) ; } param tagName public async addTag ( tagName ) { this . assertExistence ( ) ; return 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 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 ( "File does not exist" , "ERR_FILE_NOT_EXISTING" ) ; } } }