forked from reruin/sharelist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rss.js
68 lines (60 loc) · 1.6 KB
/
rss.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
62
63
64
65
66
67
68
class RSS {
constructor(app) {
this.name = 'RSS Server'
this.app = app
this.path = '/_rss'
this.start()
}
start() {
let { app, path } = this
let port = app.getConfig('port')
let sitename = app.getConfig('title')
let router = app.router().all(this.path + ':path(.*)', async (ctx, next) => {
await this.onRequest(ctx, next)
})
app.web().use(router.routes())
this.zeroconf = app.bonjour.publish({ name: `ShareList RSS(${sitename})`, type: 'http', port, txt: { path } })
}
async onRequest(ctx, next) {
let url = ctx.params.path
let resp = await this.app.command('ls', url)
if( resp ){
let k = this.createDir(resp.children)
ctx.type = 'application/rss+xml'
ctx.body = k
}else{
ctx.body = {
code:404
}
}
await next()
}
createDir(items) {
let path = ''
let body = items.filter(i => i.hidden !== true).map(i => {
let href = ((path + '/' + encodeURIComponent(i.name))).replace(/\/{2,}/g, '/')
return `<item>
<title>${i.name}</title>
<link>${href}</link>
<description>${i.name}</description>
<pubDate>${i.updated_at}</pubDate>
<enclosure url="${href}"
length="${i.size}" type="${i.type}" />
</item>`
})
return `<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
${body.join('')}
</rss>`
}
restart() {
if (this.zeroconf) {
this.zeroconf.stop(()=>{
this.start()
})
}else{
this.start()
}
}
}
module.exports = RSS