mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-27 16:47:13 +09:00
Added function in lib/file_ops.js to return a temp filename with salt
This commit is contained in:
1 parent
bdfe8e630e
commit
6910737374
1 file changed
+22
-1
@@ -4,7 +4,7 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
const fs = require("fs-extra");
|
const fs = require("fs-extra");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
|
const crypto = require("crypto"); // needed for getTempFile
|
||||||
/**
|
/**
|
||||||
* Returns the newest file in a directory
|
* Returns the newest file in a directory
|
||||||
*
|
*
|
||||||
@@ -68,8 +68,29 @@ async function findUnusedName(directory, name, extension = "") {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Returns a path to a temporary file.
|
||||||
|
*
|
||||||
|
* Warning: this function should not be relied upon to be accurate for
|
||||||
|
* security sensitive applications. The selection process is inherently
|
||||||
|
* racy and a file or folder may have been created in the folder by the time
|
||||||
|
* this function returns.
|
||||||
|
*
|
||||||
|
* @param {string} prefix - prefix for file
|
||||||
|
* @param {string} suffix - suffix for file
|
||||||
|
* @param {string} tmpdir - directory for file
|
||||||
|
*/
|
||||||
|
async function getTempFile(prefix, suffix, tmpdir) {
|
||||||
|
prefix = (typeof prefix !== "undefined") ? prefix : "tmp.";
|
||||||
|
suffix = (typeof suffix !== "undefined") ? suffix : "";
|
||||||
|
tmpdir = tmpdir ? tmpdir : os.tmpdir();
|
||||||
|
return path.join(tmpdir, prefix + crypto.randomBytes(16).toString("hex") + suffix);
|
||||||
|
// IMO, 16 bytes of random hex should be fine, and hex is allowable in any filesystem, but if
|
||||||
|
// some other system is better, please inform
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getNewestFile,
|
getNewestFile,
|
||||||
findUnusedName,
|
findUnusedName,
|
||||||
|
getTempFile,
|
||||||
};
|
};
|
||||||
Reference in new issue
Block a user