added session so multiple people can use it at once
This commit is contained in:
@@ -1,126 +1,104 @@
|
||||
let currentState = {
|
||||
"username": "guest",
|
||||
"directory": "/"
|
||||
const fileSystem = require("../filesystem.json");
|
||||
|
||||
function getCurrentDir(sessionState) {
|
||||
return sessionState.directory;
|
||||
}
|
||||
|
||||
const fileSystem = require("../filesystem.json")
|
||||
|
||||
function getCurrentDir(){
|
||||
return currentState.directory;
|
||||
function customTextResponse(req, res, txt) {
|
||||
respondToCommand(req, res, txt, req.session.state.directory, "");
|
||||
}
|
||||
|
||||
function customTextResponse(res, txt){
|
||||
respondToCommand(res, txt, currentState.directory, "");
|
||||
}
|
||||
|
||||
|
||||
function ls(res) {
|
||||
function ls(req, res) {
|
||||
const command = "ls";
|
||||
const contents = fileSystem[currentState.directory];
|
||||
const contents = fileSystem[req.session.state.directory];
|
||||
|
||||
if (contents && typeof contents === 'object') {
|
||||
let keys = Object.keys(contents)
|
||||
respondToCommand(res, keys.join(" "), currentState.directory, command);
|
||||
let keys = Object.keys(contents);
|
||||
respondToCommand(req, res, keys.join(" "), req.session.state.directory, command);
|
||||
} else {
|
||||
respondToCommand(res, "No Files Found In Current Directory", currentState.directory, command);
|
||||
respondToCommand(req, res, "No Files Found In Current Directory", req.session.state.directory, command);
|
||||
}
|
||||
}
|
||||
|
||||
function whoami(res){
|
||||
const command = "whoami"
|
||||
respondToCommand(res, currentState.username, currentState.directory, command);
|
||||
function whoami(req, res) {
|
||||
const command = "whoami";
|
||||
respondToCommand(req, res, req.session.state.username, req.session.state.directory, command);
|
||||
}
|
||||
|
||||
function cd (res, raw_command) {
|
||||
function cd(req, res, raw_command) {
|
||||
const command = "cd";
|
||||
let arguement = raw_command.split(" ")[1]
|
||||
let argument = raw_command.split(" ")[1];
|
||||
const contents = fileSystem[req.session.state.directory];
|
||||
|
||||
//check if folder to cd to is in current dir
|
||||
const contents = fileSystem[currentState.directory];
|
||||
|
||||
if (contents && typeof contents==="object"){
|
||||
if (contents && typeof contents === "object") {
|
||||
let keys = Object.keys(contents);
|
||||
for (let i = 0; i < keys.length; i++){
|
||||
let key = keys[i]
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
let key = keys[i];
|
||||
|
||||
if (key == arguement && fileSystem[currentState.directory][key] == "dir"){
|
||||
currentState.directory = currentState.directory + arguement;
|
||||
|
||||
if (!currentState.directory.endsWith("/")) currentState.directory += "/";
|
||||
respondToCommand(res, "", currentState.directory, command);
|
||||
if (key === argument && fileSystem[req.session.state.directory][key] === "dir") {
|
||||
req.session.state.directory += argument;
|
||||
if (!req.session.state.directory.endsWith("/")) req.session.state.directory += "/";
|
||||
respondToCommand(req, res, "", req.session.state.directory, command);
|
||||
return;
|
||||
}
|
||||
else if (key == arguement){
|
||||
respondToCommand(res, "You cannot open a file as a directory", currentState.directory, command);
|
||||
} else if (key === argument) {
|
||||
respondToCommand(req, res, "You cannot open a file as a directory", req.session.state.directory, command);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// check a few things:
|
||||
if (arguement == "/" || arguement == "~" || arguement == "~/"){
|
||||
currentState.directory = "/";
|
||||
respondToCommand(res, "", currentState.directory, command);
|
||||
if (argument === "/" || argument === "~" || argument === "~/") {
|
||||
req.session.state.directory = "/";
|
||||
respondToCommand(req, res, "", req.session.state.directory, command);
|
||||
return;
|
||||
}
|
||||
|
||||
if (arguement == ".." || arguement == "../"){
|
||||
//move 1 folder back
|
||||
const lastSlashIndex = currentState.directory.lastIndexOf('/');
|
||||
const pathWithoutTrailingSlash = lastSlashIndex === currentState.directory.length - 1 ? currentState.directory.substring(0, currentState.directory.length - 1) : currentState.directory;
|
||||
if (argument === ".." || argument === "../") {
|
||||
const lastSlashIndex = req.session.state.directory.lastIndexOf('/');
|
||||
const pathWithoutTrailingSlash = lastSlashIndex === req.session.state.directory.length - 1 ? req.session.state.directory.substring(0, req.session.state.directory.length - 1) : req.session.state.directory;
|
||||
const secondLastSlashIndex = pathWithoutTrailingSlash.lastIndexOf('/');
|
||||
currentState.directory = pathWithoutTrailingSlash.substring(0, secondLastSlashIndex + 1);
|
||||
req.session.state.directory = pathWithoutTrailingSlash.substring(0, secondLastSlashIndex + 1);
|
||||
|
||||
respondToCommand(res, "", currentState.directory, command);
|
||||
respondToCommand(req, res, "", req.session.state.directory, command);
|
||||
return;
|
||||
}
|
||||
|
||||
respondToCommand(res, "Unable to open directory", currentState.directory, command);
|
||||
}
|
||||
else
|
||||
{
|
||||
respondToCommand(res, "Internal Server Error, try again later.", currentState.directory, command)
|
||||
respondToCommand(req, res, "Unable to open directory", req.session.state.directory, command);
|
||||
} else {
|
||||
respondToCommand(req, res, "Internal Server Error, try again later.", req.session.state.directory, command);
|
||||
}
|
||||
}
|
||||
|
||||
function pwd (res){
|
||||
function pwd(req, res) {
|
||||
const command = "pwd";
|
||||
respondToCommand(res, currentState.directory, currentState.directory, command);
|
||||
respondToCommand(req, res, req.session.state.directory, req.session.state.directory, command);
|
||||
}
|
||||
|
||||
function cat (res, raw_command){
|
||||
function cat(req, res, raw_command) {
|
||||
const command = "cat";
|
||||
let arguement = raw_command.split(" ")[1]
|
||||
let argument = raw_command.split(" ")[1];
|
||||
const contents = fileSystem[req.session.state.directory];
|
||||
|
||||
//check if folder to cd to is in current dir
|
||||
const contents = fileSystem[currentState.directory];
|
||||
|
||||
if (contents && typeof contents==="object"){
|
||||
if (contents && typeof contents === "object") {
|
||||
let keys = Object.keys(contents);
|
||||
for (let i = 0; i < keys.length; i++){
|
||||
let key = keys[i]
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
let key = keys[i];
|
||||
|
||||
if (key == arguement && fileSystem[currentState.directory][key] != "dir"){
|
||||
respondToCommand(res, fileSystem[currentState.directory][key], currentState.directory, command);
|
||||
if (key === argument && fileSystem[req.session.state.directory][key] !== "dir") {
|
||||
respondToCommand(req, res, fileSystem[req.session.state.directory][key], req.session.state.directory, command);
|
||||
return;
|
||||
}
|
||||
else if (key == arguement){
|
||||
respondToCommand(res, "You cannot open a directory as a file", currentState.directory, command);
|
||||
} else if (key === argument) {
|
||||
respondToCommand(req, res, "You cannot open a directory as a file", req.session.state.directory, command);
|
||||
return;
|
||||
}
|
||||
}
|
||||
respondToCommand(res, "Unable to open file", currentState.directory, command)
|
||||
respondToCommand(req, res, "Unable to open file", req.session.state.directory, command);
|
||||
} else {
|
||||
respondToCommand(req, res, "Internal Server Error, try again later.", req.session.state.directory, command);
|
||||
}
|
||||
else
|
||||
{
|
||||
respondToCommand(res, "Internal Server Error, try again later.", currentState.directory, command)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
function help(res){
|
||||
const command = "help"
|
||||
function help(req, res) {
|
||||
const command = "help";
|
||||
const text = `
|
||||
Welcome to the fancy help menu!!<br>
|
||||
Here is a list of the available commands:<br><br>
|
||||
@@ -130,30 +108,27 @@ function help(res){
|
||||
"cd" - move into a folder you specify, you can use the name of the folder, or you can also use things like ../ to move back one folder<br>
|
||||
"whoami" - shows you as what user you are logged in as<br>
|
||||
"pwd" - shows you the current directory you are currently working in<br>
|
||||
"cat" - shows you the content of files, fox example cat file.txt shows you the content of file.txt if it is in the current directory<br>
|
||||
`
|
||||
respondToCommand(res, text, currentState.directory, command);
|
||||
"cat" - shows you the content of files, for example, cat file.txt shows you the content of file.txt if it is in the current directory<br>
|
||||
`;
|
||||
respondToCommand(req, res, text, req.session.state.directory, command);
|
||||
}
|
||||
|
||||
function respondToCommand(req, res, text, directory, command) {
|
||||
res.json({
|
||||
"username": req.session.state.username,
|
||||
"directory": directory,
|
||||
"response": text,
|
||||
"command": command
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getCurrentDir,
|
||||
customTextResponse,
|
||||
|
||||
ls,
|
||||
whoami,
|
||||
cd,
|
||||
pwd,
|
||||
cat,
|
||||
help,
|
||||
}
|
||||
|
||||
|
||||
|
||||
function respondToCommand(res, text, directory, command){
|
||||
res.json({
|
||||
"username": "guest",
|
||||
"directory": directory,
|
||||
"response": text,
|
||||
"command": command
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user