diff --git a/src/main.rs b/src/main.rs index 16daa14..d91bf67 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ mod sharry; use log::info; -use sharry::{Alias, NewShareRequest, Share}; +use sharry::{Alias, File, NewShareRequest, Share}; use std::time::Duration; use ureq::Agent; @@ -22,7 +22,8 @@ fn main() { let share = Share::create(&agent, &alias, share).unwrap(); info!("share: {:?}", share); - // share.create_file(&agent, "./myfile.dat").unwrap(); + let file = File::create(&agent, &share, "/lib/x86_64-linux-gnu/liblldb-14.so.1").unwrap(); + info!("file: {:?}", file); share.notify(&agent).unwrap(); } diff --git a/src/sharry/file.rs b/src/sharry/file.rs new file mode 100644 index 0000000..6af5021 --- /dev/null +++ b/src/sharry/file.rs @@ -0,0 +1,73 @@ +use std::{ + ffi::OsStr, + fs, + path::{Path, PathBuf}, +}; + +use log::debug; +use ureq::http::StatusCode; + +use super::{ + alias::{Alias, SharryAlias}, + share::Share, +}; + +#[derive(Debug)] +pub struct File<'t> { + alias: &'t Alias, + path: PathBuf, + patch_uri: String, +} + +impl<'t> File<'t> { + pub fn create( + http: &ureq::Agent, + share: &'t Share, + path: impl AsRef + AsRef, + ) -> Result { + let filepath = PathBuf::from(&path); + + let endpoint = share + .alias + .get_endpoint(format!("alias/upload/{}/files/tus", share.id)); + + let filename = filepath + .file_name() + .and_then(OsStr::to_str) + .unwrap_or("file.bin"); + + let res = http + .post(endpoint) + .sharry_header(share.alias) + .header("Sharry-File-Name", filename) + .header("Upload-Length", fs::metadata(&filepath)?.len()) + .send_empty()?; + + if res.status() != StatusCode::CREATED { + return Err(ureq::Error::Other("unexpected response status".into())); + } + + let location = res + .headers() + .get("Location") + .ok_or_else(|| ureq::Error::Other("Location header not found".into()))? + .to_str() + .map_err(|_| ureq::Error::Other("Location header invalid".into()))?; + + debug!("location: {}", location); + + // let start = 10; + // let count = 10; + + // let mut f = File::open(path)?; + // f.seek(SeekFrom::Start(0)); + // let mut buf = vec![0; count]; + // f.read_exact(&mut buf)?; + + Ok(Self { + alias: share.alias, + path: filepath, + patch_uri: location.into(), + }) + } +} diff --git a/src/sharry/mod.rs b/src/sharry/mod.rs index fd3e969..59a93f7 100644 --- a/src/sharry/mod.rs +++ b/src/sharry/mod.rs @@ -1,7 +1,11 @@ +#![allow(unused_imports)] + mod alias; mod api; +mod file; mod share; pub use alias::Alias; pub use api::{NewShareRequest, URI}; +pub use file::File; pub use share::Share;