-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
143 lines (135 loc) · 4.7 KB
/
index.ts
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import express, { Application, Request, Response } from "express";
import cors from 'cors';
import fs from 'fs';
import path from 'path';
import { promisify } from 'util';
import { resolve } from 'path';
const readdir = promisify(fs.readdir);
const stat = promisify(fs.stat);
async function getFilesRec(dir: string): Promise<any> {
const subdirs = await readdir(dir);
const files = await Promise.all(subdirs.map(async (subdir) => {
const res = resolve(dir, subdir);
return (await stat(res)).isDirectory() ? getFilesRec(res) : res;
}));
return files.reduce((a, f) => a.concat(f), []);
}
const app: Application = express();
const port = 3000;
// Body parsing Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors());
app.get(
"/",
async (req: Request, res: Response): Promise<Response> => {
try {
let folderNames = await fs.readdirSync(path.resolve('.', 'Projects'));
let projects = folderNames.filter(x =>
!x.startsWith('.')
)
return res.status(200).send({
projects: projects
});
} catch (_) {
return res.status(500).send({
message: 'An error ocurred'
});
}
}
);
app.get(
"/course/:name",
async (req: Request, res: Response): Promise<Response | void> => {
try {
let fullPath = decodeURI(path.resolve(path.join('.', 'Projects', req.params.name, 'guides.json')));
if (!fs.existsSync(fullPath)) {
return res.status(404).send({
path: fullPath,
name: req.params.name,
message: 'The course does not exist'
});
}
const contents = JSON.parse(fs.readFileSync(fullPath).toString());
return res.status(200)
.json(contents);
} catch (_) {
return res.status(500).send({
message: 'An error ocurred'
});
}
}
);
app.get(
"/course/:name/assets",
async (req: Request, res: Response): Promise<Response | void> => {
try {
let fullPath = decodeURI(path.resolve(path.join('.', 'Projects', req.params.name, 'assets')));
if (!fs.existsSync(fullPath)) {
return res.status(200).send([]);
}
const files: string[] = await getFilesRec(fullPath);
const paths = files.map(e => path.relative(path.join('.', 'Projects', req.params.name), e))
return res.status(200)
.json(paths);
} catch (_) {
return res.status(500).send({
message: 'An error ocurred'
});
}
}
);
app.get(
"/*",
async (req: Request, res: Response): Promise<Response | void> => {
try {
let fullPath = decodeURI(path.resolve(path.join('.', 'Projects', req.path)))
console.log(fullPath);
if (!fs.existsSync(fullPath)) {
return res.status(404).send({
path: req.path,
message: 'Folder of file does not exist'
});
}
if (fs.lstatSync(fullPath).isDirectory()) {
let itemNames = await fs.readdirSync(fullPath);
let items = itemNames.filter(x =>
!x.startsWith('.')
).map(x => {
console.log(x);
let pathNoProjectName = req.path.substring(req.path.indexOf('/')+1);
pathNoProjectName = pathNoProjectName.substring(pathNoProjectName.indexOf('/')+1);
if (fs.lstatSync(path.join(fullPath, x)).isDirectory()) {
return {
dir: true,
name: x,
path: req.path,
relativePath: `${pathNoProjectName}/${x}`
}
} else {
return {
dir: false,
name: x,
relativePath: `${pathNoProjectName}/${x}`
}
}
})
return res.status(200).send({
items: items,
});
}
return res.sendFile(fullPath);
} catch (_) {
return res.status(500).send({
message: 'An error ocurred'
});
}
}
);
try {
app.listen(port, (): void => {
console.log(`Connected successfully on port ${port}`);
});
} catch (error: any) {
console.error(`Error occured: ${error.message}`);
}