From 183789b1d5e9ee9d8906a29eb3fde3f651a8c29c Mon Sep 17 00:00:00 2001 From: bobzel Date: Tue, 3 Jan 2023 10:58:56 -0500 Subject: fixed occasional errors loading images --- src/client/util/request-image-size.js | 77 ----------------------------------- src/client/util/request-image-size.ts | 73 +++++++++++++++++++++++++++++++++ src/server/DashUploadUtils.ts | 4 +- 3 files changed, 76 insertions(+), 78 deletions(-) delete mode 100644 src/client/util/request-image-size.js create mode 100644 src/client/util/request-image-size.ts (limited to 'src') diff --git a/src/client/util/request-image-size.js b/src/client/util/request-image-size.js deleted file mode 100644 index 502e0fbac..000000000 --- a/src/client/util/request-image-size.js +++ /dev/null @@ -1,77 +0,0 @@ -/** - * request-image-size: Detect image dimensions via request. - * Licensed under the MIT license. - * - * https://github.com/FdezRomero/request-image-size - * © 2017 Rodrigo Fernández Romero - * - * Based on the work of Johannes J. Schmidt - * https://github.com/jo/http-image-size - */ - -const request = require('request'); -const imageSize = require('image-size'); -const HttpError = require('standard-http-error'); - -module.exports = function requestImageSize(options) { - let opts = { - encoding: null, - }; - - if (options && typeof options === 'object') { - opts = Object.assign(options, opts); - } else if (options && typeof options === 'string') { - opts = Object.assign( - { - uri: options, - }, - opts - ); - } else { - return Promise.reject(new Error('You should provide an URI string or a "request" options object.')); - } - - opts.encoding = null; - - return new Promise((resolve, reject) => { - const req = request(opts); - - req.on('response', res => { - if (res.statusCode >= 400) { - return reject(new HttpError(res.statusCode, res.statusMessage)); - } - - let buffer = Buffer.from([]); - let size; - - res.on('data', chunk => { - buffer = Buffer.concat([buffer, chunk]); - - try { - size = imageSize(buffer); - } catch (err) { - reject(err); - return req.abort(); - } - - if (size) { - resolve(size); - return req.abort(); - } - }); - - res.on('error', reject); - - res.on('end', () => { - if (!size) { - return reject(new Error('Image has no size')); - } - - size.downloaded = buffer.length; - return resolve(size); - }); - }); - - req.on('error', reject); - }); -}; diff --git a/src/client/util/request-image-size.ts b/src/client/util/request-image-size.ts new file mode 100644 index 000000000..57e8516ac --- /dev/null +++ b/src/client/util/request-image-size.ts @@ -0,0 +1,73 @@ +/** + * request-image-size: Detect image dimensions via request. + * Licensed under the MIT license. + * + * https://github.com/FdezRomero/request-image-size + * © 2017 Rodrigo Fernández Romero + * + * Based on the work of Johannes J. Schmidt + * https://github.com/jo/http-image-size + */ + +const request = require('request'); +const imageSize = require('image-size'); +const HttpError = require('standard-http-error'); + +module.exports = function requestImageSize(options: any) { + let opts = { + encoding: null, + }; + + if (options && typeof options === 'object') { + opts = Object.assign(options, opts); + } else if (options && typeof options === 'string') { + opts = Object.assign( + { + uri: options, + }, + opts + ); + } else { + return Promise.reject(new Error('You should provide an URI string or a "request" options object.')); + } + + opts.encoding = null; + + return new Promise((resolve, reject) => { + const req = request(opts); + + req.on('response', (res: any) => { + if (res.statusCode >= 400) { + return reject(new HttpError(res.statusCode, res.statusMessage)); + } + + let buffer = Buffer.from([]); + let size: any; + + res.on('data', (chunk: any) => { + buffer = Buffer.concat([buffer, chunk]); + + try { + size = imageSize(buffer); + if (size) { + resolve(size); + return req.abort(); + } + } catch (err) {} + }); + + res.on('error', reject); + + res.on('end', () => { + if (!size) { + return reject(new Error('Image has no size')); + } + + size.downloaded = buffer.length; + return resolve(size); + }); + }); + + req.on('error', reject); + }); +}; diff --git a/src/server/DashUploadUtils.ts b/src/server/DashUploadUtils.ts index 4870d218b..33809824f 100644 --- a/src/server/DashUploadUtils.ts +++ b/src/server/DashUploadUtils.ts @@ -398,7 +398,9 @@ export namespace DashUploadUtils { // Use the request library to parse out file level image information in the headers const { headers } = await new Promise((resolve, reject) => { return request.head(resolvedUrl, (error, res) => (error ? reject(error) : resolve(res))); - }).catch(e => console.log(e)); + }).catch(e => { + console.log('Error processing headers: ', e); + }); try { // Compute the native width and height ofthe image with an npm module -- cgit v1.2.3-70-g09d2