$/, '');
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 userfrom = ''
let userto = ''
let profilefrom = null
let profileto = null
let tipocontofrom = shared_consts.AccountType.USER
let tipocontoto = shared_consts.AccountType.USER
if (mov.contocomfrom && mov.contocomfrom.name) {
userfrom += mov.contocomfrom.name
tipocontofrom = shared_consts.AccountType.COMMUNITY_ACCOUNT
}
if (mov.groupfrom) {
userfrom += mov.groupfrom.groupname
tipocontofrom = shared_consts.AccountType.COLLECTIVE_ACCOUNT
}
if (mov.userfrom) {
userfrom += mov.userfrom.username
profilefrom = mov.userfrom.profile
}
if (mov.contocomto && mov.contocomto.name) {
userto += mov.contocomto.name
tipocontoto = shared_consts.AccountType.COMMUNITY_ACCOUNT
}
if (mov.groupto) {
userto += mov.groupto.groupname
tipocontoto = shared_consts.AccountType.COLLECTIVE_ACCOUNT
}
if (mov.userto) {
userto += mov.userto.username
profileto = mov.userto.profile
}
// mystr = t('movement.from') + userfrom + ' ' + t('movement.to') + userto
return { userfrom: { profile: profilefrom, username: userfrom }, userto: { profile: profileto, username: userto }, tipocontofrom, tipocontoto }
},
ImageDownloader,
getDirUpload() {
return 'upload/'
},
getURLImg(idapp, table, username, img, checkifExist) {
let dirmain = '';
try {
// let dir = this.getdirByIdApp(idapp) + dirmain + '/' + this.getDirUpload();
let dir = this.getHostByIdApp(idapp) + '/' + this.getDirUpload() + shared_consts.getDirectoryImgByTable(table, username) + dirmain;
img = dir + img;
/*if (checkifExist) {
if (!fs.existsSync(img)) {
return '';
}
}*/
if (!this.sulServer()) {
// Finta Immagine
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';
},
getFieldsForAnnunci() {
let annunciFields = {
idMyGroup: {
type: String,
},
expiryDateAdv: {
type: Date,
},
enableExpiringAdv: {
type: Boolean,
},
// **ADDFIELD_ANNUNCI
};
return annunciFields;
},
// Funzione per pulire il nome del file
cleanFileName(filePath) {
// Ottieni solo il nome del file dall'intero percorso
// const fileName = filePath.split('/').pop();
// Rimuovi apostrofi e sostituisci gli spazi con underscore
const cleanedName = fileName.replace(/'/g, '').replace(/\s+/g, '_');
// Restituisci il percorso chiaro; puoi decidere di mantenere il resto del percorso
return cleanedName;
},
fixUrl(myurl, idapp) {
if (!myurl.startsWith('http')) {
return this.getHostByIdApp(idapp) + myurl;
}
if (myurl.startsWith('http://127.0.0.1')) {
//myurl = myurl.replace('http://127.0.0.1:8084/', 'https://riso.app/')
// Se è in locale allora metti una foto finta...
myurl = 'https://images.unsplash.com/photo-1464047736614-af63643285bf?q=80&w=2874&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D'
//myurl = myurl.replace('http://127.0.0.1', 'http://localhost')
}
return myurl;
}
};