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