forked from perry-mitchell/webdav-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.js
312 lines (289 loc) · 13.1 KB
/
factory.js
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
const authTools = require("./auth.js");
const urlTools = require("./url.js");
const { merge } = require("./merge.js");
const directoryContents = require("./interface/directoryContents.js");
const createDir = require("./interface/createDirectory.js");
const createStream = require("./interface/createStream.js");
const deletion = require("./interface/delete.js");
const getFile = require("./interface/getFile.js");
const quota = require("./interface/quota.js");
const move = require("./interface/moveFile.js");
const copy = require("./interface/copyFile.js");
const putFile = require("./interface/putFile.js");
const stats = require("./interface/stat.js");
/**
* Client adapter
* @typedef {Object} ClientInterface
*/
/**
* Options for creating a resource
* @typedef {UserOptions} PutOptions
* @property {Boolean=} overwrite - Whether or not to overwrite existing files (default: true)
*/
/**
* Options with headers and format
* @typedef {UserOptions} OptionsWithFormat
* @property {String} format - The format to use (text/binary)
* @property {Boolean=} details - Provided detailed response information, such as response
* headers (defaults to false). Only available on requests that return result data.
*/
/**
* Options for methods that resturn responses
* @typedef {UserOptions} OptionsForAdvancedResponses
* @property {Boolean=} details - Provided detailed response information, such as response
* headers (defaults to false). Only available on requests that return result data.
*/
/**
* @typedef {OptionsForAdvancedResponses} GetDirectoryContentsOptions
* @property {Boolean=} deep - Return deep (infinite) items (default: false)
* @property {String=} glob - Glob pattern for matching certain files
*/
/**
* @typedef {Object} AuthToken
* @property {String} token_type - The type of token (eg "Bearer")
* @property {String} access_token - The token access code
*/
/**
* @typedef {Object} CreateClientOptions
* @property {String=} username - The username for authentication
* @property {String=} password - The password for authentication
* @property {http.Agent=} httpAgent - Override the HTTP Agent instance for requests
* @property {https.Agent=} httpsAgent - Override the HTTPS Agent instance for requests
* @property {AuthToken=} token - Optional OAuth token
*/
/**
* A stat result
* @typedef {Object} Stat
* @property {String} filename The full path and filename of the remote item
* @property {String} basename The base filename of the remote item, without the path
* @property {String} lastmod The last modification date (eg. "Sun, 13 Mar 2016 04:23:32 GMT")
* @property {Number} size The size of the remote item
* @property {String} type The type of the item (file/directory)
* @property {String=} mime The file mimetype (not present on directories)
* @property {String|null} etag The ETag of the remote item (as supported by the server)
* @property {Object=} props Additionally returned properties from the server, unprocessed, if
* `details: true` is specified in the options
*/
/**
* Create a client adapter
* @param {String} remoteURL The remote address of the webdav server
* @param {CreateClientOptions=} options Client options
* @returns {ClientInterface} A new client interface instance
* @memberof module:WebDAV
* @example
* const createClient = require("webdav");
* const client = createClient(url, { username, password });
* client
* .getDirectoryContents("/")
* .then(contents => {
* console.log(contents);
* });
* @example
* const createClient = require("webdav");
* const client = createClient(url, {
* token: { token_type: "Bearer", access_token: "tokenvalue" }
* });
* client
* .getDirectoryContents("/")
* .then(contents => {
* console.log(contents);
* });
*/
function createClient(remoteURL, { username, password, httpAgent, httpsAgent, token = null } = {}) {
const baseOptions = {
headers: {},
remotePath: urlTools.extractURLPath(remoteURL),
remoteURL,
httpAgent,
httpsAgent
};
// Configure auth
if (username) {
baseOptions.headers.Authorization = authTools.generateBasicAuthHeader(username, password);
} else if (token && typeof token === "object") {
baseOptions.headers.Authorization = authTools.generateTokenAuthHeader(token);
}
return {
/**
* Copy a remote item to another path
* @param {String} remotePath The remote item path
* @param {String} targetRemotePath The path file will be copied to
* @param {UserOptions=} options Options for the request
* @memberof ClientInterface
* @returns {Promise} A promise that resolves once the request has completed
* @example
* await client.copyFile("/photos/pic1.jpg", "/backup/pic1.jpg");
*/
copyFile: function copyFile(remotePath, targetRemotePath, options) {
const copyOptions = merge(baseOptions, options || {});
return copy.copyFile(remotePath, targetRemotePath, copyOptions);
},
/**
* Create a directory
* @param {String} dirPath The path to create
* @param {UserOptions=} options Options for the request
* @memberof ClientInterface
* @returns {Promise} A promise that resolves when the remote path has been created
* @example
* await client.createDirectory("/my/directory");
*/
createDirectory: function createDirectory(dirPath, options) {
const createOptions = merge(baseOptions, options || {});
return createDir.createDirectory(dirPath, createOptions);
},
/**
* Create a readable stream of a remote file
* @param {String} remoteFilename The file to stream
* @param {UserOptions=} options Options for the request
* @memberof ClientInterface
* @returns {Readable} A readable stream
* @example
* const remote = client.createReadStream("/data.zip");
* remote.pipe(someWriteStream);
*/
createReadStream: function createReadStream(remoteFilename, options) {
const createOptions = merge(baseOptions, options || {});
return createStream.createReadStream(remoteFilename, createOptions);
},
/**
* Create a writeable stream to a remote file
* @param {String} remoteFilename The file to write to
* @param {PutOptions=} options Options for the request
* @memberof ClientInterface
* @returns {Writeable} A writeable stream
* @example
* const remote = client.createWriteStream("/data.zip");
* fs.createReadStream("~/myData.zip").pipe(remote);
*/
createWriteStream: function createWriteStream(remoteFilename, options) {
const createOptions = merge(baseOptions, options || {});
return createStream.createWriteStream(remoteFilename, createOptions);
},
/**
* Delete a remote file
* @param {String} remotePath The remote path to delete
* @param {UserOptions=} options The options for the request
* @memberof ClientInterface
* @returns {Promise} A promise that resolves when the remote file as been deleted
* @example
* await client.deleteFile("/some/file.txt");
*/
deleteFile: function deleteFile(remotePath, options) {
const deleteOptions = merge(baseOptions, options || {});
return deletion.deleteFile(remotePath, deleteOptions);
},
/**
* Get the contents of a remote directory
* @param {String} remotePath The path to fetch the contents of
* @param {GetDirectoryContentsOptions=} options Options for the remote the request
* @returns {Promise.<Array.<Stat>>} A promise that resolves with an array of remote item stats
* @memberof ClientInterface
* @example
* const contents = await client.getDirectoryContents("/");
*/
getDirectoryContents: function getDirectoryContents(remotePath, options) {
const getOptions = merge(baseOptions, options || {});
return directoryContents.getDirectoryContents(remotePath, getOptions);
},
/**
* Get the contents of a remote file
* @param {String} remoteFilename The file to fetch
* @param {OptionsWithFormat=} options Options for the request
* @memberof ClientInterface
* @returns {Promise.<Buffer|String>} A promise that resolves with the contents of the remote file
* @example
* // Fetching data:
* const buff = await client.getFileContents("/image.png");
* // Fetching text:
* const txt = await client.getFileContents("/list.txt", { format: "text" });
*/
getFileContents: function getFileContents(remoteFilename, options) {
const getOptions = merge(baseOptions, options || {});
getOptions.format = getOptions.format || "binary";
if (["binary", "text"].indexOf(getOptions.format) < 0) {
throw new Error("Unknown format: " + getOptions.format);
}
return getOptions.format === "text"
? getFile.getFileContentsString(remoteFilename, getOptions)
: getFile.getFileContentsBuffer(remoteFilename, getOptions);
},
/**
* Get the download link of a remote file
* Only supported for Basic authentication or unauthenticated connections.
* @param {String} remoteFilename The file url to fetch
* @param {UserOptions=} options Options for the request
* @memberof ClientInterface
* @returns {String} A download URL
*/
getFileDownloadLink: function getFileDownloadLink(remoteFilename, options) {
const getOptions = merge(baseOptions, options || {});
return getFile.getFileLink(remoteFilename, getOptions);
},
/**
* Get a file upload link
* Only supported for Basic authentication or unauthenticated connections.
* @param {String} remoteFilename The path of the remote file location
* @param {PutOptions=} options The options for the request
* @memberof ClientInterface
* @returns {String} A upload URL
*/
getFileUploadLink: function getFileUploadLink(remoteFilename, options) {
var putOptions = merge(baseOptions, options || {});
return putFile.getFileUploadLink(remoteFilename, putOptions);
},
/**
* Get quota information
* @param {OptionsForAdvancedResponses=} options Options for the request
* @returns {Promise.<null|Object>} Returns null if failed, or an object with `used` and `available`
* @memberof ClientInterface
*/
getQuota: function getQuota(options) {
const getOptions = merge(baseOptions, options || {});
return quota.getQuota(getOptions);
},
/**
* Move a remote item to another path
* @param {String} remotePath The remote item path
* @param {String} targetRemotePath The new path after moving
* @param {UserOptions=} options Options for the request
* @memberof ClientInterface
* @returns {Promise} A promise that resolves once the request has completed
* @example
* await client.moveFile("/sub/file.dat", "/another/dir/file.dat");
*/
moveFile: function moveFile(remotePath, targetRemotePath, options) {
const moveOptions = merge(baseOptions, options || {});
return move.moveFile(remotePath, targetRemotePath, moveOptions);
},
/**
* Write contents to a remote file path
* @param {String} remoteFilename The path of the remote file
* @param {String|Buffer} data The data to write
* @param {PutOptions=} options The options for the request
* @returns {Promise} A promise that resolves once the contents have been written
* @memberof ClientInterface
* @example
* await client.putFileContents("/dir/image.png", myImageBuffer);
* // Put contents without overwriting:
* await client.putFileContents("/dir/image.png", myImageBuffer, { overwrite: false });
*/
putFileContents: function putFileContents(remoteFilename, data, options) {
const putOptions = merge(baseOptions, options || {});
return putFile.putFileContents(remoteFilename, data, putOptions);
},
/**
* Stat a remote object
* @param {String} remotePath The path of the item
* @param {OptionsForAdvancedResponses=} options Options for the request
* @memberof ClientInterface
* @returns {Promise.<Stat>} A promise that resolves with the stat data
*/
stat: function stat(remotePath, options) {
const getOptions = merge(baseOptions, options || {});
return stats.getStat(remotePath, getOptions);
}
};
}
module.exports = {
createClient
};