108 lines
2.5 KiB
Rust
108 lines
2.5 KiB
Rust
use std::{
|
|
fs,
|
|
io::{self, Read, Seek, SeekFrom},
|
|
path::PathBuf,
|
|
};
|
|
|
|
use log::warn;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
use super::{Checked, Chunk, FileTrait};
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
pub struct Uploading {
|
|
/// canonical path to a regular file
|
|
path: PathBuf,
|
|
/// size of that file
|
|
size: u64,
|
|
/// hash of that file
|
|
hash: Option<String>,
|
|
file_id: String,
|
|
#[serde(skip)]
|
|
last_offset: Option<u64>,
|
|
offset: u64,
|
|
}
|
|
|
|
impl Uploading {
|
|
pub(super) fn new(path: PathBuf, size: u64, hash: Option<String>, file_id: String) -> Self {
|
|
Self {
|
|
path,
|
|
size,
|
|
hash,
|
|
file_id,
|
|
last_offset: None,
|
|
offset: 0,
|
|
}
|
|
}
|
|
|
|
pub fn get_offset(&self) -> u64 {
|
|
self.offset
|
|
}
|
|
|
|
pub fn rewind(self) -> Option<Self> {
|
|
if let Some(last_offset) = self.last_offset {
|
|
Some(Self {
|
|
last_offset: None,
|
|
offset: last_offset,
|
|
..self
|
|
})
|
|
} else {
|
|
warn!("attempted to rewind twice");
|
|
None
|
|
}
|
|
}
|
|
|
|
pub fn read<'t>(&mut self, buf: &'t mut [u8]) -> io::Result<Chunk<'t>> {
|
|
let mut f = fs::File::open(&self.path)?;
|
|
|
|
f.seek(SeekFrom::Start(self.offset))?;
|
|
let read_len = f.read(buf)?;
|
|
|
|
if read_len == 0 {
|
|
return Err(io::Error::new(
|
|
io::ErrorKind::UnexpectedEof,
|
|
format!("could not read from file {:?}", self.path.display()),
|
|
));
|
|
}
|
|
|
|
let chunk = Chunk::new(self.file_id.clone(), self.offset, &buf[..read_len]);
|
|
self.last_offset = Some(self.offset);
|
|
self.offset += chunk.get_length();
|
|
|
|
Ok(chunk)
|
|
}
|
|
|
|
pub fn check_eof(self) -> Result<Self, PathBuf> {
|
|
if self.offset < self.size {
|
|
Ok(self)
|
|
} else {
|
|
Err(self.path)
|
|
}
|
|
}
|
|
|
|
pub fn abort(self) -> Checked {
|
|
Checked {
|
|
path: self.path,
|
|
size: self.size,
|
|
hash: self.hash,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'t> FileTrait<'t> for Uploading {
|
|
/// get a reference to the file's name
|
|
///
|
|
/// Uses `SharryFile::extract_file_name`, which may **panic**!
|
|
fn get_name(&'t self) -> &'t str {
|
|
<Self as FileTrait>::extract_file_name(&self.path)
|
|
}
|
|
|
|
fn get_size(&self) -> u64 {
|
|
self.size
|
|
}
|
|
|
|
fn check_hash(&self, on_progress: impl Fn(u64)) -> crate::Result<()> {
|
|
super::check_file_hash(&self.path, self.size, self.hash.as_ref(), on_progress)
|
|
}
|
|
}
|