Repository navigation
Expand file tree
/
Copy pathfile-structure.js
More file actions
317 lines (286 loc) · 7.03 KB
/
Copy pathfile-structure.js
File metadata and controls
317 lines (286 loc) · 7.03 KB
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
313
314
315
316
317
/* eslint-disable */
import { FILE_TYPE } from "../config/files.js";
/**
* Compute a file UUID.
*
* @param {Object} file
* @returns {String}
*/
function uuid(file) {
let nature = file.nature;
if (file.nature !== FILE_TYPE.FOLDER) {
nature = FILE_TYPE.DOCUMENT;
}
return `${nature}-${file.id}`;
}
/**
* Validate a file object.
*
* @param {Object} file
* @param {Array} requiredFields
*/
function validate(file, requiredFields = []) {
if (!file) {
throw new Error(`[file validation] invalid file: ${file}`);
}
if (!file.id && file.id !== 0) {
throw new Error(`[file validation] file id not set: ${file.id}`);
}
if (!file.nature) {
throw new Error(`[file validation] file type not set: ${file.nature}`);
}
for (const field of requiredFields) {
if (!file[field] && file[field] !== 0) {
throw new Error(`[file validation] file ${field} not set: ${file[field]}`);
}
}
}
/**
* Create a node map from file structure.
*
* @param {Object} fileStructure
* @returns {Map}
*/
function createNodeMap(fileStructure) {
const nodeMap = new Map();
const addToNodeMap = file => {
nodeMap.set(
uuid(file),
createFileNode(nodeMap, file)
);
(file.children || []).forEach(
child => addToNodeMap(child)
);
};
addToNodeMap(fileStructure);
return nodeMap;
}
/**
* Create a file structure with the given root from node map.
*
* @param {Map} nodeMap
* @param {Object} root
* @returns {Object}
*/
function createFileStructure(nodeMap, root) {
const rootNode = nodeMap.get(uuid(root));
const addToFileStructure = node => {
return Object.assign({}, {
...node.file,
children: node.children.map(
child => addToFileStructure(child)
)
});
};
return addToFileStructure(rootNode);
}
/**
* Create a file node to be added to the given node map from file.
*
* @param {Map} nodeMap
* @param {Object} file
* @returns {Object}
*/
function createFileNode(nodeMap, file) {
return {
file: { ...file, children: [] },
_children: (file.children || []).map(child => uuid(child)),
get parent() {
return this.file.parent_id ? nodeMap.get(`${FILE_TYPE.FOLDER}-${this.file.parent_id}`) : null;
},
get children() {
return this._children.map(nodeId => nodeMap.get(nodeId));
},
update(file) {
return Object.assign(this.file, { ...file, children: [] });
},
addChild(child) {
const childId = uuid(child);
if (!this._children.includes(childId)) {
this._children.push(childId);
}
return child;
},
removeChild(child) {
const childId = uuid(child);
this._children = this._children.filter(nodeId => nodeId !== childId);
return child;
}
};
}
class FileStructureHandler {
constructor(fileStructure) {
this.root = null;
this.nodeMap = new Map();
if (fileStructure) {
this.serialize(fileStructure);
}
}
serialize(fileStructure) {
validate(fileStructure);
this.root = {
id: fileStructure.id,
nature: fileStructure.nature
};
this.nodeMap = createNodeMap(fileStructure);
}
deserialize(root) {
if (!this.root) {
throw new Error(`[FileStructureHandler.deserialize] handler root is not set.`);
}
return createFileStructure(this.nodeMap, root || this.root);
}
/**********************
* Structure accessors
*/
exists(file) {
return this.nodeMap.has(uuid(file));
}
get(file, options = {}) {
const fileData = (this.nodeMap.get(uuid(file)) || {}).file;
return fileData && options.children
? { ...fileData, children: this.children(file) }
: fileData;
}
parent(file) {
const nodeId = uuid(file);
const node = this.nodeMap.get(nodeId);
if (node) {
return (node.parent || {}).file || null;
} else {
throw new Error(`[FileStructureHandler.parent] node ${nodeId} does not exist.`);
}
}
children(file) {
const nodeId = uuid(file);
const node = this.nodeMap.get(nodeId);
if (node) {
return node.children.map(child => child.file);
} else {
throw new Error(`[FileStructureHandler.children] node ${nodeId} does not exist.`);
}
}
ancestors(file) {
const ancestors = [];
let parent = this.parent(file);
while (parent) {
ancestors.unshift(parent);
parent = this.parent(parent);
}
return ancestors;
}
descendants(file) {
const children = this.children(file);
return children.concat(
children.flatMap(
child => this.descendants(child)
)
);
}
siblings(file) {
const nodeId = uuid(file);
const node = this.nodeMap.get(nodeId);
if (node) {
const parent = node.parent;
if (!parent) {
return [];
}
const siblings = [];
for (const child of parent.children) {
if (uuid(child.file) !== nodeId) {
siblings.push(child.file);
}
}
return siblings;
} else {
throw new Error(`[FileStructureHandler.siblings] node ${nodeId} does not exist.`);
}
}
/**********************
* Structure mutations
*/
createFile(file) {
validate(file, ['parent_id']);
const nodeId = uuid(file);
const node = createFileNode(this.nodeMap, file);
this.nodeMap.set(nodeId, node);
node.parent.addChild(file);
(file.children || []).forEach(
child => this.createFile(child)
);
}
updateFile(file) {
validate(file);
const nodeId = uuid(file);
const node = this.nodeMap.get(nodeId);
if (node) {
node.update(file);
} else {
// throw new Error(`[FileStructureHandler.updateFile] node ${nodeId} does not exist.`);
}
}
deleteFile(file) {
validate(file);
const nodeId = uuid(file);
const node = this.nodeMap.get(nodeId);
if (node) {
(file.children || []).forEach(
child => this.deleteFile(child)
);
this.nodeMap.delete(nodeId);
if (node.parent) {
node.parent.removeChild(file);
}
} else {
// throw new Error(`[FileStructureHandler.deleteFile] node ${nodeId} does not exist.`);
}
}
}
function getDescendants(file) {
const children = file.children || [];
return children.concat(
children.flatMap(
child => getDescendants(child)
)
);
}
function getAscendants(file, allFolders) {
const parent = allFolders.find(f => f.id === file.parent_id);
if (!parent) {
return [];
}
return [parent].concat(getAscendants(parent, allFolders));
}
function getModels(file) {
if (file.children?.length > 0) {
return file.children.flatMap(child => getModels(child));
} else {
return file.model_id ? [{ id: file.model_id }] : [];
}
}
function segregate(files) {
const folders = [];
const documents = [];
for (const file of files) {
if (isFolder(file)) {
folders.push(file);
} else {
documents.push(file);
}
}
return {
folders,
documents
};
}
function isFolder(file) {
return file.nature === FILE_TYPE.FOLDER;
}
export {
FileStructureHandler,
getAscendants,
getDescendants,
getModels,
segregate,
isFolder,
};