aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/request-image-size.ts
diff options
context:
space:
mode:
authorZachary Zhang <zacharyzhang7@gmail.com>2024-08-31 00:46:29 -0400
committerZachary Zhang <zacharyzhang7@gmail.com>2024-08-31 00:46:29 -0400
commit196294f331496262bef256da8b8e9dbc80288bea (patch)
tree85ff27b7a8070585f9a5ef71dff63566e03232ba /src/client/util/request-image-size.ts
parent0cf61501ec9be34294935f01973c1bd9cad6d267 (diff)
parentc36607691e0b7f5c04f3209a64958f5e51ddd785 (diff)
Merge branch 'master' into zach-starter
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);
});
});