rough impl for FileChunks
This commit is contained in:
parent
6dd7e3c08e
commit
dcee123194
2 changed files with 72 additions and 30 deletions
11
src/main.rs
11
src/main.rs
|
|
@ -22,13 +22,12 @@ fn main() {
|
||||||
let share = Share::create(&agent, &alias, share).unwrap();
|
let share = Share::create(&agent, &alias, share).unwrap();
|
||||||
info!("share: {:?}", share);
|
info!("share: {:?}", share);
|
||||||
|
|
||||||
let file = File::create(
|
let file = File::create(&agent, &share, "/lib/x86_64-linux-gnu/liblldb-14.so.1").unwrap();
|
||||||
&agent,
|
|
||||||
&share,
|
|
||||||
"/lib/x86_64-linux-gnu/liblldb-14.so.1".into(),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
info!("file: {:?}", file);
|
info!("file: {:?}", file);
|
||||||
|
|
||||||
|
for chunk in file.chunks(10_485_760) {
|
||||||
|
println!("chunk len: {}", chunk.len());
|
||||||
|
}
|
||||||
|
|
||||||
share.notify(&agent).unwrap();
|
share.notify(&agent).unwrap();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,13 @@
|
||||||
use std::{
|
use std::{
|
||||||
ffi::OsStr,
|
ffi::OsStr,
|
||||||
fs,
|
fs,
|
||||||
io::{self, Read},
|
io::{self, Read, Seek},
|
||||||
|
os::unix::fs::FileExt,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
|
str::FromStr,
|
||||||
};
|
};
|
||||||
|
|
||||||
use log::debug;
|
use log::{debug, error};
|
||||||
use ureq::http::StatusCode;
|
use ureq::http::StatusCode;
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
|
|
@ -24,17 +26,18 @@ impl<'t> File<'t> {
|
||||||
pub fn create(
|
pub fn create(
|
||||||
http: &ureq::Agent,
|
http: &ureq::Agent,
|
||||||
share: &'t Share,
|
share: &'t Share,
|
||||||
file_path: PathBuf,
|
file_path: impl Into<PathBuf>,
|
||||||
) -> Result<Self, ureq::Error> {
|
) -> Result<Self, ureq::Error> {
|
||||||
let endpoint = share
|
let file_path: PathBuf = file_path.into();
|
||||||
.alias
|
|
||||||
.get_endpoint(format!("alias/upload/{}/files/tus", share.id));
|
|
||||||
|
|
||||||
let filename = file_path
|
let filename = file_path
|
||||||
.file_name()
|
.file_name()
|
||||||
.and_then(OsStr::to_str)
|
.and_then(OsStr::to_str)
|
||||||
.unwrap_or("file.bin");
|
.unwrap_or("file.bin");
|
||||||
|
|
||||||
|
let endpoint = share
|
||||||
|
.alias
|
||||||
|
.get_endpoint(format!("alias/upload/{}/files/tus", share.id));
|
||||||
|
|
||||||
let res = http
|
let res = http
|
||||||
.post(endpoint)
|
.post(endpoint)
|
||||||
.sharry_header(share.alias)
|
.sharry_header(share.alias)
|
||||||
|
|
@ -55,14 +58,6 @@ impl<'t> File<'t> {
|
||||||
|
|
||||||
debug!("location: {}", location);
|
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 {
|
Ok(Self {
|
||||||
alias: share.alias,
|
alias: share.alias,
|
||||||
file_path,
|
file_path,
|
||||||
|
|
@ -70,23 +65,71 @@ impl<'t> File<'t> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn upload(&self, chunk_size: usize) -> UploadChunk {
|
pub fn chunks(&self, chunk_size: usize) -> FileChunks {
|
||||||
UploadChunk::new(&self.file_path, chunk_size).unwrap()
|
FileChunks::new(&self.file_path, chunk_size)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pub fn upload(&mut self) -> io::Result<()> {
|
||||||
|
// let mut f = fs::File::open(&self.path)?;
|
||||||
|
// f.seek(io::SeekFrom::Start(0));
|
||||||
|
|
||||||
|
// let patch_size = f.read(&mut self.buffer)?;
|
||||||
|
// let patch_data = &self.buffer[0..patch_size];
|
||||||
|
|
||||||
|
// Ok(())
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct UploadChunk {
|
pub struct FileChunks<'t> {
|
||||||
num: u64,
|
num: u64,
|
||||||
f: fs::File,
|
path: &'t PathBuf,
|
||||||
buffer: Vec<u8>,
|
buffer: Vec<u8>,
|
||||||
|
data_len: Option<usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UploadChunk {
|
impl<'t> FileChunks<'t> {
|
||||||
fn new(path: impl AsRef<Path>, chunk_size: usize) -> io::Result<Self> {
|
fn new(path: &'t PathBuf, chunk_size: usize) -> Self {
|
||||||
Ok(Self {
|
Self {
|
||||||
num: 0,
|
num: 0,
|
||||||
f: fs::File::open(path)?,
|
path,
|
||||||
buffer: vec![0; chunk_size],
|
buffer: vec![0; chunk_size],
|
||||||
})
|
data_len: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_offset(&self) -> Option<u64> {
|
||||||
|
let buffer_len: u64 = self
|
||||||
|
.buffer
|
||||||
|
.len()
|
||||||
|
.try_into()
|
||||||
|
.inspect_err(|e| error!("Error converting length: {}", e))
|
||||||
|
.ok()?;
|
||||||
|
|
||||||
|
Some(self.num * buffer_len)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'t> Iterator for FileChunks<'t> {
|
||||||
|
type Item = &'t [u8];
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
let mut f = fs::File::open(&self.path)
|
||||||
|
.inspect_err(|e| error!("Error opening file: {}", e))
|
||||||
|
.ok()?;
|
||||||
|
|
||||||
|
f.seek(io::SeekFrom::Start(self.get_offset()?)).ok()?;
|
||||||
|
|
||||||
|
self.data_len = {
|
||||||
|
let read_len = f
|
||||||
|
.read(&mut self.buffer)
|
||||||
|
.inspect_err(|e| error!("Error reading file: {}", e))
|
||||||
|
.ok()?;
|
||||||
|
|
||||||
|
Some(read_len).filter(|l| *l > 0)
|
||||||
|
};
|
||||||
|
|
||||||
|
self.num += 1;
|
||||||
|
|
||||||
|
Some(unsafe { std::mem::transmute(&self.buffer[0..self.data_len?]) })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue