-
Notifications
You must be signed in to change notification settings - Fork 7
/
lib.ts
141 lines (111 loc) · 4.71 KB
/
lib.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import http from "http";
import https from "https";
import fs from "fs";
import sharp, { Sharp } from "sharp";
import { extractBackground, SupportedTypes } from "./utils";
type ImageOptions = {
h?: string,
w?: string,
crop?: string,
fm?: string,
fit?: "cover" | "contain" | "fill" | "inside" | "outside",
position?: "top" | "right top" | "right" | "right bottom" | "bottom" | "bottom left" | "left top",
strategy?: "entropy" | "attention",
gravity?: "north" | "northeast" | "east" | "southeast" | "south" | "southwest" | "west" | "northwest" | "center" | "centre"
kernel?: "nearest" | "cubic" | "mitchell" | "lanczos2" | "lanczos3",
enlarge?: string,
sharpen?: string,
q?: string,
blur?: string,
background?: string
}
export function convertImage(source: { url?: string, path?: string, cache?: boolean }, opts: ImageOptions): Promise<{ buffer: Buffer, ext: string, mime: string }> {
return new Promise(async (resolve, reject) => {
let imageType = (((source.url ? source.url : source.path) || "").split(".").pop() as string).toLowerCase();
// Remove query incase the given url also contains query
if (imageType.indexOf("?")) {
imageType = imageType.split("?").shift() as string;
}
let input: string | Buffer | undefined = source.path;
if (source.url) {
let imageRequest = await getImage(source.url)
.catch(reject);
if (!imageRequest) return;
input = imageRequest.data;
imageRequest.contentType && (imageType = imageRequest.contentType.replace("image/", ""));
}
if (!input) {
return reject(`Source is undefined`);
}
// return svg as it is
if (((source.url || source.path) as string).split(".").pop() === "svg") {
if (typeof input === "string") {
try {
input = fs.readFileSync(input);
} catch (err) {
reject(`Error: File doesn't exists`);
return;
}
}
resolve({ buffer: input as Buffer, mime: "image/svg+xml", ext: "svg" });
return;
}
let output = sharp(input);
let resizeOpts: sharp.ResizeOptions | undefined;
if (opts.h || opts.w) {
resizeOpts = {};
opts.fit && (resizeOpts.fit = opts.fit);
opts.position && (resizeOpts.position = opts.position);
opts.kernel && (resizeOpts.kernel = opts.kernel)
opts.enlarge && (resizeOpts.withoutEnlargement = opts.enlarge === "true");
opts.h && (resizeOpts.height = parseInt(opts.h));
opts.w && (resizeOpts.width = parseInt(opts.w));
opts.background && (resizeOpts.background = extractBackground(opts.background))
output = output.resize(resizeOpts)
}
let type = opts.fm && SupportedTypes.indexOf(opts.fm) !== -1 ? opts.fm : imageType;
type === 'jpg' && (type = 'jpeg');
// @ts-ignore
output[type] && typeof output[type] === "function" &&
// @ts-ignore
(output = (output[type]({ quality: opts.q ? parseInt(opts.q) : 80 }) as Sharp));
opts.sharpen && (
output = output.sharpen.apply(output,
// @ts-ignore
opts.sharpen === "true" ? [undefined, 1, 2] :
// @ts-ignore
opts.sharpen.split(",").map(v => isNaN(v) ? undefined : parseInt(v))
)
);
opts.blur && (output = output.blur.apply(output, opts.blur === "true" ? [undefined] : [parseInt(opts.blur)]));
output.toBuffer()
.then((buffer) => {
resolve({ buffer, mime: `image/${type}`, ext: type });
})
.catch(reject)
})
}
function getImage(url: string): Promise<{ data: Buffer, contentType?: string }> {
url = decodeURIComponent(url);
return new Promise((resolve, reject) => {
(url.indexOf('https') === 0 ? https : http).get(url, (res) => {
if (res.statusCode && acceptedCodes.indexOf(res.statusCode) !== -1) {
var imageData = '';
res.setEncoding('binary');
res.on('data', function (chunk) {
imageData += chunk;
});
res.on('end', function () {
resolve({
data: Buffer.from(imageData, 'binary'),
contentType: res.headers["content-type"]
});
})
res.on("error", reject);
} else {
reject("Image not found");
}
})
})
}
const acceptedCodes = [200]