mirror of
https://github.com/umami-software/umami.git
synced 2025-12-06 01:18:00 +01:00
feat(geo): add support for direct .mmdb URL and custom GEO_DATABASE_URL
- Support GEO_DATABASE_URL environment variable for custom database URL - Auto-detect .mmdb files and skip decompression - Maintain backward compatibility with tar.gz archives
This commit is contained in:
parent
7ac5913c86
commit
371ff47325
1 changed files with 66 additions and 24 deletions
|
|
@ -13,12 +13,18 @@ if (process.env.VERCEL && !process.env.BUILD_GEO) {
|
|||
|
||||
const db = 'GeoLite2-City';
|
||||
|
||||
let url = `https://raw.githubusercontent.com/GitSquared/node-geolite2-redist/master/redist/${db}.tar.gz`;
|
||||
// Support custom URL via environment variable
|
||||
let url = process.env.GEO_DATABASE_URL;
|
||||
|
||||
if (process.env.MAXMIND_LICENSE_KEY) {
|
||||
url =
|
||||
`https://download.maxmind.com/app/geoip_download` +
|
||||
`?edition_id=${db}&license_key=${process.env.MAXMIND_LICENSE_KEY}&suffix=tar.gz`;
|
||||
// Fallback to default URLs if not provided
|
||||
if (!url) {
|
||||
if (process.env.MAXMIND_LICENSE_KEY) {
|
||||
url =
|
||||
`https://download.maxmind.com/app/geoip_download` +
|
||||
`?edition_id=${db}&license_key=${process.env.MAXMIND_LICENSE_KEY}&suffix=tar.gz`;
|
||||
} else {
|
||||
url = `https://raw.githubusercontent.com/GitSquared/node-geolite2-redist/master/redist/${db}.tar.gz`;
|
||||
}
|
||||
}
|
||||
|
||||
const dest = path.resolve(process.cwd(), 'geo');
|
||||
|
|
@ -27,30 +33,66 @@ if (!fs.existsSync(dest)) {
|
|||
fs.mkdirSync(dest);
|
||||
}
|
||||
|
||||
const download = url =>
|
||||
// Check if URL points to a direct .mmdb file (already extracted)
|
||||
const isDirectMmdb = url.endsWith('.mmdb');
|
||||
|
||||
// Download handler for compressed tar.gz files
|
||||
const downloadCompressed = url =>
|
||||
new Promise(resolve => {
|
||||
https.get(url, res => {
|
||||
resolve(res.pipe(zlib.createGunzip({})).pipe(tar.t()));
|
||||
});
|
||||
});
|
||||
|
||||
download(url).then(
|
||||
res =>
|
||||
new Promise((resolve, reject) => {
|
||||
res.on('entry', entry => {
|
||||
if (entry.path.endsWith('.mmdb')) {
|
||||
const filename = path.join(dest, path.basename(entry.path));
|
||||
entry.pipe(fs.createWriteStream(filename));
|
||||
|
||||
console.log('Saved geo database:', filename);
|
||||
}
|
||||
});
|
||||
|
||||
res.on('error', e => {
|
||||
reject(e);
|
||||
});
|
||||
res.on('finish', () => {
|
||||
// Download handler for direct .mmdb files
|
||||
const downloadDirect = url =>
|
||||
new Promise((resolve, reject) => {
|
||||
https.get(url, res => {
|
||||
const filename = path.join(dest, path.basename(url));
|
||||
const fileStream = fs.createWriteStream(filename);
|
||||
|
||||
res.pipe(fileStream);
|
||||
|
||||
fileStream.on('finish', () => {
|
||||
fileStream.close();
|
||||
console.log('Saved geo database:', filename);
|
||||
resolve();
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
fileStream.on('error', e => {
|
||||
reject(e);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// Execute download based on file type
|
||||
if (isDirectMmdb) {
|
||||
downloadDirect(url).catch(e => {
|
||||
console.error('Failed to download geo database:', e);
|
||||
process.exit(1);
|
||||
});
|
||||
} else {
|
||||
downloadCompressed(url).then(
|
||||
res =>
|
||||
new Promise((resolve, reject) => {
|
||||
res.on('entry', entry => {
|
||||
if (entry.path.endsWith('.mmdb')) {
|
||||
const filename = path.join(dest, path.basename(entry.path));
|
||||
entry.pipe(fs.createWriteStream(filename));
|
||||
|
||||
console.log('Saved geo database:', filename);
|
||||
}
|
||||
});
|
||||
|
||||
res.on('error', e => {
|
||||
reject(e);
|
||||
});
|
||||
res.on('finish', () => {
|
||||
resolve();
|
||||
});
|
||||
}),
|
||||
).catch(e => {
|
||||
console.error('Failed to download geo database:', e);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue