-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbytebin.js
More file actions
36 lines (27 loc) · 961 Bytes
/
bytebin.js
File metadata and controls
36 lines (27 loc) · 961 Bytes
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
import fs from 'fs'
const binDir = '/data/bytebin.org/bins'
const maxBins = 10000
export default {
create: r => {
if (fs.readdirSync(binDir).length >= maxBins) {
r.return(503, 'Service Unavailable\n')
return
}
const binCode = Math.floor(0x100000 + Math.random() * 0xefffff).toString(16).toUpperCase()
fs.writeFileSync(`${binDir}/${binCode}`, r.requestBuffer)
r.headersOut['Content-Type'] = 'application/json'
r.headersOut['Location'] = `https://bytebin.org/${binCode}`
r.return(201, `{\n "bin": "${binCode}",\n "url": "https://bytebin.org/${binCode}"\n}\n`)
},
read: r => {
const binPath = `${binDir}/${r.uri.slice(1).toUpperCase()}`
if (!fs.existsSync(binPath)) {
r.return(404, 'Bin not found!\n')
return
}
const binBuffer = fs.readFileSync(binPath)
fs.unlinkSync(binPath)
r.headersOut['Content-Type'] = 'application/octet-stream'
r.return(200, binBuffer)
},
}