rebuilt file creation in file.rs

This commit is contained in:
Jörn-Michael Miehe 2025-05-24 00:48:59 +00:00
parent 8d8f7bb0a8
commit e6957a8de1
3 changed files with 80 additions and 2 deletions

View file

@ -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();
}

73
src/sharry/file.rs Normal file
View file

@ -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<OsStr> + AsRef<Path>,
) -> Result<Self, ureq::Error> {
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(),
})
}
}

View file

@ -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;