Skip to content

Commit

Permalink
fix(pixiv): download image failed due to early-closed stream (#181)
Browse files Browse the repository at this point in the history
* fix(pixiv): download image failed due to octet-stream content-type

* fix: redirect all headers

* fix: remove content -length

* fix: get correct self url

* fix: migrate to arraybuffer temporarily

* fix format

---------

Co-authored-by: SaarChaffee <[email protected]>
  • Loading branch information
MaikoTan and SaarChaffee authored Mar 23, 2024
1 parent dff7a9c commit 2547909
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions packages/pixiv/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
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'
Expand Down Expand Up @@ -47,15 +46,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<ReadableStream>(decrypted, {
const file = await this.http<ArrayBuffer>(decrypted, {
headers: { Referer: 'https://www.pixiv.net/' },
responseType: 'stream',
responseType: 'arraybuffer',
})
ctx.set('Content-Type', file.headers['content-type'])
ctx.set('Cache-Control', 'public, max-age=31536000')
ctx.set(Object.fromEntries(file.headers.entries()))
ctx.remove('Content-Length')
ctx.response.status = file.status
ctx.response.message = file.statusText
ctx.body = Readable.fromWeb(file.data)
ctx.body = Buffer.from(file.data)
return next()
})
}
Expand Down Expand Up @@ -204,7 +203,9 @@ class PixivImageSource extends ImageSource<PixivImageSource.Config> {
return url.replace(/^https?:\/\/i\.pximg\.net/, trimSlash(proxy))
} else if (this.config.bypassMethod === 'route' && this.config.route && this.ctx.get('server')) {
const encrypted = Cipher.encrypt(url, this.config.aesKey)
return trimSlash(this.ctx.server.selfUrl) + trimSlash(this.config.route) + '/' + encodeURIComponent(encrypted)
return (
trimSlash(this.ctx.server.config.selfUrl) + trimSlash(this.config.route) + '/' + encodeURIComponent(encrypted)
)
} else if (this.config.bypassMethod === 'asset' && this.ctx.get('assets')) {
const filename = url.split('/').pop().split('?')[0]
const file = await this.http<ArrayBuffer>(url, { headers: { Referer: 'https://www.pixiv.net/' } })
Expand Down

0 comments on commit 2547909

Please sign in to comment.