shrupl/src/main.rs

88 lines
2.1 KiB
Rust
Raw Normal View History

2025-05-22 17:34:44 +00:00
mod sharry;
2025-05-27 14:01:09 +00:00
use std::{path::PathBuf, time::Duration};
use clap::Parser;
use log::{error, info};
2025-05-22 17:34:44 +00:00
use ureq::Agent;
2025-05-27 14:01:09 +00:00
use sharry::{Alias, File, NewShareRequest, Share, Uri};
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Cli {
/// Protocol for Sharry instance
#[arg(
short, long,
default_value_t = String::from("https"),
value_parser = clap::builder::PossibleValuesParser::new(["http", "https"]),
)]
proto: String,
/// Name of the new share
#[arg(
short, long,
default_value_t = String::from("shrupl upload"),
)]
name: String,
/// Description of the new share
#[arg(short, long)]
desc: Option<String>,
/// Maximum number of views for the new share
#[arg(short, long, default_value_t = 10)]
views: u32,
/// Chunk size for uploading, in MiB
#[arg(short, long, default_value_t = 10)]
chunk: usize,
/// Base URL for Sharry Instance
url: String,
/// ID of a public alias to use
alias: String,
/// Files to upload to the new share
#[arg(value_name = "FILE")]
files: Vec<PathBuf>,
}
2025-05-17 23:57:52 +00:00
fn main() {
2025-05-22 17:34:44 +00:00
env_logger::init();
let agent: Agent = Agent::config_builder()
.timeout_global(Some(Duration::from_secs(5)))
.build()
.into();
2025-05-27 14:01:09 +00:00
let args = Cli::parse();
2025-05-27 17:18:29 +00:00
let files: Vec<File> = (args.files.iter())
.map(File::new)
.map(Result::unwrap)
.collect();
2025-05-27 14:01:09 +00:00
let alias = Alias::new(Uri::with_protocol(args.proto, args.url), args.alias);
2025-05-22 17:34:44 +00:00
2025-05-27 14:01:09 +00:00
let share = NewShareRequest::new(args.name, args.desc, 10);
2025-05-24 00:22:24 +00:00
let share = Share::create(&agent, &alias, share).unwrap();
2025-05-26 23:56:31 +00:00
info!("share: {share:?}");
2025-05-22 21:07:49 +00:00
2025-05-27 17:18:29 +00:00
for file in files {
let file = file.create(&agent, &share).unwrap();
info!("file: {file:?}");
for chunk in file.chunked(args.chunk * 1024 * 1024) {
info!("chunk len: {}", chunk.bytes.len());
file.upload_chunk(&agent, &alias, &chunk)
.inspect_err(|e| error!("error: {e}"))
.unwrap();
}
2025-05-26 20:31:22 +00:00
}
2025-05-22 21:07:49 +00:00
share.notify(&agent).unwrap();
2025-05-17 23:57:52 +00:00
}