First Commit

This commit is contained in:
killerboss 2024-10-19 14:52:43 +02:00
commit 1696bc5dc6
8 changed files with 1509 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

8
.idea/.gitignore vendored Normal file
View file

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

11
.idea/downloader.iml Normal file
View file

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="EMPTY_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/downloader.iml" filepath="$PROJECT_DIR$/.idea/downloader.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

1259
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

8
Cargo.toml Normal file
View file

@ -0,0 +1,8 @@
[package]
name = "downloader"
version = "0.1.0"
edition = "2021"
[dependencies]
reqwest = { version = "0.12.8", features = ["stream"] }
tokio = "1.40.0"

208
src/lib.rs Normal file
View file

@ -0,0 +1,208 @@
use std::collections::VecDeque;
use reqwest::header::RANGE;
use reqwest::Client;
use std::fs::File;
use std::io::{BufWriter, Write};
/// Represents a download task with an ID, byte range, assigned worker, and status flags.
struct DownloadTask {
id: u64,
range_from: u64,
range_to: u64,
assigned_to: Option<u64>,
downloaded: bool,
inserted: bool,
}
/// Manages a queue of download tasks.
struct DownloadQueue {
queue: VecDeque<DownloadTask>,
}
impl DownloadQueue {
/// Creates a new, empty `DownloadQueue`.
fn new() -> DownloadQueue {
DownloadQueue {
queue: VecDeque::new(),
}
}
/// Adds a new download task to the queue.
///
/// # Arguments
///
/// * `id` - The unique identifier for the download task.
/// * `range_from` - The starting byte of the range to download.
/// * `range_to` - The ending byte of the range to download.
fn add_task(&mut self, id: u64, range_from: u64, range_to: u64) {
let task = DownloadTask {
id,
range_from,
range_to,
assigned_to: None,
downloaded: false,
inserted: false,
};
self.queue.push_back(task);
}
/// Assigns a download task to a worker.
///
/// # Arguments
///
/// * `worker_id` - The unique identifier of the worker.
/// * `id` - The unique identifier of the download task to assign.
fn assign_task(&mut self, worker_id: u64, id: u64) {
for task in self.queue.iter_mut() {
if task.id == id {
task.assigned_to = Some(worker_id);
break;
}
}
}
/// Marks a download task as downloaded.
///
/// # Arguments
///
/// * `id` - The unique identifier of the download task to mark as downloaded.
fn set_downloaded(&mut self, id: u64) {
for task in self.queue.iter_mut() {
if task.id == id {
task.downloaded = true;
break;
}
}
}
/// Marks a download task as inserted.
///
/// # Arguments
///
/// * `id` - The unique identifier of the download task to mark as inserted.
fn set_inserted(&mut self, id: u64) {
for task in self.queue.iter_mut() {
if task.id == id {
task.inserted = true;
break;
}
}
}
}
/// Represents a worker that processes download tasks.
struct Worker {
id: u64,
range_from: u64,
range_to: u64,
downloaded: u64,
download_queue: DownloadQueue,
link: String,
filename: String,
process: Option<tokio::task::JoinHandle<Result<(), reqwest::Error>>>,
}
impl Worker {
/// Creates a new `Worker`.
///
/// # Arguments
///
/// * `id` - The unique identifier of the worker.
/// * `range_from` - The starting byte of the range to download.
/// * `range_to` - The ending byte of the range to download.
/// * `download_queue` - The queue of download tasks.
/// * `link` - The URL to download from.
/// * `filename` - The name of the file to save the downloaded data to.
fn new(id: u64, range_from: u64, range_to: u64, download_queue: DownloadQueue, link: String, filename: String) -> Worker {
Worker {
id,
range_from,
range_to,
downloaded: 0,
download_queue,
link,
filename,
process: None,
}
}
/// Starts the worker to process a download task.
async fn work(&mut self) {
// Find the first unassigned task
let task = self.download_queue.queue.iter()
.find(|task| task.assigned_to.is_none())
.expect("No task found");
self.download_queue.assign_task(self.id, task.id);
self.process = Some(tokio::spawn(self.download_part(&*self.link, task.range_from, task.range_to, &*self.filename)));
}
/// Downloads a part of a file.
///
/// # Arguments
///
/// * `url` - The URL to download from.
/// * `range_from` - The starting byte of the range to download.
/// * `range_to` - The ending byte of the range to download.
/// * `file_path` - The path to save the downloaded data to.
async fn download_part(&mut self, url: &str, range_from: u64, range_to: u64, file_path: &str) -> Result<(), reqwest::Error> {
let client = Client::new();
let range_header_value = format!("bytes={}-{}", range_from, range_to);
let mut response = client
.get(url)
.header(RANGE, range_header_value)
.send()
.await?
.error_for_status()?;
let file = File::create(format!("{}.part{}", file_path, self.id)).expect("Failed to create file");
let mut writer = BufWriter::new(file);
let mut stream = response.bytes_stream();
while let Some(chunk) = stream.next().await {
let chunk = chunk?;
writer.write_all(&chunk).expect("Failed to write to file");
self.downloaded += chunk.len() as u64;
}
Ok(())
}
/// Waits for the worker to finish processing the download task.
async fn join(&mut self) {
if let Some(process) = self.process.take() {
process.await.expect("Failed to join worker").expect("Failed to download part");
}
}
/// Stops the worker from processing the download task.
async fn stop(&mut self) {
if let Some(process) = self.process.take() {
process.abort();
}
}
}
/// Initiates the download process.
///
/// # Arguments
///
/// * `url` - The URL to download from.
/// * `initial_workers` - The number of initial workers to start.
pub fn download(url: String, initial_workers: u64) -> u64 {
1
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = download("".to_string(), 1);
assert_eq!(result, 4);
}
}