aboutsummaryrefslogtreecommitdiff
path: root/src/api/util.js
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2021-05-26 18:50:23 +0200
committerHampusM <hampus@hampusmat.com>2021-05-26 18:50:23 +0200
commitdf806dda764ff722742301042ab88b3d91f472c5 (patch)
tree4fc53caa4dc6a3acc3e61ebff664da56af9a464d /src/api/util.js
parent446b4d8ccc50555a3d9d47237b5511986ad16fe6 (diff)
Switched web framework from Express.js to Fastify & improved the backend
Diffstat (limited to 'src/api/util.js')
-rw-r--r--src/api/util.js45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/api/util.js b/src/api/util.js
new file mode 100644
index 0000000..aa31296
--- /dev/null
+++ b/src/api/util.js
@@ -0,0 +1,45 @@
+const fs = require("fs");
+const git = require("./git");
+
+function verifyRepoName(dirty, base_dir)
+{
+ return new Promise((resolve) =>
+ {
+ const is_valid_repo_name = /^[a-zA-Z0-9\\.\-_]+$/.test(dirty);
+ if(!is_valid_repo_name) {
+ resolve("ERR_REPO_REGEX");
+ }
+
+ fs.readdir(base_dir, (err, dir_content) =>
+ {
+ if(err) {
+ resolve("ERR_REPO_NOT_FOUND");
+ }
+
+ dir_content = dir_content.filter(repo => repo.endsWith(".git"));
+ if(!dir_content.includes(dirty + ".git")) {
+ resolve("ERR_REPO_NOT_FOUND");
+ }
+
+ resolve(true);
+ });
+ });
+}
+
+async function verifyCommitID(base_dir, repo, dirty)
+{
+ if(!/^[a-fA-F0-9]+$/.test(dirty)) {
+ return "ERR_COMMIT_REGEX";
+ }
+
+ const commit_exists = await git.doesCommitExist(base_dir, repo, dirty);
+
+ if(!commit_exists) {
+ return "ERR_COMMIT_NOT_FOUND";
+ }
+
+ return true;
+}
+
+module.exports.verifyRepoName = verifyRepoName;
+module.exports.verifyCommitID = verifyCommitID; \ No newline at end of file