2022-07-06 07:06:53 -04:00
|
|
|
import { fallbackRequest, fetchRange } from './pdf-caching'
|
|
|
|
import getMeta from '../../../utils/meta'
|
|
|
|
import { captureException } from '../../../infrastructure/error-reporter'
|
2022-07-20 04:15:41 -04:00
|
|
|
import { getPdfCachingMetrics } from './metrics'
|
2022-07-06 07:06:53 -04:00
|
|
|
|
|
|
|
export function generatePdfCachingTransportFactory(PDFJS) {
|
2022-07-08 04:15:13 -04:00
|
|
|
if (getMeta('ol-pdfCachingMode') !== 'enabled') {
|
2022-07-06 07:06:53 -04:00
|
|
|
return () => null
|
|
|
|
}
|
|
|
|
const cached = new Set()
|
2022-07-20 04:15:41 -04:00
|
|
|
const metrics = Object.assign(getPdfCachingMetrics(), {
|
2022-07-06 07:06:53 -04:00
|
|
|
failedCount: 0,
|
|
|
|
tooLargeOverheadCount: 0,
|
|
|
|
tooManyRequestsCount: 0,
|
|
|
|
cachedCount: 0,
|
|
|
|
cachedBytes: 0,
|
|
|
|
fetchedCount: 0,
|
|
|
|
fetchedBytes: 0,
|
|
|
|
requestedCount: 0,
|
|
|
|
requestedBytes: 0,
|
2022-07-20 04:15:41 -04:00
|
|
|
})
|
2022-07-08 04:15:13 -04:00
|
|
|
const verifyChunks =
|
|
|
|
new URLSearchParams(window.location.search).get('verify_chunks') === 'true'
|
2022-07-06 07:06:53 -04:00
|
|
|
|
|
|
|
class PDFDataRangeTransport extends PDFJS.PDFDataRangeTransport {
|
|
|
|
constructor(url, pdfFile, reject) {
|
|
|
|
super(pdfFile.size, new Uint8Array())
|
|
|
|
this.url = url
|
|
|
|
this.pdfFile = pdfFile
|
|
|
|
this.reject = reject
|
|
|
|
}
|
|
|
|
|
|
|
|
requestDataRange(start, end) {
|
|
|
|
fetchRange({
|
|
|
|
url: this.url,
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
file: this.pdfFile,
|
|
|
|
metrics,
|
|
|
|
cached,
|
2022-07-08 04:15:13 -04:00
|
|
|
verifyChunks,
|
2022-07-06 07:06:53 -04:00
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
metrics.failedCount++
|
|
|
|
console.error('optimized pdf download error', err)
|
|
|
|
captureException(err)
|
|
|
|
return fallbackRequest({ url: this.url, start, end })
|
|
|
|
})
|
|
|
|
.then(blob => {
|
|
|
|
this.onDataRange(start, blob)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.error('fatal pdf download error', err)
|
|
|
|
captureException(err)
|
|
|
|
this.reject(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return function (url, pdfFile, reject) {
|
|
|
|
return new PDFDataRangeTransport(url, pdfFile, reject)
|
|
|
|
}
|
|
|
|
}
|