UNPKG

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