-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
178 lines (161 loc) · 4.98 KB
/
Copy pathserver.js
File metadata and controls
178 lines (161 loc) · 4.98 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const express = require('express');
const fs = require('fs');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
const DATA_DIR = path.join(__dirname, 'data');
const DOCUMENTS_DIR = path.join(DATA_DIR, 'documents');
const SETTINGS_FILE = path.join(DATA_DIR, 'settings.json');
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
const DEFAULT_SETTINGS = {
theme: 'study',
textWidth: 60,
font: "'Crimson Pro', Georgia, serif",
fontSize: 18,
lineSpacing: 1.6,
distractionFree: false,
spellCheck: false,
typewriterSounds: false,
typewriterVolume: 0.7,
showTableOfContents: false,
};
function ensureDataDir() {
if (!fs.existsSync(DATA_DIR)) {
fs.mkdirSync(DATA_DIR, { recursive: true });
}
if (!fs.existsSync(DOCUMENTS_DIR)) {
fs.mkdirSync(DOCUMENTS_DIR, { recursive: true });
}
}
function filePath(name) {
let safe = path.basename(name).replace(/[^\p{L}\p{N}._-]/gu, '');
if (!safe) safe = 'untitled';
if (!safe.toLowerCase().endsWith('.md')) safe += '.md';
return path.join(DOCUMENTS_DIR, safe);
}
app.get('/api/files', (req, res) => {
ensureDataDir();
try {
const files = fs.readdirSync(DOCUMENTS_DIR)
.filter(f => f.endsWith('.md'))
.map(f => ({
name: f,
path: f,
size: fs.statSync(path.join(DOCUMENTS_DIR, f)).size,
modified: fs.statSync(path.join(DOCUMENTS_DIR, f)).mtime,
}));
res.json(files);
} catch (err) {
console.error('Failed to list files:', err);
res.status(500).json({ error: 'Failed to list files' });
}
});
app.get('/api/files/:name', (req, res) => {
ensureDataDir();
const fp = filePath(req.params.name);
try {
if (!fs.existsSync(fp)) {
return res.status(404).json({ error: 'File not found' });
}
const content = fs.readFileSync(fp, 'utf-8');
res.json({ name: path.basename(fp), content });
} catch (err) {
console.error('Failed to read file:', err);
res.status(500).json({ error: 'Failed to read file' });
}
});
app.post('/api/files', (req, res) => {
ensureDataDir();
const { name, content } = req.body;
if (!name) return res.status(400).json({ error: 'Name is required' });
const fp = filePath(name);
if (fs.existsSync(fp)) {
return res.status(409).json({ error: 'File already exists' });
}
try {
fs.writeFileSync(fp, content || '', 'utf-8');
res.status(201).json({ name: path.basename(fp), path: path.basename(fp) });
} catch (err) {
console.error('Failed to create file:', err);
res.status(500).json({ error: 'Failed to create file' });
}
});
app.put('/api/files/:name', (req, res) => {
ensureDataDir();
const fp = filePath(req.params.name);
const { content } = req.body;
if (content === undefined) return res.status(400).json({ error: 'Content is required' });
try {
fs.writeFileSync(fp, content, 'utf-8');
res.json({ name: path.basename(fp), saved: true });
} catch (err) {
console.error('Failed to save file:', err);
res.status(500).json({ error: 'Failed to save file' });
}
});
app.delete('/api/files/:name', (req, res) => {
ensureDataDir();
const fp = filePath(req.params.name);
try {
if (!fs.existsSync(fp)) {
return res.status(404).json({ error: 'File not found' });
}
fs.unlinkSync(fp);
res.json({ name: path.basename(fp), deleted: true });
} catch (err) {
console.error('Failed to delete file:', err);
res.status(500).json({ error: 'Failed to delete file' });
}
});
app.get('/api/settings', (req, res) => {
ensureDataDir();
try {
if (!fs.existsSync(SETTINGS_FILE)) {
return res.json(DEFAULT_SETTINGS);
}
const settings = JSON.parse(fs.readFileSync(SETTINGS_FILE, 'utf-8'));
res.json({ ...DEFAULT_SETTINGS, ...settings });
} catch (err) {
console.error('Failed to read settings:', err);
res.json(DEFAULT_SETTINGS);
}
});
const SETTINGS_TYPES = {
theme: 'string',
textWidth: 'number',
font: 'string',
fontSize: 'number',
lineSpacing: 'number',
distractionFree: 'boolean',
spellCheck: 'boolean',
typewriterSounds: 'boolean',
typewriterVolume: 'number',
showTableOfContents: 'boolean',
};
app.put('/api/settings', (req, res) => {
ensureDataDir();
try {
const current = fs.existsSync(SETTINGS_FILE)
? JSON.parse(fs.readFileSync(SETTINGS_FILE, 'utf-8'))
: {};
const validated = {};
for (const [key, expectedType] of Object.entries(SETTINGS_TYPES)) {
if (req.body[key] !== undefined) {
if (typeof req.body[key] === expectedType) {
validated[key] = req.body[key];
}
}
}
const updated = { ...DEFAULT_SETTINGS, ...current, ...validated };
fs.writeFileSync(SETTINGS_FILE, JSON.stringify(updated, null, 2), 'utf-8');
res.json(updated);
} catch (err) {
console.error('Failed to save settings:', err);
res.status(500).json({ error: 'Failed to save settings' });
}
});
ensureDataDir();
app.listen(PORT, () => {
console.log(`Scriptorium running at http://localhost:${PORT}`);
});