code cleanup
This commit is contained in:
parent
8ce3acde5e
commit
9765227f0a
3 changed files with 33 additions and 34 deletions
|
|
@ -1,7 +1,6 @@
|
||||||
use std::{
|
use std::{
|
||||||
fs,
|
fs::File,
|
||||||
io::{self, Read, Seek},
|
io::{Read, Seek, SeekFrom},
|
||||||
mem,
|
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -32,29 +31,32 @@ impl<'t> Iterator for ChunkedFile<'t> {
|
||||||
self.cnum * csize
|
self.cnum * csize
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut f = fs::File::open(&self.path)
|
let mut f = File::open(&self.path)
|
||||||
.inspect_err(|e| error!("Error opening file: {}", e))
|
.inspect_err(|e| error!("Error opening file: {}", e))
|
||||||
.ok()?;
|
.ok()?;
|
||||||
f.seek(io::SeekFrom::Start(offset)).ok()?;
|
f.seek(SeekFrom::Start(offset)).ok()?;
|
||||||
|
|
||||||
let mut buffer = vec![0; self.csize];
|
let mut bytes = vec![0; self.csize];
|
||||||
let read_len = f
|
let read_len = f
|
||||||
.read(&mut buffer)
|
.read(&mut bytes)
|
||||||
.inspect_err(|e| error!("Error reading file: {}", e))
|
.inspect_err(|e| error!("Error reading file: {}", e))
|
||||||
.ok()?;
|
.ok()?;
|
||||||
buffer.truncate(read_len);
|
bytes.truncate(read_len);
|
||||||
|
|
||||||
self.cnum += 1;
|
self.cnum += 1;
|
||||||
|
|
||||||
Some(Self::Item {
|
Some(Self::Item { offset, bytes }).filter(|c| c.bytes.len() > 0)
|
||||||
offset: offset.try_into().unwrap(),
|
|
||||||
bytes: buffer,
|
|
||||||
})
|
|
||||||
.filter(|c| c.bytes.len() > 0)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Chunk {
|
pub struct Chunk {
|
||||||
pub offset: usize,
|
pub offset: u64,
|
||||||
pub bytes: Vec<u8>,
|
pub bytes: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Chunk {
|
||||||
|
pub fn after(&self) -> u64 {
|
||||||
|
let len: u64 = self.bytes.len().try_into().unwrap();
|
||||||
|
self.offset + len
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,12 @@
|
||||||
use std::{
|
use std::{
|
||||||
ffi::OsStr,
|
ffi::OsStr,
|
||||||
fs,
|
fs::metadata,
|
||||||
io::{self, Read, Seek},
|
io::{Read, Seek},
|
||||||
os::unix::fs::FileExt,
|
path::PathBuf,
|
||||||
path::{Path, PathBuf},
|
|
||||||
str::FromStr,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use log::{debug, error};
|
use log::debug;
|
||||||
use ureq::http::StatusCode;
|
use ureq::{Error::Other, http::StatusCode};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
alias::{Alias, SharryAlias},
|
alias::{Alias, SharryAlias},
|
||||||
|
|
@ -43,19 +41,19 @@ impl<'t> File<'t> {
|
||||||
.post(endpoint)
|
.post(endpoint)
|
||||||
.sharry_header(share.alias)
|
.sharry_header(share.alias)
|
||||||
.header("Sharry-File-Name", filename)
|
.header("Sharry-File-Name", filename)
|
||||||
.header("Upload-Length", fs::metadata(&file_path)?.len())
|
.header("Upload-Length", metadata(&file_path)?.len())
|
||||||
.send_empty()?;
|
.send_empty()?;
|
||||||
|
|
||||||
if res.status() != StatusCode::CREATED {
|
if res.status() != StatusCode::CREATED {
|
||||||
return Err(ureq::Error::Other("unexpected response status".into()));
|
return Err(Other("unexpected response status".into()));
|
||||||
}
|
}
|
||||||
|
|
||||||
let location = res
|
let location = res
|
||||||
.headers()
|
.headers()
|
||||||
.get("Location")
|
.get("Location")
|
||||||
.ok_or_else(|| ureq::Error::Other("Location header not found".into()))?
|
.ok_or_else(|| Other("Location header not found".into()))?
|
||||||
.to_str()
|
.to_str()
|
||||||
.map_err(|_| ureq::Error::Other("Location header invalid".into()))?;
|
.map_err(|_| Other("Location header invalid".into()))?;
|
||||||
|
|
||||||
debug!("location: {}", location);
|
debug!("location: {}", location);
|
||||||
|
|
||||||
|
|
@ -80,20 +78,20 @@ impl<'t> File<'t> {
|
||||||
.send(&chunk.bytes)?;
|
.send(&chunk.bytes)?;
|
||||||
|
|
||||||
if res.status() != StatusCode::NO_CONTENT {
|
if res.status() != StatusCode::NO_CONTENT {
|
||||||
return Err(ureq::Error::Other("unexpected response status".into()));
|
return Err(Other("unexpected response status".into()));
|
||||||
}
|
}
|
||||||
|
|
||||||
let offset = res
|
let offset = res
|
||||||
.headers()
|
.headers()
|
||||||
.get("Upload-Offset")
|
.get("Upload-Offset")
|
||||||
.ok_or_else(|| ureq::Error::Other("Upload-Offset header not found".into()))?
|
.ok_or_else(|| Other("Upload-Offset header not found".into()))?
|
||||||
.to_str()
|
.to_str()
|
||||||
.map_err(|_| ureq::Error::Other("Upload-Offset header invalid".into()))?
|
.map_err(|_| Other("Upload-Offset header invalid".into()))?
|
||||||
.parse::<usize>()
|
.parse::<u64>()
|
||||||
.map_err(|_| ureq::Error::Other("Upload-Offset header not an integer".into()))?;
|
.map_err(|_| Other("Upload-Offset header not an integer".into()))?;
|
||||||
|
|
||||||
if chunk.offset + chunk.bytes.len() != offset {
|
if chunk.after() != offset {
|
||||||
return Err(ureq::Error::Other("unexpected offset response".into()));
|
return Err(Other("unexpected offset response".into()));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use log::{debug, warn};
|
use log::debug;
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
alias::{Alias, SharryAlias},
|
alias::{Alias, SharryAlias},
|
||||||
|
|
@ -27,7 +27,6 @@ impl<'t> Share<'t> {
|
||||||
debug!("response: {:?}", res);
|
debug!("response: {:?}", res);
|
||||||
|
|
||||||
if !(res.success && (res.message == "Share created.")) {
|
if !(res.success && (res.message == "Share created.")) {
|
||||||
warn!("unexpected json response");
|
|
||||||
return Err(ureq::Error::Other("unexpected json response".into()));
|
return Err(ureq::Error::Other("unexpected json response".into()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue