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();
|
||||
info!("share: {:?}", share);
|
||||
|
||||
let file = File::create(
|
||||
&agent,
|
||||
&share,
|
||||
"/lib/x86_64-linux-gnu/liblldb-14.so.1".into(),
|
||||
)
|
||||
.unwrap();
|
||||
let file = File::create(&agent, &share, "/lib/x86_64-linux-gnu/liblldb-14.so.1").unwrap();
|
||||
info!("file: {:?}", file);
|
||||
|
||||
for chunk in file.chunks(10_485_760) {
|
||||
println!("chunk len: {}", chunk.len());
|
||||
}
|
||||
|
||||
share.notify(&agent).unwrap();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,13 @@
|
|||
use std::{
|
||||
ffi::OsStr,
|
||||
fs,
|
||||
io::{self, Read},
|
||||
io::{self, Read, Seek},
|
||||
os::unix::fs::FileExt,
|
||||
path::{Path, PathBuf},
|
||||
str::FromStr,
|
||||
};
|
||||
|
||||
use log::debug;
|
||||
use log::{debug, error};
|
||||
use ureq::http::StatusCode;
|
||||
|
||||
use super::{
|
||||
|
|
@ -24,17 +26,18 @@ impl<'t> File<'t> {
|
|||
pub fn create(
|
||||
http: &ureq::Agent,
|
||||
share: &'t Share,
|
||||
file_path: PathBuf,
|
||||
file_path: impl Into<PathBuf>,
|
||||
) -> Result<Self, ureq::Error> {
|
||||
let endpoint = share
|
||||
.alias
|
||||
.get_endpoint(format!("alias/upload/{}/files/tus", share.id));
|
||||
|
||||
let file_path: PathBuf = file_path.into();
|
||||
let filename = file_path
|
||||
.file_name()
|
||||
.and_then(OsStr::to_str)
|
||||
.unwrap_or("file.bin");
|
||||
|
||||
let endpoint = share
|
||||
.alias
|
||||
.get_endpoint(format!("alias/upload/{}/files/tus", share.id));
|
||||
|
||||
let res = http
|
||||
.post(endpoint)
|
||||
.sharry_header(share.alias)
|
||||
|
|
@ -55,14 +58,6 @@ impl<'t> File<'t> {
|
|||
|
||||
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,
|
||||
file_path,
|
||||
|
|
@ -70,23 +65,71 @@ impl<'t> File<'t> {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn upload(&self, chunk_size: usize) -> UploadChunk {
|
||||
UploadChunk::new(&self.file_path, chunk_size).unwrap()
|
||||
pub fn chunks(&self, chunk_size: usize) -> FileChunks {
|
||||
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,
|
||||
f: fs::File,
|
||||
path: &'t PathBuf,
|
||||
buffer: Vec<u8>,
|
||||
data_len: Option<usize>,
|
||||
}
|
||||
|
||||
impl UploadChunk {
|
||||
fn new(path: impl AsRef<Path>, chunk_size: usize) -> io::Result<Self> {
|
||||
Ok(Self {
|
||||
impl<'t> FileChunks<'t> {
|
||||
fn new(path: &'t PathBuf, chunk_size: usize) -> Self {
|
||||
Self {
|
||||
num: 0,
|
||||
f: fs::File::open(path)?,
|
||||
path,
|
||||
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