UNPKG

9.8 kBTypeScriptView Raw
1/// <reference types="node" />
2import ClientError from "./error";
3import FakeServer from "./fakeServer";
4import File from "./file";
5import FileSystemElement from "./fileSystemElement";
6import Folder from "./folder";
7import RequestResponseLogEntry from "./requestResponseLogEntry";
8import Server from "./server";
9import Share, { ICreateShare, SharePermission } from "./share";
10import Tag from "./tag";
11export { Client, ClientError, Folder, File, FileSystemElement, ICreateShare, Tag, FakeServer, Server, SharePermission, RequestResponseLogEntry, };
12export interface ISysInfoNextcloudSystem {
13 "version": string;
14}
15export interface ISysInfoNextcloudClient {
16 "version": string;
17}
18export interface ISysInfoNextcloud {
19 "system": ISysInfoNextcloudSystem;
20}
21export interface ISystemInfo {
22 "nextcloud": ISysInfoNextcloud;
23 "nextcloudClient": ISysInfoNextcloudClient;
24}
25export interface IQuota {
26 used: number;
27 available: number | string;
28}
29/**
30 * The nextcloud client is the root object to access the remote api of the nextcloud server.<br>
31 */
32export default class Client {
33 static webDavUrlPath: string;
34 private nextcloudOrigin;
35 private nextcloudAuthHeader;
36 private nextcloudRequestToken;
37 private webDAVUrl;
38 private proxy?;
39 private fakeServer?;
40 private logRequestResponse;
41 private httpClient?;
42 /**
43 * Creates a new instance of a nextcloud client.<br/>
44 * Use the server to provide server connectivity information to the client.<br/>
45 * (The FakeServer is only used for testing and code coverage)<br/><br/>
46 * If the server is not provided the client tries to find the connectivity information
47 * in the environment.<br/>
48 * If a <b>VCAP_SERVICES</b> environment variable is available, the client tries to find
49 * a service with the name <b>"nextcloud"</b> in the user-provides-services section.<br/>
50 * If no VCAP_SERVICES are available, the client uses the following variables
51 * from the envirnonment for the connectivity:<br/>
52 * <ul>
53 * <li>NEXTCLOUD_URL - the WebDAV url of the nextcloud server</li>
54 * <li>NEXTCLOUD_USERNAME - the user name</li>
55 * <li>NEXTCLOUD_PASSWORD - the application password</li>
56 * </ul>
57 * @param server optional server information to connection to a nextcloud server
58 * @constructor
59 */
60 constructor(server?: Server | FakeServer);
61 /**
62 * returns the used and free quota of the nextcloud account
63 */
64 getQuota(): Promise<IQuota>;
65 /**
66 * creates a new tag, if not already existing
67 * this function will fail with http 403 if the user does not have admin privileges
68 * @param tagName the name of the tag
69 * @returns tagId
70 */
71 createTag(tagName: string): Promise<Tag>;
72 /**
73 * returns a tag identified by the name or null if not found
74 * @param tagName the name of the tag
75 * @returns tag or null
76 */
77 getTagByName(tagName: string): Promise<Tag | null>;
78 /**
79 * returns a tag identified by the id or null if not found
80 * @param tagId the id of the tag
81 * @returns tag or null
82 */
83 getTagById(tagId: number): Promise<Tag | null>;
84 /**
85 * deletes the tag by id
86 * this function will fail with http 403 if the user does not have admin privileges
87 * @param tagId the id of the tag like "/remote.php/dav/systemtags/234"
88 */
89 deleteTag(tagId: number): Promise<void>;
90 /**
91 * deletes all visible assignable tags
92 * @throws Error
93 */
94 deleteAllTags(): Promise<void>;
95 /**
96 * returns a list of tags
97 * @returns array of tags
98 */
99 getTags(): Promise<Tag[]>;
100 /**
101 * returns the list of tag names and the tag ids
102 * @param fileId the id of the file
103 */
104 getTagsOfFile(fileId: number): Promise<Map<string, number>>;
105 /**
106 * removes the tag from the file
107 * @param fileId the file id
108 * @param tagId the tag id
109 */
110 removeTagOfFile(fileId: number, tagId: number): Promise<void>;
111 /**
112 * returns the id of the file or -1 of not found
113 * @returns id of the file or -1 if not found
114 */
115 getFileId(fileUrl: string): Promise<number>;
116 getFolderContents(folderName: string): Promise<any[]>;
117 /**
118 * creates a folder and all parent folders in the path if they do not exist
119 * @param folderName name of the folder /folder/subfolder/subfolder
120 * @returns a folder object
121 */
122 createFolder(folderName: string): Promise<Folder>;
123 /**
124 * deletes a file
125 * @param fileName name of folder "/f1/f2/f3/x.txt"
126 */
127 deleteFile(fileName: string): Promise<void>;
128 /**
129 * deletes a folder
130 * @param folderName name of folder "/f1/f2/f3"
131 */
132 deleteFolder(folderName: string): Promise<void>;
133 /**
134 * get a folder object from a path string
135 * @param folderName Name of the folder like "/company/branches/germany"
136 * @returns null if the folder does not exist or an folder object
137 */
138 getFolder(folderName: string): Promise<Folder | null>;
139 /**
140 * get a array of folders from a folder path string
141 * @param folderName Name of the folder like "/company/branches/germany"
142 * @returns array of folder objects
143 */
144 getSubFolders(folderName: string): Promise<Folder[]>;
145 /**
146 * get files of a folder
147 * @param folderName Name of the folder like "/company/branches/germany"
148 * @returns array of file objects
149 */
150 getFiles(folderName: string): Promise<File[]>;
151 /**
152 * create a new file of overwrites an existing file
153 * @param fileName the file name /folder1/folder2/filename.txt
154 * @param data the buffer object
155 */
156 createFile(fileName: string, data: Buffer): Promise<File>;
157 /**
158 * returns a nextcloud file object
159 * @param fileName the full file name /folder1/folder2/file.pdf
160 */
161 getFile(fileName: string): Promise<File | null>;
162 /**
163 * renames the file or moves it to an other location
164 * @param sourceFileName source file name
165 * @param targetFileName target file name
166 */
167 moveFile(sourceFileName: string, targetFileName: string): Promise<File>;
168 /**
169 * renames the folder or moves it to an other location
170 * @param sourceFolderName source folder name
171 * @param tarName target folder name
172 */
173 moveFolder(sourceFolderName: string, tarName: string): Promise<Folder>;
174 /**
175 * returns the content of a file
176 * @param fileName name of the file /d1/file1.txt
177 * @returns Buffer with file content
178 */
179 getContent(fileName: string): Promise<Buffer>;
180 /**
181 * returns the link to a file for downloading
182 * @param fileName name of the file /folder1/folder1.txt
183 * @returns url
184 */
185 getLink(fileName: string): string;
186 /**
187 * returns the url to the file in the nextcloud UI
188 * @param fileId the id of the file
189 */
190 getUILink(fileId: number): string;
191 /**
192 * adds a tag to a file or folder
193 * if the tag does not exist, it is automatically created
194 * if the tag is created, the user must have damin privileges
195 * @param fileId the id of the file
196 * @param tagName the name of the tag
197 * @returns nothing
198 * @throws Error
199 */
200 addTagToFile(fileId: number, tagName: string): Promise<void>;
201 /**
202 * adds a comment to a file
203 * @param fileId the id of the file
204 * @param comment the comment to be added to the file
205 */
206 addCommentToFile(fileId: number, comment: string): Promise<void>;
207 /**
208 * returns comments of a file / folder
209 * @param fileId the id of the file / folder
210 * @param top number of comments to return
211 * @param skip the offset
212 * @returns array of comment strings
213 * @throws Exception
214 */
215 getFileComments(fileId: number, top?: number, skip?: number): Promise<string[]>;
216 /**
217 * returns system information about the nextcloud server and the nextcloud client
218 */
219 getSystemInfo(): Promise<ISystemInfo>;
220 /**
221 * returns users
222 */
223 getUserIDs(): Promise<string[]>;
224 createUser(options: {
225 userId: string;
226 displayName: string;
227 password: string;
228 }): Promise<void>;
229 /**
230 * create a new share
231 */
232 createShare(options: ICreateShare): Promise<Share>;
233 /**
234 * update a new share
235 */
236 updateShare(shareId: string, body: {
237 password: string;
238 } | {
239 expireDate: string;
240 } | {
241 note: string;
242 }): Promise<void>;
243 /**
244 * get share information
245 * @param shareId
246 */
247 getShare(shareId: string): Promise<any>;
248 /**
249 * get share information
250 * @param shareId
251 */
252 deleteShare(shareId: string): Promise<any>;
253 /**
254 * asserts valid xml
255 * asserts multistatus response
256 * asserts that a href is available in the multistatus response
257 * asserts propstats and prop
258 * @param response the http response
259 * @param href get only properties that match the href
260 * @returns array of properties
261 * @throws GeneralError
262 */
263 private getPropertiesFromWebDAVMultistatusResponse;
264 /**
265 * nextcloud creates a csrf token and stores it in the html header attribute
266 * data-requesttoken
267 * this function is currently not used
268 * @returns the csrf token / requesttoken
269 */
270 private getHttpResponse;
271 /**
272 * get contents array of a folder
273 * @param folderName Name of the folder like "/company/branches/germany"
274 * @param folderIndicator true if folders are requested otherwise files
275 * @returns array of folder contents meta data
276 */
277 private Contents;
278 private sanitizeFolderName;
279 private getTagIdFromHref;
280 private createFolderInternal;
281 private stat;
282 private putFileContents;
283}