-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfilestoreutils.js
executable file
·138 lines (122 loc) · 3.45 KB
/
filestoreutils.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
'use strict';
/**
* Object type
* @type {{file: string, folder: string}}
*/
var OBJECT_TYPE = {
file: 'file',
folder: 'folder'
};
/**
* HTTP status
* @type {{ok: number, created: number, not_authorized: number, not_found: number, not_allowed: number}}
*/
var HTTPSTAT = {
ok: 200,
created: 201,
not_authorized: 403,
not_found: 404,
not_allowed: 405
};
/**
* Modification Identifier
* @type {{create: string, update: string, delete: string}}
*/
var MODIDF = {
create: 'create',
update: 'update',
delete: 'delete'
};
/**
* Resolves file structure into single folders and files
* @param {(string|string)} resolve file or file array
* @param {string} sPathStartWith path has to start with that value
* @returns {Array} array with resolved folders and files
*/
function structureResolve(resolve, sPathStartWith) {
var aToResolve = [];
var aResolved = [];
if (typeof resolve === 'object' && resolve instanceof Array) {
aToResolve = resolve;
} else if (typeof resolve === 'string') {
aToResolve.push(resolve);
} else {
return null;
}
// resolve
aToResolve.forEach(function (item) {
var aSplit = item.split('/');
for (var i = 0; i < aSplit.length; i++) {
var aConc = aSplit.slice(0, i + 1);
var sConc = (aConc.length > 1) ? aConc.join('/') : aConc[0];
if (sConc.length > 0) {
aResolved.push({
type: (i < (aSplit.length - 1)) ? OBJECT_TYPE.folder : OBJECT_TYPE.file,
id: (sConc.charAt(0) !== sPathStartWith) ? sPathStartWith + sConc : sConc
});
}
}
});
// remove dups
aResolved = aResolved.sort(function (sVal1, sVal2) {
var sA = JSON.stringify(sVal1);
var sB = JSON.stringify(sVal2);
if (sA === sB) {
return 0;
} else if (sA <= sB) {
return -1;
} else {
return 1;
}
})
.filter(function (oItem, iPos) {
//console.log(aResolved.indexOf(item) + ' -- ' + JSON.stringify(item));
if (iPos > 0) {
return JSON.stringify(aResolved[iPos - 1]) !== JSON.stringify(oItem);
} else {
return true;
}
});
return aResolved;
}
/**
* Split a value into the path and object information
* @param {string} sValue values like /test/test1.txt
* @returns {{path: string, obj: string}}
*/
function splitIntoPathAndObject(sValue) {
var aValues = sValue.split('/');
var sObject = aValues.pop();
var sPath = aValues.join('/');
if (sPath.length > 0 && sPath.charAt(0) !== '/') {
sPath = '/' + sPath;
}
return {
path: sPath,
obj: sObject
};
}
/**
*
* @param {object} oRequest request
* @param {object} oResponse response
* @returns {string} response error string
*/
function createResponseError(oRequest, oResponse) {
if (oResponse.error) {
return 'Request-Url: ' + oRequest.options.url +
'\nRequest-Method: ' + oRequest.options.method +
'\n' + oResponse.error +
'\nDetails: \n' + oResponse.body ;
}
return null;
}
/**
* export
*/
exports.OBJECT_TYPE = OBJECT_TYPE;
exports.HTTPSTAT = HTTPSTAT;
exports.MODIDF = MODIDF;
exports.structureResolve = structureResolve;
exports.splitIntoPathAndObject = splitIntoPathAndObject;
exports.createResponseError = createResponseError;