|
1 | | -var config = require('config'); |
2 | | -var Paste = require('../models/paste'); |
| 1 | +const config = require('config'); |
| 2 | +const Paste = require('../models/paste'); |
3 | 3 |
|
4 | 4 | module.exports = { |
5 | | - *view() { |
| 5 | + async view(ctx) { |
6 | 6 | try { |
7 | | - let paste = yield Paste.findById(this.params.id).exec(); |
8 | | - let lang = Object.keys(this.query)[0]; |
| 7 | + let paste = await Paste.findById(ctx.params.id).exec(); |
| 8 | + let lang = Object.keys(ctx.query)[0]; |
9 | 9 |
|
10 | 10 | if (lang) { |
11 | | - yield this.render('highlight', { |
| 11 | + await ctx.render('highlight', { |
12 | 12 | pretty: config.prettyHtml, |
13 | | - title: 'Paste ' + paste.id, |
| 13 | + title: config.name + ' ' + paste.id, |
14 | 14 | paste: paste.paste, |
15 | 15 | lang: lang |
16 | 16 | }); |
17 | 17 | } else { |
18 | | - this.type = 'text/plain'; |
19 | | - this.body = paste.paste; |
| 18 | + ctx.type = 'text/plain'; |
| 19 | + ctx.body = paste.paste; |
20 | 20 | } |
21 | 21 | } catch (ex) { |
22 | | - this.throw('Paste Not Found', 404); |
| 22 | + ctx.throw('Paste Not Found', 404); |
23 | 23 | } |
24 | 24 | }, |
25 | 25 |
|
26 | | - *create() { |
27 | | - if (this.request.body.fields) { |
28 | | - if (this.request.body.fields.paste) { |
29 | | - this.request.body.paste = this.request.body.fields.paste; |
| 26 | + async create(ctx) { |
| 27 | + if (ctx.request.body.fields) { |
| 28 | + if (ctx.request.body.fields.paste) { |
| 29 | + ctx.request.body.paste = ctx.request.body.fields.paste; |
30 | 30 | } |
31 | | - if (this.request.body.fields.highlight) { |
32 | | - this.request.body.highlight = this.request.body.fields.highlight; |
| 31 | + if (ctx.request.body.fields.highlight) { |
| 32 | + ctx.request.body.highlight = ctx.request.body.fields.highlight; |
33 | 33 | } |
34 | | - if (this.request.body.fields.expire) { |
35 | | - this.request.body.expire = this.request.body.fields.expire; |
| 34 | + if (ctx.request.body.fields.expire) { |
| 35 | + ctx.request.body.expire = ctx.request.body.fields.expire; |
36 | 36 | } |
37 | 37 | } |
38 | 38 |
|
39 | | - if (!this.request.body.expire) { |
40 | | - this.request.body.expire = config.expiresDefault; |
| 39 | + if (!ctx.request.body.expire) { |
| 40 | + ctx.request.body.expire = config.expiresDefault; |
41 | 41 | } |
42 | 42 |
|
43 | 43 | let paste = new Paste({ |
44 | | - paste: this.request.body.paste, |
45 | | - expiresAt: new Date(Date.now() + this.request.body.expire * 1000) |
| 44 | + paste: ctx.request.body.paste, |
| 45 | + expiresAt: new Date(Date.now() + ctx.request.body.expire * 1000) |
46 | 46 | }); |
47 | 47 |
|
48 | | - yield paste.save(); |
| 48 | + await paste.save(); |
49 | 49 |
|
50 | 50 | let link = paste.id; |
51 | 51 |
|
52 | | - if (this.request.body.highlight) { |
53 | | - link += '?' + this.request.body.highlight; |
| 52 | + if (ctx.request.body.highlight) { |
| 53 | + link += '?' + ctx.request.body.highlight; |
54 | 54 | } |
55 | 55 |
|
56 | | - if (Object.keys(this.query).includes('redirect')) { |
57 | | - this.redirect(link); |
| 56 | + if (Object.keys(ctx.query).includes('redirect')) { |
| 57 | + ctx.redirect(link); |
58 | 58 | } else { |
59 | | - this.body = this.request.origin + '/' + link + '\n'; |
| 59 | + ctx.body = ctx.request.origin + '/' + link + '\n'; |
60 | 60 | } |
61 | 61 | } |
62 | 62 | }; |
0 commit comments