-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimg.js
82 lines (74 loc) · 2.68 KB
/
img.js
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
'use strict';
const ExifTransformer = require('exif-be-gone');
const fs = require('fs');
const Jimp = require('jimp');
const path = require('path');
const cachedJpegDecoder = Jimp.decoders["image/jpeg"];
Jimp.decoders["image/jpeg"] = (data) => {
const userOpts = { maxMemoryUsageInMB: 2048 };
return cachedJpegDecoder(data, userOpts);
};
module.exports = class ImgPage {
id = "";
width = 0;
height = 0;
format = "";
config = {};
constructor(imageRequest, config) {
const widthString = imageRequest.width;
const heightString = imageRequest.height;
this.config = config;
this.id = imageRequest.id;
this.format = imageRequest.format;
if (widthString) {
this.width = parseInt(widthString);
}
if (heightString) {
this.height = parseInt(heightString);
}
}
async returnImage(res, store) {
var imagePath = this.getImagePath(store);
if (this.width != 0 || this.height != 0) {
var cacheImagePath = path.join(__dirname,
this.config.site.folders.cache ,
`${this.width}x${this.height}`,
imagePath);
if (!fs.existsSync(cacheImagePath)) {
await this.createImage(path.join(__dirname, imagePath), cacheImagePath);
console.log(`[returnImage] Created image: ${cacheImagePath}`);
}
imagePath = cacheImagePath;
}
if (res) {
this.sendFile(res, imagePath);
}
}
sendFile(res, cacheImagePath) {
const reader = fs.createReadStream(cacheImagePath);
reader.pipe(new ExifTransformer()).pipe(res);
}
async createImage(inputPath, outputPath) {
fs.mkdirSync(path.dirname(outputPath), {recursive: true});
var width = (this.width == 0) ? Jimp.AUTO : Number(this.width);
var height = (this.height == 0) ? Jimp.AUTO : Number(this.height);
let file = await Jimp.read(inputPath);
if (width != Jimp.AUTO && height != Jimp.AUTO) {
if ((height * file.getWidth() / width) < file.getHeight()) {
width = Jimp.AUTO;
} else {
height = Jimp.AUTO;
}
}
await file.resize(width, height).quality(Number(this.config.images.view.quality));
await file.writeAsync(outputPath);
}
getImagePath(store) {
var imagepath = store.getImagePath(this.id);
if (imagepath) {
return path.join(this.config.site.folders.images, imagepath);
} else {
return this.config.error.image_not_found;
}
}
}