-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
138 lines (112 loc) · 4.17 KB
/
index.js
File metadata and controls
138 lines (112 loc) · 4.17 KB
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
const express = require('express');
const fs = require('fs');
const app = express();
const path = require('path');
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));
app.set('view engine', 'ejs');
const filesDirectory = path.join(__dirname, 'files');
if (!fs.existsSync(filesDirectory)) {
fs.mkdirSync(filesDirectory, { recursive: true });
}
app.get('/', (req, res) => {
fs.readdir(filesDirectory, (err, files) => {
if (err) {
console.error('Error reading files directory:', err);
return res.render('index', {
files: [],
error: 'Error loading files',
success: req.query.success || null
});
}
const textFiles = files.filter(file => file.endsWith('.txt'));
const fileContents = [];
textFiles.forEach(file => {
try {
const content = fs.readFileSync(path.join(filesDirectory, file), 'utf-8');
fileContents.push({
name: file.replace('.txt', ''),
content: content
});
} catch (readErr) {
console.error(`Error reading file ${file}:`, readErr);
}
});
res.render('index', {
files: fileContents,
error: req.query.error || null,
success: req.query.success || null
});
});
});
app.post('/bolimaga', (req, res) => {
const { url, details } = req.body;
if (!url || !details) {
return res.redirect('/?error=Please fill all fields');
}
const fileName = url.split(' ').join('') + '.txt';
const filePath = path.join(filesDirectory, fileName);
fs.writeFile(filePath, details, (err) => {
if (err) {
console.error('Error writing file:', err);
return res.redirect('/?error=Error saving file');
}
res.redirect('/?success=File saved successfully');
});
});
app.get('/read/:fileName', (req, res) => {
const fileName = req.params.fileName;
const filePath = path.join(filesDirectory, fileName + '.txt');
fs.readFile(filePath, 'utf-8', (err, content) => {
if (err) {
console.error('Error reading file:', err);
return res.redirect('/?error=File not found');
}
res.render('read', {
fileName: fileName,
fileContent: content
});
});
});
app.get('/rename/:fileName', (req, res) => {
const fileName = req.params.fileName;
const filePath = path.join(filesDirectory, fileName + '.txt');
fs.access(filePath, fs.constants.F_OK, (err) => {
if (err) {
return res.redirect('/?error=File not found');
}
res.render('rename', {
oldFileName: fileName,
error: req.query.error || null
});
});
});
app.post('/rename/:fileName', (req, res) => {
const oldFileName = req.params.fileName;
const newFileName = req.body.newName;
if (!newFileName || newFileName.trim() === '') {
return res.redirect(`/rename/${oldFileName}?error=Please enter a new file name`);
}
if (newFileName === oldFileName) {
return res.redirect(`/rename/${oldFileName}?error=New name must be different from current name`);
}
const oldFilePath = path.join(filesDirectory, oldFileName + '.txt');
const newFilePath = path.join(filesDirectory, newFileName + '.txt');
fs.access(newFilePath, fs.constants.F_OK, (err) => {
if (!err) {
return res.redirect(`/rename/${oldFileName}?error=File with this name already exists`);
}
fs.rename(oldFilePath, newFilePath, (err) => {
if (err) {
console.error('Error renaming file:', err);
return res.redirect(`/rename/${oldFileName}?error=Error renaming file`);
}
res.redirect('/?success=File renamed successfully');
});
});
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});