This repository was archived by the owner on Nov 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.js
More file actions
47 lines (44 loc) · 1.38 KB
/
utility.js
File metadata and controls
47 lines (44 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//create folder in the uploads folder if not exists, the folder name is the user name
import fs from "fs";
/**
* Creates a new directory at specified `folderpath`.
*
* @param {string} folderPath - Folderpath to create a folder at.
* @returns {boolean} `true` if succes, `false` otherwise.
*/
export function createFolder(folderPath) {
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath, { recursive: true });
console.log("Upload folder created at " + folderPath);
return true;
} else {
console.log("Upload folder already exists at " + folderPath);
return false;
}
}
/**
* Uses fs.writeFile to write data to a specified file.
*
* @param {string} uploadPath - The path write the data to
* @param {object} Jobdata - The data to be written to the file.
* @throw Will throw an error if the writing fails.
*/
export function writeFile(uploadPath, Jobdata) {
fs.writeFile(uploadPath, JSON.stringify(Jobdata), (error) => {
if (error) {
throw error;
} else {
console.log("File uploaded to " + uploadPath);
}
});
}
/**
* Sanitizes a string by removing potentially undesired characters.
* @param {string} str - The string to be sanitzed.
* @returns {string} The sanitized string.
*/
export function sanitize(str) {
return str
.replace(/[^a-zA-Z0-9-_]/g, "")
.trim();
}