@runnerpro/backend 1.2.4 → 1.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -19,29 +19,39 @@ const fs_1 = __importDefault(require("fs"));
|
|
|
19
19
|
const uuid_1 = require("uuid");
|
|
20
20
|
const path_1 = __importDefault(require("path"));
|
|
21
21
|
const reduceSizeImage = (file, newWidth = 100, quality = 80) => __awaiter(void 0, void 0, void 0, function* () {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
22
|
+
try {
|
|
23
|
+
let filePath;
|
|
24
|
+
if (typeof file === 'object') {
|
|
25
|
+
// Write this image to a file in uploads
|
|
26
|
+
const uid = (0, uuid_1.v4)();
|
|
27
|
+
filePath = path_1.default.join(process.cwd(), 'uploads', `${uid}.webp`);
|
|
28
|
+
fs_1.default.writeFileSync(filePath, file);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
// If it's not an object, it's already a file path
|
|
32
|
+
filePath = file;
|
|
33
|
+
}
|
|
34
|
+
const sizeOf = (0, util_1.promisify)(require('image-size'));
|
|
35
|
+
const dimensions = yield sizeOf(filePath);
|
|
36
|
+
const imageWidth = dimensions.width;
|
|
37
|
+
const imageHeight = dimensions.height;
|
|
38
|
+
const newHeight = Math.round((imageHeight * newWidth) / imageWidth);
|
|
39
|
+
let image = yield jimp_1.default.read(filePath);
|
|
40
|
+
image = yield image.resize(newWidth, newHeight);
|
|
41
|
+
image = yield image.quality(quality);
|
|
42
|
+
// @ts-ignore
|
|
43
|
+
image = yield image.getBufferAsync(jimp_1.default.MIME_JPEG);
|
|
44
|
+
if (typeof file === 'object')
|
|
45
|
+
fs_1.default.unlinkSync(filePath);
|
|
46
|
+
return image;
|
|
28
47
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
48
|
+
catch (error) {
|
|
49
|
+
if (typeof file === 'object')
|
|
50
|
+
return file;
|
|
51
|
+
else {
|
|
52
|
+
const fileFromPath = fs_1.default.readFileSync(file);
|
|
53
|
+
return fileFromPath;
|
|
54
|
+
}
|
|
32
55
|
}
|
|
33
|
-
const sizeOf = (0, util_1.promisify)(require('image-size'));
|
|
34
|
-
const dimensions = yield sizeOf(filePath);
|
|
35
|
-
const imageWidth = dimensions.width;
|
|
36
|
-
const imageHeight = dimensions.height;
|
|
37
|
-
const newHeight = Math.round((imageHeight * newWidth) / imageWidth);
|
|
38
|
-
let image = yield jimp_1.default.read(filePath);
|
|
39
|
-
image = yield image.resize(newWidth, newHeight);
|
|
40
|
-
image = yield image.quality(quality);
|
|
41
|
-
// @ts-ignore
|
|
42
|
-
image = yield image.getBufferAsync(jimp_1.default.MIME_JPEG);
|
|
43
|
-
if (typeof filePath === 'object')
|
|
44
|
-
fs_1.default.unlinkSync(filePath);
|
|
45
|
-
return image;
|
|
46
56
|
});
|
|
47
57
|
exports.reduceSizeImage = reduceSizeImage;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
declare const reduceSizeImage: (file: any, newWidth?: number, quality?: number) => Promise<
|
|
1
|
+
declare const reduceSizeImage: (file: any, newWidth?: number, quality?: number) => Promise<any>;
|
|
2
2
|
export { reduceSizeImage };
|
|
3
3
|
//# sourceMappingURL=reduceSizeImage.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reduceSizeImage.d.ts","sourceRoot":"","sources":["../../../../src/image/reduceSizeImage.ts"],"names":[],"mappings":"AAMA,QAAA,MAAM,eAAe,
|
|
1
|
+
{"version":3,"file":"reduceSizeImage.d.ts","sourceRoot":"","sources":["../../../../src/image/reduceSizeImage.ts"],"names":[],"mappings":"AAMA,QAAA,MAAM,eAAe,kEAmCpB,CAAC;AAEF,OAAO,EACH,eAAe,EAClB,CAAC"}
|