UNPKG

6.62 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 });
15const path_1 = __importDefault(require("path"));
16const client_1 = require("./client");
17/**
18 * The file class represents a file in nextcloud.
19 * It exposes file properties and content handling, commenting and tagging
20 */
21class File {
22 constructor(client, name, baseName, lastmod, size, mime, id) {
23 this.memento = {
24 baseName,
25 deleted: false,
26 id,
27 lastmod: new Date(lastmod),
28 mime,
29 name,
30 size,
31 };
32 this.client = client;
33 }
34 /**
35 * The name of the file including the path
36 * The name is readonly
37 */
38 get name() {
39 this.assertExistence();
40 return this.memento.name;
41 }
42 /**
43 * The base name of the file (file name without path)
44 * The base name is readonly
45 */
46 get baseName() {
47 this.assertExistence();
48 return this.memento.baseName;
49 }
50 /**
51 * The timestamp of the last file change
52 * readonly
53 */
54 get lastmod() {
55 this.assertExistence();
56 return this.memento.lastmod;
57 }
58 /**
59 * The file size in bytes
60 * readonly
61 */
62 get size() {
63 this.assertExistence();
64 return this.memento.size;
65 }
66 /**
67 * The mime type (content type) of the file
68 */
69 get mime() {
70 this.assertExistence();
71 return this.memento.mime;
72 }
73 /**
74 * The unique id of the file.
75 */
76 get id() {
77 this.assertExistence();
78 return this.memento.id;
79 }
80 /**
81 * deletes a file
82 * @throws Error
83 */
84 delete() {
85 return __awaiter(this, void 0, void 0, function* () {
86 this.memento.deleted = true;
87 return yield this.client.deleteFile(this.memento.name);
88 });
89 }
90 /**
91 * get folder of the file
92 * @throws ClientError
93 * @returns the parent folder
94 */
95 getFolder() {
96 return __awaiter(this, void 0, void 0, function* () {
97 this.assertExistence();
98 const folder = yield this.client.getFolder(path_1.default.dirname((this.memento.name)));
99 if (folder) {
100 return folder;
101 }
102 throw new client_1.ClientError("Error, the folder of the file does not exist anymore", "ERR_FILE_FOLDER_DOES_NOT_EXIST");
103 });
104 }
105 /**
106 * moves or renames the current file to the new location
107 * target folder must exists
108 * @param targetFileName the name of the target file /f1/f2/myfile.txt
109 * @throws Error
110 */
111 move(targetFileName) {
112 return __awaiter(this, void 0, void 0, function* () {
113 this.assertExistence();
114 const file = yield this.client.moveFile(this.name, targetFileName);
115 this.memento.name = file.name;
116 this.memento.baseName = file.baseName;
117 this.memento.lastmod = file.lastmod;
118 this.memento.mime = file.mime;
119 this.memento.size = file.size;
120 return this;
121 });
122 }
123 /**
124 * @returns the buffer of the file content
125 * @throws Error
126 */
127 getContent() {
128 return __awaiter(this, void 0, void 0, function* () {
129 this.assertExistence();
130 return this.client.getContent(this.name);
131 });
132 }
133 /**
134 * @returns the url of the file
135 * @throws Error
136 */
137 getUrl() {
138 this.assertExistence();
139 return this.client.getLink(this.name);
140 }
141 /**
142 * @returns the url of the file in the UI
143 * @throws Error
144 */
145 getUIUrl() {
146 this.assertExistence();
147 return this.client.getUILink(this.id);
148 }
149 /**
150 * adds a tag name to the file
151 * @param tagName name of the tag
152 */
153 addTag(tagName) {
154 return __awaiter(this, void 0, void 0, function* () {
155 this.assertExistence();
156 return this.client.addTagToFile(this.id, tagName);
157 });
158 }
159 /**
160 * get tag names
161 * @returns array of tag names
162 */
163 getTags() {
164 return __awaiter(this, void 0, void 0, function* () {
165 this.assertExistence();
166 const map = yield this.client.getTagsOfFile(this.id);
167 const tagNames = [];
168 for (const tagName of map) {
169 tagNames.push(tagName[0]);
170 }
171 return tagNames;
172 });
173 }
174 /**
175 * removes a tag of the file
176 * @param tagName the name of the tag
177 */
178 removeTag(tagName) {
179 return __awaiter(this, void 0, void 0, function* () {
180 this.assertExistence();
181 const map = yield this.client.getTagsOfFile(this.id);
182 const tagId = map.get(tagName);
183 if (tagId) {
184 yield this.client.removeTagOfFile(this.id, tagId);
185 }
186 });
187 }
188 /**
189 * add comment to file
190 * @param comment the comment
191 */
192 addComment(comment) {
193 return __awaiter(this, void 0, void 0, function* () {
194 this.assertExistence();
195 return yield this.client.addCommentToFile(this.id, comment);
196 });
197 }
198 /**
199 * get list of comments of file
200 * @param top number of comments to return
201 * @param skip the offset
202 * @returns array of comment strings
203 * @throws Exception
204 */
205 getComments(top, skip) {
206 return __awaiter(this, void 0, void 0, function* () {
207 this.assertExistence();
208 return yield this.client.getFileComments(this.id, top, skip);
209 });
210 }
211 assertExistence() {
212 if (this.memento.deleted) {
213 throw new client_1.ClientError("File does not exist", "ERR_FILE_NOT_EXISTING");
214 }
215 }
216}
217exports.default = File;