-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (54 loc) · 1.55 KB
/
index.js
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
const IPFSApi = require('ipfs-api')
const ipfs = IPFSApi('localhost', '5001')
const app = require('express')()
const os = require('os')
const mkdirp = require('mkdirp')
const exec = require('child_process').exec
const tmpdir = os.tmpdir() + '/stay-node'
mkdirp.sync(tmpdir)
const SIZE_LIMIT = 10000000
// Empty PEER_WHITELIST means everyone is allowed to pin
const PEER_WHITELIST = []
const log = (hash, msg) => {
console.log(hash + ' > ' + msg)
}
const sendStatus = (res, code) => {
res.status(code).send(code.toString())
}
app.get('/health', (req, res) => {
sendStatus(res, 200)
})
// 1. Check size of hash contents
// TODO: Check that package.json exists
// 2. Pin contents
// 3. Done!
app.post('/api/pin/add/:hash/:peer_id', (req, res) => {
const hash = req.params.hash
const peer_id = req.params.peer_id
if (!hash || !peer_id) {
log('None', 'No hash and/or peer_id provided')
sendStatus(res, 400)
return
}
if (PEER_WHITELIST.length > 0 && PEER_WHITELIST.indexOf(peer_id) === -1) {
log(hash, 'Peer not allowed to pin content')
sendStatus(res, 403)
return
}
log(hash, 'Checking size of hash...')
ipfs.object.stat(hash, (err, statRes) => {
if (err) throw new Error(err)
const hash_size = statRes.CumulativeSize
if (hash_size > SIZE_LIMIT) {
sendStatus(res, 413)
} else {
log(hash, 'Size is ok ('+hash_size+')... Pinning')
ipfs.pin.add(hash, (err, pinRes) => {
if (err) throw new Error(err)
log(hash, 'Pinned!')
sendStatus(res, 200)
})
}
})
})
app.listen(9000)