2016-11-08 22:50:21 +01:00
|
|
|
export function resolvePathToFile(filePath, files) {
|
|
|
|
const filePathArray = filePath.split('/');
|
|
|
|
let resolvedFile;
|
2016-11-16 19:12:36 +01:00
|
|
|
let currentFile = files.find(file => file.name === 'root');
|
2016-11-08 22:50:21 +01:00
|
|
|
filePathArray.some((filePathSegment, index) => {
|
2016-11-16 19:12:36 +01:00
|
|
|
if (filePathSegment === '' || filePathSegment === '.') {
|
2016-11-08 22:50:21 +01:00
|
|
|
return false;
|
2016-11-16 19:12:36 +01:00
|
|
|
} else if (filePathSegment === '..') {
|
2016-11-08 22:50:21 +01:00
|
|
|
return true;
|
2016-11-16 19:12:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let foundChild = false;
|
|
|
|
const childFiles = currentFile.children.map(childFileId =>
|
|
|
|
files.find(file =>
|
2018-05-05 02:22:39 +02:00
|
|
|
file._id.valueOf().toString() === childFileId.valueOf()));
|
2017-02-22 20:29:35 +01:00
|
|
|
childFiles.some((childFile) => {
|
2016-11-16 19:12:36 +01:00
|
|
|
if (childFile.name === filePathSegment) {
|
|
|
|
currentFile = childFile;
|
|
|
|
foundChild = true;
|
2016-11-08 22:50:21 +01:00
|
|
|
if (index === filePathArray.length - 1) {
|
2016-11-16 19:12:36 +01:00
|
|
|
resolvedFile = childFile;
|
2016-11-08 22:50:21 +01:00
|
|
|
}
|
2016-11-16 19:12:36 +01:00
|
|
|
return true;
|
2016-11-08 22:50:21 +01:00
|
|
|
}
|
2016-11-16 19:12:36 +01:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
return !foundChild;
|
2016-11-08 22:50:21 +01:00
|
|
|
});
|
|
|
|
return resolvedFile;
|
2016-11-16 19:12:36 +01:00
|
|
|
}
|
2017-02-22 20:29:35 +01:00
|
|
|
|
|
|
|
export default resolvePathToFile;
|