Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | 1x 1x 1x 1x 285x 285x 144x 143x 2x 1x 2x 1x 22x 21x 3x 3x 2x 2x 2x 1x 1x 3x 3x 1x 1x 4x 4x 16x 16x 16x 16x 128x 16x 20x 20x 20x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 9x 2x 2x 2x 2x 5x 2x 2x 2x 2x 2x 2x 1x 4x 4x 2x 2x 212x 4x | import Client from "./client"; import ClientError from "./error"; import File from "./file"; import FileSystemElement from "./fileSystemElement"; import Logger from "./logger"; const log: Logger = 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: Client, name: string, baseName: string, lastmod: string, id: number = -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 an array of subfolders * @throws Error */ public async getSubFolders(): Promise<Folder[]> { this.assertExistence(); return await this.client.getSubFolders(this.name); } /** * returns true if the current folder has a subfolder with the given base name * @param subFolderBaseName the base name of the subfolder like "products" */ public async hasSubFolder(subFolderBaseName: string): Promise<boolean> { this.assertExistence(); const subFolder: Folder | null = await this.client.getFolder(this.name + "/" + subFolderBaseName); if (subFolder) { return true; } return false; } /** * @returns all files of the folder */ public async getFiles(options?: FolderGetFilesOptions): Promise<File[]> { this.assertExistence(); return await this.client.getFiles(this.name, options); } /** * creates a subfolder * @param subFolderBaseName name of the subfolder basename */ public async createSubFolder(subFolderBaseName: string): Promise<Folder> { this.assertExistence(); return await this.client.createFolder(this.name + "/" + subFolderBaseName); } /** * get a file by basename * @param fileBaseName the base name of the file * @returns the file of null * @throws Error */ public async getFile(fileBaseName: string): Promise<File | null> { this.assertExistence(); return this.client.getFile(this.name + "/" + fileBaseName); } /** * creates a file in the folder * @param fileBaseName the base name of the file * @param data the buffer or stream with file content * @returns the new file or null * @throws Error */ public async createFile(fileBaseName: string, data: Buffer | NodeJS.ReadableStream): Promise<File> { this.assertExistence(); // must not contain :/\*"<>? log.debug("createFile fileBaseName = ", fileBaseName); const invalidChars: string[] = [":", "*", "/", "\\", "\"", "?", "<", ">"]; // log.debug("createFile invalidChars = ", invalidChars); for (const invalidChar of invalidChars) { Iif (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); } /** * deletes the folder and the contained files * @throws Error */ public async delete(): Promise<void> { log.debug("delete"); this.memento.deleted = true; return await this.client.deleteFolder(this.memento.name); } /** * renames or moves the current folder to the new location * target folder must exists * @param targetFolderName the name of the target folder /f1/f2/target * @throws Error */ public async move(targetFolderName: string): Promise<Folder> { this.assertExistence(); const folder: 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 the url of the folder * @throws Error */ public getUrl(): string { this.assertExistence(); return this.client.getLink(this.name); } /** * @returns the url of the folder in the UI * @throws Error */ public getUIUrl(): string { this.assertExistence(); return this.client.getUILink(this.id); } /** * @returns true if the folder contains a file with the given basename * @param fileBaseName file basename * @throws Error */ public async containsFile(fileBaseName: string): Promise<boolean> { this.assertExistence(); let file: File | null; file = await this.getFile(fileBaseName); if (file) { return true; } return false; } /** * adds a tag name to the folder * @param tagName name of the tag */ public async addTag(tagName: string): Promise<void> { return await this.client.addTagToFile(this.id, tagName); } /** * get tag names * @returns array of tag names */ public async getTags(): Promise<string[]> { this.assertExistence(); const map: Map<string, number> = await this.client.getTagsOfFile(this.id); const tagNames: string[] = []; for (const tagName of map) { tagNames.push(tagName[0]); } return tagNames; } /** * removes a tag of the file * @param tagName the name of the tag */ public async removeTag(tagName: string): Promise<void> { this.assertExistence(); const map: Map<string, number> = await this.client.getTagsOfFile(this.id); const tagNames: string[] = []; const tagId: number | undefined = map.get(tagName); if (tagId) { await this.client.removeTagOfFile(this.id, tagId); } } /** * add comment to folder * @param comment the comment */ public async addComment(comment: string): Promise<void> { this.assertExistence(); return await this.client.addCommentToFile(this.id, comment); } /** * get list of comments of folder * @param top number of comments to return * @param skip the offset * @returns array of comment strings * @throws Exception */ public async getComments(top?: number, skip?: number): Promise<string[]> { this.assertExistence(); return await this.client.getFileComments(this.id, top, skip); } private assertExistence(): void { if (this.memento.deleted) { throw new ClientError("Folder does not exist", "ERR_FOLDER_NOT_EXISTING"); } } } |