diff options
| author | A.J. Shulman <Shulman.aj@gmail.com> | 2024-09-05 11:40:24 -0400 | 
|---|---|---|
| committer | A.J. Shulman <Shulman.aj@gmail.com> | 2024-09-05 11:40:24 -0400 | 
| commit | c1053475810a1b1b3a9963c3f1ef0b1a9509d222 (patch) | |
| tree | 41da9f26592ba7f189f8d00536435182e38272da /src/client/util/request-image-size.ts | |
| parent | 785e55141cab178a761080f5c99384bb19855969 (diff) | |
| parent | 9014cc474a039f0daef6cc0ee2011329da7703ac (diff) | |
merged with master
Diffstat (limited to 'src/client/util/request-image-size.ts')
| -rw-r--r-- | src/client/util/request-image-size.ts | 45 | 
1 files changed, 16 insertions, 29 deletions
diff --git a/src/client/util/request-image-size.ts b/src/client/util/request-image-size.ts index 48cb6e3a5..c619192ed 100644 --- a/src/client/util/request-image-size.ts +++ b/src/client/util/request-image-size.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-var-requires */  /**   * request-image-size: Detect image dimensions via request.   * Licensed under the MIT license. @@ -9,43 +10,36 @@   * https://github.com/jo/http-image-size   */ -const request = require('request'); -const imageSize = require('image-size'); +// const imageSize = require('image-size');  const HttpError = require('standard-http-error'); +import * as request from 'request'; +import { imageSize } from 'image-size'; +import { ISizeCalculationResult } from 'image-size/dist/types/interface'; -module.exports = function requestImageSize(options: any) { -    let opts: any = { -        encoding: null, -    }; - -    if (options && typeof options === 'object') { -        opts = Object.assign(options, opts); -    } else if (options && typeof options === 'string') { -        opts = { -            uri: options, -            ...opts, -        }; -    } else { +module.exports = function requestImageSize(url: string) { +    if (!url) {          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); +        const req = request(url); -        req.on('response', (res: any) => { +        req.on('response', res => {              if (res.statusCode >= 400) {                  reject(new HttpError(res.statusCode, res.statusMessage));                  return;              }              let buffer = Buffer.from([]); -            let size: any; +            let size: ISizeCalculationResult; -            res.on('data', (chunk: any) => { +            res.on('data', chunk => {                  buffer = Buffer.concat([buffer, chunk]); +            }); +            res.on('error', reject); + +            res.on('end', () => {                  try {                      size = imageSize(buffer);                      if (size) { @@ -54,19 +48,12 @@ module.exports = function requestImageSize(options: any) {                      }                  } catch (err) {                      /* empty */ -                    console.log("Error: ", err) +                    console.log('Error: ', err);                  } -            }); - -            res.on('error', reject); - -            res.on('end', () => {                  if (!size) {                      reject(new Error('Image has no size'));                      return;                  } - -                size.downloaded = buffer.length;                  resolve(size);              });          });  | 
