GeoIPDatabase *GeoIPDatabase::load(const QString &filename, QString &error) { GeoIPDatabase *db = nullptr; QFile file(filename); if (file.size() > MAX_FILE_SIZE) { error = tr("Unsupported database file size."); return 0; } if (!file.open(QFile::ReadOnly)) { error = file.errorString(); return 0; } db = new GeoIPDatabase(file.size()); if (file.read(reinterpret_cast<char *>(db->m_data), db->m_size) != db->m_size) { error = file.errorString(); delete db; return 0; } if (!db->parseMetadata(db->readMetadata(), error) || !db->loadDB(error)) { delete db; return 0; } return db; }
GeoIPDatabase *GeoIPDatabase::load(const QByteArray &data, QString &error) { GeoIPDatabase *db = nullptr; if (data.size() > MAX_FILE_SIZE) { error = tr("Unsupported database file size."); return 0; } db = new GeoIPDatabase(data.size()); memcpy(reinterpret_cast<char *>(db->m_data), data.constData(), db->m_size); if (!db->parseMetadata(db->readMetadata(), error) || !db->loadDB(error)) { delete db; return 0; } return db; }
void GeoIPManager::downloadFinished(const DownloadResult &result) { if (result.status != DownloadStatus::Success) { LogMsg(tr("Couldn't download GeoIP database file. Reason: %1").arg(result.errorString), Log::WARNING); return; } bool ok = false; const QByteArray data = Utils::Gzip::decompress(result.data, &ok); if (!ok) { LogMsg(tr("Could not decompress GeoIP database file."), Log::WARNING); return; } QString error; GeoIPDatabase *geoIPDatabase = GeoIPDatabase::load(data, error); if (geoIPDatabase) { if (!m_geoIPDatabase || (geoIPDatabase->buildEpoch() > m_geoIPDatabase->buildEpoch())) { if (m_geoIPDatabase) delete m_geoIPDatabase; m_geoIPDatabase = geoIPDatabase; LogMsg(tr("GeoIP database loaded. Type: %1. Build time: %2.") .arg(m_geoIPDatabase->type(), m_geoIPDatabase->buildEpoch().toString()), Log::INFO); const QString targetPath = Utils::Fs::expandPathAbs( specialFolderLocation(SpecialFolder::Data) + GEOIP_FOLDER); if (!QDir(targetPath).exists()) QDir().mkpath(targetPath); QFile targetFile(QString("%1/%2").arg(targetPath, GEOIP_FILENAME)); if (!targetFile.open(QFile::WriteOnly) || (targetFile.write(data) == -1)) LogMsg(tr("Couldn't save downloaded GeoIP database file."), Log::WARNING); else LogMsg(tr("Successfully updated GeoIP database."), Log::INFO); } else { delete geoIPDatabase; } } else { LogMsg(tr("Couldn't load GeoIP database. Reason: %1").arg(error), Log::WARNING); } }
void GeoIPManager::downloadFinished(const QString &url, QByteArray data) { Q_UNUSED(url); if (!Utils::Gzip::uncompress(data, data)) { Logger::instance()->addMessage(tr("Could not uncompress GeoIP database file."), Log::WARNING); return; } QString error; GeoIPDatabase *geoIPDatabase = GeoIPDatabase::load(data, error); if (geoIPDatabase) { if (!m_geoIPDatabase || (geoIPDatabase->buildEpoch() > m_geoIPDatabase->buildEpoch())) { if (m_geoIPDatabase) delete m_geoIPDatabase; m_geoIPDatabase = geoIPDatabase; Logger::instance()->addMessage(tr("GeoIP database loaded. Type: %1. Build time: %2.") .arg(m_geoIPDatabase->type()).arg(m_geoIPDatabase->buildEpoch().toString()), Log::INFO); QString targetPath = Utils::Fs::expandPathAbs( Utils::Fs::QDesktopServicesDataLocation() + GEOIP_FOLDER); if (!QDir(targetPath).exists()) QDir().mkpath(targetPath); QFile targetFile(QString("%1/%2").arg(targetPath).arg(GEOIP_FILENAME)); if (!targetFile.open(QFile::WriteOnly) || (targetFile.write(data) == -1)) { Logger::instance()->addMessage( tr("Couldn't save downloaded GeoIP database file."), Log::WARNING); } else { Logger::instance()->addMessage(tr("Successfully updated GeoIP database."), Log::INFO); } } else { delete geoIPDatabase; } } else { Logger::instance()->addMessage(tr("Couldn't load GeoIP database. Reason: %1").arg(error), Log::WARNING); } }