Skip to content

Commit 53506a5

Browse files
committed
pasta API
1 parent c2544a7 commit 53506a5

File tree

4 files changed

+25
-15
lines changed

4 files changed

+25
-15
lines changed

database/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
const { createCommandDB } = require('./commands');
22
const { createServerDB } = require('./server');
33
const { useSQLDB, waitSQLClose } = require('./sql');
4+
const { loadPasta, savePasta } = require('./pasta');
45

56
class Database {
67
constructor(parent) {
78
this.commands = createCommandDB(parent);
89

10+
this.pasta = {
11+
load: loadPasta,
12+
save: savePasta,
13+
};
14+
915
// references to db runtime are stored here,
1016
// so flushing the require cache has no unintended consequences
1117

database/pasta.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
// check bytes
21
const fs = require('fs').promises;
32
const path = require('path');
4-
const { commandHash } = require('../commands');
3+
const { commandHash } = require('./commands');
54

65
function getFilename(commandName, name) {
76
const namespace = commandHash(commandName);
@@ -11,15 +10,15 @@ function getFilename(commandName, name) {
1110

1211
async function loadPasta(commandName, pastaName) {
1312
const filename = getFilename(commandName, pastaName);
14-
if (!await fs.exists(filename)) return;
13+
if (!await fs.access(filename)) return;
1514
return await fs.readFile(filename, 'utf8');
1615
}
1716
async function savePasta(commandName, pastaName, content) {
1817
const filename = getFilename(commandName, pastaName);
1918
if (typeof content !== 'string') throw new Error('content must be a string');
2019
if (!content.length) throw new Error('string cannot be length zero');
2120
if (content.length > 1048576) throw new Error('paste size limit is 1MB');
22-
return await writeFile(filename);
21+
return await fs.writeFile(filename, content, 'utf8');
2322
}
2423

2524
module.exports = { loadPasta, savePasta };

web/server/pasta/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
</head>
3232
<body>
3333
<iframe sandbox="allow-downloads alllow-forms allow-scripts"></iframe>
34-
<script id="content" type="text/plain">--content--</script>
34+
<script id="content" type="text/plain">----content----</script>
3535
<script>
3636
const content = atob(document.getElementById('content').textContent);
3737
const url = URL.createObjectURL(new Blob([content]));

web/server/pasta/index.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@ const { readFileSync } = require('fs');
33
module.exports = ({ app, parent }) => {
44

55
const htmlTemplate = readFileSync(__dirname + '/index.html', 'utf8');
6+
const { pasta } = parent.database;
67

7-
app.get('/html/:cmd/:name', (_req, res) => {
8-
// handle 404
9-
res.send();
10-
11-
});
12-
13-
// IRC.copypasta
14-
app.get('/text/:cmd/:name', (_req, res) => {
15-
res.send();
16-
8+
app.get('/:type(html|text)/:cmd/:name', (req, res) => {
9+
const { cmd, name, type } = req.params;
10+
pasta.load(cmd, name)
11+
.then(data => {
12+
if (type === 'html') {
13+
res.send(htmlTemplate.replace(/----content----/, data));
14+
} else {
15+
res.type('text');
16+
res.send(data);
17+
}
18+
})
19+
.catch(err => {
20+
res.sendStatus(404);
21+
});
1722
});
1823
};

0 commit comments

Comments
 (0)