UNPKG

nurlresolver

Version:
96 lines 3.56 kB
import { parse } from "content-disposition"; import { BaseUrlResolver } from "../BaseResolver.js"; const tryParseContentLengthFromRangeHeader = (h) => { if (h) { return parseInt(h.split('/').pop() || '0'); } }; export class cloudFlareStorageResolver extends BaseUrlResolver { fn1 = async (_urlToResolve) => { const purl = new URL(_urlToResolve); purl.searchParams.delete('x-nu-org'); const newur = purl.href; const rs = await this.gotInstance(newur, { headers: { Range: 'bytes=0-0' } }); const title = this.extractFileNameFromUrl(_urlToResolve); return { link: newur, title, isPlayable: true, parent: _urlToResolve, size: tryParseContentLengthFromRangeHeader(rs.headers['content-range']), lastModified: rs.headers['last-modified'], contentType: rs.headers['content-type'] }; }; constructor() { super({ domains: [/https?:\/\/\w+\.r2\.cloudflarestorage\.com/], speedRank: 95 }); } async resolveInner(_urlToResolve) { try { return await this.fn1(_urlToResolve); } catch { const xnr = new URL(_urlToResolve).searchParams.get('x-nu-org'); if (xnr) { const nlinks = await this._context?.resolve(xnr); const whichIsResolvable = nlinks?.find(x => this.canResolve(x.link)); //this will do the trick as long as there's only one cloudflarestorage link in that page. if (whichIsResolvable) { return this.fn1(whichIsResolvable.link); } } } return []; } async fillMetaInfo() { ////empty one } } export class pixeldraStorageResolver extends BaseUrlResolver { fn1 = async (_urlToResolve) => { const purl = new URL(_urlToResolve); purl.searchParams.delete('x-nu-org'); const newur = purl.href; const rs = await this.gotInstance.head(newur); const title = parse(rs.headers['content-disposition'] || '').parameters.filename || this.extractFileNameFromUrl(_urlToResolve); return { link: newur, title, isPlayable: true, parent: _urlToResolve, size: rs.headers["content-length"] && parseInt(rs.headers["content-length"]), lastModified: rs.headers['last-modified'], contentType: rs.headers['content-type'] }; }; constructor() { super({ domains: [/https?:\/\/pixeldra/], speedRank: 95 }); } async resolveInner(_urlToResolve) { try { const urlInstance = new URL(_urlToResolve); const id = urlInstance.pathname.split('/').at(-1); return await this.fn1(`${urlInstance.origin}/api/file/${id}`); } catch { const xnr = new URL(_urlToResolve).searchParams.get('x-nu-org'); if (xnr) { const nlinks = await this._context?.resolve(xnr); const whichIsResolvable = nlinks?.find(x => this.canResolve(x.link)); //this will do the trick as long as there's only one cloudflarestorage link in that page. if (whichIsResolvable) { return this.fn1(whichIsResolvable.link); } } } return []; } async fillMetaInfo() { ////empty one } } //# sourceMappingURL=cloudFlareStorage.js.map