2016-09-19 21:15:19 +00:00
|
|
|
"use strict";
|
2019-05-05 16:35:50 +00:00
|
|
|
|
|
|
|
const path = require('path');
|
2020-08-03 19:07:20 +00:00
|
|
|
const os = require('os');
|
|
|
|
const crypto = require('crypto');
|
2019-05-05 16:35:50 +00:00
|
|
|
|
2016-08-02 16:07:54 +00:00
|
|
|
module.exports = {
|
2019-01-04 16:36:14 +00:00
|
|
|
get: function(scope, prop, defaultValue){
|
|
|
|
let parts = prop.split(".");
|
|
|
|
let current = scope;
|
|
|
|
for (let i = 0; i < parts.length; i++){
|
|
|
|
if (current[parts[i]] !== undefined && i < parts.length - 1){
|
|
|
|
current = current[parts[i]];
|
|
|
|
}else if (current[parts[i]] !== undefined && i < parts.length){
|
|
|
|
return current[parts[i]];
|
|
|
|
}else{
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return defaultValue;
|
2019-02-01 21:20:03 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
sanitize: function(filePath){
|
2019-04-10 15:13:47 +00:00
|
|
|
filePath = filePath.replace(/[^\w.-]/g, "_");
|
2019-03-08 15:36:20 +00:00
|
|
|
return filePath;
|
2019-05-05 16:35:50 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
parseUnsafePathsList: function(paths){
|
|
|
|
// Parse a list (or a JSON encoded string representing a list)
|
|
|
|
// of paths and remove all traversals (., ..) and guarantee
|
|
|
|
// that the paths are relative
|
|
|
|
|
|
|
|
if (typeof paths === "string"){
|
|
|
|
try{
|
|
|
|
paths = JSON.parse(paths);
|
|
|
|
}catch(e){
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Array.isArray(paths)){
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return paths.map(p => {
|
|
|
|
const safeSuffix = path.normalize(p).replace(/^(\.\.(\/|\\|$))+/, '');
|
|
|
|
return path.join('./', safeSuffix);
|
|
|
|
});
|
2019-07-15 19:02:59 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
clone: function(json){
|
|
|
|
return JSON.parse(JSON.stringify(json));
|
2020-08-03 19:07:20 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
tmpPath: function(extension = ".txt"){
|
2020-08-03 19:15:46 +00:00
|
|
|
return path.join(os.tmpdir(), `nodeodm_${crypto.randomBytes(6).readUIntLE(0,6).toString(36)}${extension}`);
|
2019-01-04 16:36:14 +00:00
|
|
|
}
|
2016-08-02 16:07:54 +00:00
|
|
|
};
|