1
0
mirror of https://github.com/oceanprotocol/commons.git synced 2023-03-15 18:03:00 +01:00

add content-range

This commit is contained in:
Matthias Kretschmann 2019-09-09 22:22:25 +02:00
parent 2f741d300e
commit da357b82c2
Signed by: m
GPG Key ID: 606EEEF3C479A91F

View File

@ -32,34 +32,45 @@ export class UrlCheckRouter {
headers: { Range: 'bytes=0-' } headers: { Range: 'bytes=0-' }
}, },
(error, response) => { (error, response) => {
if ( const { headers, statusCode } = response
response && const successResponses =
(response.statusCode.toString().startsWith('2') || statusCode.toString().startsWith('2') ||
response.statusCode.toString().startsWith('416')) statusCode.toString().startsWith('416')
) {
if (response && successResponses) {
const result: any = {} const result: any = {}
result.found = true result.found = true
if (response.headers['content-length']) { if (headers['content-length']) {
result.contentLength = parseInt( result.contentLength = parseInt(
response.headers['content-length'] headers['content-length']
) // convert to number ) // convert to number
} }
if (response.headers['content-type']) { // sometimes servers send content-range header,
const typeAndCharset = response.headers[ // try to use it if content-length is not present
'content-type' if (
].split(';') headers['content-range'] &&
!headers['content-length']
) {
const size = headers['content-range'].split('/')[1]
result.contentLength = parseInt(size) // convert to number
}
result.contentType = typeAndCharset[0] // eslint-disable-line prefer-destructuring if (headers['content-type']) {
const typeAndCharset = headers['content-type'].split(
';'
)
/* eslint-disable prefer-destructuring */
result.contentType = typeAndCharset[0]
if (typeAndCharset[1]) { if (typeAndCharset[1]) {
/* eslint-disable prefer-destructuring */
result.contentCharset = typeAndCharset[1].split( result.contentCharset = typeAndCharset[1].split(
'=' '='
)[1] )[1]
/* eslint-enable prefer-destructuring */
} }
/* eslint-enable prefer-destructuring */
} }
return res.send({ status: 'success', result }) return res.send({ status: 'success', result })
} }