This repository has been archived by the owner on Jun 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.js
131 lines (104 loc) · 3.49 KB
/
utils.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
'use strict';
const _ = require('lodash');
const fetch = require('node-fetch');
const contentDisposition = require('content-disposition');
// CODE TIP: This function is used to process the response of several endpoints
// on the OneDrive API. We intentionally don't set it up as an `afterResposne`
// handler because not *all* calls need it (i.e. the auth test and file create),
// so we break it out and share the code this way instead.
const parseResponse = (type, response) => {
let results = [];
if (response.status >= 200 && response.status < 300) {
results = JSON.parse(response.content);
// OneDrive puts the contents of lists inside .value property
if (!_.isArray(results) && _.isArray(results.value)) {
results = results.value;
}
} else {
throw new Error(response.content);
}
// Only return files or folders, according to type
if (_.isArray(results)) {
results = results.filter((result) => result.hasOwnProperty(type));
} else {
if (!results.hasOwnProperty(type)) {
results = {};
}
}
return results;
};
const handleError = (error) => {
if (typeof error === 'string') {
throw new Error(error);
}
throw error;
};
const extractParentsFromPath = (path) => {
const parts = path.split('/');
const results = [];
parts.splice(parts.length - 1);// Last is the current directory, so we remove it
let i = (parts.length - 1);
while (parts.length > 0) {
const name = parts.splice(i)[0];
const result = {
id: (i + 1) * -1,
name: (name === '' ? '/' : name),
_path: parts.join('/') + (name === '' ? name : `/${name}`),
folder: {},
};
results.push(result);
i -= 1;
}
results.reverse();
return results;
};
// UX TIP: Sometimes it can be helpful to translate raw values from an API into
// terms that end users are familiar with. In this example, OneDrive returns
// parent and the path of a file with a '/drive/root:' that end users of OneDrive
// never see. To help it read better in a dynamic dropdown, we clean that off
const cleanupPaths = (results) => {
// We can get a single object here as well
if (!_.isArray(results)) {
results._parent = _.get(results, 'parentReference.path', '').replace('/drive/root:', '');
results._path = `${results._parent}/${results.name}`;
return results;
}
// Adds easier to reference paths, cleaning up the "/drive/root:" clutter
return results.map((result) => {
result._parent = _.get(result, 'parentReference.path', '').replace('/drive/root:', '');
result._path = `${result._parent}/${result.name}`;
return result;
});
};
const getFileDetailsFromRequest = (url) => new Promise((resolve, reject) => {
const fileDetails = {
filename: '',
size: 0,
content: '',
contentType: '',
};
fetch(url)
.then((response) => {
fileDetails.size = response.headers.get('content-length');
fileDetails.contentType = response.headers.get('content-type');
const disposition = response.headers.get('content-disposition');
if (disposition) {
fileDetails.filename = contentDisposition.parse(disposition).parameters.filename;
}
return response.buffer();
})
.then((content) => {
fileDetails.content = content;
return resolve(fileDetails);
})
.catch(reject);
});
const getStringByteSize = (string) => Buffer.byteLength(string, 'utf8');
module.exports = {
parseResponse,
handleError,
extractParentsFromPath,
cleanupPaths,
getFileDetailsFromRequest,
getStringByteSize,
};