All files / src share.ts

98.36% Statements 60/61
100% Branches 26/26
94.74% Functions 18/19
98.36% Lines 60/61

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 2261x         1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x                   1x 1x 1x   1x     11x 11x 7x       3x             3x           3x 1x     3x                                                               11x 11x                       1x       1x 1x               1x       2x 1x 1x         1x 1x       11x   11x 1x     10x 1x   9x   9x 1x   8x   8x 1x     7x 5x   2x   7x 2x     7x 1x                       2x               2x               1x               2x               2x             1x                        
import Client, { ClientError } from "./client";
import File from "./file";
import FileSystemElement from "./fileSystemElement";
import Folder from "./folder";
 
export enum SharePermission {
    all = 31,
    read = 1,
    update = 2,
    create = 4,
    delete = 8,
    share = 16,
}
 
enum ShareType {
    user = 0,
    group = 1,
    publicLink = 3,
    email = 4,
}
 
export interface ICreateShare {
    "fileSystemElement": FileSystemElement;
    // @todo "shareWith"?: User | UserGroup | EMail;
    "publicUpload"?: boolean;
    "password"?: string;
}
 
export enum ShareItemType {
    file = "file",
    folder = "folder",
}
export default class Share {
 
    public static async getShare(client: Client, id: string): Promise<Share> {
        const share: Share = new Share(client, id);
        await share.initialize();
        return share;
    }
 
    public static createShareRequestBody(createShare: ICreateShare): string {
        const shareType: ShareType = ShareType.publicLink;
 
        const shareRequest: {
            path: string,
            shareType: number,
            // @todo   permissions: number | number[]
            password?: string,
        } = {
            path: createShare.fileSystemElement.name,
            //  @todo    permissions: 1,
            shareType,
        };
 
        if (createShare.password) {
            shareRequest.password = createShare.password;
        }
 
        return JSON.stringify(shareRequest, null, 4);
    }
 
    private client: Client;
    private memento: {
        expiration: Date | null,
        id: string;
        itemType: ShareItemType,
        note: string,
        token: string,
        url: string,
        publicUpload: boolean,
        // share_type: number,
        // "uid_owner": string,
        // "displayname_owner": string,
        // "permissions": SharePermission,
        // "can_edit": boolean,
        // "can_delete": boolean,
        // "stime": Date,
        // "parent"?: Share,
        // "uid_file_owner": string,
        // "label"?: string,
        // "displayname_file_owner": string,
        // "path": string,
        // "mimetype"?: string,
        // "share_with"?: string,
        // "share_with_displayname"?: string,
        // "mail_send": boolean,
        // "hide_download": boolean,
    };
 
    private constructor(client: Client, id: string) {
        this.client = client;
        this.memento = {
            expiration: null,
            id,
            itemType: ShareItemType.file,
            note: "",
            token: "",
            url: "",
            publicUpload: false,
        };
    }
 
    public async delete(): Promise<void> {
        await this.client.deleteShare(this.memento.id);
    }
 
    public async setExpiration(expiration: Date): Promise<void> {
        this.memento.expiration = expiration;
        await this.client.updateShare(this.memento.id, { expireDate: expiration.toISOString().split("T")[0] });
    }
 
    /**
     * set a new password
     * @param password
     */
    public async setPassword(password: string): Promise<void> {
        await this.client.updateShare(this.memento.id, { password });
    }
 
    public async setPublicUpload(): Promise<void> {
        if (this.memento.itemType === ShareItemType.folder) {
            this.memento.publicUpload = true;
            await this.client.updateShare(this.memento.id, { permissions: 15 });
        }
    }
 
    public async setNote(note: string): Promise<void> {
        this.memento.note = note;
        await this.client.updateShare(this.memento.id, { note });
    }
 
    private async initialize(): Promise<void> {
        const rawShareData = await this.client.getShare(this.memento.id);
 
        if (!rawShareData.ocs || !rawShareData.ocs.data[0]) {
            throw new ClientError(`Error invalid share data received "ocs.data" missing`, "ERR_INVALID_SHARE_RESPONSE");
        }
 
        if (!rawShareData.ocs.data[0].url) {
            throw new ClientError(`Error invalid share data received "url" missing`, "ERR_INVALID_SHARE_RESPONSE");
        }
        this.memento.url = rawShareData.ocs.data[0].url;
 
        if (!rawShareData.ocs.data[0].token) {
            throw new ClientError(`Error invalid share data received "token" missing`, "ERR_INVALID_SHARE_RESPONSE");
        }
        this.memento.token = rawShareData.ocs.data[0].token;
 
        if (!rawShareData.ocs.data[0].item_type) {
            throw new ClientError(`Error invalid share data received "item_type" missing`, "ERR_INVALID_SHARE_RESPONSE");
        }
 
        if (rawShareData.ocs.data[0].item_type === "file") {
            this.memento.itemType = ShareItemType.file;
        } else {
            this.memento.itemType = ShareItemType.folder;
        }
        if (rawShareData.ocs.data[0].expiration) {
            this.memento.expiration = new Date(rawShareData.ocs.data[0].expiration);
        }
 
        if (rawShareData.ocs.data[0].note) {
            this.memento.note = rawShareData.ocs.data[0].note;
        }
 
        // console.log(JSON.stringify(rawShareData, null, 4));
        // console.log(JSON.stringify(this, null, 4));
    }
 
    /**
     * token
     * The token is readonly
     */
    public get token(): string {
        return this.memento.token;
    }
 
    /**
     * share url
     * The share url is readonly
     */
    public get url(): string {
        return this.memento.url;
    }
 
    /**
     * expiration
     * The expiration is readonly
     */
    public get expiration(): Date | null {
        return this.memento.expiration;
    }
 
    /**
     * note
     * The note is readonly
     */
    public get note(): string {
        return this.memento.note;
    }
 
    /**
     * id
     * The id is readonly
     */
    public get id(): string {
        return this.memento.id;
    }
 
    /**
     * returns true if the share akkows upload
     */
    public get publicUpload(): boolean {
        return this.memento.publicUpload;
    }
 
    /**
     * item type
     * The type of the share item file or folder
     */
    public get itemType(): ShareItemType {
        return this.memento.itemType;
    }
 
}