Skip to content

Commit

Permalink
feat: add support tobs image path
Browse files Browse the repository at this point in the history
  • Loading branch information
lgou2w committed Mar 8, 2022
1 parent 3ac41f1 commit 2b228a8
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,27 +299,23 @@ export class PicaComicAPI {
.catch(catchError)
}

stringifyImageUrl (image: { fileServer: string, path: string }): string {
const { path, fileServer } = image
const url = new URL(
`${fileServer.replace(/\/$/, '')}/static/${path.replace(/^\//, '')}`
)

if (url.pathname.startsWith('/static/tobeimg')) {
url.host = 'img.picacomic.com'
url.pathname = url.pathname.replace('/static/tobeimg', '')
return url.href
}

if (url.pathname.startsWith('/static/static')) {
url.host = 'storage1.picacomic.com'
url.pathname = url.pathname.replace('/static/static', '/static')
stringifyImageUrl (image: { path: string, fileServer?: string }): string {
let { path, fileServer } = image

if (path.startsWith('tobeimg/')) {
fileServer = fileServer || 'https://img.picacomic.com'
path = '/' + path.substring(8)
} else if (path.startsWith('tobs/')) {
fileServer = fileServer || 'https://storage-b.picacomic.com'
path = '/static/' + path.substring(5)
} else {
fileServer = fileServer || 'https://storage1.picacomic.com'
path = '/static/' + path
}

return url.href
return fileServer.replace(/\/$/, '') + path
}

async fetchImage (image: { fileServer: string, path: string }): Promise<Duplex> {
async fetchImage (image: { path: string, fileServer?: string }): Promise<Duplex> {
const url = this.stringifyImageUrl(image)
return this.fetch.stream({ prefixUrl: '', url, context: { fetchImage: true } })
}
Expand Down

0 comments on commit 2b228a8

Please sign in to comment.