-
-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathdirectoryContents.js
66 lines (63 loc) · 2.94 KB
/
directoryContents.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
const pathPosix = require("path-posix");
const joinURL = require("url-join");
const { merge } = require("../merge.js");
const { handleResponseCode, processGlobFilter, processResponsePayload } = require("../response.js");
const { normaliseHREF, normalisePath } = require("../url.js");
const { getSingleValue, getValueForKey, parseXML, propsToStat } = require("./dav.js");
const { encodePath, prepareRequestOptions, request } = require("../request.js");
function getDirectoryContents(remotePath, options) {
// Join the URL and path for the request
const requestOptions = {
url: joinURL(options.remoteURL, encodePath(remotePath), "/"),
method: "PROPFIND",
headers: {
Accept: "text/plain",
Depth: options.deep ? "infinity" : 1
},
responseType: "text"
};
let response = null;
prepareRequestOptions(requestOptions, options);
return request(requestOptions)
.then(handleResponseCode)
.then(res => {
response = res;
return res.data;
})
.then(parseXML)
.then(result => getDirectoryFiles(result, options.remotePath, remotePath, options.details))
.then(files => processResponsePayload(response, files, options.details))
.then(files => (options.glob ? processGlobFilter(files, options.glob) : files));
}
function getDirectoryFiles(result, serverBasePath, requestPath, isDetailed = false) {
const remoteTargetPath = pathPosix.join(serverBasePath, requestPath, "/");
const serverBase = pathPosix.join(serverBasePath, "/");
// Extract the response items (directory contents)
const multiStatus = getValueForKey("multistatus", result);
const responseItems = getValueForKey("response", multiStatus);
return (
responseItems
// Filter out the item pointing to the current directory (not needed)
.filter(item => {
let href = getSingleValue(getValueForKey("href", item));
href = pathPosix.join(normalisePath(normaliseHREF(href)), "/");
return href !== serverBase && href !== remoteTargetPath;
})
// Map all items to a consistent output structure (results)
.map(item => {
// HREF is the file path (in full)
let href = getSingleValue(getValueForKey("href", item));
href = normaliseHREF(href);
// Each item should contain a stat object
const propStat = getSingleValue(getValueForKey("propstat", item));
const props = getSingleValue(getValueForKey("prop", propStat));
// Process the true full filename (minus the base server path)
const filename =
serverBase === "/" ? normalisePath(href) : normalisePath(pathPosix.relative(serverBase, href));
return propsToStat(props, filename, isDetailed);
})
);
}
module.exports = {
getDirectoryContents
};