-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
132 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Application, Request, Response } from "express"; | ||
import { apiMessage, errorMessage } from "../logger"; | ||
import { existsSync } from "fs"; | ||
import { join } from "path"; | ||
|
||
export default (app: Application) => { | ||
app.get("/:name", async (req: Request, res: Response) => { | ||
try { | ||
const fileName = req.params.name; | ||
apiMessage(req.path, `User is trying to get a file - ${fileName}`); | ||
const filePath = join(__dirname, "../", "files", fileName); | ||
if (!existsSync(filePath)) { | ||
errorMessage(`File ${fileName} not found`); | ||
return res | ||
.status(404) | ||
.send({ sucsess: false, message: `File ${fileName} not found` }); | ||
} | ||
|
||
apiMessage(req.path, `File ${fileName} found`); | ||
return res.sendFile(filePath); | ||
} catch (err) { | ||
errorMessage(err as string); | ||
return res | ||
.status(500) | ||
.send({ sucsess: false, message: "Internal server error" }); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Application, Request, Response } from "express"; | ||
import { apiMessage, errorMessage } from "../logger"; | ||
import { UploadedFile } from "express-fileupload"; | ||
import { url, key } from "../../config.json"; | ||
import { existsSync } from "fs"; | ||
import { join } from "path"; | ||
|
||
export default (app: Application) => { | ||
app.post("/save/:name", async (req: Request, res: Response) => { | ||
try { | ||
const apiKey = req.headers["API-KEY"]; | ||
if (apiKey !== key) { | ||
errorMessage("Invalid API key provided"); | ||
return res | ||
.status(400) | ||
.send({ sucsess: false, message: "Invalid API key" }); | ||
} | ||
apiMessage(req.path, "User is trying to save a file"); | ||
|
||
const file = req.files?.file as UploadedFile; | ||
if (!file) { | ||
errorMessage("No file provided for upload"); | ||
return res | ||
.status(400) | ||
.send({ sucsess: false, message: "No file provided" }); | ||
} | ||
|
||
const fileName = req.params.name; | ||
const filePath = join(__dirname, "../", "files", fileName); | ||
if (existsSync(filePath)) { | ||
errorMessage(`File ${fileName} already exists`); | ||
return res | ||
.status(400) | ||
.json({ sucsess: false, message: `File ${fileName} already exists` }); | ||
} | ||
|
||
await file.mv(filePath); | ||
|
||
apiMessage(req.path, `File ${fileName} saved successfully`); | ||
return res.status(200).json({ | ||
sucsess: true, | ||
message: `File has been saved at ${url}/${fileName}`, | ||
}); | ||
} catch (err) { | ||
errorMessage(err as string); | ||
return res | ||
.status(500) | ||
.json({ sucsess: false, message: "Internal server error" }); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { otherMessage, errorMessage } from './logger'; | ||
import { readdirSync, statSync } from 'fs'; | ||
import { Application } from 'express'; | ||
import { join } from 'path'; | ||
|
||
export const loadEndpoints = (directory: string, app: Application) => { | ||
try { | ||
const items = readdirSync(directory); | ||
|
||
let skipped = 0; | ||
let loaded = 0; | ||
|
||
for (const item of items) { | ||
const itemPath = join(directory, item); | ||
const stats = statSync(itemPath); | ||
if (stats.isDirectory()) { | ||
const result = loadEndpoints(itemPath, app); | ||
if (result) { | ||
skipped += result.skipped; | ||
loaded += result.loaded; | ||
} | ||
} else if (item.toLowerCase().endsWith('.ts')) { | ||
if (item.toLowerCase().includes('disabled')) { | ||
skipped++; | ||
continue; | ||
} | ||
loaded++; | ||
// eslint-disable-next-line | ||
const route = require(itemPath).default; | ||
route(app); | ||
otherMessage(`Loaded ${itemPath.split('/src/endpoints/')[1].split('.ts')[0]} endpoint`); | ||
} | ||
} | ||
return { loaded, skipped }; | ||
} catch (error: any) { | ||
errorMessage(`Error loading endpoints: ${error}`); | ||
} | ||
}; |