n8n-nodes-zalo-nnt
Version:
Unofficial Zalo integration for n8n - Send messages, manage groups, user operations with QR login. No API key required, works via browser simulation.
111 lines • 4.42 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.saveFile = saveFile;
exports.removeFile = removeFile;
const axios_1 = __importDefault(require("axios"));
const fs_1 = __importDefault(require("fs"));
const os_1 = __importDefault(require("os"));
const path_1 = __importDefault(require("path"));
let heicConvert;
try {
heicConvert = require('heic-convert');
console.log('[HEIC] heic-convert library loaded successfully');
}
catch (error) {
console.error('[HEIC] Failed to load heic-convert library:', error.message);
console.log('[HEIC] HEIC files will not be converted to JPEG');
heicConvert = null;
}
async function convertHeicToJpeg(inputPath) {
console.log(`[HEIC] Starting conversion for: ${inputPath}`);
if (!heicConvert) {
console.error('[HEIC] heic-convert library not available, cannot convert HEIC files');
return null;
}
try {
const outputPath = inputPath.replace(/\.(heic|heif)$/i, '.jpg');
console.log(`[HEIC] Converting to: ${outputPath}`);
if (!fs_1.default.existsSync(inputPath)) {
console.error(`[HEIC] Input file does not exist: ${inputPath}`);
return null;
}
console.log(`[HEIC] Input file size: ${fs_1.default.statSync(inputPath).size} bytes`);
const inputBuffer = fs_1.default.readFileSync(inputPath);
const outputBuffer = await heicConvert({
buffer: inputBuffer,
format: 'JPEG',
quality: 0.9
});
fs_1.default.writeFileSync(outputPath, outputBuffer);
if (!fs_1.default.existsSync(outputPath)) {
console.error(`[HEIC] Output file was not created: ${outputPath}`);
return null;
}
console.log(`[HEIC] Output file size: ${fs_1.default.statSync(outputPath).size} bytes`);
fs_1.default.unlinkSync(inputPath);
console.log(`[HEIC] Removed original file: ${inputPath}`);
console.log(`[HEIC] Successfully converted HEIC to JPEG: ${outputPath}`);
return outputPath;
}
catch (error) {
console.error('[HEIC] Error converting HEIC to JPEG:', error);
console.error('[HEIC] Error details:', JSON.stringify(error, null, 2));
return null;
}
}
async function saveFile(url) {
try {
const n8nUserFolder = process.env.N8N_USER_FOLDER || path_1.default.join(os_1.default.homedir(), '.n8n');
const dataStoragePath = path_1.default.join(n8nUserFolder, 'temp_files');
if (!fs_1.default.existsSync(dataStoragePath)) {
fs_1.default.mkdirSync(dataStoragePath, { recursive: true });
}
const urlPath = new URL(url).pathname;
let ext = path_1.default.extname(urlPath);
if (!ext || ext === '') {
const fileName = path_1.default.basename(urlPath);
const dotIndex = fileName.lastIndexOf('.');
if (dotIndex > 0) {
ext = fileName.substring(dotIndex);
}
else {
ext = '.bin';
}
}
ext = ext.toLowerCase();
const timestamp = Date.now();
let filePath = path_1.default.join(dataStoragePath, `temp-${timestamp}${ext}`);
const { data } = await axios_1.default.get(url, { responseType: 'arraybuffer' });
fs_1.default.writeFileSync(filePath, data);
if (ext === '.heic' || ext === '.heif') {
console.log(`[HEIC] Detected HEIC/HEIF file: ${filePath}`);
const convertedPath = await convertHeicToJpeg(filePath);
if (convertedPath) {
filePath = convertedPath;
console.log(`[HEIC] Using converted file: ${filePath}`);
}
else {
console.error(`[HEIC] Failed to convert HEIC file, using original: ${filePath}`);
}
}
return filePath;
}
catch (error) {
console.error('Lỗi khi tải/lưu file:', error);
return null;
}
}
function removeFile(filePath) {
try {
if (fs_1.default.existsSync(filePath)) {
fs_1.default.unlinkSync(filePath);
}
}
catch (error) {
console.error('Lỗi khi xoá file:', error);
}
}
//# sourceMappingURL=helper.js.map