$/, '');
miastr = miastr.trimRight().replace(/[\n\r]+$/, '');
miastr = miastr.replace(/
<\/div>$/, '');
miastr = miastr.replace(/
$/, '');
miastr = miastr.replace(/
$/, '');
miastr = miastr.replace(/
$/, '');
return miastr;
},
rimuoviAtPrimoCarattere(stringa) {
if (stringa.charAt(0) === '@') {
return stringa.slice(1);
} else {
return stringa;
}
},
// Funzione per ottenere le coordinate di una singola città
async getCityCoordinates(city) {
try {
const response = await axios.get(`https://nominatim.openstreetmap.org/search?format=json&q=${city.descr},${city.prov},Italia`);
if (response.data.length > 0) {
city.lat = parseFloat(response.data[0].lat);
city.long = parseFloat(response.data[0].lon);
// console.log(`${city.descr}: Lat ${city.lat}, Long ${city.long}`);
return city;
} else {
console.error(`Coordinate non trovate per ${city.descr}, ${city.prov}`);
}
return null;
} catch (error) {
console.error(`Errore durante il recupero delle coordinate per ${city.descr}, ${city.prov}:`, error.message);
}
},
/**
* Funzione per scaricare dati GeoJSON dei comuni d'Italia e salvarli localmente.
* @param {string} url - L'URL dal quale scaricare i dati GeoJSON.
* @param {string} outputPath - Il percorso del file in cui salvare i dati GeoJSON.
*/
async downloadGeoJSON(url, outputPath) {
try {
// Effettua una richiesta GET all'URL specificato per scaricare i dati.
const response = await axios.get(url);
const data = response.data;
// Salva i dati GeoJSON in un file locale.
await fs.writeFile(outputPath, JSON.stringify(data, null, 2));
console.log(`Dati GeoJSON salvati in ${outputPath}`);
} catch (error) {
console.error('Errore durante il download o il salvataggio dei dati GeoJSON:', error);
}
},
arrotondaA2Decimali(valore) {
try {
return parseFloat(valore).toFixed(2);
} catch (error) {
return valore;
}
},
cleanHtmlForTelegram(htmlContent) {
try {
// Assicurati che l'HTML abbia un elemento body
const dom = new JSDOM(`${htmlContent}`);
const document = dom.window.document;
// Definisci i tag permessi su Telegram
const allowedTags = ['b', 'strong', 'i', 'em', 'u', 's', 'strike', 'del', 'a', 'code'];
// Seleziona tutti gli elementi all'interno del body
const allElements = document.body.querySelectorAll('*');
allElements.forEach(element => {
const tagName = element.tagName.toLowerCase();
if (!allowedTags.includes(tagName)) {
// Crea un nodo di testo e sostituisci l'elemento con quel nodo di testo
const textNode = document.createTextNode(element.textContent || '');
element.replaceWith(textNode);
} else {
// Se il tag è permesso, rimuovi tutti gli attributi ad eccezione di 'href' per i link
if (tagName === 'a') {
const href = element.getAttribute('href');
while (element.attributes.length > 0) {
element.removeAttribute(element.attributes[0].name);
}
if (href) {
element.setAttribute('href', href);
}
} else {
while (element.attributes.length > 0) {
element.removeAttribute(element.attributes[0].name);
}
}
}
});
// Ritorna l'HTML pulito
return document.body.innerHTML;
} catch (error) {
console.error('Errore durante la pulizia dell\'HTML:', error);
throw error; // Oppure, gestisci l'errore come preferisci
}
},
getHostWithNoHttporHttps(url) {
try {
const parsedUrl = new URL(url);
return parsedUrl.host;
} catch (error) {
console.error('Errore durante la pulizia dell\'HTML:', error);
throw error; // Oppure, gestisci l'errore come preferisci
}
},
// Crea un file con all'interno il nome del dominio per ogni app:
createFileWithDomainName() {
const arrapps = getApps();
const filename = server_constants.FILECONFIG_SERVER;
for (const app of arrapps) {
fs.writeFile(filename, this.getHostWithNoHttporHttps(app.host), function (err) {
if (err) {
console.log(err);
}
});
if (app.host_test) {
fs.writeFile(filename, this.getHostWithNoHttporHttps(app.host_test), function (err) {
if (err) {
console.log(err);
}
});
}
}
},
async listCollectionsBySize() {
let output = ""; // Variabile per memorizzare l'output
try {
const conn = mongoose.connection;
// Ottieni le collezioni e calcola la dimensione
const collections = await conn.db.listCollections().toArray();
// Calcola la dimensione per ciascuna collezione
const collectionSizes = await Promise.all(collections.map(async (collection) => {
const stats = await conn.db.collection(collection.name).stats();
return { name: collection.name, size: stats.size };
}));
// Ordina le collezioni per dimensione
collectionSizes.sort((a, b) => b.size - a.size); // Ordine decrescente
output += "Collezioni ordinate per dimensione:\n"; // Inizio dell'output
collectionSizes.forEach(collection => {
const sizeInMB = (collection.size / (1024 * 1024)).toFixed(2); // Converti in MB
output += `Collezione: ${collection.name}, Dimensione: ${sizeInMB} MB\n`; // Aggiungi all'output
});
return output; // Restituisci l'output alla fine
} catch (error) {
console.error("Errore:", error);
return `Errore: ${error.message}`; // Restituisci l'errore come stringa
}
},
getStringaConto(mov) {
let mystr = ''
let myfrom = ''
let myto = ''
let tipocontofrom = shared_consts.AccountType.USER
let tipocontoto = shared_consts.AccountType.USER
if (mov.contocomfrom && mov.contocomfrom.name) {
myfrom += mov.contocomfrom.name + ' '
tipocontofrom = shared_consts.AccountType.COMMUNITY_ACCOUNT
}
if (mov.groupfrom) {
myfrom += mov.groupfrom.groupname + ' '
tipocontofrom = shared_consts.AccountType.COLLECTIVE_ACCOUNT
}
if (mov.userfrom) {
myfrom += mov.userfrom.username + ' '
}
if (mov.contocomto && mov.contocomto.name) {
myto += mov.contocomto.name + ' '
tipocontoto = shared_consts.AccountType.COMMUNITY_ACCOUNT
}
if (mov.groupto) {
myto += mov.groupto.groupname + ' '
tipocontoto = shared_consts.AccountType.COLLECTIVE_ACCOUNT
}
if (mov.userto) {
myto += mov.userto.username + ' '
}
// mystr = t('movement.from') + myfrom + ' ' + t('movement.to') + myto
return { myfrom, myto, tipocontofrom, tipocontoto }
},
ImageDownloader,
getDirUpload() {
return 'upload/'
},
getURLImg(idapp, table, username, img, checkifExist) {
let dirmain = '';
try {
let dir = this.getdirByIdApp(idapp) + dirmain + '/' + this.getDirUpload();
let foldermain = dir + 'profile/' + username;
img = foldermain + '/' + table + '/' + img;
if (checkifExist) {
if (!fs.existsSync(img)) {
return '';
}
}
img = 'https://riso.app/upload/profile/SoniaVioletFlame/myskills/1000133092.jpg';
return img;
} catch (e) {
console.error('Err GetURLImg', e);
return '';
}
},
getLangByUsername(idapp, username) {
//++Todo LANG: Estrarre il Lang dall'username (user, group)
return 'it';
},
};