Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the default location for database files to the data local directory #17

Merged
merged 3 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Change the default location for database files to the data local dire…
…ctory #16
  • Loading branch information
qtfkwk committed May 6, 2024
commit 7f610a66036e79914598d2441696d080b5dc8b83
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ Download the city databases, v4 and v6 from the repository using this command:

```sh
# IPV4 database
curl -LS https://raw.githubusercontent.com/wiseaidev/ipcap/main/data/geo_ip_city_v4.dat --create-dirs -o ~/ipcap/geo_ip_city_v4.dat
curl -LS https://raw.githubusercontent.com/wiseaidev/ipcap/main/data/geo_ip_city_v4.dat --create-dirs -o ~/.local/share/ipcap/geo_ip_city_v4.dat

# IPV6 database
curl -LS https://raw.githubusercontent.com/wiseaidev/ipcap/main/data/geo_ip_city_v6.dat --create-dirs -o ~/ipcap/geo_ip_city_v6.dat
curl -LS https://raw.githubusercontent.com/wiseaidev/ipcap/main/data/geo_ip_city_v6.dat --create-dirs -o ~/.local/share/ipcap/geo_ip_city_v6.dat
```

This will download the `data/geo_ip_city_v4.dat` and or `data/geo_ip_city_v4.dat` database(s) from the repository and put it under `~/ipcap/`.
This will download the `data/geo_ip_city_v4.dat` and or `data/geo_ip_city_v4.dat` database(s) from the repository and put it under `~/.local/share/ipcap/`.

If, for some reason, you decide to change this file location, just set this environment variable to help the CLI read this file. To set the environment variable before running your Rust program, you can do something like:

Expand All @@ -66,6 +66,9 @@ export IPCAP_FILE_PATH=/your/custom/path/geo_ip_city_v6.dat

Replace `/your/custom/path/geo_ip_city_v4.dat` with the desired file path. If the environment variable is not set, the program will use the default path (`/home/username/ipcap/geo_ip_city_v4.dat`).

> [!NOTE]
The default location to find the databases is `~/Library/Application Support/ipcap/` on macOS, and `C:\Users\Username\AppData\Local\ipcap\` on Windows.

> [!NOTE]
The databases were shamelessly taken from the fedora website at [https://src.fedoraproject.org/repo/pkgs/GeoIP-GeoLite-data/](https://src.fedoraproject.org/repo/pkgs/GeoIP-GeoLite-data/).

Expand Down
41 changes: 16 additions & 25 deletions src/geo_ip_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ use crate::designated_market_area::DesignatedMarketArea;
use crate::errors::GeoIpReaderError;
use crate::time_zones::time_zone_by_country;
use crate::utils::{ip_to_number, read_data};
use dirs::home_dir;
use std::env;
use std::fs::File;
use std::io::{Read, Seek, SeekFrom};
use std::{
env,
fs::File,
io::{Read, Seek, SeekFrom},
path::PathBuf,
};

/// `GeoIpReader` represents a reader for GeoIP databases, allowing the retrieval
/// of information based on IP addresses.
Expand Down Expand Up @@ -73,32 +75,21 @@ where
/// }
/// ```
pub fn new(type_: &str) -> Result<GeoIpReader<File>, GeoIpReaderError> {
const ENV_VAR_NAME: &str = "IPCAP_FILE_PATH";
let file_path = match env::var(ENV_VAR_NAME) {
Ok(val) => val,
Err(_) => {
let default_path = match type_ {
"v4" => {
let mut path = home_dir().unwrap_or_default();
path.push("ipcap");
path.push("geo_ip_city_v4.dat");
path
}
"v6" => {
let mut path = home_dir().unwrap_or_default();
path.push("ipcap");
path.push("geo_ip_city_v6.dat");
path
}
let file_path = match env::var("IPCAP_FILE_PATH") {
Ok(val) => PathBuf::from(val),
Err(_) => dirs::data_local_dir()
.expect("Failed to get the data local directory")
.join(env!("CARGO_PKG_NAME"))
.join(match type_ {
"v4" => "geo_ip_city_v4.dat",
"v6" => "geo_ip_city_v6.dat",
_ => {
return Err(GeoIpReaderError::OpenFileError);
}
};
default_path.to_string_lossy().into_owned()
}
}),
};

let fp = File::open(&file_path).map_err(|_| GeoIpReaderError::OpenFileError)?;
let fp = File::open(file_path).map_err(|_| GeoIpReaderError::OpenFileError)?;

let mut geoip_reader = GeoIpReader {
fp,
Expand Down
Loading