mirror of
https://github.com/Waradu/streamshare.git
synced 2025-04-22 04:14:06 +02:00
added download
This commit is contained in:
parent
2d635f5807
commit
156472e310
4 changed files with 69 additions and 3 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -1084,7 +1084,7 @@ checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "streamshare"
|
name = "streamshare"
|
||||||
version = "4.0.0"
|
version = "4.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"futures",
|
"futures",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "streamshare"
|
name = "streamshare"
|
||||||
version = "4.0.0"
|
version = "4.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
description = "Upload to streamshare library"
|
description = "Upload to streamshare library"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|
11
README.md
11
README.md
|
@ -42,4 +42,15 @@ match client.delete(file_identifier, deletion_token).await {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Download:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
let client = StreamShare::default();
|
||||||
|
|
||||||
|
match client.download(file_identifier, path).await {
|
||||||
|
Ok(_) => println!("File downloaded successfully"),
|
||||||
|
Err(e) => eprintln!("Error downloaded file: {}", e),
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Check [toss](https://github.com/Waradu/to-streamshare) for a better example on how to use it.
|
Check [toss](https://github.com/Waradu/to-streamshare) for a better example on how to use it.
|
||||||
|
|
57
src/lib.rs
57
src/lib.rs
|
@ -2,9 +2,9 @@ use futures::{SinkExt, StreamExt};
|
||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use tokio::fs;
|
|
||||||
use tokio::fs::File;
|
use tokio::fs::File;
|
||||||
use tokio::io::AsyncReadExt;
|
use tokio::io::AsyncReadExt;
|
||||||
|
use tokio::{fs, io::AsyncWriteExt};
|
||||||
use tokio_tungstenite::{
|
use tokio_tungstenite::{
|
||||||
connect_async,
|
connect_async,
|
||||||
tungstenite::{self, Message},
|
tungstenite::{self, Message},
|
||||||
|
@ -127,6 +127,61 @@ impl StreamShare {
|
||||||
Err(format!("Failed to delete file: {}", res.status()).into())
|
Err(format!("Failed to delete file: {}", res.status()).into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn download(
|
||||||
|
&self,
|
||||||
|
file_identifier: &str,
|
||||||
|
download_path: &str,
|
||||||
|
) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
let res = self
|
||||||
|
.client
|
||||||
|
.get(format!(
|
||||||
|
"https://{}/download/{}",
|
||||||
|
self.server_url, file_identifier
|
||||||
|
))
|
||||||
|
.send()
|
||||||
|
.await?
|
||||||
|
.error_for_status()?;
|
||||||
|
|
||||||
|
let unknown = format!("{}.unknown", file_identifier);
|
||||||
|
|
||||||
|
let file_name = res
|
||||||
|
.headers()
|
||||||
|
.get("content-disposition")
|
||||||
|
.and_then(|header| header.to_str().ok())
|
||||||
|
.and_then(|header_value| {
|
||||||
|
header_value.split(';').find_map(|part| {
|
||||||
|
let trimmed = part.trim();
|
||||||
|
if trimmed.starts_with("filename=") {
|
||||||
|
Some(trimmed.trim_start_matches("filename=").trim_matches('"'))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.unwrap_or(unknown.as_str());
|
||||||
|
|
||||||
|
let file_path = {
|
||||||
|
let path = Path::new(download_path);
|
||||||
|
if path.as_os_str().is_empty() {
|
||||||
|
Path::new("").join(file_name)
|
||||||
|
} else if path.is_dir() {
|
||||||
|
path.join(file_name)
|
||||||
|
} else {
|
||||||
|
path.to_path_buf()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if file_path.exists() {
|
||||||
|
return Err(format!("File already exists: {}", file_path.display()).into());
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut file = File::create(file_path).await?;
|
||||||
|
let content = res.bytes().await?;
|
||||||
|
|
||||||
|
file.write_all(&content).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for StreamShare {
|
impl Default for StreamShare {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue