-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.rs
106 lines (83 loc) · 2.26 KB
/
file.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use std::{path::PathBuf, usize, borrow::Cow};
use serde::{Serialize, Deserialize};
use tokio::sync::oneshot;
use uuid::Uuid;
use crate::errors::FragmentError;
// use crate::errors::BackendErrors;
#[derive(Debug, Serialize, Deserialize )]
pub struct FileObject {
pub path: PathBuf,
state: UploadState,
pub file_size: usize,
name: String,
uuid: Uuid,
hash: Vec<u8>,
}
impl FileObject {
pub fn new(
path: impl Into<PathBuf>,
size: usize,
name: impl ToString,
hash: Option<impl ToString>
) -> Self {
let vec_hash = hash.map(|e| e.to_string().as_bytes().to_vec()).unwrap_or(vec![]);
Self {
path: path.into(),
state: UploadState::UnInit,
file_size: size,
name: name.to_string(),
uuid: Uuid::new_v4(),
hash: vec_hash
}
}
#[inline(always)]
pub fn set_state(&mut self, state: UploadState) {
self.state = state;
}
#[inline(always)]
pub fn get_state(&self) -> UploadState {
self.state
}
#[inline(always)]
pub fn get_uuid(&self) -> Cow<'_, Uuid> {
Cow::Borrowed(&(self.uuid))
}
pub fn output_file_path(&self) -> PathBuf {
self.path.join(self.uuid.as_hyphenated().to_string())
}
}
pub mod file_drop_handler {
use std::sync::atomic::{AtomicBool, Ordering};
use scopeguard::ScopeGuard;
pub static SETTER: AtomicBool = AtomicBool::new(false);
enum FileDropHandler{}
impl scopeguard::Strategy for FileDropHandler {
fn should_run() -> bool {
if SETTER.load(Ordering::SeqCst) == true {
SETTER.store(false, Ordering::Release);
return true;
}
return false;
}
}
pub fn guard_on_error<T, F>(v: T, dropfn: F) -> ScopeGuard<T, F, FileDropHandler>
where
F: FnOnce(T),
{
scopeguard::ScopeGuard::with_strategy(v, dropfn)
}
}
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub enum UploadState {
UnInit,
Init,
Broken(usize),
Progress(usize),
Resume(usize),
Complete,
Failed,
}
#[derive(Debug)]
pub struct SharedFileState {
state: oneshot::Receiver<usize>,
}