To modify your Rust script to save downloaded assets using the {placeID}/{versionNum}_{hash}.rbxl naming convention instead of the existing {placeID}/{versionNum}/{hash}.rbxl structure, you'll need to adjust the download_asset function and how the target paths are constructed.
Here's a step-by-step guide to implement the desired changes:
-
Extract the Hash from the URL:
Assuming that the hash is part of the filename in the URL (e.g., http://example.com/assets/{hash}.rbxl), you can extract it by removing the file extension from the original filename.
-
Modify the Target Directory and Filename:
Instead of creating a subdirectory for each versionNum, you'll construct the filename by combining versionNum and hash.
-
Update the download_asset Function:
Adjust the path construction within this function to follow the new naming convention.
Below is the updated code with the necessary modifications:
use std::fs;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::time::Duration;
use flate2::read::GzDecoder;
use futures::stream::{self, StreamExt};
use reqwest::Client;
use tokio::fs::{create_dir_all, File};
use tokio::io::AsyncWriteExt;
use tracing::{error, info};
#[tokio::main]
async fn main() {
// Initialize the tracing subscriber for logging
tracing_subscriber::fmt::init();
// Define the base directory where index.txt files are located (reading directory)
let read_dir = Path::new(r"C:\Users\Lukas\Downloads\lkjhgfd2");
// Define the base directory where downloads will be saved (download directory)
let download_dir = Path::new(r"C:\Users\Lukas\Downloads\lkjhgfd3");
// Ensure the download directory exists
if let Err(e) = fs::create_dir_all(&download_dir) {
error!(
"Failed to create download directory {}: {}",
download_dir.display(),
e
);
return;
}
// List of placeIDs
let place_ids = vec![
"10789565990",
"11181971058",
"11181982436",
"11182034319",
"11182037286",
"11263735303",
"11263873956",
"12647442701",
"33564694",
"4568576845",
"4737314314",
"4748963513",
"4956810680",
"4998492434",
"5000291183",
"5063342777",
"5229799451",
"5400427674",
"5465723678",
"5914914682",
"5972110179",
"6087359275",
"6701464352",
"6767882272",
"6768240609",
"6783491168",
"7162161171",
"7730658758",
"9048939285",
];
// Initialize HTTP client with timeout and other configurations
let client = Client::builder()
.timeout(Duration::from_secs(30))
.build()
.expect("Failed to build HTTP client");
// Process each placeID concurrently with a limit on concurrency
let concurrency_limit = 5; // Adjust based on your network capability
stream::iter(place_ids)
.for_each_concurrent(concurrency_limit, |place_id| {
let client = client.clone();
let read_dir = read_dir.to_path_buf();
let download_dir = download_dir.to_path_buf();
async move {
if let Err(e) = process_place_id(&client, &read_dir, &download_dir, place_id).await {
error!("Error processing placeID {}: {}", place_id, e);
}
}
})
.await;
info!("All downloads completed.");
}
/// Processes a single placeID:
/// - Reads index.txt from the reading directory
/// - Parses entries
/// - Downloads assets to the download directory
async fn process_place_id(
client: &Client,
read_dir: &PathBuf,
download_dir: &PathBuf,
place_id: &str,
) -> Result<(), Box<dyn std::error::Error>> {
info!("Processing placeID: {}", place_id);
// Path to index.txt in the reading directory
let index_path = read_dir.join(place_id).join("index.txt");
// Read index.txt content
let index_content = match fs::read_to_string(&index_path) {
Ok(content) => content,
Err(e) => {
error!("Failed to read {}: {}", index_path.display(), e);
return Err(Box::new(e));
}
};
// Parse index.txt lines into a vector of (version, url) tuples
let assets: Vec<(String, String)> = index_content
.lines()
.filter_map(|line| {
let parts: Vec<&str> = line.split('|').map(|s| s.trim()).collect();
if parts.len() == 2 {
Some((parts[0].to_string(), parts[1].to_string()))
} else {
None
}
})
.collect();
info!("---------------------");
info!("Found {} assets for placeID {}", assets.len(), place_id);
info!("---------------------");
// Define concurrency for asset downloads per placeID
let asset_concurrency = 10; // Adjust as needed
// Process asset downloads concurrently
stream::iter(assets)
.for_each_concurrent(asset_concurrency, |(version, url)| {
let client = client.clone();
let download_dir = download_dir.clone();
let place_id = place_id.to_string();
async move {
if let Err(e) =
download_asset(&client, &download_dir, &place_id, &version, &url).await
{
error!(
"Failed to download asset {} for placeID {}: {}",
url, place_id, e
);
}
}
})
.await;
info!("Completed placeID: {}", place_id);
Ok(())
}
/// Downloads a single asset, decompresses it if gzipped, and saves it to the designated path
async fn download_asset(
client: &Client,
download_dir: &PathBuf,
place_id: &str,
version: &str,
url: &str,
) -> Result<(), Box<dyn std::error::Error>> {
// Extract asset filename from URL
let original_filename = match url.split('/').last() {
Some(name) => name,
None => {
return Err("Failed to extract asset filename from URL".into());
}
};
// Remove any query parameters or fragments from the filename if present
let original_filename = original_filename.split('?').next().unwrap_or(original_filename);
let original_filename = original_filename.split('#').next().unwrap_or(original_filename);
// Extract the hash by removing the file extension
let hash = Path::new(original_filename)
.file_stem()
.ok_or("Failed to extract hash from filename")?
.to_string_lossy()
.to_string();
// Define the target directory (place_id) and construct the new filename
let target_dir = download_dir.join(place_id);
let asset_filename = format!("{}_{}.rbxl", version, hash);
let target_path = target_dir.join(&asset_filename);
// Create the target directory if it doesn't exist
create_dir_all(&target_dir).await?;
// Check if the file already exists to avoid re-downloading
if target_path.exists() {
info!("Asset already exists: {:?}", target_path);
return Ok(());
}
// Make the HTTP GET request
let response = client.get(url).send().await?;
if !response.status().is_success() {
return Err(format!("Failed to download {}: HTTP {}", url, response.status()).into());
}
// Determine if the content is gzipped
let is_gzipped = response
.headers()
.get(reqwest::header::CONTENT_ENCODING)
.and_then(|v| v.to_str().ok())
.map_or(false, |encoding| encoding.contains("gzip"));
// Collect the response bytes
let bytes = response.bytes().await?;
// Check magic numbers if header is absent
let is_gzipped = is_gzipped || bytes.starts_with(&[0x1f, 0x8b]);
// Decompress if gzipped
let data: Vec<u8> = if is_gzipped {
info!("Decompressing gzipped asset: {}", url);
let mut d = GzDecoder::new(&bytes[..]);
let mut decompressed = Vec::new();
d.read_to_end(&mut decompressed)?;
decompressed
} else {
bytes.to_vec()
};
// Write the (decompressed) data to the file
let mut file = File::create(&target_path).await?;
file.write_all(&data).await?;
info!("Downloaded and saved: {:?}", target_path);
Ok(())
}
Detailed Changes Explained
-
Extracting the Hash:
// Extract the hash by removing the file extension
let hash = Path::new(original_filename)
.file_stem()
.ok_or("Failed to extract hash from filename")?
.to_string_lossy()
.to_string();
- Purpose: This snippet takes the
original_filename (e.g., abcdef123456.rbxl) and extracts the hash part (abcdef123456) by removing the .rbxl extension.
- Method:
Path::new(original_filename).file_stem() retrieves the filename without its extension. If extraction fails, it returns an error.
-
Constructing the New Filename:
// Define the target directory (place_id) and construct the new filename
let target_dir = download_dir.join(place_id);
let asset_filename = format!("{}_{}.rbxl", version, hash);
let target_path = target_dir.join(&asset_filename);
- Purpose: Instead of creating a subdirectory for each
version, the script now saves the file directly under the place_id directory with the new naming convention.
- Filename Structure: Combines
version and hash separated by an underscore (_), followed by the .rbxl extension. For example, v1_abcdef123456.rbxl.
-
Removing the Version Subdirectory Creation:
-
Original Code:
let target_dir = download_dir.join(place_id).join(version);
-
Updated Code:
let target_dir = download_dir.join(place_id);
-
Purpose: By removing .join(version), the script avoids creating a subdirectory for each version and places all downloaded files directly under the place_id directory.
-
Avoiding Name Conflicts:
By combining version and hash in the filename, you ensure that each file is uniquely identifiable without needing separate directories for versions. This approach simplifies the directory structure and makes it easier to locate specific assets.
-
Handling Existing Files:
Additional Recommendations
-
Error Handling:
Ensure that the extraction of the hash is reliable. If the URL structure varies, you might need a more robust method to extract the hash.
-
Logging:
The existing logging using tracing is sufficient. However, consider adding more detailed logs if needed for debugging purposes.
-
Concurrency Limits:
Adjust the concurrency_limit and asset_concurrency based on your system's capabilities and network bandwidth to optimize download performance without overloading resources.
-
Path Handling:
Ensure that the paths (read_dir and download_dir) are correctly specified and accessible. Consider making them configurable via command-line arguments or a configuration file for flexibility.
-
Dependency Management:
Ensure that your Cargo.toml includes all necessary dependencies with appropriate versions. Here's a sample snippet based on your code:
[dependencies]
tokio = { version = "1.28", features = ["full"] }
reqwest = { version = "0.11", features = ["json", "gzip"] }
flate2 = "1.0"
futures = "0.3"
tracing = "0.1"
tracing-subscriber = "0.3"
Example Output
After making these changes, the downloaded files will be saved in the download_dir following the {placeID}/{versionNum}_{hash}.rbxl structure. For example:
C:\Users\Lukas\Downloads\lkjhgfd3
├── 10789565990
│ ├── v1_abcdef123456.rbxl
│ ├── v2_123456abcdef.rbxl
│ └── ...
├── 11181971058
│ ├── v1_7890ghijk123.rbxl
│ └── ...
└── ...
This structure maintains all assets for a specific placeID within a single directory, with each asset uniquely identified by its versionNum and hash.
Testing the Changes
Before deploying the script for all assets, it's advisable to test it with a subset of placeIDs and assets to ensure that the new path structure works as expected. Verify that:
- Files are saved with the correct
{versionNum}_{hash}.rbxl naming convention.
- There are no naming conflicts or overwrites unless intended.
- The script correctly handles already existing files by skipping downloads.
- Compressed files are properly decompressed and saved.
By following these modifications, your script will now save downloaded assets in the desired {placeID}/{versionNum}_{hash}.rbxl format, streamlining your directory structure and making asset management more straightforward.