Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
99 lines (86 loc) · 3.08 KB
/
Copy pathserver.js
File metadata and controls
99 lines (86 loc) · 3.08 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
const express = require('express');
const fs = require('fs').promises;
const fsSync = require('fs');
const path = require('path');
const os = require('os');
const { exec } = require('child_process');
const app = express();
const port = 3300;
const host = '0.0.0.0';
const dataFile = getOrCreateFilePath();
app.use(express.json());
app.use(express.static('public'));
// Route to send all data
app.get('/data', async (req, res) => {
try {
const data = await fs.readFile(dataFile, 'utf8');
res.json(JSON.parse(data));
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Error reading data' });
}
});
// Route to add a new entry
app.post('/newentry', async (req, res) => {
const { title, pdfPath, page } = req.body; // Get data from request body
const newEntry = {
title: title,
pdfPath: decodeURIComponent(pdfPath), // Decode the escaped path
page: parseInt(page, 10)
};
try {
const data = await fs.readFile(dataFile, 'utf8');
const jsonData = JSON.parse(data);
jsonData.pieces.push(newEntry); // Add new entry to pieces
await fs.writeFile(dataFile, JSON.stringify(jsonData, null, 2)); // Save updated data
res.json({ message: 'Entry added successfully', entry: newEntry });
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Error updating data' });
}
});
// Route to open PDF with Okular
app.get('/open-pdf/:file/:page', (req, res) => {
const { file, page } = req.params;
const command = `okular --unique -p ${page} "${file}"`;
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error executing Okular: ${error}`);
return res.status(500).send('Error opening PDF');
}
res.send('PDF opened successfully');
});
});
app.delete('/delete/:index', async (req, res) => {
const index = parseInt(req.params.index);
try {
const data = await fs.readFile(dataFile, 'utf8');
const jsonData = JSON.parse(data);
if (index >= 0 && index < jsonData.pieces.length) {
jsonData.pieces.splice(index, 1);
await fs.writeFile(dataFile, JSON.stringify(jsonData, null, 2));
res.json({ message: 'Entry deleted successfully' });
} else {
res.status(400).json({ error: 'Invalid index' });
}
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Error deleting entry' });
}
});
app.listen(port, host, () => {
console.log(`Server running on http://${host}:${port}`);
});
function getOrCreateFilePath() {
const filePath = path.join(os.homedir(), '.config', 'realbook-indexer', 'musicnotes.json');
const initialContent = JSON.stringify({ pieces: [] }, null, 2);
if (!fsSync.existsSync(path.dirname(filePath))) {
fsSync.mkdirSync(path.dirname(filePath), { recursive: true });
}
if (!fsSync.existsSync(filePath)) {
fsSync.writeFileSync(filePath, initialContent);
console.log('Datei erfolgreich erstellt');
}
console.log(filePath);
return filePath;
}