Skip to content

Commit

Permalink
Updated fs and improved api
Browse files Browse the repository at this point in the history
  • Loading branch information
FunnyGuy9796 committed Aug 14, 2024
1 parent b407daf commit cda42f3
Show file tree
Hide file tree
Showing 27 changed files with 161 additions and 591 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
node_modules
filo_modules/filesystem/*
!filo_modules/filesystem/
node_modules
67 changes: 53 additions & 14 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const memory = require("./filo_modules/mem");
const os = require("os");
const realFs = require("fs");
const path = require("path");
const configFile= "./config.json";
const configFile = "./config.json";
const servicesFile = "./services.json";

let fsStatus = false;
Expand Down Expand Up @@ -166,9 +166,8 @@ async function startServices() {
}

function genRoutes(res, appId, appIndexFile) {
const appPath = path.join("/home/christopher/Documents/filo/filo_modules/filesystem/sys/apps", appId);
const appPath = fs.resolvePath(`/sys/apps/${appId}`);
const appFilePath = path.join(appPath, appIndexFile);
const ext = path.extname(appFilePath);
const status = realFs.existsSync(appFilePath);

if (status) {
Expand Down Expand Up @@ -230,9 +229,10 @@ async function installApp(appId) {
const absPath = path.join(appsPath, appId);

if (realFs.existsSync(absPath)) {
const installPath = `/home/christopher/Documents/filo/filo_modules/filesystem/sys/apps/${appId}`;
const appDataPath = `/home/christopher/Documents/filo/filo_modules/filesystem/.appData/${appId}`;
const installPath = fs.resolvePath(`/sys/apps/${appId}`);
const appDataPath = fs.resolvePath(`.appData/${appId}`);
const status = realFs.existsSync(installPath);
const dataStatus = realFs.existsSync(appDataPath);

if (!status) {
realFs.mkdirSync(installPath);
Expand All @@ -243,13 +243,17 @@ async function installApp(appId) {
try {
const appData = realFs.readFileSync(path.join(installPath, "appConfig.json"), "utf8");

console.log(chalk.cyan.bold("[FILO/APPLICATIONS]") + " -> Installing" + chalk.bold("[" + appId + "]") + ".. [" + chalk.green.bold("DONE") + "]");
console.log(chalk.cyan.bold("[FILO/APPLICATIONS]") + " -> Installing " + chalk.bold(appId) + ".. [" + chalk.green.bold("DONE") + "]");
} catch (error) {
console.log(chalk.cyan.bold("[FILO/APPLICATIONS") + "::" + chalk.red.bold("ERROR") + chalk.cyan.bold("]") + " -> Config file for app" + chalk.bold("[" + appId + "]") + " could not be found: " + error);
}
} catch(error) {
console.log(chalk.cyan.bold("[FILO/APPLICATION") + "::" + chalk.red.bold("ERROR") + chalk.cyan.bold("]") + " -> Failed installing app" + chalk.bold("[" + appId + "]") + ": " + error);
}

if (!dataStatus) {
realFs.mkdirSync(appDataPath);
}
} else {
console.log(chalk.cyan.bold("[FILO/APPLICATIONS]") + " -> app" + chalk.bold("[" + appId + "]") + " already installed");
}
Expand All @@ -258,8 +262,30 @@ async function installApp(appId) {
}
}

function removeApp(appId) {
const appPath = fs.resolvePath(`/sys/apps/${appId}`);
const appDataPath = fs.resolvePath(`.appData/${appId}`);

try {
const status = realFs.existsSync(appPath);
const dataStatus = realFs.existsSync(appDataPath);

if (status) {
realFs.rmSync(appPath, { recursive: true, force: true });
console.log(chalk.cyan.bold("[FILO/APPLICATIONS]") + " -> Removing " + chalk.bold(appId) + ".. [" + chalk.green.bold("DONE") + "]");
}

if (dataStatus) {
realFs.rmSync(appDataPath, { recursive: true, force: true });
console.log(chalk.cyan.bold("[FILO/APPLICATIONS]") + " -> Removing data for " + chalk.bold(appId) + ".. [" + chalk.green.bold("DONE") + "]");
}
} catch (error) {
console.log(chalk.cyan.bold("[FILO/APPLICATION") + "::" + chalk.red.bold("ERROR") + chalk.cyan.bold("]") + " -> Failed removing app" + chalk.bold("[" + appId + "]") + ": " + error);
}
}

function startApp(appId, uuid) {
const appPath = `/home/christopher/Documents/filo/filo_modules/filesystem/sys/apps/${appId}`;
const appPath = fs.resolvePath(`/sys/apps/${appId}`);
const appData = realFs.readFileSync(path.join(appPath, "appConfig.json"), "utf8");
const appConfigData = JSON.parse(appData);

Expand Down Expand Up @@ -321,7 +347,7 @@ function startAppsProcessor(callback) {
app.get("/app-info/:appId", (req, res) => {
const { appId } = req.params;

const absPath = path.join("/home/christopher/Documents/filo/filo_modules/filesystem/sys/apps", appId);
const absPath = fs.resolvePath(`/sys/apps/${appId}`);
const appConfigFile = path.join(absPath, "appConfig.json");
let appData;
try {
Expand Down Expand Up @@ -402,7 +428,7 @@ async function boot() {

app.get("/file/*", (req, res) => {
const currPath = decodeURIComponent(req.params[0]);
const filePath = path.join("/home/christopher/Documents/filo/filo_modules/filesystem", currPath);
const filePath = fs.resolvePath(currPath);

try {
const stats = realFs.statSync(filePath);
Expand Down Expand Up @@ -570,38 +596,51 @@ async function boot() {
}
}

async function cleanup() {
function cleanup() {
console.log(chalk.cyan.bold("[FILO]") + " -> Cleaning up before shutting down..");

const defaultApps = [
"about",
"file_explorer"
];

for (const appId of defaultApps) {
try {
removeApp(appId);
} catch (error) {
console.log(chalk.cyan.bold("[FILO") + "::" + chalk.red.bold("ERROR") + chalk.cyan.bold("]") + " -> Failed removing app" + chalk.bold(`[${appId}]`) + ": " + error);
}
}

memory.db.close();
console.log(chalk.cyan.bold("[FILO/MODULES]") + " -> Cleaning up memory.. [" + chalk.green.bold("DONE") + ']');
}

process.on('SIGINT', () => {
process.on('SIGINT', async () => {
console.log(" ");
console.log(chalk.cyan.bold("[FILO]") + " -> Terminated by user");

cleanup();
process.exit(0);
});

process.on('SIGTERM', () => {
process.on('SIGTERM', async () => {
console.log(" ");
console.log(chalk.cyan.bold("[FILO]") + " -> Terminated by system");

cleanup();
process.exit(0);
});

process.on('uncaughtException', (err) => {
process.on('uncaughtException', async (err) => {
console.log(" ");
console.log(chalk.cyan.bold("[FILO") + "::" + chalk.red.bold("ERROR") + chalk.cyan.bold("]") + " -> " + chalk.bold("UNCAUGHT EXCEPTION: ") + err);

cleanup();
process.exit(1);
});

process.on('unhandledRejection', (reason, promise) => {
process.on('unhandledRejection', async (reason, promise) => {
console.log(" ");
console.log(chalk.cyan.bold("[FILO") + "::" + chalk.red.bold("ERROR") + chalk.cyan.bold("]") + " -> " + chalk.bold("UNHANDLED REJECTION: ") + "Location: " + promise + " | Reason: " + reason);

Expand Down
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"port": 3001,
"services_path": "/home/christopher/Documents/filo/filo_services",
"apps_path": "/home/christopher/Documents/filo/filo_apps",
"fs_path": "/home/christopher/Documents/filo/filo_modules/filesystem",
"network_required": true
}
1 change: 0 additions & 1 deletion filo_apps/about/test.ejs

This file was deleted.

16 changes: 14 additions & 2 deletions filo_apps/file_explorer/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -340,15 +340,27 @@
pathInput.style.marginLeft = "250px";
pathInput.style.width = "calc(100% - 350px)";
explorerView.style.left = "180px";
quickAccess.style.display = "block";
explorerView.style.width = "calc(100% - 220px)";
quickAccess.style.display = "block";
}
pathInput.style.width = width - "350px";
});
window.onload = function() {
window.onload = async function() {
searchPath("/usr/home");
filo.fs.read(`/.appData/${filo.appId}`).then(result => {
if (result.files.length < 1) {
const testObject = {
name: "John",
age: 30
};
filo.fs.createFile(`/.appData/${filo.appId}/test.json`, JSON.stringify(testObject, null, 4));
}
console.log("app data result: ", result);
});
}
</script>
</body>
Expand Down
5 changes: 0 additions & 5 deletions filo_modules/filesystem/sys/apps/about/appConfig.json

This file was deleted.

Binary file not shown.
3 changes: 0 additions & 3 deletions filo_modules/filesystem/sys/apps/about/images/github.svg

This file was deleted.

96 changes: 0 additions & 96 deletions filo_modules/filesystem/sys/apps/about/index.ejs

This file was deleted.

1 change: 0 additions & 1 deletion filo_modules/filesystem/sys/apps/about/test.ejs

This file was deleted.

5 changes: 0 additions & 5 deletions filo_modules/filesystem/sys/apps/file_explorer/appConfig.json

This file was deleted.

Binary file not shown.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit cda42f3

Please sign in to comment.