aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/request-image-size.ts
diff options
context:
space:
mode:
authoreleanor-park <eleanor_park@brown.edu>2024-08-27 16:44:12 -0400
committereleanor-park <eleanor_park@brown.edu>2024-08-27 16:44:12 -0400
commit39d2bba7bf4b0cc3759931691640083a48cce662 (patch)
tree8bf110760aa926237b6294aec545f48cfc92747d /src/client/util/request-image-size.ts
parent6f73686ec4dc3e01ae3eacc0150aa59eafea0325 (diff)
parentb8a04a0fedf8ef3612395764a0ecd01f6824ebd1 (diff)
Merge branch 'master' into eleanor-gptdraw
Diffstat (limited to 'src/client/util/request-image-size.ts')
-rw-r--r--src/client/util/request-image-size.ts36
1 files changed, 12 insertions, 24 deletions
diff --git a/src/client/util/request-image-size.ts b/src/client/util/request-image-size.ts
index 48cb6e3a5..7a2ecd486 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,41 +10,30 @@
* 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]);
try {
@@ -54,7 +44,7 @@ module.exports = function requestImageSize(options: any) {
}
} catch (err) {
/* empty */
- console.log("Error: ", err)
+ console.log('Error: ', err);
}
});
@@ -65,8 +55,6 @@ module.exports = function requestImageSize(options: any) {
reject(new Error('Image has no size'));
return;
}
-
- size.downloaded = buffer.length;
resolve(size);
});
});