- Ricerca Titolo per nome o autore o ISBN o codice articolo
This commit is contained in:
@@ -243,7 +243,7 @@ const getTableContent = async (options) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Esegue la query per recuperare i dati
|
// Esegue la query per recuperare i dati
|
||||||
console.log('dataQuery', dataQuery);
|
// console.log('dataQuery', dataQuery);
|
||||||
const dataResponse = await axios.post(SERVER_A_URL + '/query', { query: dataQuery }, {
|
const dataResponse = await axios.post(SERVER_A_URL + '/query', { query: dataQuery }, {
|
||||||
headers: { 'x-api-key': API_KEY }
|
headers: { 'x-api-key': API_KEY }
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -29,7 +29,8 @@ const CatalogSchema = new Schema({
|
|||||||
},
|
},
|
||||||
foto_collana: IImg,
|
foto_collana: IImg,
|
||||||
idCollane: [{
|
idCollane: [{
|
||||||
type: Number,
|
type: Schema.Types.ObjectId,
|
||||||
|
ref: 'Collana',
|
||||||
}],
|
}],
|
||||||
argomenti: [{
|
argomenti: [{
|
||||||
type: String,
|
type: String,
|
||||||
@@ -72,6 +73,10 @@ const CatalogSchema = new Schema({
|
|||||||
date_updated: {
|
date_updated: {
|
||||||
type: Date,
|
type: Date,
|
||||||
},
|
},
|
||||||
|
lista_prodotti: [{
|
||||||
|
type: Schema.Types.ObjectId,
|
||||||
|
ref: 'Product',
|
||||||
|
}],
|
||||||
});
|
});
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -94,7 +99,7 @@ CatalogSchema.statics.executeQueryTable = function (idapp, params, user) {
|
|||||||
return tools.executeQueryTable(this, idapp, params, user);
|
return tools.executeQueryTable(this, idapp, params, user);
|
||||||
};
|
};
|
||||||
|
|
||||||
CatalogSchema.statics.findAllIdApp = async function (idapp) {
|
CatalogSchema.statics.OLD_findAllIdApp = async function (idapp) {
|
||||||
const Catalog = this;
|
const Catalog = this;
|
||||||
|
|
||||||
const arrrec = await Catalog.aggregate([
|
const arrrec = await Catalog.aggregate([
|
||||||
@@ -119,6 +124,94 @@ CatalogSchema.statics.findAllIdApp = async function (idapp) {
|
|||||||
return arrrec;
|
return arrrec;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
CatalogSchema.statics.findAllIdApp = async function (idapp) {
|
||||||
|
const Catalog = this;
|
||||||
|
|
||||||
|
let arrrec = await Catalog.find({ idapp })
|
||||||
|
.sort({ title: 1 }) // Ordina i risultati per titolo
|
||||||
|
.populate({
|
||||||
|
path: "idCollane", // Popola il campo idCollane
|
||||||
|
model: "Collana" // Specifica il modello della collezione Collana
|
||||||
|
})
|
||||||
|
.populate({
|
||||||
|
path: "lista_prodotti", // Popola il campo lista_prodotti
|
||||||
|
populate: {
|
||||||
|
path: "idProductInfo", // Popola il campo idProductInfo dentro ogni prodotto
|
||||||
|
model: "ProductInfo", // Specifica il modello della collezione ProductInfo
|
||||||
|
populate: [
|
||||||
|
{
|
||||||
|
path: "idCatProds",
|
||||||
|
model: "CatProd"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: "idSubCatProds",
|
||||||
|
model: "SubCatProd"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: "idAuthors",
|
||||||
|
model: "Author"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.populate({
|
||||||
|
path: "lista_prodotti",
|
||||||
|
populate: {
|
||||||
|
path: "idProducer",
|
||||||
|
model: "Producer"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.populate({
|
||||||
|
path: "lista_prodotti",
|
||||||
|
populate: {
|
||||||
|
path: "idProvider",
|
||||||
|
model: "Provider"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.populate({
|
||||||
|
path: "lista_prodotti",
|
||||||
|
populate: {
|
||||||
|
path: "idStorehouses",
|
||||||
|
model: "Storehouse"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.populate({
|
||||||
|
path: "lista_prodotti",
|
||||||
|
populate: {
|
||||||
|
path: "idScontisticas",
|
||||||
|
model: "Scontistica"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.populate({
|
||||||
|
path: "lista_prodotti",
|
||||||
|
populate: {
|
||||||
|
path: "idGasordine",
|
||||||
|
model: "Gasordine"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
;
|
||||||
|
|
||||||
|
const transformedArrRec = arrrec.map(catalog => ({
|
||||||
|
...catalog.toObject(), // Converte il documento Mongoose in un oggetto JavaScript puro
|
||||||
|
lista_prodotti: catalog.lista_prodotti.map(product => ({
|
||||||
|
...product.toObject(),
|
||||||
|
productInfo: {
|
||||||
|
...product.idProductInfo.toObject(), // Copia tutti i campi di idProductInfo
|
||||||
|
catprods: product.idProductInfo.idCatProds, // Rinomina idCatProds in catprods
|
||||||
|
subcatprods: product.idProductInfo.idSubCatProds,
|
||||||
|
collana: product.idProductInfo.idCollana,
|
||||||
|
authors: product.idProductInfo.idAuthors,
|
||||||
|
},
|
||||||
|
producer: product.idProducer,
|
||||||
|
storehouse: product.idStorehouses,
|
||||||
|
scontisticas: product.idScontisticas,
|
||||||
|
gasordine: product.idGasordine,
|
||||||
|
})),
|
||||||
|
}));
|
||||||
|
|
||||||
|
return transformedArrRec;
|
||||||
|
};
|
||||||
|
|
||||||
const Catalog = mongoose.model('Catalog', CatalogSchema);
|
const Catalog = mongoose.model('Catalog', CatalogSchema);
|
||||||
|
|
||||||
Catalog.createIndexes()
|
Catalog.createIndexes()
|
||||||
|
|||||||
@@ -56,6 +56,7 @@ const IDimensioni = new Schema({
|
|||||||
});
|
});
|
||||||
const IPagina = new Schema({
|
const IPagina = new Schema({
|
||||||
dimensioni: IDimensioni,
|
dimensioni: IDimensioni,
|
||||||
|
testo_title: IText,
|
||||||
testo_up: IText,
|
testo_up: IText,
|
||||||
testo_down: IText,
|
testo_down: IText,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -254,55 +254,44 @@ module.exports.executeQueryPickup = async function (idapp, params) {
|
|||||||
filterfindexact = { comune: strfind };
|
filterfindexact = { comune: strfind };
|
||||||
}
|
}
|
||||||
|
|
||||||
let limit = 10;
|
let limit = 20;
|
||||||
let risexact = [];
|
let risexact = [];
|
||||||
|
|
||||||
let filterfind = {
|
let filterfind = {
|
||||||
idapp,
|
idapp,
|
||||||
'productInfo.name': {
|
$or: [
|
||||||
$regex: `\\b${strfind}`, // Usa \\b per trovare solo le parole che iniziano con strfind
|
{
|
||||||
$options: 'i' // Rendi la ricerca case-insensitive
|
'productInfo.name': {
|
||||||
}
|
$regex: `\\b${strfind}`, // Cerca parole che iniziano con strfind
|
||||||
};
|
$options: 'i' // Rende la ricerca case-insensitive
|
||||||
|
}
|
||||||
/*
|
|
||||||
let aggr1 = [
|
|
||||||
{
|
|
||||||
$lookup: {
|
|
||||||
from: 'productinfos',
|
|
||||||
localField: 'idProductInfo',
|
|
||||||
foreignField: '_id',
|
|
||||||
as: 'productInfo'
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$lookup: {
|
|
||||||
from: 'authors',
|
|
||||||
localField: 'idAuthors',
|
|
||||||
foreignField: '_id',
|
|
||||||
as: 'authors'
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$match: { 'productInfo.name': strfind },
|
|
||||||
},
|
|
||||||
{ $limit: 1 },
|
|
||||||
{
|
|
||||||
$project: {
|
|
||||||
name: { $concat: ["$productInfo.name", " (", "$authors", ")"] },
|
|
||||||
},
|
},
|
||||||
},
|
{
|
||||||
];
|
'productInfo.code': {
|
||||||
|
$regex: `\\b${strfind}`, // Cerca parole che iniziano con strfind
|
||||||
|
$options: 'i' // Rende la ricerca case-insensitive
|
||||||
if (params.filter) {
|
}
|
||||||
filterfind = { ...params.filter, ...filterfind };
|
},
|
||||||
limit = 200;
|
{
|
||||||
} else {
|
'productInfo.sku': {
|
||||||
// risexact = await City.find(filterfindexact, {comune: 1, prov: 1, reg: 1}).lean();
|
$regex: `\\b${strfind}`, // Cerca parole che iniziano con strfind
|
||||||
risexact = await City.aggregate(aggr1);
|
$options: 'i' // Rende la ricerca case-insensitive
|
||||||
}
|
}
|
||||||
*/
|
},
|
||||||
|
{
|
||||||
|
'productInfo.authors.name': {
|
||||||
|
$regex: `\\b${strfind}`, // Cerca parole che iniziano con strfind nel nome dell'autore
|
||||||
|
$options: 'i' // Rende la ricerca case-insensitive
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'productInfo.authors.surname': {
|
||||||
|
$regex: `\\b${strfind}`, // Cerca parole che iniziano con strfind nel cognome dell'autore
|
||||||
|
$options: 'i' // Rende la ricerca case-insensitive
|
||||||
|
}
|
||||||
|
},
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
if (params.filter) {
|
if (params.filter) {
|
||||||
filterfind = { ...params.filter, ...filterfind };
|
filterfind = { ...params.filter, ...filterfind };
|
||||||
@@ -327,24 +316,34 @@ module.exports.executeQueryPickup = async function (idapp, params) {
|
|||||||
{
|
{
|
||||||
$lookup: {
|
$lookup: {
|
||||||
from: 'authors',
|
from: 'authors',
|
||||||
localField: 'idAuthors',
|
localField: 'productInfo.idAuthors',
|
||||||
foreignField: '_id',
|
foreignField: '_id',
|
||||||
as: 'authors'
|
as: 'productInfo.authors'
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
$unwind: {
|
||||||
|
path: '$authors',
|
||||||
|
preserveNullAndEmptyArrays: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
$match: filterfind,
|
$match: filterfind,
|
||||||
},
|
},
|
||||||
{ $limit: limit },
|
{ $limit: limit },
|
||||||
{
|
{
|
||||||
$project: {
|
$project: {
|
||||||
name: '$productInfo.name',
|
name: '$productInfo.name', // Nome del prodotto
|
||||||
},
|
authors: '$productInfo.authors',
|
||||||
},
|
productInfo: {
|
||||||
|
name: '$productInfo.name', // Nome del prodotto
|
||||||
|
authors: '$productInfo.authors',
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
// let ris = await City.find(filterfind, {comune: 1, prov: 1, reg: 1}).lean().limit(limit);
|
|
||||||
let ris = await this.aggregate(aggr2).limit(limit);
|
let ris = await this.aggregate(aggr2).limit(limit);
|
||||||
|
|
||||||
return [...risexact, ...ris];
|
return [...risexact, ...ris];
|
||||||
|
|||||||
@@ -115,16 +115,19 @@ async function findOrCreateCatProd(idapp, idArgomento, DescrArgomento) {
|
|||||||
|
|
||||||
function updateProductInfoCatProds(productInfo, reccatprod) {
|
function updateProductInfoCatProds(productInfo, reccatprod) {
|
||||||
if (productInfo) {
|
if (productInfo) {
|
||||||
const isChanged = (!productInfo.idCatProds || productInfo.idCatProds.length < 1) ||
|
// Controllo che nell'array productInfo.idCatProds ci sia esattamente 1 record e che sia uguale a reccatprod._id
|
||||||
(productInfo.idCatProds[0].toString() !== reccatprod._id.toString());
|
const cond3 = Array.isArray(productInfo.idCatProds) && productInfo.idCatProds[0].toString() !== reccatprod._id.toString();
|
||||||
|
const isChanged =
|
||||||
|
!Array.isArray(productInfo.idCatProds) || // Assicurati che sia un array
|
||||||
|
productInfo.idCatProds.length !== 1 || // L'array deve contenere esattamente 1 elemento
|
||||||
|
cond3; // Il primo elemento deve essere uguale a reccatprod._id
|
||||||
|
|
||||||
if (isChanged) {
|
if (isChanged) {
|
||||||
productInfo.idCatProds = [reccatprod._id];
|
productInfo.idCatProds = [reccatprod._id];
|
||||||
console.log('ARGOMENTO VARIATO!', reccatprod.name);
|
console.log('ARGOMENTO VARIATO!', reccatprod.name, ' Libro:', productInfo.name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function compressPdf(inputFile, outputFile, compressione) {
|
async function compressPdf(inputFile, outputFile, compressione) {
|
||||||
try {
|
try {
|
||||||
const tempFolder = path.join(cwd, "temp");
|
const tempFolder = path.join(cwd, "temp");
|
||||||
@@ -406,63 +409,138 @@ function fixURL(url) {
|
|||||||
|
|
||||||
async function downloadImgIfMissing(productInfo) {
|
async function downloadImgIfMissing(productInfo) {
|
||||||
|
|
||||||
if (!productInfo.image_link)
|
try {
|
||||||
return { prodInfo: null, aggiornatoimg: false };
|
if (tools.sulServer()) {
|
||||||
|
dirmain = '';
|
||||||
const relativeimg = productInfo.image_link.split('/').pop();
|
|
||||||
const img = '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);
|
|
||||||
|
|
||||||
if (!productInfo.imagefile && fs.existsSync(savePath)) {
|
|
||||||
// esiste il file, ma sul DB non è corretto
|
|
||||||
const stats = fs.statSync(savePath); // Ottieni informazioni sul file
|
|
||||||
|
|
||||||
if (stats.size > 0) { // Controlla se la dimensione del file è maggiore di zero
|
|
||||||
// Esiste il file ed è non vuoto, ma sul DB non è corretto
|
|
||||||
productInfo.imagefile = relativeimg;
|
|
||||||
return { prodInfo: productInfo, aggiornatoimg: true };
|
|
||||||
} else {
|
} else {
|
||||||
scaricaimg = true;
|
dirmain = server_constants.DIR_PUBLIC_LOCALE;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (productInfo.imagefile && fs.existsSync(savePath)) {
|
const vecchiomodo = false;
|
||||||
// esiste il file, ma sul DB non è corretto
|
|
||||||
const stats = fs.statSync(savePath); // Ottieni informazioni sul file
|
|
||||||
|
|
||||||
if (stats.size <= 0) { // Controlla se la dimensione del file è maggiore di zero
|
if (productInfo.image_link && vecchiomodo) {
|
||||||
scaricaimg = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (scaricaimg) {
|
const relativeimg = productInfo.image_link.split('/').pop();
|
||||||
// Download image from the URL productInfo.image_link
|
const img = tools.getdirByIdApp(productInfo.idapp) + dirmain +
|
||||||
productInfo.imagefile = relativeimg;
|
server_constants.DIR_UPLOAD + '/products/' + productInfo.image_link.split('/').pop();
|
||||||
|
const savePath = path.resolve(__dirname, img); // Sostituisci con il percorso dove salvare l'immagine
|
||||||
|
|
||||||
const downloader = new ImageDownloader();
|
let scaricaimg = !productInfo.imagefile || !fs.existsSync(savePath);
|
||||||
|
|
||||||
const aggiornatoimg = await downloader.downloadImage(productInfo.image_link, savePath,
|
if (!productInfo.imagefile && fs.existsSync(savePath)) {
|
||||||
{
|
// esiste il file, ma sul DB non è corretto
|
||||||
maxRetries: 3,
|
const stats = fs.statSync(savePath); // Ottieni informazioni sul file
|
||||||
initialDelay: 300,
|
|
||||||
timeout: 15000,
|
if (stats.size > 0) { // Controlla se la dimensione del file è maggiore di zero
|
||||||
}).then(result => {
|
// Esiste il file ed è non vuoto, ma sul DB non è corretto
|
||||||
if (result) {
|
productInfo.imagefile = relativeimg;
|
||||||
// console.log('Download completato con successo!');
|
return { prodInfo: productInfo, aggiornatoimg: true };
|
||||||
} else {
|
} else {
|
||||||
console.log('Download non riuscito.');
|
scaricaimg = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (productInfo.imagefile && fs.existsSync(savePath)) {
|
||||||
|
// esiste il file, ma sul DB non è corretto
|
||||||
|
const stats = fs.statSync(savePath); // Ottieni informazioni sul file
|
||||||
|
|
||||||
|
if (stats.size <= 0) { // Controlla se la dimensione del file è maggiore di zero
|
||||||
|
scaricaimg = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (scaricaimg && vecchiomodo) {
|
||||||
|
// Download image from the URL productInfo.image_link
|
||||||
|
productInfo.imagefile = relativeimg;
|
||||||
|
|
||||||
|
const downloader = new ImageDownloader();
|
||||||
|
|
||||||
|
const aggiornatoimg = await downloader.downloadImage(productInfo.image_link, savePath,
|
||||||
|
{
|
||||||
|
maxRetries: 3,
|
||||||
|
initialDelay: 300,
|
||||||
|
timeout: 15000,
|
||||||
|
}).then(result => {
|
||||||
|
if (result) {
|
||||||
|
// console.log('Download completato con successo!');
|
||||||
|
} else {
|
||||||
|
console.log('Download non riuscito.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
|
||||||
|
});
|
||||||
|
return { prodInfo: productInfo, aggiornatoimg: aggiornatoimg.ris };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let fileesistente = false;
|
||||||
|
if (productInfo.imagefile) {
|
||||||
|
// controlla se esiste il file
|
||||||
|
const img = tools.getdirByIdApp(productInfo.idapp) + dirmain +
|
||||||
|
server_constants.DIR_UPLOAD + '/products/' + productInfo.imagefile.split('/').pop();
|
||||||
|
const filecompleto = path.resolve(__dirname, img); // Sostituisci con il percorso dove salvare l'immagine
|
||||||
|
|
||||||
|
// Se non esiste lo scarico !
|
||||||
|
fileesistente = fs.existsSync(filecompleto);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!vecchiomodo && (!productInfo.image_link || !fileesistente)) {
|
||||||
|
|
||||||
|
let scarica_da_sito = !productInfo.imagefile;
|
||||||
|
|
||||||
|
if (!scarica_da_sito && productInfo.imagefile) {
|
||||||
|
scarica_da_sito = !fileesistente; // Se non esiste lo scarico !
|
||||||
|
}
|
||||||
|
|
||||||
|
if (scarica_da_sito) {
|
||||||
|
// date and time
|
||||||
|
productInfo.imagefile = 'img_' + new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
|
||||||
|
const img = tools.getdirByIdApp(productInfo.idapp) + dirmain +
|
||||||
|
server_constants.DIR_UPLOAD + '/products/' + productInfo.imagefile.split('/').pop();
|
||||||
|
let savePath = path.resolve(__dirname, img); // Sostituisci con il percorso dove salvare l'immagine
|
||||||
|
|
||||||
|
|
||||||
|
let link = 'https://www.gruppomacro.com/copertine.php?id_gm=' + productInfo.sku
|
||||||
|
|
||||||
|
const downloader = new ImageDownloader();
|
||||||
|
|
||||||
|
|
||||||
|
const aggiornatoimg = await downloader.downloadImage(link, savePath,
|
||||||
|
{
|
||||||
|
maxRetries: 3,
|
||||||
|
initialDelay: 300,
|
||||||
|
timeout: 15000,
|
||||||
|
nomefileoriginale: true,
|
||||||
|
}).then(result => {
|
||||||
|
if (result) {
|
||||||
|
// console.log('Download completato con successo!');
|
||||||
|
} else {
|
||||||
|
console.log('Download non riuscito.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
|
||||||
|
});
|
||||||
|
if (aggiornatoimg.filepath) {
|
||||||
|
const filenamebase = path.basename(aggiornatoimg.filepath);
|
||||||
|
// const img = '/upload/products/' + filenamebase;
|
||||||
|
productInfo.imagefile = filenamebase;
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return { prodInfo: productInfo, aggiornatoimg: aggiornatoimg.ris };
|
||||||
|
} else {
|
||||||
|
return { prodInfo: null, aggiornatoimg: false };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
});
|
} catch (e) {
|
||||||
return { prodInfo: productInfo, aggiornatoimg };
|
console.error('downloadImgIfMissing', e.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return { prodInfo: null, aggiornatoimg: false };
|
return { prodInfo: null, aggiornatoimg: false };
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function completaSettaggioProduct_AndProductInfo(arrcampi_productInfo, arrcampi_product, rec, product, productInfo) {
|
async function completaSettaggioProduct_AndProductInfo(arrcampi_productInfo, arrcampi_product, rec, product, productInfo) {
|
||||||
@@ -1042,8 +1120,7 @@ router.post('/import', authenticate, async (req, res) => {
|
|||||||
let imported = 0;
|
let imported = 0;
|
||||||
let errors = 0;
|
let errors = 0;
|
||||||
|
|
||||||
const ripopola = true;
|
const ripopola = false; //++MODIFICARE!
|
||||||
|
|
||||||
|
|
||||||
if (ripopola) {
|
if (ripopola) {
|
||||||
dataObjects = null;
|
dataObjects = null;
|
||||||
@@ -1131,6 +1208,8 @@ router.post('/import', authenticate, async (req, res) => {
|
|||||||
|
|
||||||
let importa = true;
|
let importa = true;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (!product.title || !product.sku)
|
if (!product.title || !product.sku)
|
||||||
importa = false;
|
importa = false;
|
||||||
|
|
||||||
@@ -1145,12 +1224,15 @@ router.post('/import', authenticate, async (req, res) => {
|
|||||||
if (!product.isbn) {
|
if (!product.isbn) {
|
||||||
const rectrovare = await ImportaIsbn.findOne({ sku: product.sku }).lean();
|
const rectrovare = await ImportaIsbn.findOne({ sku: product.sku }).lean();
|
||||||
if (rectrovare) {
|
if (rectrovare) {
|
||||||
product.isbn = rectrovare.isbn;
|
// se l'isbn non inizia con 'field' allora è buono
|
||||||
|
if (rectrovare.isbn && !rectrovare.isbn.startsWith('field')) {
|
||||||
|
product.isbn = rectrovare.isbn;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
nontrovati++;
|
nontrovati++;
|
||||||
console.log(`${nontrovati} - ISBN non trovato [sku=${product.sku} title=${product.title}]`)
|
// console.log(`${nontrovati} - ISBN non trovato [sku=${product.sku} title=${product.title}]`)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let productInfo = {
|
let productInfo = {
|
||||||
@@ -1169,12 +1251,21 @@ router.post('/import', authenticate, async (req, res) => {
|
|||||||
idCatProds: [],
|
idCatProds: [],
|
||||||
idSubCatProds: [],
|
idSubCatProds: [],
|
||||||
//img: 'upload/products/' + product.code + '.jpg',
|
//img: 'upload/products/' + product.code + '.jpg',
|
||||||
image_link: product.image_link ? product.image_link : '',
|
// image_link: product.image_link ? product.image_link : '',
|
||||||
img2: product.img2 ? product.img2 : '',
|
img2: product.img2 ? product.img2 : '',
|
||||||
img3: product.img3 ? product.img3 : '',
|
img3: product.img3 ? product.img3 : '',
|
||||||
img4: product.img4 ? product.img4 : '',
|
img4: product.img4 ? product.img4 : '',
|
||||||
checkout_link: product.checkout_link ? product.checkout_link : '',
|
checkout_link: product.checkout_link ? product.checkout_link : '',
|
||||||
}
|
}
|
||||||
|
const test = false;
|
||||||
|
|
||||||
|
if (test) {
|
||||||
|
productInfo.imagefile = ''
|
||||||
|
productInfo.image_link = '';
|
||||||
|
let { prodInfo, aggiornatoimg } = await downloadImgIfMissing(productInfo);
|
||||||
|
console.log('imagefile=', prodInfo.imagefile)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
let esisteindb = await ProductInfo.findOne({ code: productInfo.code }).lean();
|
let esisteindb = await ProductInfo.findOne({ code: productInfo.code }).lean();
|
||||||
if (esisteindb) {
|
if (esisteindb) {
|
||||||
@@ -1250,6 +1341,7 @@ router.post('/import', authenticate, async (req, res) => {
|
|||||||
} else {
|
} else {
|
||||||
|
|
||||||
arrcat = product.categories.trim().split(',');
|
arrcat = product.categories.trim().split(',');
|
||||||
|
productInfo.idCatProds = [];
|
||||||
for (const mycat of arrcat) {
|
for (const mycat of arrcat) {
|
||||||
let mycatstr = mycat.trim();
|
let mycatstr = mycat.trim();
|
||||||
|
|
||||||
@@ -1336,6 +1428,7 @@ router.post('/import', authenticate, async (req, res) => {
|
|||||||
|
|
||||||
if (product.subcat_name) {
|
if (product.subcat_name) {
|
||||||
arrsubcat = product.subcat_name.trim().split(',');
|
arrsubcat = product.subcat_name.trim().split(',');
|
||||||
|
productInfo.idSubCatProds = [];
|
||||||
for (const mysubcat of arrsubcat) {
|
for (const mysubcat of arrsubcat) {
|
||||||
let mysubcatstr = mysubcat.trim();
|
let mysubcatstr = mysubcat.trim();
|
||||||
|
|
||||||
@@ -1662,6 +1755,7 @@ router.post('/import', authenticate, async (req, res) => {
|
|||||||
|
|
||||||
if (product.subcat_name) {
|
if (product.subcat_name) {
|
||||||
arrsubcat = product.subcat_name.trim().split(',');
|
arrsubcat = product.subcat_name.trim().split(',');
|
||||||
|
productInfo.idSubCatProds = [];
|
||||||
for (const mysubcat of arrsubcat) {
|
for (const mysubcat of arrsubcat) {
|
||||||
let mysubcatstr = mysubcat.trim();
|
let mysubcatstr = mysubcat.trim();
|
||||||
|
|
||||||
|
|||||||
@@ -2304,7 +2304,7 @@ function uploadFile(req, res, version) {
|
|||||||
if (tools.sulServer()) {
|
if (tools.sulServer()) {
|
||||||
dirmain = '';
|
dirmain = '';
|
||||||
} else {
|
} else {
|
||||||
dirmain = '/public';
|
dirmain = server_constants.DIR_PUBLIC_LOCALE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2503,7 +2503,7 @@ function deleteFile(req, res, version) {
|
|||||||
let dirmain = '';
|
let dirmain = '';
|
||||||
if (version > 0) {
|
if (version > 0) {
|
||||||
if (!tools.sulServer()) {
|
if (!tools.sulServer()) {
|
||||||
dirmain = '/public';
|
dirmain = server_constants.DIR_PUBLIC_LOCALE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -540,7 +540,7 @@ connectToDatabase(connectionUrl, options)
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
if (!tools.sulServer()) {
|
if (!tools.sulServer()) {
|
||||||
dirmain = '/public';
|
dirmain = server_constants.DIR_PUBLIC_LOCALE;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const rec of arrlist) {
|
for (const rec of arrlist) {
|
||||||
|
|||||||
@@ -433,7 +433,8 @@ class ImageDownloader {
|
|||||||
initialDelay = 1000, // Ritardo iniziale
|
initialDelay = 1000, // Ritardo iniziale
|
||||||
maxDelay = 10000, // Ritardo massimo
|
maxDelay = 10000, // Ritardo massimo
|
||||||
timeout = 30000, // Timeout della richiesta
|
timeout = 30000, // Timeout della richiesta
|
||||||
validateContentType = true // Verifica del tipo di contenuto
|
validateContentType = true, // Verifica del tipo di contenuto
|
||||||
|
nomefileoriginale = true,
|
||||||
} = options;
|
} = options;
|
||||||
|
|
||||||
// Funzione per il backoff esponenziale
|
// Funzione per il backoff esponenziale
|
||||||
@@ -448,7 +449,6 @@ class ImageDownloader {
|
|||||||
fs.unlinkSync(filepath);
|
fs.unlinkSync(filepath);
|
||||||
}
|
}
|
||||||
|
|
||||||
const writer = fs.createWriteStream(filepath);
|
|
||||||
if (attempt > 1)
|
if (attempt > 1)
|
||||||
console.log(`📥 Tentativo ${attempt}/${maxRetries} - Scaricamento: ${url}`);
|
console.log(`📥 Tentativo ${attempt}/${maxRetries} - Scaricamento: ${url}`);
|
||||||
|
|
||||||
@@ -487,6 +487,28 @@ class ImageDownloader {
|
|||||||
downloadedBytes += chunk.length;
|
downloadedBytes += chunk.length;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let writer = null;
|
||||||
|
|
||||||
|
if (nomefileoriginale) {
|
||||||
|
// Estrai il nome del file dall'URL o da Content-Disposition
|
||||||
|
//let fileName = this.extractFileNameFromUrl(url) || this.extractFileNameFromHeaders(response.headers);
|
||||||
|
let fileName = path.basename(response.data.responseUrl);
|
||||||
|
|
||||||
|
// Se il nome del file non è specificato, genera un nome predefinito
|
||||||
|
if (!fileName) {
|
||||||
|
fileName = `image_${Date.now()}.jpg`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Genera il percorso completo del file
|
||||||
|
const fullPath = path.join(path.dirname(filepath), fileName);
|
||||||
|
|
||||||
|
filepath = fullPath;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Scrivi il file sul disco
|
||||||
|
writer = fs.createWriteStream(filepath);
|
||||||
|
|
||||||
response.data.pipe(writer);
|
response.data.pipe(writer);
|
||||||
|
|
||||||
await new Promise((resolve, reject) => {
|
await new Promise((resolve, reject) => {
|
||||||
@@ -501,8 +523,11 @@ class ImageDownloader {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
console.info(`✅ Immagine scaricata con successo in ${filepath}`);
|
console.info(`✅ Immagine scaricata con successo in ${filepath}`);
|
||||||
return true;
|
|
||||||
|
|
||||||
|
return { ris: true, filepath };
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`❌ Errore nel tentativo ${attempt}/${maxRetries}:`, error.message);
|
console.error(`❌ Errore nel tentativo ${attempt}/${maxRetries}:`, error.message);
|
||||||
@@ -514,7 +539,7 @@ class ImageDownloader {
|
|||||||
|
|
||||||
if (attempt === maxRetries) {
|
if (attempt === maxRetries) {
|
||||||
console.error(`Download fallito dopo ${maxRetries} tentativi: ${error.message}`);
|
console.error(`Download fallito dopo ${maxRetries} tentativi: ${error.message}`);
|
||||||
return false;
|
return { ris: false };
|
||||||
}
|
}
|
||||||
|
|
||||||
const delay = getDelay(attempt);
|
const delay = getDelay(attempt);
|
||||||
@@ -523,6 +548,24 @@ class ImageDownloader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Funzione per estrarre il nome del file dall'URL
|
||||||
|
extractFileNameFromUrl(url) {
|
||||||
|
const match = url.match(/\/([^/?#]+)(?:[?#]|$)/);
|
||||||
|
return match ? decodeURIComponent(match[1]) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Funzione per estrarre il nome del file da Content-Disposition
|
||||||
|
extractFileNameFromHeaders(headers) {
|
||||||
|
const contentDisposition = headers['content-disposition'];
|
||||||
|
if (contentDisposition) {
|
||||||
|
const match = contentDisposition.match(/filename="([^"]+)"/);
|
||||||
|
if (match) {
|
||||||
|
return decodeURIComponent(match[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -3380,7 +3423,7 @@ module.exports = {
|
|||||||
try {
|
try {
|
||||||
console.time(name);
|
console.time(name);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -101,6 +101,7 @@ module.exports = Object.freeze({
|
|||||||
PREFIX_IMG_SMALL: 'sm_',
|
PREFIX_IMG_SMALL: 'sm_',
|
||||||
|
|
||||||
DIR_UPLOAD: '/upload', //'/upload'
|
DIR_UPLOAD: '/upload', //'/upload'
|
||||||
|
DIR_PUBLIC_LOCALE: '/public',
|
||||||
|
|
||||||
Privacy: {
|
Privacy: {
|
||||||
all: 'all',
|
all: 'all',
|
||||||
|
|||||||
Reference in New Issue
Block a user