All files / src file.ts

78.95% Statements 45/57
50% Branches 3/6
89.47% Functions 17/19
78.95% Lines 45/57

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 2171x 1x               1x                       70x                 70x             24x 23x               3x 2x               2x 1x               2x 1x             2x 1x             19x 17x               8x 8x                 1x 1x   1x     1x                   2x 1x 1x 1x 1x 1x 1x 1x               2x 2x               3x 3x               1x 1x               8x 8x                                                                       4x 4x                     1x 1x       74x 8x        
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: Client, name: string, baseName: string, lastmod: string, size: number, mime: string, id: number) {
        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;
    }
 
    /**
     * deletes a file
     * @throws Error
     */
    public async delete(): Promise<void> {
        this.memento.deleted = true;
        return await this.client.deleteFile(this.memento.name);
    }
 
    /**
     * get folder of the file
     * @throws ClientError
     * @returns the parent folder
     */
    public async getFolder(): Promise<Folder> {
        this.assertExistence();
        const folder: Folder | null = await this.client.getFolder(path.dirname((this.memento.name)));
 
        Iif (folder) {
            return folder;
        }
        throw new ClientError("Error, the folder of the file does not exist anymore", "ERR_FILE_FOLDER_DOES_NOT_EXIST");
    }
 
    /**
     * moves or renames the current file to the new location
     * target folder must exists
     * @param targetFileName the name of the target file /f1/f2/myfile.txt
     * @throws Error
     */
    public async move(targetFileName: string): Promise<File> {
        this.assertExistence();
        const file: 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 the buffer of the file content
     * @throws Error
     */
    public async getContent(): Promise<Buffer> {
        this.assertExistence();
        return this.client.getContent(this.name);
    }
 
    /**
     * @returns the url of the file
     * @throws Error
     */
    public getUrl(): string {
        this.assertExistence();
        return this.client.getLink(this.name);
    }
 
    /**
     * @returns the url of the file in the UI
     * @throws Error
     */
    public getUIUrl(): string {
        this.assertExistence();
        return this.client.getUILink(this.id);
    }
 
    /**
     * adds a tag name to the file
     * @param tagName name of the tag
     */
    public async addTag(tagName: string): Promise<void> {
        this.assertExistence();
        return 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 tagId: number | undefined = map.get(tagName);
        if (tagId) {
            await this.client.removeTagOfFile(this.id, tagId);
        }
    }
 
    /**
     * add comment to file
     * @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 file
     * @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("File does not exist", "ERR_FILE_NOT_EXISTING");
        }
    }
}