mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-24 18:49:00 +01:00
@@ -8,16 +8,23 @@
|
||||
import * as path from 'path';
|
||||
import * as http from 'http';
|
||||
import * as https from 'https';
|
||||
import { parse as parseUrl } from 'url';
|
||||
import * as sizeOf from 'image-size';
|
||||
import { URL } from 'url';
|
||||
import { imageSize } from 'image-size';
|
||||
import { ISizeCalculationResult } from 'image-size/dist/types/interface';
|
||||
|
||||
const reUrl = /^https?:/;
|
||||
export type ImageInfoWithScale = {
|
||||
realWidth: number,
|
||||
realHeight: number,
|
||||
width: number,
|
||||
height: number
|
||||
};
|
||||
|
||||
/**
|
||||
* Get size of given image file. Supports files from local filesystem,
|
||||
* as well as URLs
|
||||
*/
|
||||
export function getImageSize(file: string) {
|
||||
export function getImageSize(file: string): Promise<ImageInfoWithScale | undefined> {
|
||||
file = file.replace(/^file:\/\//, '');
|
||||
return reUrl.test(file) ? getImageSizeFromURL(file) : getImageSizeFromFile(file);
|
||||
}
|
||||
@@ -25,7 +32,7 @@ export function getImageSize(file: string) {
|
||||
/**
|
||||
* Get image size from file on local file system
|
||||
*/
|
||||
function getImageSizeFromFile(file: string) {
|
||||
function getImageSizeFromFile(file: string): Promise<ImageInfoWithScale | undefined> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const isDataUrl = file.match(/^data:.+?;base64,/);
|
||||
|
||||
@@ -33,13 +40,13 @@ function getImageSizeFromFile(file: string) {
|
||||
// NB should use sync version of `sizeOf()` for buffers
|
||||
try {
|
||||
const data = Buffer.from(file.slice(isDataUrl[0].length), 'base64');
|
||||
return resolve(sizeForFileName('', sizeOf(data)));
|
||||
return resolve(sizeForFileName('', imageSize(data)));
|
||||
} catch (err) {
|
||||
return reject(err);
|
||||
}
|
||||
}
|
||||
|
||||
sizeOf(file, (err: any, size: any) => {
|
||||
imageSize(file, (err: Error | null, size?: ISizeCalculationResult) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
@@ -52,9 +59,9 @@ function getImageSizeFromFile(file: string) {
|
||||
/**
|
||||
* Get image size from given remove URL
|
||||
*/
|
||||
function getImageSizeFromURL(urlStr: string) {
|
||||
function getImageSizeFromURL(urlStr: string): Promise<ImageInfoWithScale | undefined> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const url = parseUrl(urlStr);
|
||||
const url = new URL(urlStr);
|
||||
const getTransport = url.protocol === 'https:' ? https.get : http.get;
|
||||
|
||||
if (!url.pathname) {
|
||||
@@ -62,13 +69,13 @@ function getImageSizeFromURL(urlStr: string) {
|
||||
}
|
||||
const urlPath: string = url.pathname;
|
||||
|
||||
getTransport(url as any, resp => {
|
||||
getTransport(url, resp => {
|
||||
const chunks: Buffer[] = [];
|
||||
let bufSize = 0;
|
||||
|
||||
const trySize = (chunks: Buffer[]) => {
|
||||
try {
|
||||
const size = sizeOf(Buffer.concat(chunks, bufSize));
|
||||
const size: ISizeCalculationResult = imageSize(Buffer.concat(chunks, bufSize));
|
||||
resp.removeListener('data', onData);
|
||||
resp.destroy(); // no need to read further
|
||||
resolve(sizeForFileName(path.basename(urlPath), size));
|
||||
@@ -90,8 +97,7 @@ function getImageSizeFromURL(urlStr: string) {
|
||||
resp.removeListener('data', onData);
|
||||
reject(err);
|
||||
});
|
||||
})
|
||||
.once('error', reject);
|
||||
}).once('error', reject);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -99,10 +105,14 @@ function getImageSizeFromURL(urlStr: string) {
|
||||
* Returns size object for given file name. If file name contains `@Nx` token,
|
||||
* the final dimentions will be downscaled by N
|
||||
*/
|
||||
function sizeForFileName(fileName: string, size: any) {
|
||||
function sizeForFileName(fileName: string, size?: ISizeCalculationResult): ImageInfoWithScale | undefined {
|
||||
const m = fileName.match(/@(\d+)x\./);
|
||||
const scale = m ? +m[1] : 1;
|
||||
|
||||
if (!size || !size.width || !size.height) {
|
||||
return;
|
||||
}
|
||||
|
||||
return {
|
||||
realWidth: size.width,
|
||||
realHeight: size.height,
|
||||
|
||||
Reference in New Issue
Block a user