sharry::file: begin UploadChunk impl

This commit is contained in:
Jörn-Michael Miehe 2025-05-24 01:47:02 +00:00
parent e6957a8de1
commit 6dd7e3c08e
2 changed files with 32 additions and 8 deletions

View file

@ -22,7 +22,12 @@ fn main() {
let share = Share::create(&agent, &alias, share).unwrap();
info!("share: {:?}", share);
let file = File::create(&agent, &share, "/lib/x86_64-linux-gnu/liblldb-14.so.1").unwrap();
let file = File::create(
&agent,
&share,
"/lib/x86_64-linux-gnu/liblldb-14.so.1".into(),
)
.unwrap();
info!("file: {:?}", file);
share.notify(&agent).unwrap();

View file

@ -1,6 +1,7 @@
use std::{
ffi::OsStr,
fs,
io::{self, Read},
path::{Path, PathBuf},
};
@ -15,7 +16,7 @@ use super::{
#[derive(Debug)]
pub struct File<'t> {
alias: &'t Alias,
path: PathBuf,
file_path: PathBuf,
patch_uri: String,
}
@ -23,15 +24,13 @@ impl<'t> File<'t> {
pub fn create(
http: &ureq::Agent,
share: &'t Share,
path: impl AsRef<OsStr> + AsRef<Path>,
file_path: PathBuf,
) -> 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
let filename = file_path
.file_name()
.and_then(OsStr::to_str)
.unwrap_or("file.bin");
@ -40,7 +39,7 @@ impl<'t> File<'t> {
.post(endpoint)
.sharry_header(share.alias)
.header("Sharry-File-Name", filename)
.header("Upload-Length", fs::metadata(&filepath)?.len())
.header("Upload-Length", fs::metadata(&file_path)?.len())
.send_empty()?;
if res.status() != StatusCode::CREATED {
@ -66,8 +65,28 @@ impl<'t> File<'t> {
Ok(Self {
alias: share.alias,
path: filepath,
file_path,
patch_uri: location.into(),
})
}
pub fn upload(&self, chunk_size: usize) -> UploadChunk {
UploadChunk::new(&self.file_path, chunk_size).unwrap()
}
}
pub struct UploadChunk {
num: u64,
f: fs::File,
buffer: Vec<u8>,
}
impl UploadChunk {
fn new(path: impl AsRef<Path>, chunk_size: usize) -> io::Result<Self> {
Ok(Self {
num: 0,
f: fs::File::open(path)?,
buffer: vec![0; chunk_size],
})
}
}