@pi-r/jimp 0.7.2 → 0.8.0
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.
- package/index.js +110 -55
- package/package.json +3 -3
- package/types/index.d.ts +20 -7
- package/util.js +8 -8
package/index.js
CHANGED
|
@@ -5,12 +5,13 @@ const child_process = require("child_process");
|
|
|
5
5
|
const jimp = require("jimp");
|
|
6
6
|
const gifwrap = require("gifwrap");
|
|
7
7
|
const bmp = require("bmp-js");
|
|
8
|
+
const crypto_1 = require("crypto");
|
|
8
9
|
const types_1 = require("@e-mc/types");
|
|
9
10
|
const Image = require('@e-mc/image');
|
|
10
11
|
let WEBPMUX = null, WEBPMUX_INIT = false;
|
|
11
12
|
try {
|
|
12
13
|
WEBPMUX = require('node-webpmux');
|
|
13
|
-
new WEBPMUX.Image().initLib().then(() => WEBPMUX_INIT = true);
|
|
14
|
+
void new WEBPMUX.Image().initLib().then(() => WEBPMUX_INIT = true);
|
|
14
15
|
}
|
|
15
16
|
catch {
|
|
16
17
|
}
|
|
@@ -80,9 +81,9 @@ function getMethodName(value) {
|
|
|
80
81
|
}
|
|
81
82
|
}
|
|
82
83
|
async function performCommand(host, instance, localUri, command, outputType, finalAs, buffer, parent) {
|
|
83
|
-
return jimp.read((buffer || localUri))
|
|
84
|
+
return await jimp.read((buffer || localUri))
|
|
84
85
|
.then(async (img) => {
|
|
85
|
-
return transformCommand(localUri, new JimpHandler(img, instance, host), command, outputType, finalAs, parent);
|
|
86
|
+
return await transformCommand(localUri, new JimpHandler(img, instance, host), command, outputType, finalAs, parent);
|
|
86
87
|
});
|
|
87
88
|
}
|
|
88
89
|
function execOptions(settings) {
|
|
@@ -129,7 +130,7 @@ async function transformCommand(localFile, handler, command, outputType, outputA
|
|
|
129
130
|
});
|
|
130
131
|
}
|
|
131
132
|
}
|
|
132
|
-
function setImageCache(tempKey, tempFile, output, localFile) {
|
|
133
|
+
function setImageCache(instance, tempKey, tempFile, output, localFile) {
|
|
133
134
|
try {
|
|
134
135
|
if (typeof output === 'string') {
|
|
135
136
|
fs.copyFileSync(output, tempFile);
|
|
@@ -137,7 +138,7 @@ function setImageCache(tempKey, tempFile, output, localFile) {
|
|
|
137
138
|
else {
|
|
138
139
|
fs.writeFileSync(tempFile, output);
|
|
139
140
|
}
|
|
140
|
-
const stored = getCacheData
|
|
141
|
+
const stored = getCacheData(instance);
|
|
141
142
|
if (localFile) {
|
|
142
143
|
const { ctimeMs, mtimeMs, size } = fs.statSync(localFile);
|
|
143
144
|
stored[tempKey] = { tempKey, tempFile, ctimeMs, localFile, mtimeMs, size };
|
|
@@ -145,7 +146,7 @@ function setImageCache(tempKey, tempFile, output, localFile) {
|
|
|
145
146
|
else {
|
|
146
147
|
stored[tempKey] = { tempKey, tempFile, ctimeMs: Date.now() };
|
|
147
148
|
}
|
|
148
|
-
if (
|
|
149
|
+
if (instance.settings.jimp.cache_expires) {
|
|
149
150
|
fs.writeFile(tempFile + '.json', JSON.stringify(stored[tempKey]), 'utf-8', () => { });
|
|
150
151
|
}
|
|
151
152
|
}
|
|
@@ -153,8 +154,8 @@ function setImageCache(tempKey, tempFile, output, localFile) {
|
|
|
153
154
|
TEMP_DIR = '';
|
|
154
155
|
}
|
|
155
156
|
}
|
|
156
|
-
function getImageCache(tempKey) {
|
|
157
|
-
const stored = getCacheData
|
|
157
|
+
function getImageCache(instance, tempKey) {
|
|
158
|
+
const stored = getCacheData(instance);
|
|
158
159
|
const data = stored[tempKey];
|
|
159
160
|
if (data) {
|
|
160
161
|
const { tempFile, localFile, ctimeMs, mtimeMs, size } = data;
|
|
@@ -173,13 +174,13 @@ function getImageCache(tempKey) {
|
|
|
173
174
|
removeFile(tempFile);
|
|
174
175
|
delete stored[tempKey];
|
|
175
176
|
}
|
|
176
|
-
TEMP_DIR ||=
|
|
177
|
-
return [null, TEMP_DIR ? path.join(TEMP_DIR, (0,
|
|
177
|
+
TEMP_DIR ||= instance.getTempDir({ moduleDir: true, increment: 5 });
|
|
178
|
+
return [null, TEMP_DIR ? path.join(TEMP_DIR, (0, crypto_1.randomUUID)()) : ''];
|
|
178
179
|
}
|
|
179
|
-
function getCacheData() {
|
|
180
|
+
function getCacheData(instance) {
|
|
180
181
|
if (!CACHE_INIT) {
|
|
181
|
-
TEMP_DIR =
|
|
182
|
-
const settings =
|
|
182
|
+
TEMP_DIR = instance.getTempDir({ moduleDir: true, increment: 5 });
|
|
183
|
+
const settings = instance.settings.jimp ||= {};
|
|
183
184
|
const expires = (0, types_1.parseExpires)(settings.cache_expires || 0);
|
|
184
185
|
if (expires === 0) {
|
|
185
186
|
settings.cache_expires = 0;
|
|
@@ -218,19 +219,22 @@ function getCacheData() {
|
|
|
218
219
|
}
|
|
219
220
|
return CACHE_TRANSFORM;
|
|
220
221
|
}
|
|
221
|
-
function formatMessage(value, startTime, failed, cTimeMs) {
|
|
222
|
+
function formatMessage(instance, value, startTime, failed, cTimeMs) {
|
|
222
223
|
if (cTimeMs) {
|
|
223
|
-
|
|
224
|
+
instance.formatMessage(2048, "jimp", [value, 'cache'], new Date(cTimeMs).toLocaleString(), { ...Image.LOG_STYLE_NOTICE, hintBold: true });
|
|
224
225
|
}
|
|
225
226
|
else if (startTime) {
|
|
226
|
-
|
|
227
|
+
instance.writeTimeProcess("jimp", value, startTime, { type: 2048, failed });
|
|
227
228
|
}
|
|
228
229
|
}
|
|
229
|
-
function getTempPath(ext) {
|
|
230
|
-
const tempDir = TEMP_DIR ||
|
|
231
|
-
return path.join(tempDir, (0,
|
|
230
|
+
function getTempPath(instance, ext) {
|
|
231
|
+
const tempDir = TEMP_DIR || instance.getTempDir({ moduleDir: true, createDir: true }) || instance.getTempDir();
|
|
232
|
+
return path.join(tempDir, (0, crypto_1.randomUUID)() + '.' + ext);
|
|
232
233
|
}
|
|
233
|
-
|
|
234
|
+
function removeFile(pathname) {
|
|
235
|
+
fs.unlink(pathname, () => { });
|
|
236
|
+
}
|
|
237
|
+
const emptyResult = (options) => (options.tempFile ? '' : null);
|
|
234
238
|
class JimpHandler {
|
|
235
239
|
handler;
|
|
236
240
|
instance;
|
|
@@ -264,14 +268,18 @@ class JimpHandler {
|
|
|
264
268
|
const img = handler.clone().rotate(value);
|
|
265
269
|
const output = leading + value + ext;
|
|
266
270
|
tasks.push(img.writeAsync(output)
|
|
267
|
-
.then(() =>
|
|
268
|
-
|
|
271
|
+
.then(() => {
|
|
272
|
+
this.finalize(output, callback);
|
|
273
|
+
})
|
|
274
|
+
.catch((err) => {
|
|
275
|
+
this.instance.writeFail(["Unable to rotate image", "jimp"], err, 2048);
|
|
276
|
+
}));
|
|
269
277
|
}
|
|
270
278
|
}
|
|
271
279
|
if (deg) {
|
|
272
280
|
handler.rotate(deg);
|
|
273
281
|
}
|
|
274
|
-
if (tasks.length) {
|
|
282
|
+
if (tasks.length > 0) {
|
|
275
283
|
return Promise.all(tasks).then(() => this);
|
|
276
284
|
}
|
|
277
285
|
}
|
|
@@ -440,7 +448,11 @@ class JimpHandler {
|
|
|
440
448
|
else if (webp !== output) {
|
|
441
449
|
const tempFile = output;
|
|
442
450
|
queueMicrotask(() => {
|
|
443
|
-
fs.unlink(tempFile, error =>
|
|
451
|
+
fs.unlink(tempFile, error => {
|
|
452
|
+
if (!error) {
|
|
453
|
+
fs.rmdir(path.dirname(tempFile), () => { });
|
|
454
|
+
}
|
|
455
|
+
});
|
|
444
456
|
});
|
|
445
457
|
output = filename;
|
|
446
458
|
}
|
|
@@ -462,7 +474,7 @@ class JimpHandler {
|
|
|
462
474
|
}
|
|
463
475
|
async getBuffer(tempFile, saveAs) {
|
|
464
476
|
const empty = () => tempFile ? '' : null;
|
|
465
|
-
const output = getTempPath
|
|
477
|
+
const output = getTempPath(this.instance, this.instance.outputAs || (saveAs && util_1.MIME_OUTPUT.has('image/' + (saveAs === 'jpg' ? 'jpeg' : saveAs)) ? saveAs : this.handler.getMIME().split('/').pop()));
|
|
466
478
|
return !output ? empty() : new Promise(resolve => {
|
|
467
479
|
this.handler.write(output, error => {
|
|
468
480
|
if (error) {
|
|
@@ -484,7 +496,13 @@ class JimpHandler {
|
|
|
484
496
|
this.instance.writeFail(["Unable to read file", path.basename(result)], err, 32);
|
|
485
497
|
resolve(null);
|
|
486
498
|
}
|
|
487
|
-
|
|
499
|
+
finally {
|
|
500
|
+
fs.unlink(result, err => {
|
|
501
|
+
if (!err) {
|
|
502
|
+
this.instance.emit('file:delete', result);
|
|
503
|
+
}
|
|
504
|
+
});
|
|
505
|
+
}
|
|
488
506
|
}
|
|
489
507
|
});
|
|
490
508
|
});
|
|
@@ -541,7 +559,9 @@ class JimpHandler {
|
|
|
541
559
|
return;
|
|
542
560
|
}
|
|
543
561
|
return this.handler.writeAsync(output)
|
|
544
|
-
.then(() =>
|
|
562
|
+
.then(() => {
|
|
563
|
+
this.finalize(output, callback);
|
|
564
|
+
})
|
|
545
565
|
.catch((err) => {
|
|
546
566
|
if (callback) {
|
|
547
567
|
callback(err, '');
|
|
@@ -564,24 +584,23 @@ class JimpHandler {
|
|
|
564
584
|
class Jimp extends Image {
|
|
565
585
|
static async transform(file, command, options = {}) {
|
|
566
586
|
const [outputType, saveAs, finalAs] = (0, util_1.parseFormat)(command = command.trim(), options.mimeType);
|
|
567
|
-
const empty = () => options.tempFile ? '' : null;
|
|
568
587
|
if (!outputType) {
|
|
569
|
-
return
|
|
588
|
+
return emptyResult(options);
|
|
570
589
|
}
|
|
571
590
|
const instance = new Jimp(options.module);
|
|
572
591
|
let buffer = null;
|
|
573
592
|
if (Buffer.isBuffer(file)) {
|
|
574
593
|
const tempDir = TEMP_DIR || instance.getTempDir();
|
|
575
594
|
if (!this.createDir(tempDir)) {
|
|
576
|
-
return
|
|
595
|
+
return emptyResult(options);
|
|
577
596
|
}
|
|
578
597
|
try {
|
|
579
598
|
const { ext } = await this.resolveMime(file) || { ext: 'unknown' };
|
|
580
599
|
buffer = file;
|
|
581
|
-
fs.writeFileSync(file = path.join(tempDir, (0,
|
|
600
|
+
fs.writeFileSync(file = path.join(tempDir, (0, crypto_1.randomUUID)() + '.' + ext), buffer);
|
|
582
601
|
}
|
|
583
602
|
catch {
|
|
584
|
-
return
|
|
603
|
+
return emptyResult(options);
|
|
585
604
|
}
|
|
586
605
|
options.cache = false;
|
|
587
606
|
}
|
|
@@ -600,13 +619,13 @@ class Jimp extends Image {
|
|
|
600
619
|
}
|
|
601
620
|
const writeMessage = (failed, cTimeMs) => {
|
|
602
621
|
if (cTimeMs || options.startTime) {
|
|
603
|
-
formatMessage
|
|
622
|
+
formatMessage(instance, filename + (0, util_1.showOutputType)(options.mimeType, outputType, finalAs), options.startTime, failed, cTimeMs);
|
|
604
623
|
}
|
|
605
624
|
};
|
|
606
625
|
let tempKey, tempFile;
|
|
607
626
|
if (options.cache) {
|
|
608
627
|
let ctimeMs;
|
|
609
|
-
[buffer, tempFile, ctimeMs] = getImageCache
|
|
628
|
+
[buffer, tempFile, ctimeMs] = getImageCache(instance, tempKey = file + command + (options.mimeType || ''));
|
|
610
629
|
if (buffer) {
|
|
611
630
|
writeMessage(false, ctimeMs);
|
|
612
631
|
return buffer;
|
|
@@ -620,12 +639,18 @@ class Jimp extends Image {
|
|
|
620
639
|
instance.flushLog();
|
|
621
640
|
writeMessage(!result || instance.errors.length > 0);
|
|
622
641
|
if (result && tempKey && tempFile) {
|
|
623
|
-
setImageCache
|
|
642
|
+
setImageCache(instance, tempKey, tempFile, result, file);
|
|
624
643
|
}
|
|
625
644
|
return result;
|
|
626
645
|
})
|
|
627
|
-
.catch(() =>
|
|
628
|
-
|
|
646
|
+
.catch(() => {
|
|
647
|
+
return emptyResult(options);
|
|
648
|
+
})
|
|
649
|
+
.finally(() => {
|
|
650
|
+
if (buffer && !options.cache) {
|
|
651
|
+
removeFile(file);
|
|
652
|
+
}
|
|
653
|
+
});
|
|
629
654
|
}
|
|
630
655
|
_moduleName = "jimp";
|
|
631
656
|
_threadable = true;
|
|
@@ -697,13 +722,13 @@ class Jimp extends Image {
|
|
|
697
722
|
if (replace && file.localUri !== output && !host.assets.find(item => item.localUri === output && !item.invalid)) {
|
|
698
723
|
host.filesToRemove.add(output);
|
|
699
724
|
}
|
|
700
|
-
formatMessage
|
|
725
|
+
formatMessage(this, (0, util_1.showInputType)(mimeType, outputType, finalAs) + filename, startTime, false, ctimeMs);
|
|
701
726
|
resolve();
|
|
702
727
|
};
|
|
703
728
|
let tempKey, tempFile;
|
|
704
729
|
if (this.settings.cache && (file.etag || file.buffer)) {
|
|
705
730
|
let buffer, ctimeMs;
|
|
706
|
-
[buffer, tempFile] = getImageCache
|
|
731
|
+
[buffer, tempFile] = getImageCache(this, tempKey = (file.etag || Image.asHash(file.buffer)) + command + mimeType);
|
|
707
732
|
if (buffer) {
|
|
708
733
|
const result = finalAs === 'webp' ? (0, util_1.renameExt)(output, 'webp', replace) : output;
|
|
709
734
|
fs.writeFileSync(result, file.buffer = buffer);
|
|
@@ -713,7 +738,7 @@ class Jimp extends Image {
|
|
|
713
738
|
}
|
|
714
739
|
const finalize = (value) => {
|
|
715
740
|
if (tempFile && tempKey) {
|
|
716
|
-
setImageCache
|
|
741
|
+
setImageCache(this, tempKey, tempFile, value);
|
|
717
742
|
}
|
|
718
743
|
if (replace) {
|
|
719
744
|
delete file.buffer;
|
|
@@ -737,8 +762,12 @@ class Jimp extends Image {
|
|
|
737
762
|
const frame = new GifFrame(img.handler.bitmap);
|
|
738
763
|
GifUtil.quantizeSorokin(frame, 256);
|
|
739
764
|
GifUtil.write(output, [frame])
|
|
740
|
-
.then(() =>
|
|
741
|
-
|
|
765
|
+
.then(() => {
|
|
766
|
+
finalize(output);
|
|
767
|
+
})
|
|
768
|
+
.catch((err) => {
|
|
769
|
+
errorResponse(err);
|
|
770
|
+
});
|
|
742
771
|
}
|
|
743
772
|
catch (err) {
|
|
744
773
|
errorResponse(err);
|
|
@@ -771,7 +800,9 @@ class Jimp extends Image {
|
|
|
771
800
|
}
|
|
772
801
|
};
|
|
773
802
|
const hasTransform = (cmd) => !!(cmd.rotate || cmd.opacity >= 0 && cmd.opacity < 1 || cmd.resize || cmd.crop || cmd.method);
|
|
774
|
-
const startMessage = () =>
|
|
803
|
+
const startMessage = () => {
|
|
804
|
+
host.formatMessage(2048, "jimp", ["Transforming image...", path.basename(localUri)], command);
|
|
805
|
+
};
|
|
775
806
|
if (mimeType === jimp.MIME_GIF) {
|
|
776
807
|
if (outputType === jimp.MIME_GIF || outputType === "image/webp") {
|
|
777
808
|
const cmd = this.parseCommand(command);
|
|
@@ -864,12 +895,20 @@ class Jimp extends Image {
|
|
|
864
895
|
frames[i].bitmap = img.bitmap;
|
|
865
896
|
}
|
|
866
897
|
GifUtil.write(output, frames, gif)
|
|
867
|
-
.then(() =>
|
|
868
|
-
|
|
898
|
+
.then(() => {
|
|
899
|
+
transformWebP(output, true);
|
|
900
|
+
})
|
|
901
|
+
.catch((err) => {
|
|
902
|
+
errorResponse(err);
|
|
903
|
+
});
|
|
869
904
|
})
|
|
870
|
-
.catch((err) =>
|
|
905
|
+
.catch((err) => {
|
|
906
|
+
errorResponse(err);
|
|
907
|
+
});
|
|
871
908
|
})
|
|
872
|
-
.catch((err) =>
|
|
909
|
+
.catch((err) => {
|
|
910
|
+
errorResponse(err);
|
|
911
|
+
});
|
|
873
912
|
}
|
|
874
913
|
catch (err) {
|
|
875
914
|
errorResponse(err);
|
|
@@ -917,8 +956,12 @@ class Jimp extends Image {
|
|
|
917
956
|
frames[i] = new GifFrame(new BitmapImage(img), { xOffset: x, yOffset: y, delayCentisecs: delay / 10 });
|
|
918
957
|
}
|
|
919
958
|
GifUtil.write(output, frames, { loops: webp.anim.loops })
|
|
920
|
-
.then(() =>
|
|
921
|
-
|
|
959
|
+
.then(() => {
|
|
960
|
+
finalize(output);
|
|
961
|
+
})
|
|
962
|
+
.catch((err) => {
|
|
963
|
+
errorResponse(err);
|
|
964
|
+
});
|
|
922
965
|
}
|
|
923
966
|
catch (err) {
|
|
924
967
|
errorResponse(err);
|
|
@@ -940,13 +983,21 @@ class Jimp extends Image {
|
|
|
940
983
|
Promise.all(frames)
|
|
941
984
|
.then(() => {
|
|
942
985
|
webp.save(output, { width: w, height: h, bgColor: cmd.rotate ? [0, 0, 0, 0] : undefined })
|
|
943
|
-
.then(() =>
|
|
944
|
-
|
|
986
|
+
.then(() => {
|
|
987
|
+
finalize(output);
|
|
988
|
+
})
|
|
989
|
+
.catch((err) => {
|
|
990
|
+
errorResponse(err);
|
|
991
|
+
});
|
|
945
992
|
})
|
|
946
|
-
.catch((err) =>
|
|
993
|
+
.catch((err) => {
|
|
994
|
+
errorResponse(err);
|
|
995
|
+
});
|
|
947
996
|
}
|
|
948
997
|
})
|
|
949
|
-
.catch((err) =>
|
|
998
|
+
.catch((err) => {
|
|
999
|
+
errorResponse(err);
|
|
1000
|
+
});
|
|
950
1001
|
}
|
|
951
1002
|
else {
|
|
952
1003
|
resolve();
|
|
@@ -966,7 +1017,7 @@ class Jimp extends Image {
|
|
|
966
1017
|
};
|
|
967
1018
|
const settings = this.settings;
|
|
968
1019
|
const { path: webp_path } = settings.webp ||= {};
|
|
969
|
-
const bmpFile = getTempPath
|
|
1020
|
+
const bmpFile = getTempPath(this, 'bmp');
|
|
970
1021
|
try {
|
|
971
1022
|
child_process.execFile((0, util_1.getWebP_bin)('dwebp', webp_path), [(0, util_1.normalizePath)(localUri), '-bmp', '-o', (0, util_1.normalizePath)(bmpFile)], { shell: true, signal: this.signal, ...execOptions(settings) }, err => {
|
|
972
1023
|
if (!err) {
|
|
@@ -974,7 +1025,11 @@ class Jimp extends Image {
|
|
|
974
1025
|
}
|
|
975
1026
|
else {
|
|
976
1027
|
removeFile(bmpFile);
|
|
977
|
-
tryWebpMux().then(valid =>
|
|
1028
|
+
void tryWebpMux().then(valid => {
|
|
1029
|
+
if (!valid) {
|
|
1030
|
+
errorResponse(err);
|
|
1031
|
+
}
|
|
1032
|
+
});
|
|
978
1033
|
}
|
|
979
1034
|
});
|
|
980
1035
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pi-r/jimp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Jimp image constructor for E-mc.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"publishConfig": {
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"homepage": "https://github.com/anpham6/pi-r#readme",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@e-mc/image": "^0.
|
|
24
|
-
"@e-mc/types": "^0.
|
|
23
|
+
"@e-mc/image": "^0.10.0",
|
|
24
|
+
"@e-mc/types": "^0.10.0",
|
|
25
25
|
"bmp-js": "^0.1.0",
|
|
26
26
|
"gifwrap": "^0.10.1",
|
|
27
27
|
"jimp": "^0.22.12"
|
package/types/index.d.ts
CHANGED
|
@@ -1,10 +1,23 @@
|
|
|
1
|
-
import type { IHost, IImage, ImageConstructor } from '@e-mc/types/lib';
|
|
2
|
-
import type {
|
|
1
|
+
import type { IFileManager, IHost, IImage, ImageConstructor } from '@e-mc/types/lib';
|
|
2
|
+
import type { ExternalAsset } from '@e-mc/types/lib/asset';
|
|
3
|
+
import type { TransformOptions } from '@e-mc/types/lib/image';
|
|
4
|
+
import type { ImageModule, ImageSettings } from '@e-mc/types/lib/settings';
|
|
5
|
+
import type { ExecAction } from '@e-mc/types/lib/module';
|
|
3
6
|
|
|
4
7
|
import type { ImageHandler } from '@e-mc/image/types';
|
|
5
8
|
|
|
6
9
|
import type * as jimp from 'jimp';
|
|
7
10
|
|
|
11
|
+
export interface JimpSettings extends ImageSettings {
|
|
12
|
+
jimp?: {
|
|
13
|
+
exec?: ExecAction;
|
|
14
|
+
cache_expires?: number | string;
|
|
15
|
+
rotate_clockwise?: boolean;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type ResultCallback<T = unknown, U = void, V = unknown> = (err: V, result: T) => U;
|
|
20
|
+
|
|
8
21
|
export interface IJimpHandler<T extends IHost = IHost, U extends ImageModule = ImageModule> extends ImageHandler<jimp, T, IImage<T, U>> {
|
|
9
22
|
method(): Promise<void>;
|
|
10
23
|
rotate(localFile?: string, callback?: ResultCallback<string>): Promise<this>;
|
|
@@ -12,8 +25,8 @@ export interface IJimpHandler<T extends IHost = IHost, U extends ImageModule = I
|
|
|
12
25
|
writeAsync(output: string, callback?: ResultCallback): Promise<void>;
|
|
13
26
|
}
|
|
14
27
|
|
|
15
|
-
export interface JimpImageConstructor extends ImageConstructor {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
28
|
+
export interface JimpImageConstructor<T extends IFileManager<U>, U extends ExternalAsset = ExternalAsset, V extends ImageModule = ImageModule<JimpSettings>> extends ConstructorDerived<ImageConstructor<T, V>> {
|
|
29
|
+
transform<W extends TransformOptions>(file: string, command: string, options?: W): Promise<W extends { tempFile: true } ? string : Null<Buffer>>;
|
|
30
|
+
readonly prototype: IImage<T, V>;
|
|
31
|
+
new(module?: V, ...args: unknown[]): IImage<T, V>;
|
|
32
|
+
}
|
package/util.js
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
exports.
|
|
2
|
+
exports.MIME_OUTPUT = exports.MIME_INPUT = void 0;
|
|
3
|
+
exports.parseFormat = parseFormat;
|
|
4
|
+
exports.renameExt = renameExt;
|
|
5
|
+
exports.normalizePath = normalizePath;
|
|
6
|
+
exports.getWebP_bin = getWebP_bin;
|
|
7
|
+
exports.showInputType = showInputType;
|
|
8
|
+
exports.showOutputType = showOutputType;
|
|
3
9
|
const path = require("path");
|
|
4
10
|
const fs = require("fs");
|
|
5
11
|
const jimp = require("jimp");
|
|
@@ -52,7 +58,6 @@ function parseFormat(command, mimeType, gif) {
|
|
|
52
58
|
}
|
|
53
59
|
return ['', '', ''];
|
|
54
60
|
}
|
|
55
|
-
exports.parseFormat = parseFormat;
|
|
56
61
|
function renameExt(output, ext, replace) {
|
|
57
62
|
let result = (0, types_1.renameExt)(output.replace('.__copy__.', '.'), ext);
|
|
58
63
|
if (!replace) {
|
|
@@ -64,31 +69,26 @@ function renameExt(output, ext, replace) {
|
|
|
64
69
|
}
|
|
65
70
|
return result;
|
|
66
71
|
}
|
|
67
|
-
exports.renameExt = renameExt;
|
|
68
72
|
function normalizePath(value) {
|
|
69
73
|
return '"' + value.replace(/"/g, '\\"') + '"';
|
|
70
74
|
}
|
|
71
|
-
exports.normalizePath = normalizePath;
|
|
72
75
|
function getWebP_bin(name, pathname) {
|
|
73
76
|
if (pathname && fs.existsSync(pathname)) {
|
|
74
|
-
name +=
|
|
77
|
+
name += Image.PLATFORM_WIN32 ? '.exe' : '';
|
|
75
78
|
const bin = path.join(pathname, name);
|
|
76
79
|
return Image.sanitizeCmd(fs.existsSync(bin) ? bin : path.join(pathname, 'bin', name));
|
|
77
80
|
}
|
|
78
81
|
return require(name + '-bin');
|
|
79
82
|
}
|
|
80
|
-
exports.getWebP_bin = getWebP_bin;
|
|
81
83
|
function showInputType(value, outputType, finalAs) {
|
|
82
84
|
if (finalAs) {
|
|
83
85
|
outputType = 'image/' + finalAs;
|
|
84
86
|
}
|
|
85
87
|
return value && outputType !== value ? value.split('/').pop() + ' -> ' : '';
|
|
86
88
|
}
|
|
87
|
-
exports.showInputType = showInputType;
|
|
88
89
|
function showOutputType(value, outputType, finalAs) {
|
|
89
90
|
if (finalAs) {
|
|
90
91
|
outputType = 'image/' + finalAs;
|
|
91
92
|
}
|
|
92
93
|
return value !== outputType ? ' -> ' + outputType.split('/').pop() : '';
|
|
93
94
|
}
|
|
94
|
-
exports.showOutputType = showOutputType;
|