-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
129 lines (114 loc) · 4.7 KB
/
app.js
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
const express = require('express');
const fs = require('fs');
const path = require('path');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use('/stories', express.static(path.join(__dirname, 'public')));
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
const documentsDir = path.join(__dirname, 'documents');
// Helper functions
const updateMetadata = (folder) => {
const folderPath = path.join(documentsDir, folder);
const files = fs.readdirSync(folderPath).filter(f => f !== 'metadata.json');
const metadata = {
filelist: files.map((file, index) => ({
name: file,
index: index,
bias: 1
})),
pointer: 0
};
fs.writeFileSync(path.join(folderPath, 'metadata.json'), JSON.stringify(metadata, null, 2));
};
const getMetadata = (folder) => {
const folderPath = path.join(documentsDir, folder);
const metadataPath = path.join(folderPath, 'metadata.json');
return JSON.parse(fs.readFileSync(metadataPath, 'utf8'));
};
// Routes
app.get('/stories/folders', (req, res) => {
const folders = fs.readdirSync(documentsDir).filter(f => fs.statSync(path.join(documentsDir, f)).isDirectory());
res.render('folders', { folders });
});
app.get('/stories/documents/:folder', (req, res) => {
const folderName = req.params.folder;
const metadata = getMetadata(folderName);
res.render('documents', { folder: folderName, documents: metadata.filelist });
});
app.get('/stories/stats/:folder', (req, res) => {
const folderName = req.params.folder;
const metadata = getMetadata(folderName);
const accessLogPath = path.join(documentsDir, 'access_log.txt');
const accessLog = fs.existsSync(accessLogPath) ? fs.readFileSync(accessLogPath, 'utf8').split('\n') : [];
res.render('stats', { folder: folderName, documents: metadata.filelist, accessLog });
});
app.post('/stories/folders', (req, res) => {
const folderName = req.body.folderName;
const folderPath = path.join(documentsDir, folderName);
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath);
fs.writeFileSync(path.join(folderPath, 'new_file.txt'), ''); // Create an empty file
updateMetadata(folderName);
res.status(201).redirect('/stories/folders');
} else {
res.status(400).send('Folder already exists');
}
});
app.post('/stories/documents/:folder', (req, res) => {
const folderName = req.params.folder;
const documentName = req.body.documentName || 'new_file.txt';
const folderPath = path.join(documentsDir, folderName);
const filePath = path.join(folderPath, documentName);
if (!fs.existsSync(filePath)) {
fs.writeFileSync(filePath, 'Type here');
updateMetadata(folderName);
res.status(201).redirect(`/stories/documents/${folderName}`);
} else {
res.status(400).send('Document already exists');
}
});
app.delete('/stories/folders/:folder', (req, res) => {
const folderName = req.params.folder;
const folderPath = path.join(documentsDir, folderName);
if (fs.existsSync(folderPath)) {
fs.rmdirSync(folderPath, { recursive: true });
res.status(200).redirect('/stories/folders');
} else {
res.status(404).send('Folder not found');
}
});
app.delete('/stories/documents/:folder/:document', (req, res) => {
const folderName = req.params.folder;
const documentName = req.params.document;
const folderPath = path.join(documentsDir, folderName);
const filePath = path.join(folderPath, documentName);
const metadata = getMetadata(folderName);
if (metadata.filelist.length > 1) {
fs.unlinkSync(filePath);
updateMetadata(folderName);
res.status(200).redirect(`/stories/documents/${folderName}`);
} else {
res.status(400).send('Cannot delete the last document in the folder');
}
});
app.get('/stories/document/:folder/:document', (req, res) => {
const folderName = req.params.folder;
const documentName = req.params.document;
const filePath = path.join(documentsDir, folderName, documentName);
const content = fs.readFileSync(filePath, 'utf8');
res.json({ title: documentName, content: content });
});
app.post('/stories/document/:folder/:document', (req, res) => {
const folderName = req.params.folder;
const documentName = req.params.document;
const content = req.body.content;
const filePath = path.join(documentsDir, folderName, documentName);
fs.writeFileSync(filePath, content);
res.status(200).send('Document updated');
});
// Start server
const PORT = 4000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));