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.
190 lines • 7.23 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertHeicToJpeg = convertHeicToJpeg;
exports.saveFile = saveFile;
exports.removeFile = removeFile;
exports.imageMetadataGetter = imageMetadataGetter;
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 sharp;
try {
sharp = require('sharp');
console.log('[Sharp] sharp library loaded successfully');
}
catch (error) {
console.error('[Sharp] Failed to load sharp library:', error.message);
console.log('[Sharp] Image metadata getter will use fallback method');
sharp = null;
}
let imageSize;
try {
imageSize = require('image-size');
console.log('[ImageSize] image-size library loaded successfully');
}
catch (error) {
console.error('[ImageSize] Failed to load image-size library:', error.message);
imageSize = null;
}
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 {
if (!url || typeof url !== 'string') {
console.error('[saveFile] Invalid URL: not a string');
return null;
}
const trimmedUrl = url.trim();
if (!trimmedUrl.startsWith('http://') && !trimmedUrl.startsWith('https://')) {
console.error(`[saveFile] Invalid URL: only HTTP/HTTPS supported. Got: ${trimmedUrl.substring(0, 50)}...`);
return null;
}
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 });
}
let ext = '';
let urlPath = '';
try {
const parsedUrl = new URL(trimmedUrl);
urlPath = parsedUrl.pathname;
ext = path_1.default.extname(urlPath);
}
catch (urlError) {
console.warn(`[saveFile] Failed to parse URL: ${trimmedUrl.substring(0, 100)}...`);
ext = '';
}
if (!ext || ext === '') {
if (urlPath) {
const fileName = path_1.default.basename(urlPath);
const dotIndex = fileName.lastIndexOf('.');
if (dotIndex > 0) {
ext = fileName.substring(dotIndex);
}
}
if (!ext) {
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(trimmedUrl, { 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);
}
}
async function imageMetadataGetter(filePath) {
try {
const data = await fs_1.default.promises.readFile(filePath);
if (sharp) {
const metadata = await sharp(data).metadata();
if (metadata.width && metadata.height) {
return {
height: metadata.height,
width: metadata.width,
size: metadata.size || data.length,
};
}
else {
console.warn('[imageMetadataGetter] Sharp metadata incomplete, trying fallback');
}
}
if (imageSize) {
console.log('[imageMetadataGetter] Using image-size fallback for metadata');
const dimensions = imageSize(data);
if (dimensions.width && dimensions.height) {
return {
height: dimensions.height,
width: dimensions.width,
size: data.length,
};
}
else {
console.warn('[imageMetadataGetter] image-size metadata incomplete');
}
}
console.error('[imageMetadataGetter] No metadata library available');
return null;
}
catch (error) {
console.error('[imageMetadataGetter] Error reading file metadata:', error);
return null;
}
}
//# sourceMappingURL=helper.js.map