forked from NodeBB/NodeBB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.js
182 lines (155 loc) · 4.76 KB
/
image.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
'use strict';
const os = require('os');
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const winston = require('winston');
const file = require('./file');
const plugins = require('./plugins');
const meta = require('./meta');
const image = module.exports;
function requireSharp() {
const sharp = require('sharp');
if (os.platform() === 'win32') {
// https://github.com/lovell/sharp/issues/1259
sharp.cache(false);
}
return sharp;
}
image.isFileTypeAllowed = async function (path) {
const plugins = require('./plugins');
if (plugins.hooks.hasListeners('filter:image.isFileTypeAllowed')) {
return await plugins.hooks.fire('filter:image.isFileTypeAllowed', path);
}
const sharp = require('sharp');
await sharp(path, {
failOnError: true,
}).metadata();
};
image.resizeImage = async function (data) {
if (plugins.hooks.hasListeners('filter:image.resize')) {
await plugins.hooks.fire('filter:image.resize', {
path: data.path,
target: data.target,
width: data.width,
height: data.height,
quality: data.quality,
});
} else {
const sharp = requireSharp();
const buffer = await fs.promises.readFile(data.path);
const sharpImage = sharp(buffer, {
failOnError: true,
animated: data.path.endsWith('gif'),
});
const metadata = await sharpImage.metadata();
sharpImage.rotate(); // auto-orients based on exif data
sharpImage.resize(data.hasOwnProperty('width') ? data.width : null, data.hasOwnProperty('height') ? data.height : null);
if (data.quality) {
switch (metadata.format) {
case 'jpeg': {
sharpImage.jpeg({
quality: data.quality,
mozjpeg: true,
});
break;
}
case 'png': {
sharpImage.png({
quality: data.quality,
compressionLevel: 9,
});
break;
}
}
}
await sharpImage.toFile(data.target || data.path);
}
};
image.normalise = async function (path) {
if (plugins.hooks.hasListeners('filter:image.normalise')) {
await plugins.hooks.fire('filter:image.normalise', {
path: path,
});
} else {
const sharp = requireSharp();
await sharp(path, { failOnError: true }).png().toFile(`${path}.png`);
}
return `${path}.png`;
};
image.size = async function (path) {
let imageData;
if (plugins.hooks.hasListeners('filter:image.size')) {
imageData = await plugins.hooks.fire('filter:image.size', {
path: path,
});
} else {
const sharp = requireSharp();
imageData = await sharp(path, { failOnError: true }).metadata();
}
return imageData ? { width: imageData.width, height: imageData.height } : undefined;
};
image.stripEXIF = async function (path) {
if (!meta.config.stripEXIFData || path.endsWith('.gif') || path.endsWith('.svg')) {
return;
}
try {
if (plugins.hooks.hasListeners('filter:image.stripEXIF')) {
await plugins.hooks.fire('filter:image.stripEXIF', {
path: path,
});
return;
}
const buffer = await fs.promises.readFile(path);
const sharp = requireSharp();
await sharp(buffer, { failOnError: true }).rotate().toFile(path);
} catch (err) {
winston.error(err.stack);
}
};
image.checkDimensions = async function (path) {
const meta = require('./meta');
const result = await image.size(path);
if (result.width > meta.config.rejectImageWidth || result.height > meta.config.rejectImageHeight) {
throw new Error('[[error:invalid-image-dimensions]]');
}
return result;
};
image.convertImageToBase64 = async function (path) {
return await fs.promises.readFile(path, 'base64');
};
image.mimeFromBase64 = function (imageData) {
return imageData.slice(5, imageData.indexOf('base64') - 1);
};
image.extensionFromBase64 = function (imageData) {
return file.typeToExtension(image.mimeFromBase64(imageData));
};
image.writeImageDataToTempFile = async function (imageData) {
const filename = crypto.createHash('md5').update(imageData).digest('hex');
const type = image.mimeFromBase64(imageData);
const extension = file.typeToExtension(type);
const filepath = path.join(os.tmpdir(), filename + extension);
const buffer = Buffer.from(imageData.slice(imageData.indexOf('base64') + 7), 'base64');
await fs.promises.writeFile(filepath, buffer, { encoding: 'base64' });
return filepath;
};
image.sizeFromBase64 = function (imageData) {
return Buffer.from(imageData.slice(imageData.indexOf('base64') + 7), 'base64').length;
};
image.uploadImage = async function (filename, folder, imageData) {
if (plugins.hooks.hasListeners('filter:uploadImage')) {
return await plugins.hooks.fire('filter:uploadImage', {
image: imageData,
uid: imageData.uid,
folder: folder,
});
}
await image.isFileTypeAllowed(imageData.path);
const upload = await file.saveFileToLocal(filename, folder, imageData.path);
return {
url: upload.url,
path: upload.path,
name: imageData.name,
};
};
require('./promisify')(image);