@shibam/sticker-maker 1.0.10 โ 1.0.11
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/dist/index.d.ts +48 -0
- package/dist/index.js +222 -0
- package/dist/lib/ToWebp.d.ts +13 -0
- package/dist/lib/ToWebp.js +170 -0
- package/dist/lib/changeMetaInfo.d.ts +33 -0
- package/dist/lib/changeMetaInfo.js +141 -0
- package/dist/lib/extractMetaData.d.ts +7 -0
- package/dist/lib/extractMetaData.js +62 -0
- package/dist/lib/toGif.d.ts +1 -0
- package/dist/lib/toGif.js +67 -0
- package/dist/lib/utils.d.ts +39 -0
- package/dist/lib/utils.js +172 -0
- package/dist/types/StickerTypes.d.ts +6 -0
- package/dist/types/StickerTypes.js +10 -0
- package/dist/types/categoryType.d.ts +8 -0
- package/dist/types/categoryType.js +2 -0
- package/dist/types/metaInfoType.d.ts +11 -0
- package/dist/types/metaInfoType.js +2 -0
- package/package.json +1 -1
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import { Readable } from 'stream';
|
|
4
|
+
import { MetaDataType } from './types/metaInfoType.js';
|
|
5
|
+
import { StickerTypes } from './types/StickerTypes.js';
|
|
6
|
+
declare class Sticker {
|
|
7
|
+
private data;
|
|
8
|
+
metaInfo: Partial<MetaDataType>;
|
|
9
|
+
private buffer;
|
|
10
|
+
private mimeType;
|
|
11
|
+
private extType;
|
|
12
|
+
private utils;
|
|
13
|
+
private outBuffer;
|
|
14
|
+
private activeBuff;
|
|
15
|
+
private activeMeta;
|
|
16
|
+
constructor(data: Buffer | string | Readable, metaInfo?: Partial<MetaDataType>);
|
|
17
|
+
/**
|
|
18
|
+
* Initializes the Sticker instance.
|
|
19
|
+
* - Sets default values for metaInfo.
|
|
20
|
+
* - Reads and analyzes input data to determine MIME type and extension type.
|
|
21
|
+
* - Generates ID and quality metadata if not provided.
|
|
22
|
+
*/
|
|
23
|
+
private initialize;
|
|
24
|
+
/**
|
|
25
|
+
* Converts input data to a Buffer containing the converted content.
|
|
26
|
+
* @returns Promise<Buffer> A Promise resolving to the converted content as Buffer.
|
|
27
|
+
*/
|
|
28
|
+
toBuffer(): Promise<Buffer>;
|
|
29
|
+
/**
|
|
30
|
+
* Converts input data and writes it to a file at the specified outputPath.
|
|
31
|
+
* @param outputPath The path where the converted file will be saved.
|
|
32
|
+
* @returns Promise<void> A Promise resolving when the file is successfully written.
|
|
33
|
+
*/
|
|
34
|
+
toFile(outputPath: string): Promise<void>;
|
|
35
|
+
/**
|
|
36
|
+
* Changes the metadata of the sticker.
|
|
37
|
+
* @param newMetaInfo Partial metadata to update.
|
|
38
|
+
* @returns Promise<Buffer> A Promise resolving to the Buffer with updated metadata.
|
|
39
|
+
*/
|
|
40
|
+
changeMetaInfo(): Promise<Buffer>;
|
|
41
|
+
/**
|
|
42
|
+
* Extracts metadata from the provided data.
|
|
43
|
+
* @param data Buffer containing the data to extract metadata from.
|
|
44
|
+
* @returns Promise<Partial<MetaDataType>> A Promise resolving to the extracted metadata.
|
|
45
|
+
*/
|
|
46
|
+
extractMetaData(data: Buffer): Promise<Partial<MetaDataType>>;
|
|
47
|
+
}
|
|
48
|
+
export { Sticker, StickerTypes };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
exports.StickerTypes = exports.Sticker = void 0;
|
|
43
|
+
var fs_1 = __importDefault(require("fs"));
|
|
44
|
+
var utils_js_1 = __importDefault(require("./lib/utils.js"));
|
|
45
|
+
var ToWebp_js_1 = __importDefault(require("./lib/ToWebp.js"));
|
|
46
|
+
var StickerTypes_js_1 = require("./types/StickerTypes.js");
|
|
47
|
+
Object.defineProperty(exports, "StickerTypes", { enumerable: true, get: function () { return StickerTypes_js_1.StickerTypes; } });
|
|
48
|
+
var changeMetaInfo_js_1 = __importDefault(require("./lib/changeMetaInfo.js"));
|
|
49
|
+
var extractMetaData_js_1 = __importDefault(require("./lib/extractMetaData.js"));
|
|
50
|
+
var Sticker = /** @class */ (function () {
|
|
51
|
+
function Sticker(data, metaInfo) {
|
|
52
|
+
if (metaInfo === void 0) { metaInfo = {}; }
|
|
53
|
+
this.data = data;
|
|
54
|
+
this.metaInfo = metaInfo;
|
|
55
|
+
this.utils = new utils_js_1.default();
|
|
56
|
+
this.buffer = Buffer.from([]);
|
|
57
|
+
this.outBuffer = Buffer.from([]);
|
|
58
|
+
this.activeBuff = false;
|
|
59
|
+
this.activeMeta = false;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Initializes the Sticker instance.
|
|
63
|
+
* - Sets default values for metaInfo.
|
|
64
|
+
* - Reads and analyzes input data to determine MIME type and extension type.
|
|
65
|
+
* - Generates ID and quality metadata if not provided.
|
|
66
|
+
*/
|
|
67
|
+
Sticker.prototype.initialize = function () {
|
|
68
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
69
|
+
var _a, fileType, error_1;
|
|
70
|
+
var _b, _c, _d, _e, _f, _g;
|
|
71
|
+
return __generator(this, function (_h) {
|
|
72
|
+
switch (_h.label) {
|
|
73
|
+
case 0:
|
|
74
|
+
_h.trys.push([0, 3, , 4]);
|
|
75
|
+
_a = this;
|
|
76
|
+
return [4 /*yield*/, this.utils.buffer(this.data)];
|
|
77
|
+
case 1:
|
|
78
|
+
_a.buffer = _h.sent();
|
|
79
|
+
return [4 /*yield*/, this.utils.getMimeType(this.buffer)];
|
|
80
|
+
case 2:
|
|
81
|
+
fileType = _h.sent();
|
|
82
|
+
this.mimeType = fileType === null || fileType === void 0 ? void 0 : fileType.mime;
|
|
83
|
+
this.extType = fileType === null || fileType === void 0 ? void 0 : fileType.ext;
|
|
84
|
+
this.metaInfo.pack = (_b = this.metaInfo.pack) !== null && _b !== void 0 ? _b : '';
|
|
85
|
+
this.metaInfo.author = (_c = this.metaInfo.author) !== null && _c !== void 0 ? _c : '';
|
|
86
|
+
this.metaInfo.id = (_d = this.metaInfo.id) !== null && _d !== void 0 ? _d : this.utils.getId();
|
|
87
|
+
this.metaInfo.category = (_e = this.metaInfo.category) !== null && _e !== void 0 ? _e : [];
|
|
88
|
+
this.metaInfo.type = (_f = this.metaInfo.type) !== null && _f !== void 0 ? _f : StickerTypes_js_1.StickerTypes.DEFAULT;
|
|
89
|
+
this.metaInfo.quality = (_g = this.metaInfo.quality) !== null && _g !== void 0 ? _g : this.utils.getQuality(this.buffer);
|
|
90
|
+
this.metaInfo.background = this.metaInfo.background;
|
|
91
|
+
return [3 /*break*/, 4];
|
|
92
|
+
case 3:
|
|
93
|
+
error_1 = _h.sent();
|
|
94
|
+
throw new Error("Initialization error: ".concat(error_1));
|
|
95
|
+
case 4: return [2 /*return*/];
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* Converts input data to a Buffer containing the converted content.
|
|
102
|
+
* @returns Promise<Buffer> A Promise resolving to the converted content as Buffer.
|
|
103
|
+
*/
|
|
104
|
+
Sticker.prototype.toBuffer = function () {
|
|
105
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
106
|
+
var buffer, MetaBuff, error_2;
|
|
107
|
+
return __generator(this, function (_a) {
|
|
108
|
+
switch (_a.label) {
|
|
109
|
+
case 0:
|
|
110
|
+
_a.trys.push([0, 4, , 5]);
|
|
111
|
+
return [4 /*yield*/, this.initialize()];
|
|
112
|
+
case 1:
|
|
113
|
+
_a.sent();
|
|
114
|
+
return [4 /*yield*/, (0, ToWebp_js_1.default)(this.buffer, this.metaInfo, this.extType, this.mimeType)];
|
|
115
|
+
case 2:
|
|
116
|
+
buffer = _a.sent();
|
|
117
|
+
return [4 /*yield*/, new changeMetaInfo_js_1.default(this.metaInfo).add(buffer)];
|
|
118
|
+
case 3:
|
|
119
|
+
MetaBuff = _a.sent();
|
|
120
|
+
this.activeBuff = true;
|
|
121
|
+
this.outBuffer = this.extType !== 'gif' ? MetaBuff : buffer;
|
|
122
|
+
return [2 /*return*/, this.outBuffer];
|
|
123
|
+
case 4:
|
|
124
|
+
error_2 = _a.sent();
|
|
125
|
+
this.activeBuff = false;
|
|
126
|
+
throw new Error("Conversion to buffer failed: ".concat(error_2));
|
|
127
|
+
case 5: return [2 /*return*/];
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
};
|
|
132
|
+
/**
|
|
133
|
+
* Converts input data and writes it to a file at the specified outputPath.
|
|
134
|
+
* @param outputPath The path where the converted file will be saved.
|
|
135
|
+
* @returns Promise<void> A Promise resolving when the file is successfully written.
|
|
136
|
+
*/
|
|
137
|
+
Sticker.prototype.toFile = function (outputPath) {
|
|
138
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
139
|
+
var error_3;
|
|
140
|
+
var _a;
|
|
141
|
+
return __generator(this, function (_b) {
|
|
142
|
+
switch (_b.label) {
|
|
143
|
+
case 0:
|
|
144
|
+
_b.trys.push([0, 6, , 7]);
|
|
145
|
+
if (!(!this.activeBuff && !this.activeMeta && ((_a = this.extType) === null || _a === void 0 ? void 0 : _a.includes('webp')))) return [3 /*break*/, 2];
|
|
146
|
+
return [4 /*yield*/, this.changeMetaInfo()];
|
|
147
|
+
case 1:
|
|
148
|
+
_b.sent();
|
|
149
|
+
return [3 /*break*/, 4];
|
|
150
|
+
case 2: return [4 /*yield*/, this.toBuffer()];
|
|
151
|
+
case 3:
|
|
152
|
+
_b.sent();
|
|
153
|
+
_b.label = 4;
|
|
154
|
+
case 4: return [4 /*yield*/, fs_1.default.promises.writeFile(outputPath, this.outBuffer)];
|
|
155
|
+
case 5:
|
|
156
|
+
_b.sent();
|
|
157
|
+
return [3 /*break*/, 7];
|
|
158
|
+
case 6:
|
|
159
|
+
error_3 = _b.sent();
|
|
160
|
+
throw new Error("Conversion to file failed: ".concat(error_3));
|
|
161
|
+
case 7: return [2 /*return*/];
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
};
|
|
166
|
+
/**
|
|
167
|
+
* Changes the metadata of the sticker.
|
|
168
|
+
* @param newMetaInfo Partial metadata to update.
|
|
169
|
+
* @returns Promise<Buffer> A Promise resolving to the Buffer with updated metadata.
|
|
170
|
+
*/
|
|
171
|
+
Sticker.prototype.changeMetaInfo = function () {
|
|
172
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
173
|
+
var _a, error_4;
|
|
174
|
+
return __generator(this, function (_b) {
|
|
175
|
+
switch (_b.label) {
|
|
176
|
+
case 0:
|
|
177
|
+
_b.trys.push([0, 3, , 4]);
|
|
178
|
+
return [4 /*yield*/, this.initialize()];
|
|
179
|
+
case 1:
|
|
180
|
+
_b.sent();
|
|
181
|
+
_a = this;
|
|
182
|
+
return [4 /*yield*/, new changeMetaInfo_js_1.default(this.metaInfo).add(this.buffer)];
|
|
183
|
+
case 2:
|
|
184
|
+
_a.outBuffer = _b.sent();
|
|
185
|
+
this.activeMeta = true;
|
|
186
|
+
return [2 /*return*/, this.outBuffer];
|
|
187
|
+
case 3:
|
|
188
|
+
error_4 = _b.sent();
|
|
189
|
+
this.activeMeta = false;
|
|
190
|
+
throw new Error("Error changing meta info: ".concat(error_4));
|
|
191
|
+
case 4: return [2 /*return*/];
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
};
|
|
196
|
+
/**
|
|
197
|
+
* Extracts metadata from the provided data.
|
|
198
|
+
* @param data Buffer containing the data to extract metadata from.
|
|
199
|
+
* @returns Promise<Partial<MetaDataType>> A Promise resolving to the extracted metadata.
|
|
200
|
+
*/
|
|
201
|
+
Sticker.prototype.extractMetaData = function (data) {
|
|
202
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
203
|
+
var error_5;
|
|
204
|
+
return __generator(this, function (_a) {
|
|
205
|
+
switch (_a.label) {
|
|
206
|
+
case 0:
|
|
207
|
+
_a.trys.push([0, 2, , 3]);
|
|
208
|
+
return [4 /*yield*/, this.initialize()];
|
|
209
|
+
case 1:
|
|
210
|
+
_a.sent();
|
|
211
|
+
return [2 /*return*/, (0, extractMetaData_js_1.default)(data)];
|
|
212
|
+
case 2:
|
|
213
|
+
error_5 = _a.sent();
|
|
214
|
+
throw new Error("Error extracting meta data: ".concat(error_5));
|
|
215
|
+
case 3: return [2 /*return*/];
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
};
|
|
220
|
+
return Sticker;
|
|
221
|
+
}());
|
|
222
|
+
exports.Sticker = Sticker;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { MetaDataType } from '../types/metaInfoType.js';
|
|
3
|
+
/**
|
|
4
|
+
* Converts input buffer to WebP format with optional metadata and background color.
|
|
5
|
+
* @param buffer The input buffer to convert.
|
|
6
|
+
* @param metaInfo Optional metadata for the converted WebP.
|
|
7
|
+
* @param mimeExt File extension of the input buffer.
|
|
8
|
+
* @param mimeType MIME type of the input buffer.
|
|
9
|
+
* @returns Promise<Buffer | undefined> A Promise resolving to the converted content as Buffer.
|
|
10
|
+
* @throws Error if conversion fails.
|
|
11
|
+
*/
|
|
12
|
+
declare const ToWebp: (buffer: Buffer, metaInfo: Partial<MetaDataType>, mimeExt: string | undefined, mimeType: string | undefined) => Promise<Buffer>;
|
|
13
|
+
export default ToWebp;
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
var child_process_1 = require("child_process");
|
|
43
|
+
var path_1 = require("path");
|
|
44
|
+
var promises_1 = require("fs/promises");
|
|
45
|
+
var os_1 = require("os");
|
|
46
|
+
var util_1 = require("util");
|
|
47
|
+
var StickerTypes_js_1 = require("../types/StickerTypes.js");
|
|
48
|
+
var toGif_js_1 = __importDefault(require("./toGif.js"));
|
|
49
|
+
var execAsync = (0, util_1.promisify)(child_process_1.exec);
|
|
50
|
+
/**
|
|
51
|
+
* Converts input buffer to WebP format with optional metadata and background color.
|
|
52
|
+
* @param buffer The input buffer to convert.
|
|
53
|
+
* @param metaInfo Optional metadata for the converted WebP.
|
|
54
|
+
* @param mimeExt File extension of the input buffer.
|
|
55
|
+
* @param mimeType MIME type of the input buffer.
|
|
56
|
+
* @returns Promise<Buffer | undefined> A Promise resolving to the converted content as Buffer.
|
|
57
|
+
* @throws Error if conversion fails.
|
|
58
|
+
*/
|
|
59
|
+
var ToWebp = function (buffer, metaInfo, mimeExt, mimeType) { return __awaiter(void 0, void 0, void 0, function () {
|
|
60
|
+
var inputPath, outputPath, _a, args, outputBuffer, error_1;
|
|
61
|
+
var _b, _c;
|
|
62
|
+
return __generator(this, function (_d) {
|
|
63
|
+
switch (_d.label) {
|
|
64
|
+
case 0:
|
|
65
|
+
inputPath = (0, path_1.join)((0, os_1.tmpdir)(), "".concat(Date.now(), "input.").concat(mimeExt));
|
|
66
|
+
outputPath = (0, path_1.join)((0, os_1.tmpdir)(), "".concat(Date.now(), "output.webp"));
|
|
67
|
+
_d.label = 1;
|
|
68
|
+
case 1:
|
|
69
|
+
_d.trys.push([1, 8, 9, 11]);
|
|
70
|
+
// Write input buffer to temporary file
|
|
71
|
+
return [4 /*yield*/, (0, promises_1.writeFile)(inputPath, buffer)];
|
|
72
|
+
case 2:
|
|
73
|
+
// Write input buffer to temporary file
|
|
74
|
+
_d.sent();
|
|
75
|
+
if (!((mimeType === null || mimeType === void 0 ? void 0 : mimeType.includes('video')) || (mimeType === null || mimeType === void 0 ? void 0 : mimeType.includes('webp')))) return [3 /*break*/, 4];
|
|
76
|
+
return [4 /*yield*/, (0, toGif_js_1.default)(inputPath)];
|
|
77
|
+
case 3:
|
|
78
|
+
_a = _d.sent();
|
|
79
|
+
return [3 /*break*/, 5];
|
|
80
|
+
case 4:
|
|
81
|
+
_a = inputPath;
|
|
82
|
+
_d.label = 5;
|
|
83
|
+
case 5:
|
|
84
|
+
inputPath = _a;
|
|
85
|
+
args = [
|
|
86
|
+
'-i',
|
|
87
|
+
inputPath,
|
|
88
|
+
metaInfo.type === StickerTypes_js_1.StickerTypes.CIRCLE && !metaInfo.background
|
|
89
|
+
? StickerTypes_js_1.StickerTypes.CIRCLE
|
|
90
|
+
: metaInfo.type === StickerTypes_js_1.StickerTypes.SQUARE && !metaInfo.background
|
|
91
|
+
? StickerTypes_js_1.StickerTypes.SQUARE
|
|
92
|
+
: metaInfo.type === StickerTypes_js_1.StickerTypes.FIT && !metaInfo.background
|
|
93
|
+
? StickerTypes_js_1.StickerTypes.FIT
|
|
94
|
+
: '',
|
|
95
|
+
metaInfo.background
|
|
96
|
+
? "-filter_complex \"color=".concat(metaInfo.background, " [c]; [c][0]scale2ref[cs][0s];[cs][0s]overlay=shortest=1, unsharp=3:3:0.5").concat(metaInfo.type === StickerTypes_js_1.StickerTypes.CIRCLE ||
|
|
97
|
+
metaInfo.type === StickerTypes_js_1.StickerTypes.SQUARE ||
|
|
98
|
+
metaInfo.type === StickerTypes_js_1.StickerTypes.FIT
|
|
99
|
+
? ", ".concat(metaInfo.type.replace(/-vf/g, '').replace(/"/g, '').replace('-filter_complex', '').trim())
|
|
100
|
+
: '', "\"")
|
|
101
|
+
: '-pix_fmt yuva420p',
|
|
102
|
+
(mimeExt === null || mimeExt === void 0 ? void 0 : mimeExt.includes('gif')) || (mimeType === null || mimeType === void 0 ? void 0 : mimeType.includes('video'))
|
|
103
|
+
? [
|
|
104
|
+
'-c:v libwebp_anim',
|
|
105
|
+
'-auto-alt-ref 0',
|
|
106
|
+
'-lag-in-frames 0',
|
|
107
|
+
'-preset default',
|
|
108
|
+
'-t 10',
|
|
109
|
+
'-loop 0',
|
|
110
|
+
].join(' ')
|
|
111
|
+
: '-c:v libwebp',
|
|
112
|
+
'-q:v',
|
|
113
|
+
(_c = (_b = metaInfo.quality) === null || _b === void 0 ? void 0 : _b.toString()) !== null && _c !== void 0 ? _c : '50',
|
|
114
|
+
'-y',
|
|
115
|
+
outputPath
|
|
116
|
+
];
|
|
117
|
+
//console.log(args.join(" "))
|
|
118
|
+
// Execute ffmpeg command with constructed arguments
|
|
119
|
+
return [4 /*yield*/, execAsync("ffmpeg ".concat(args.join(' ')))
|
|
120
|
+
// Read converted WebP file back into a buffer
|
|
121
|
+
];
|
|
122
|
+
case 6:
|
|
123
|
+
//console.log(args.join(" "))
|
|
124
|
+
// Execute ffmpeg command with constructed arguments
|
|
125
|
+
_d.sent();
|
|
126
|
+
return [4 /*yield*/, (0, promises_1.readFile)(outputPath)];
|
|
127
|
+
case 7:
|
|
128
|
+
outputBuffer = _d.sent();
|
|
129
|
+
return [2 /*return*/, outputBuffer];
|
|
130
|
+
case 8:
|
|
131
|
+
error_1 = _d.sent();
|
|
132
|
+
console.error("Conversion error: ".concat(error_1));
|
|
133
|
+
throw new Error("Conversion failed: ".concat(error_1));
|
|
134
|
+
case 9:
|
|
135
|
+
// Cleanup temporary files
|
|
136
|
+
return [4 /*yield*/, cleanup(inputPath, outputPath).catch(function (e) { return console.error(e); })];
|
|
137
|
+
case 10:
|
|
138
|
+
// Cleanup temporary files
|
|
139
|
+
_d.sent();
|
|
140
|
+
return [7 /*endfinally*/];
|
|
141
|
+
case 11: return [2 /*return*/];
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
}); };
|
|
145
|
+
/**
|
|
146
|
+
* Cleans up temporary files used during conversion.
|
|
147
|
+
* @param inputPath Path to the input temporary file.
|
|
148
|
+
* @param outputPath Path to the output temporary file.
|
|
149
|
+
*/
|
|
150
|
+
function cleanup(inputPath, outputPath) {
|
|
151
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
152
|
+
var unlinkError_1;
|
|
153
|
+
return __generator(this, function (_a) {
|
|
154
|
+
switch (_a.label) {
|
|
155
|
+
case 0:
|
|
156
|
+
_a.trys.push([0, 2, , 3]);
|
|
157
|
+
return [4 /*yield*/, Promise.all([(0, promises_1.unlink)(inputPath), (0, promises_1.unlink)(outputPath)])];
|
|
158
|
+
case 1:
|
|
159
|
+
_a.sent();
|
|
160
|
+
return [3 /*break*/, 3];
|
|
161
|
+
case 2:
|
|
162
|
+
unlinkError_1 = _a.sent();
|
|
163
|
+
console.error('Error cleaning output file:', unlinkError_1);
|
|
164
|
+
return [3 /*break*/, 3];
|
|
165
|
+
case 3: return [2 /*return*/];
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
exports.default = ToWebp;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/// <reference path="../../src/lib/node-webpmux.d.ts" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import Image from 'node-webpmux';
|
|
4
|
+
/**
|
|
5
|
+
* The Exif class is responsible for handling the metadata (Exif data)
|
|
6
|
+
* for sticker images, particularly those used in messaging applications.
|
|
7
|
+
*/
|
|
8
|
+
export default class Exif {
|
|
9
|
+
private data;
|
|
10
|
+
private exif;
|
|
11
|
+
/**
|
|
12
|
+
* Constructs an Exif instance with the given options.
|
|
13
|
+
* @param options - An object containing metadata for the sticker.
|
|
14
|
+
*/
|
|
15
|
+
constructor(options: any);
|
|
16
|
+
/**
|
|
17
|
+
* Builds the Exif metadata as a Buffer.
|
|
18
|
+
* @returns A Buffer containing the constructed Exif data.
|
|
19
|
+
*/
|
|
20
|
+
build: () => Buffer;
|
|
21
|
+
/**
|
|
22
|
+
* Adds the Exif metadata to the given image.
|
|
23
|
+
* @param image - A Buffer or Image instance representing the image to which Exif data should be added.
|
|
24
|
+
* @returns A Promise that resolves to a Buffer containing the image with the added Exif data.
|
|
25
|
+
*/
|
|
26
|
+
add: (image: Buffer | Image.Image) => Promise<Buffer>;
|
|
27
|
+
/**
|
|
28
|
+
* Loads the image from a Buffer or string and returns an Image instance.
|
|
29
|
+
* @param image - A Buffer or string representing the image to be loaded.
|
|
30
|
+
* @returns A Promise that resolves to an Image instance.
|
|
31
|
+
*/
|
|
32
|
+
load: (image: Buffer | string) => Promise<Image.Image>;
|
|
33
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
var node_webpmux_1 = __importDefault(require("node-webpmux"));
|
|
43
|
+
var util_1 = require("util");
|
|
44
|
+
/**
|
|
45
|
+
* The Exif class is responsible for handling the metadata (Exif data)
|
|
46
|
+
* for sticker images, particularly those used in messaging applications.
|
|
47
|
+
*/
|
|
48
|
+
var Exif = /** @class */ (function () {
|
|
49
|
+
/**
|
|
50
|
+
* Constructs an Exif instance with the given options.
|
|
51
|
+
* @param options - An object containing metadata for the sticker.
|
|
52
|
+
*/
|
|
53
|
+
function Exif(options) {
|
|
54
|
+
var _this = this;
|
|
55
|
+
this.data = {};
|
|
56
|
+
this.exif = null;
|
|
57
|
+
/**
|
|
58
|
+
* Builds the Exif metadata as a Buffer.
|
|
59
|
+
* @returns A Buffer containing the constructed Exif data.
|
|
60
|
+
*/
|
|
61
|
+
this.build = function () {
|
|
62
|
+
var data = JSON.stringify(_this.data);
|
|
63
|
+
var exif = Buffer.concat([
|
|
64
|
+
Buffer.from([
|
|
65
|
+
0x49, 0x49, 0x2a, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x41, 0x57, 0x07, 0x00, 0x00, 0x00, 0x00,
|
|
66
|
+
0x00, 0x16, 0x00, 0x00, 0x00
|
|
67
|
+
]),
|
|
68
|
+
Buffer.from(data, 'utf-8')
|
|
69
|
+
]);
|
|
70
|
+
exif.writeUIntLE(new util_1.TextEncoder().encode(data).length, 14, 4);
|
|
71
|
+
return exif;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Adds the Exif metadata to the given image.
|
|
75
|
+
* @param image - A Buffer or Image instance representing the image to which Exif data should be added.
|
|
76
|
+
* @returns A Promise that resolves to a Buffer containing the image with the added Exif data.
|
|
77
|
+
*/
|
|
78
|
+
this.add = function (image) { return __awaiter(_this, void 0, void 0, function () {
|
|
79
|
+
var exif, _a;
|
|
80
|
+
var _this = this;
|
|
81
|
+
return __generator(this, function (_b) {
|
|
82
|
+
switch (_b.label) {
|
|
83
|
+
case 0:
|
|
84
|
+
exif = this.exif || this.build();
|
|
85
|
+
if (!(image instanceof node_webpmux_1.default.Image)) return [3 /*break*/, 1];
|
|
86
|
+
_a = image;
|
|
87
|
+
return [3 /*break*/, 3];
|
|
88
|
+
case 1: return [4 /*yield*/, (function () { return __awaiter(_this, void 0, void 0, function () {
|
|
89
|
+
var img;
|
|
90
|
+
return __generator(this, function (_a) {
|
|
91
|
+
switch (_a.label) {
|
|
92
|
+
case 0:
|
|
93
|
+
img = new node_webpmux_1.default.Image();
|
|
94
|
+
return [4 /*yield*/, img.load(image)];
|
|
95
|
+
case 1:
|
|
96
|
+
_a.sent();
|
|
97
|
+
return [2 /*return*/, img];
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
}); })()
|
|
101
|
+
// Set the Exif data on the image and save it.
|
|
102
|
+
];
|
|
103
|
+
case 2:
|
|
104
|
+
_a = _b.sent();
|
|
105
|
+
_b.label = 3;
|
|
106
|
+
case 3:
|
|
107
|
+
// Load the image if it is not already an instance of Image.Image.
|
|
108
|
+
image = _a;
|
|
109
|
+
// Set the Exif data on the image and save it.
|
|
110
|
+
image.exif = exif;
|
|
111
|
+
return [4 /*yield*/, image.save(null)];
|
|
112
|
+
case 4: return [2 /*return*/, _b.sent()];
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
}); };
|
|
116
|
+
/**
|
|
117
|
+
* Loads the image from a Buffer or string and returns an Image instance.
|
|
118
|
+
* @param image - A Buffer or string representing the image to be loaded.
|
|
119
|
+
* @returns A Promise that resolves to an Image instance.
|
|
120
|
+
*/
|
|
121
|
+
this.load = function (image) { return __awaiter(_this, void 0, void 0, function () {
|
|
122
|
+
var img;
|
|
123
|
+
return __generator(this, function (_a) {
|
|
124
|
+
switch (_a.label) {
|
|
125
|
+
case 0:
|
|
126
|
+
img = new node_webpmux_1.default.Image();
|
|
127
|
+
return [4 /*yield*/, img.load(image)];
|
|
128
|
+
case 1:
|
|
129
|
+
_a.sent();
|
|
130
|
+
return [2 /*return*/, img];
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
}); };
|
|
134
|
+
this.data['sticker-pack-id'] = options.id || '';
|
|
135
|
+
this.data['sticker-pack-name'] = options.pack || '';
|
|
136
|
+
this.data['sticker-pack-publisher'] = options.author || '';
|
|
137
|
+
this.data['emojis'] = options.category || ['๐น'];
|
|
138
|
+
}
|
|
139
|
+
return Exif;
|
|
140
|
+
}());
|
|
141
|
+
exports.default = Exif;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
var node_webpmux_1 = __importDefault(require("node-webpmux"));
|
|
43
|
+
/**
|
|
44
|
+
* Extracts metadata from a WebP image.
|
|
45
|
+
* @param {Buffer}image - The image buffer to extract metadata from
|
|
46
|
+
*/
|
|
47
|
+
var extractMetadata = function (image) { return __awaiter(void 0, void 0, void 0, function () {
|
|
48
|
+
var img, exif;
|
|
49
|
+
var _a, _b, _c;
|
|
50
|
+
return __generator(this, function (_d) {
|
|
51
|
+
switch (_d.label) {
|
|
52
|
+
case 0:
|
|
53
|
+
img = new node_webpmux_1.default.Image();
|
|
54
|
+
return [4 /*yield*/, img.load(image)];
|
|
55
|
+
case 1:
|
|
56
|
+
_d.sent();
|
|
57
|
+
exif = (_b = (_a = img.exif) === null || _a === void 0 ? void 0 : _a.toString('utf-8')) !== null && _b !== void 0 ? _b : '{}';
|
|
58
|
+
return [2 /*return*/, JSON.parse((_c = exif.substring(exif.indexOf('{'), exif.lastIndexOf('}') + 1)) !== null && _c !== void 0 ? _c : '{}')];
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}); };
|
|
62
|
+
exports.default = extractMetadata;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function toGif(inputPath: string): Promise<string>;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
var child_process_1 = require("child_process");
|
|
40
|
+
var util_1 = require("util");
|
|
41
|
+
var os_1 = require("os");
|
|
42
|
+
var path_1 = require("path");
|
|
43
|
+
var execAsync = (0, util_1.promisify)(child_process_1.exec);
|
|
44
|
+
function toGif(inputPath) {
|
|
45
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
46
|
+
var outputPath, args, error_1;
|
|
47
|
+
return __generator(this, function (_a) {
|
|
48
|
+
switch (_a.label) {
|
|
49
|
+
case 0:
|
|
50
|
+
outputPath = (0, path_1.join)((0, os_1.tmpdir)(), "".concat(Date.now(), ".gif"));
|
|
51
|
+
_a.label = 1;
|
|
52
|
+
case 1:
|
|
53
|
+
_a.trys.push([1, 3, , 4]);
|
|
54
|
+
args = "ffmpeg -i ".concat(inputPath, " -vf \"fps=10,scale=320:-1:flags=lanczos\" -t 7 -c:v gif -r 15 -preset slow -pix_fmt rgb24 ").concat(outputPath);
|
|
55
|
+
return [4 /*yield*/, execAsync(args)];
|
|
56
|
+
case 2:
|
|
57
|
+
_a.sent();
|
|
58
|
+
return [2 /*return*/, outputPath !== null && outputPath !== void 0 ? outputPath : ''];
|
|
59
|
+
case 3:
|
|
60
|
+
error_1 = _a.sent();
|
|
61
|
+
throw Error("Error converting to gif: ".concat(error_1));
|
|
62
|
+
case 4: return [2 /*return*/];
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
exports.default = toGif;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import { Readable } from 'stream';
|
|
4
|
+
export default class Utils {
|
|
5
|
+
constructor();
|
|
6
|
+
/**
|
|
7
|
+
* Converts various types of input data to a Buffer.
|
|
8
|
+
* @param data Input data as Buffer, string (file path), or Readable stream.
|
|
9
|
+
* @returns Promise<Buffer> A Promise resolving to the converted Buffer.
|
|
10
|
+
* @throws Error if conversion fails.
|
|
11
|
+
*/
|
|
12
|
+
buffer(data: Buffer | string | Readable): Promise<Buffer>;
|
|
13
|
+
/**
|
|
14
|
+
* Converts a Readable stream to a Buffer.
|
|
15
|
+
* @param stream Readable stream to convert.
|
|
16
|
+
* @returns Promise<Buffer> A Promise resolving to the converted Buffer.
|
|
17
|
+
*/
|
|
18
|
+
streamToBuffer(stream: Readable): Promise<Buffer>;
|
|
19
|
+
/**
|
|
20
|
+
* Determines the quality of the data based on its size.
|
|
21
|
+
* @param data Buffer containing the data.
|
|
22
|
+
* @returns number Quality value based on data size.
|
|
23
|
+
*/
|
|
24
|
+
getQuality(data: Buffer): number;
|
|
25
|
+
/**
|
|
26
|
+
* Determines the MIME type of the data buffer.
|
|
27
|
+
* @param data Buffer containing the data.
|
|
28
|
+
* @returns Promise<{ mime: string; ext: string }> A Promise resolving to an object with MIME type and extension.
|
|
29
|
+
*/
|
|
30
|
+
getMimeType(data: Buffer): Promise<{
|
|
31
|
+
mime: string;
|
|
32
|
+
ext: string;
|
|
33
|
+
} | undefined>;
|
|
34
|
+
/**
|
|
35
|
+
* Generates a random ID.
|
|
36
|
+
* @returns string A random alphanumeric ID.
|
|
37
|
+
*/
|
|
38
|
+
getId(): string;
|
|
39
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
39
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
40
|
+
if (ar || !(i in from)) {
|
|
41
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
42
|
+
ar[i] = from[i];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
46
|
+
};
|
|
47
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
48
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
49
|
+
};
|
|
50
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
51
|
+
var stream_1 = require("stream");
|
|
52
|
+
var fs_1 = __importDefault(require("fs"));
|
|
53
|
+
var file_type_1 = require("file-type");
|
|
54
|
+
var Utils = /** @class */ (function () {
|
|
55
|
+
function Utils() {
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Converts various types of input data to a Buffer.
|
|
59
|
+
* @param data Input data as Buffer, string (file path), or Readable stream.
|
|
60
|
+
* @returns Promise<Buffer> A Promise resolving to the converted Buffer.
|
|
61
|
+
* @throws Error if conversion fails.
|
|
62
|
+
*/
|
|
63
|
+
Utils.prototype.buffer = function (data) {
|
|
64
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
65
|
+
var buffer, _a, _b, error_1;
|
|
66
|
+
return __generator(this, function (_c) {
|
|
67
|
+
switch (_c.label) {
|
|
68
|
+
case 0:
|
|
69
|
+
_c.trys.push([0, 7, , 8]);
|
|
70
|
+
if (!(typeof data === 'string')) return [3 /*break*/, 2];
|
|
71
|
+
return [4 /*yield*/, fs_1.default.promises.readFile(data)];
|
|
72
|
+
case 1:
|
|
73
|
+
_a = _c.sent();
|
|
74
|
+
return [3 /*break*/, 6];
|
|
75
|
+
case 2:
|
|
76
|
+
if (!(data instanceof stream_1.Readable)) return [3 /*break*/, 4];
|
|
77
|
+
return [4 /*yield*/, this.streamToBuffer(data)];
|
|
78
|
+
case 3:
|
|
79
|
+
_b = _c.sent();
|
|
80
|
+
return [3 /*break*/, 5];
|
|
81
|
+
case 4:
|
|
82
|
+
_b = Buffer.from(data);
|
|
83
|
+
_c.label = 5;
|
|
84
|
+
case 5:
|
|
85
|
+
_a = _b;
|
|
86
|
+
_c.label = 6;
|
|
87
|
+
case 6:
|
|
88
|
+
buffer = _a;
|
|
89
|
+
return [2 /*return*/, buffer];
|
|
90
|
+
case 7:
|
|
91
|
+
error_1 = _c.sent();
|
|
92
|
+
throw new Error("Error converting to buffer: ".concat(error_1));
|
|
93
|
+
case 8: return [2 /*return*/];
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* Converts a Readable stream to a Buffer.
|
|
100
|
+
* @param stream Readable stream to convert.
|
|
101
|
+
* @returns Promise<Buffer> A Promise resolving to the converted Buffer.
|
|
102
|
+
*/
|
|
103
|
+
Utils.prototype.streamToBuffer = function (stream) {
|
|
104
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
105
|
+
var chunks;
|
|
106
|
+
return __generator(this, function (_a) {
|
|
107
|
+
chunks = [];
|
|
108
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
109
|
+
stream.on('data', function (chunk) {
|
|
110
|
+
if (Buffer.isBuffer(chunk)) {
|
|
111
|
+
chunks.push(chunk);
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
chunks.push(Buffer.from(chunk));
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
stream.on('end', function () {
|
|
118
|
+
resolve(Buffer.concat(chunks));
|
|
119
|
+
});
|
|
120
|
+
stream.on('error', function (err) {
|
|
121
|
+
reject(err);
|
|
122
|
+
});
|
|
123
|
+
})];
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
};
|
|
127
|
+
/**
|
|
128
|
+
* Determines the quality of the data based on its size.
|
|
129
|
+
* @param data Buffer containing the data.
|
|
130
|
+
* @returns number Quality value based on data size.
|
|
131
|
+
*/
|
|
132
|
+
Utils.prototype.getQuality = function (data) {
|
|
133
|
+
var buffer = Buffer.from(data);
|
|
134
|
+
var bytes = buffer.length / 1024;
|
|
135
|
+
var quality = bytes > 4 * 1024 ? 8 : bytes > 3 * 1024 ? 10 : bytes > 2 * 1024 ? 12 : 15;
|
|
136
|
+
return quality;
|
|
137
|
+
};
|
|
138
|
+
/**
|
|
139
|
+
* Determines the MIME type of the data buffer.
|
|
140
|
+
* @param data Buffer containing the data.
|
|
141
|
+
* @returns Promise<{ mime: string; ext: string }> A Promise resolving to an object with MIME type and extension.
|
|
142
|
+
*/
|
|
143
|
+
Utils.prototype.getMimeType = function (data) {
|
|
144
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
145
|
+
var fileType, error_2;
|
|
146
|
+
return __generator(this, function (_a) {
|
|
147
|
+
switch (_a.label) {
|
|
148
|
+
case 0:
|
|
149
|
+
_a.trys.push([0, 2, , 3]);
|
|
150
|
+
return [4 /*yield*/, (0, file_type_1.fromBuffer)(data)];
|
|
151
|
+
case 1:
|
|
152
|
+
fileType = _a.sent();
|
|
153
|
+
return [2 /*return*/, fileType];
|
|
154
|
+
case 2:
|
|
155
|
+
error_2 = _a.sent();
|
|
156
|
+
console.error("Error getting MIME type: ".concat(error_2));
|
|
157
|
+
return [2 /*return*/, undefined];
|
|
158
|
+
case 3: return [2 /*return*/];
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
};
|
|
163
|
+
/**
|
|
164
|
+
* Generates a random ID.
|
|
165
|
+
* @returns string A random alphanumeric ID.
|
|
166
|
+
*/
|
|
167
|
+
Utils.prototype.getId = function () {
|
|
168
|
+
return __spreadArray([], Array(5), true).map(function () { return Math.random().toString(36).substring(2, 15); }).join('');
|
|
169
|
+
};
|
|
170
|
+
return Utils;
|
|
171
|
+
}());
|
|
172
|
+
exports.default = Utils;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare enum StickerTypes {
|
|
2
|
+
DEFAULT = "",
|
|
3
|
+
SQUARE = "-vf \"crop=min(iw\\,ih):min(iw\\,ih)\"",
|
|
4
|
+
CIRCLE = "-filter_complex \"crop=min(iw\\,ih):min(iw\\,ih), scale=iw*2:ih*2, format=rgba, geq=r='r(X\\,Y)':g='g(X\\,Y)':b='b(X\\,Y)':a='if(lte(sqrt((X-W/2)^2 + (Y-H/2)^2)\\, min(W\\,H)/2)\\, 255\\, 0)'\"",
|
|
5
|
+
FIT = "-vf \"scale=iw*min(200/iw\\,200/ih):ih*min(200/iw\\,200/ih),pad=200:200:(200-iw*min(200/iw\\,200/ih))/2:(200-ih*min(200/iw\\,200/ih))/2\""
|
|
6
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StickerTypes = void 0;
|
|
4
|
+
var StickerTypes;
|
|
5
|
+
(function (StickerTypes) {
|
|
6
|
+
StickerTypes["DEFAULT"] = "";
|
|
7
|
+
StickerTypes["SQUARE"] = "-vf \"crop=min(iw\\,ih):min(iw\\,ih)\"";
|
|
8
|
+
StickerTypes["CIRCLE"] = "-filter_complex \"crop=min(iw\\,ih):min(iw\\,ih), scale=iw*2:ih*2, format=rgba, geq=r='r(X\\,Y)':g='g(X\\,Y)':b='b(X\\,Y)':a='if(lte(sqrt((X-W/2)^2 + (Y-H/2)^2)\\, min(W\\,H)/2)\\, 255\\, 0)'\"";
|
|
9
|
+
StickerTypes["FIT"] = "-vf \"scale=iw*min(200/iw\\,200/ih):ih*min(200/iw\\,200/ih),pad=200:200:(200-iw*min(200/iw\\,200/ih))/2:(200-ih*min(200/iw\\,200/ih))/2\"";
|
|
10
|
+
})(StickerTypes || (exports.StickerTypes = StickerTypes = {}));
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
type LoveEmojis = 'โค' | '๐' | '๐' | '๐' | '๐ป' | '๐' | '๐ฉโโคโ๐ฉ' | '๐จโโคโ๐จ' | '๐' | '๐ฉโโคโ๐โ๐ฉ' | '๐จโโคโ๐โ๐จ' | '๐งก' | '๐' | '๐' | '๐' | '๐' | '๐ค' | '๐' | 'โฃ' | '๐' | '๐' | '๐' | '๐' | '๐' | '๐' | '๐' | 'โฅ' | '๐' | '๐' | '๐ฉโโค๏ธโ๐โ๐ฉ' | '๐จโโค๏ธโ๐โ๐จ' | '๐ฉโโค๏ธโ๐จ' | '๐ฉโโค๏ธโ๐ฉ' | '๐จโโค๏ธโ๐จ' | '๐ฉโโค๏ธโ๐โ๐จ' | '๐ฌ' | '๐ญ' | '๐ซ' | '๐ฅฐ' | '๐' | '๐' | '๐' | '๐น' | '๐ฝ' | 'โฃ๏ธ' | 'โค๏ธ';
|
|
2
|
+
type HappyEmojis = '๐' | '๐' | '๐' | '๐' | '๐' | '๐
' | '๐' | '๐คฃ' | '๐' | '๐' | '๐' | '๐' | '๐คช' | '๐ค' | '๐บ' | '๐ธ' | '๐น' | 'โบ' | '๐' | '๐' | '๐ค' | '๐';
|
|
3
|
+
type SadEmojis = 'โน' | '๐ฃ' | '๐' | '๐ซ' | '๐ฉ' | '๐ข' | '๐ญ' | '๐' | '๐' | '๐' | '๐' | '๐ค' | '๐ ' | '๐ฅ' | '๐ฐ' | '๐จ' | '๐ฟ' | '๐พ' | '๐' | '๐โโ๏ธ' | '๐โโ๏ธ' | '๐' | '๐' | '๐ฅบ' | '๐ค' | 'โ๏ธ' | 'โ' | '๐ฉ' | '๐ง';
|
|
4
|
+
type AngryEmojis = '๐ฏ' | '๐ฆ' | '๐ง' | '๐ฎ' | '๐ฒ' | '๐' | '๐ฑ' | '๐คฏ' | '๐ณ' | 'โ' | 'โ' | '๐คฌ' | '๐ก' | '๐ ' | '๐' | '๐ฟ' | '๐พ' | '๐ค' | '๐ข' | '๐บ' | '๐ฏ๏ธ' | '๐' | '๐ฅต';
|
|
5
|
+
type GreetEmojis = '๐';
|
|
6
|
+
type CelebrateEmojis = '๐' | '๐' | '๐' | '๐' | '๐ฏโโ๏ธ' | '๐ฏ' | '๐ฏโโ๏ธ' | '๐' | '๐บ' | '๐ฅ' | 'โญ๏ธ' | 'โจ' | '๐ซ' | '๐' | '๐' | '๐ป' | '๐ฅ' | '๐พ' | '๐' | '๐ฐ';
|
|
7
|
+
export type category = LoveEmojis | HappyEmojis | SadEmojis | AngryEmojis | GreetEmojis | CelebrateEmojis;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { category } from './categoryType.js';
|
|
2
|
+
import { StickerTypes } from './StickerTypes.js';
|
|
3
|
+
export interface MetaDataType {
|
|
4
|
+
pack?: string;
|
|
5
|
+
author?: string;
|
|
6
|
+
id?: string | null;
|
|
7
|
+
category?: category[];
|
|
8
|
+
type?: StickerTypes | string;
|
|
9
|
+
quality?: number;
|
|
10
|
+
background?: String | undefined;
|
|
11
|
+
}
|