UNPKG

6.14 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12const client_1 = require("./client");
13var SharePermission;
14(function (SharePermission) {
15 SharePermission[SharePermission["all"] = 31] = "all";
16 SharePermission[SharePermission["read"] = 1] = "read";
17 SharePermission[SharePermission["update"] = 2] = "update";
18 SharePermission[SharePermission["create"] = 4] = "create";
19 SharePermission[SharePermission["delete"] = 8] = "delete";
20 SharePermission[SharePermission["share"] = 16] = "share";
21})(SharePermission = exports.SharePermission || (exports.SharePermission = {}));
22var ShareType;
23(function (ShareType) {
24 ShareType[ShareType["user"] = 0] = "user";
25 ShareType[ShareType["group"] = 1] = "group";
26 ShareType[ShareType["publicLink"] = 3] = "publicLink";
27 ShareType[ShareType["email"] = 4] = "email";
28})(ShareType || (ShareType = {}));
29var ShareItemType;
30(function (ShareItemType) {
31 ShareItemType["file"] = "file";
32 ShareItemType["folder"] = "folder";
33})(ShareItemType || (ShareItemType = {}));
34class Share {
35 constructor(client, id) {
36 this.client = client;
37 this.memento = {
38 expiration: null,
39 id,
40 itemType: ShareItemType.file,
41 note: "",
42 token: "",
43 url: "",
44 };
45 }
46 static getShare(client, id) {
47 return __awaiter(this, void 0, void 0, function* () {
48 const share = new Share(client, id);
49 yield share.initialize();
50 return share;
51 });
52 }
53 static createShareRequestBody(createShare) {
54 const shareType = ShareType.publicLink;
55 const shareRequest = {
56 path: createShare.fileSystemElement.name,
57 // @todo permissions: 1,
58 shareType,
59 };
60 if (createShare.publicUpload && createShare.publicUpload === true) {
61 shareRequest.publicUpload = "enable";
62 }
63 else {
64 shareRequest.publicUpload = "disable";
65 }
66 if (createShare.password) {
67 shareRequest.password = createShare.password;
68 }
69 return JSON.stringify(shareRequest, null, 4);
70 }
71 delete() {
72 return __awaiter(this, void 0, void 0, function* () {
73 yield this.client.deleteShare(this.memento.id);
74 });
75 }
76 setExpiration(expiration) {
77 return __awaiter(this, void 0, void 0, function* () {
78 this.memento.expiration = expiration;
79 yield this.client.updateShare(this.memento.id, { expireDate: expiration.toISOString().split("T")[0] });
80 });
81 }
82 /**
83 * set a new password
84 * @param password
85 */
86 setPassword(password) {
87 return __awaiter(this, void 0, void 0, function* () {
88 yield this.client.updateShare(this.memento.id, { password });
89 });
90 }
91 setNote(note) {
92 return __awaiter(this, void 0, void 0, function* () {
93 this.memento.note = note;
94 yield this.client.updateShare(this.memento.id, { note });
95 });
96 }
97 initialize() {
98 return __awaiter(this, void 0, void 0, function* () {
99 const rawShareData = yield this.client.getShare(this.memento.id);
100 if (!rawShareData.ocs || !rawShareData.ocs.data[0]) {
101 throw new client_1.ClientError(`Error invalid share data received "ocs.data" missing`, "ERR_INVALID_SHARE_RESPONSE");
102 }
103 if (!rawShareData.ocs.data[0].url) {
104 throw new client_1.ClientError(`Error invalid share data received "url" missing`, "ERR_INVALID_SHARE_RESPONSE");
105 }
106 this.memento.url = rawShareData.ocs.data[0].url;
107 if (!rawShareData.ocs.data[0].token) {
108 throw new client_1.ClientError(`Error invalid share data received "token" missing`, "ERR_INVALID_SHARE_RESPONSE");
109 }
110 this.memento.token = rawShareData.ocs.data[0].token;
111 if (!rawShareData.ocs.data[0].item_type) {
112 throw new client_1.ClientError(`Error invalid share data received "item_type" missing`, "ERR_INVALID_SHARE_RESPONSE");
113 }
114 if (rawShareData.ocs.data[0].item_type === "file") {
115 this.memento.itemType = ShareItemType.file;
116 }
117 else {
118 this.memento.itemType = ShareItemType.folder;
119 }
120 if (rawShareData.ocs.data[0].expiration) {
121 this.memento.expiration = new Date(rawShareData.ocs.data[0].expiration);
122 }
123 if (rawShareData.ocs.data[0].note) {
124 this.memento.note = rawShareData.ocs.data[0].note;
125 }
126 // console.log(JSON.stringify(rawShareData, null, 4));
127 // console.log(JSON.stringify(this, null, 4));
128 });
129 }
130 /**
131 * token
132 * The token is readonly
133 */
134 get token() {
135 return this.memento.token;
136 }
137 /**
138 * share url
139 * The share url is readonly
140 */
141 get url() {
142 return this.memento.url;
143 }
144 /**
145 * expiration
146 * The expiration is readonly
147 */
148 get expiration() {
149 return this.memento.expiration;
150 }
151 /**
152 * note
153 * The note is readonly
154 */
155 get note() {
156 return this.memento.note;
157 }
158 /**
159 * id
160 * The id is readonly
161 */
162 get id() {
163 return this.memento.id;
164 }
165}
166exports.default = Share;