- sistemato timeout corto

- corretto errori sulla generazione del PDF
- corretto alcune directory
- corretto fs.promise
- corretto CORS !
This commit is contained in:
Surya Paolo
2025-05-15 14:27:46 +02:00
parent a76d6c9b12
commit 768d299881
10 changed files with 402 additions and 383 deletions

View File

@@ -1,5 +1,5 @@
const os = require('os');
const fs = require('fs');
const fs = require('fs'); // 👈 Usa il modulo promises
const xml2js = require('xml2js');
const path = require('path');
const WebSocket = require('ws');
@@ -445,7 +445,7 @@ class ImageDownloader {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
// Verifica se il filepath esiste già
if (fs.existsSync(filepath)) {
if (await this.existsSync(filepath)) {
fs.unlinkSync(filepath);
}
@@ -533,7 +533,7 @@ class ImageDownloader {
console.error(`❌ Errore nel tentativo ${attempt}/${maxRetries}:`, error.message);
// Pulizia del file in caso di errore
if (fs.existsSync(filepath)) {
if (await this.existsSync(filepath)) {
fs.unlinkSync(filepath);
}
@@ -554,6 +554,15 @@ class ImageDownloader {
}
}
async existsSync(tempFolder) {
try {
await fs.access(tempFolder);
// La directory esiste
} catch {
// La directory NON esiste
}
}
// Funzione per estrarre il nome del file dall'URL
extractFileNameFromUrl(url) {
const match = url.match(/\/([^/?#]+)(?:[?#]|$)/);
@@ -727,6 +736,15 @@ module.exports = {
console.log(args);
},
existsSync: async function (tempFolder) {
try {
await fs.access(tempFolder);
// La directory esiste
} catch {
// La directory NON esiste
}
},
mylogserr: function (...args) {
console.error(args);
},
@@ -4184,15 +4202,15 @@ module.exports = {
});
},
mkdirpath(dirPath) {
async mkdirpath(dirPath) {
try {
if (!fs.existsSync(dirPath)) {
if (!await this.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
} catch (e) {
if (dirPath !== path.dirname(dirPath)) {
const myname = path.dirname(dirPath);
this.mkdirpath(myname);
await this.mkdirpath(myname);
// this.mkdirpath(dirPath);
}
}
@@ -4242,9 +4260,9 @@ module.exports = {
return namesurname;
},
isFileExists(filename) {
async isFileExists(filename) {
try {
return fs.existsSync(filename);
return await this.existsSync(filename);
} catch (e) {
return false
}
@@ -5958,7 +5976,7 @@ module.exports = {
img = dir + img;
/*if (checkifExist) {
if (!fs.existsSync(img)) {
if (!this.existsSync(img)) {
return '';
}
}*/
@@ -6132,9 +6150,9 @@ module.exports = {
server_constants.DIR_UPLOAD + '/products/' + productInfo.image_link.split('/').pop();
const savePath = path.resolve(__dirname, img); // Sostituisci con il percorso dove salvare l'immagine
let scaricaimg = !productInfo.imagefile || !fs.existsSync(savePath);
let scaricaimg = !productInfo.imagefile || !await this.existsSync(savePath);
if (!productInfo.imagefile && fs.existsSync(savePath)) {
if (!productInfo.imagefile && await this.existsSync(savePath)) {
// esiste il file, ma sul DB non è corretto
const stats = fs.statSync(savePath); // Ottieni informazioni sul file
@@ -6148,7 +6166,7 @@ module.exports = {
}
if (productInfo.imagefile && fs.existsSync(savePath)) {
if (productInfo.imagefile && await this.existsSync(savePath)) {
// esiste il file, ma sul DB non è corretto
const stats = fs.statSync(savePath); // Ottieni informazioni sul file
@@ -6191,7 +6209,7 @@ module.exports = {
const filecompleto = path.resolve(__dirname, img); // Sostituisci con il percorso dove salvare l'immagine
// Se non esiste lo scarico !
fileesistente = fs.existsSync(filecompleto);
fileesistente = await this.existsSync(filecompleto);
}
if (!vecchiomodo && (!productInfo.image_link || !fileesistente)) {
@@ -6296,4 +6314,5 @@ module.exports = {
return filename;
},
};