OpenDroneMap-NodeODM/libs/utils.js

58 wiersze
1.6 KiB
JavaScript
Czysty Zwykły widok Historia

"use strict";
2019-05-05 16:35:50 +00:00
const path = require('path');
const os = require('os');
const crypto = require('crypto');
2019-05-05 16:35:50 +00:00
module.exports = {
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, "_");
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));
},
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}`);
}
};