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::{
|
||||
fs,
|
||||
io::{self, Read, Seek},
|
||||
mem,
|
||||
fs::File,
|
||||
io::{Read, Seek, SeekFrom},
|
||||
path::PathBuf,
|
||||
};
|
||||
|
||||
|
|
@ -32,29 +31,32 @@ impl<'t> Iterator for ChunkedFile<'t> {
|
|||
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))
|
||||
.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
|
||||
.read(&mut buffer)
|
||||
.read(&mut bytes)
|
||||
.inspect_err(|e| error!("Error reading file: {}", e))
|
||||
.ok()?;
|
||||
buffer.truncate(read_len);
|
||||
bytes.truncate(read_len);
|
||||
|
||||
self.cnum += 1;
|
||||
|
||||
Some(Self::Item {
|
||||
offset: offset.try_into().unwrap(),
|
||||
bytes: buffer,
|
||||
})
|
||||
.filter(|c| c.bytes.len() > 0)
|
||||
Some(Self::Item { offset, bytes }).filter(|c| c.bytes.len() > 0)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Chunk {
|
||||
pub offset: usize,
|
||||
pub offset: u64,
|
||||
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::{
|
||||
ffi::OsStr,
|
||||
fs,
|
||||
io::{self, Read, Seek},
|
||||
os::unix::fs::FileExt,
|
||||
path::{Path, PathBuf},
|
||||
str::FromStr,
|
||||
fs::metadata,
|
||||
io::{Read, Seek},
|
||||
path::PathBuf,
|
||||
};
|
||||
|
||||
use log::{debug, error};
|
||||
use ureq::http::StatusCode;
|
||||
use log::debug;
|
||||
use ureq::{Error::Other, http::StatusCode};
|
||||
|
||||
use super::{
|
||||
alias::{Alias, SharryAlias},
|
||||
|
|
@ -43,19 +41,19 @@ impl<'t> File<'t> {
|
|||
.post(endpoint)
|
||||
.sharry_header(share.alias)
|
||||
.header("Sharry-File-Name", filename)
|
||||
.header("Upload-Length", fs::metadata(&file_path)?.len())
|
||||
.header("Upload-Length", metadata(&file_path)?.len())
|
||||
.send_empty()?;
|
||||
|
||||
if res.status() != StatusCode::CREATED {
|
||||
return Err(ureq::Error::Other("unexpected response status".into()));
|
||||
return Err(Other("unexpected response status".into()));
|
||||
}
|
||||
|
||||
let location = res
|
||||
.headers()
|
||||
.get("Location")
|
||||
.ok_or_else(|| ureq::Error::Other("Location header not found".into()))?
|
||||
.ok_or_else(|| Other("Location header not found".into()))?
|
||||
.to_str()
|
||||
.map_err(|_| ureq::Error::Other("Location header invalid".into()))?;
|
||||
.map_err(|_| Other("Location header invalid".into()))?;
|
||||
|
||||
debug!("location: {}", location);
|
||||
|
||||
|
|
@ -80,20 +78,20 @@ impl<'t> File<'t> {
|
|||
.send(&chunk.bytes)?;
|
||||
|
||||
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
|
||||
.headers()
|
||||
.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()
|
||||
.map_err(|_| ureq::Error::Other("Upload-Offset header invalid".into()))?
|
||||
.parse::<usize>()
|
||||
.map_err(|_| ureq::Error::Other("Upload-Offset header not an integer".into()))?;
|
||||
.map_err(|_| Other("Upload-Offset header invalid".into()))?
|
||||
.parse::<u64>()
|
||||
.map_err(|_| Other("Upload-Offset header not an integer".into()))?;
|
||||
|
||||
if chunk.offset + chunk.bytes.len() != offset {
|
||||
return Err(ureq::Error::Other("unexpected offset response".into()));
|
||||
if chunk.after() != offset {
|
||||
return Err(Other("unexpected offset response".into()));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use log::{debug, warn};
|
||||
use log::debug;
|
||||
|
||||
use super::{
|
||||
alias::{Alias, SharryAlias},
|
||||
|
|
@ -27,7 +27,6 @@ impl<'t> Share<'t> {
|
|||
debug!("response: {:?}", res);
|
||||
|
||||
if !(res.success && (res.message == "Share created.")) {
|
||||
warn!("unexpected json response");
|
||||
return Err(ureq::Error::Other("unexpected json response".into()));
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue