Skip to content

Commit

Permalink
v1.2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
a632079 committed Mar 4, 2018
1 parent 3c7cbfd commit 643fcab
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/controllers/hitokoto.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ async function hitokoto (ctx, next) {
const encode = ctx.query.encode
let response
let js = false
let text
switch (encode) {
case 'json':
response = Buffer.from(JSON.stringify(ret))
break
case 'text':
text = true
response = Buffer.from(ret.hitokoto)
break
case 'js':
Expand All @@ -48,13 +50,17 @@ async function hitokoto (ctx, next) {
if (ctx.query && ctx.query.charset && ctx.query.charset.toLocaleLowerCase() === 'gbk') {
if (js) {
ctx.set('Content-Type', 'text/javascript; charset=gbk')
} else if (text) {
ctx.set('Content-Type', 'text/plain; charset=gbk')
} else {
ctx.set('Content-Type', 'application/json; charset=gbk')
}
ctx.body = iconv.encode(response, 'GBK')
} else {
if (js) {
ctx.set('Content-Type', 'text/javascript; charset=utf-8')
} else if (text) {
ctx.set('Content-Type', 'text/plain; charset=utf-8')
} else {
ctx.set('Content-Type', 'application/json; charset=utf-8')
}
Expand All @@ -71,11 +77,13 @@ async function hitokoto (ctx, next) {
const encode = ctx.query.encode
let response
let js = false
let text = false
switch (encode) {
case 'json':
response = Buffer.from(JSON.stringify(ret))
break
case 'text':
text = true
response = Buffer.from(ret.hitokoto)
break
case 'js':
Expand All @@ -91,13 +99,17 @@ async function hitokoto (ctx, next) {
if (ctx.query && ctx.query.charset && ctx.query.charset.toLocaleLowerCase() === 'gbk') {
if (js) {
ctx.set('Content-Type', 'text/javascript; charset=gbk')
} else if (text) {
ctx.set('Content-Type', 'text/plain; charset=gbk')
} else {
ctx.set('Content-Type', 'application/json; charset=gbk')
}
ctx.body = iconv.encode(response, 'GBK')
} else {
if (js) {
ctx.set('Content-Type', 'text/javascript; charset=utf-8')
} else if (text) {
ctx.set('Content-Type', 'text/plain; charset=utf-8')
} else {
ctx.set('Content-Type', 'application/json; charset=utf-8')
}
Expand Down

0 comments on commit 643fcab

Please sign in to comment.