Skip to content

Commit

Permalink
feat(pixiv): migrate arraybuffer to stream again (#216)
Browse files Browse the repository at this point in the history
* feat(pixiv): migrate arraybuffer to stream again

* chore: update koishi to 4.17.3
  • Loading branch information
MaikoTan authored Mar 26, 2024
1 parent f1f1b87 commit 8295cee
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/pixiv/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"peerDependencies": {
"@koishijs/assets": "^1.0.4",
"@koishijs/plugin-server": "^3.1.8",
"koishi": "^4.17.0",
"koishi": "^4.17.3",
"koishi-plugin-booru": "^1.2.0"
},
"peerDependenciesMeta": {
Expand All @@ -66,6 +66,6 @@
"devDependencies": {
"@koishijs/assets": "^1.0.4",
"@koishijs/plugin-server": "^3.1.8",
"koishi": "^4.17.0"
"koishi": "^4.17.3"
}
}
9 changes: 6 additions & 3 deletions packages/pixiv/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { createCipheriv, createDecipheriv, randomBytes } from 'node:crypto'
import { Readable } from 'node:stream'
import { ReadableStream } from 'node:stream/web'

import {} from '@koishijs/assets'
import {} from '@koishijs/plugin-server'

import { Context, Quester, Random, Schema, trimSlash } from 'koishi'
import { ImageSource } from 'koishi-plugin-booru'

Expand Down Expand Up @@ -47,15 +50,15 @@ class PixivImageSource extends ImageSource<PixivImageSource.Config> {
const url = ctx.request.url.replace(/^\/booru\/pixiv\/proxy\//, '')
const decrypted = Cipher.decrypt(decodeURIComponent(url), config.aesKey)
if (typeof decrypted !== 'string' || !decrypted.startsWith('https://i.pximg.net/')) return next()
const file = await this.http<ArrayBuffer>(decrypted, {
const file = await this.http<ReadableStream>(decrypted, {
headers: { Referer: 'https://www.pixiv.net/' },
responseType: 'arraybuffer',
responseType: 'stream',
})
ctx.set(Object.fromEntries(file.headers.entries()))
ctx.remove('Content-Length')
ctx.response.status = file.status
ctx.response.message = file.statusText
ctx.body = Buffer.from(file.data)
ctx.body = Readable.fromWeb(file.data)
return next()
})
}
Expand Down

0 comments on commit 8295cee

Please sign in to comment.