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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use super::UploadedPart;
use anyhow::Result as AnyResult;
use qiniu_apis::{
    http::{ResponseParts, TransferProgressInfo},
    http_client::{RequestBuilderParts, Response, ResponseError},
};
use std::{
    fmt::{self, Debug},
    sync::Arc,
};

type BeforeRequestCallback<'c> = Arc<dyn Fn(&mut RequestBuilderParts<'_>) -> AnyResult<()> + Send + Sync + 'c>;
type UploadProgressCallback<'c> = Arc<dyn Fn(&UploadingProgressInfo) -> AnyResult<()> + Send + Sync + 'c>;
type PartUploadedCallback<'c> = Arc<dyn Fn(&dyn UploadedPart) -> AnyResult<()> + Send + Sync + 'c>;
type AfterResponseOkCallback<'c> = Arc<dyn Fn(&mut ResponseParts) -> AnyResult<()> + Send + Sync + 'c>;
type AfterResponseErrorCallback<'c> = Arc<dyn Fn(&mut ResponseError) -> AnyResult<()> + Send + Sync + 'c>;

/// 上传回调函数提供者
pub trait UploaderWithCallbacks {
    /// 设置请求前的回调函数
    fn on_before_request<F: Fn(&mut RequestBuilderParts<'_>) -> AnyResult<()> + Send + Sync + 'static>(
        &mut self,
        callback: F,
    ) -> &mut Self;

    /// 设置上传进度回调函数
    fn on_upload_progress<F: Fn(&UploadingProgressInfo) -> AnyResult<()> + Send + Sync + 'static>(
        &mut self,
        callback: F,
    ) -> &mut Self;

    /// 设置响应成功的回调函数
    fn on_response_ok<F: Fn(&mut ResponseParts) -> AnyResult<()> + Send + Sync + 'static>(
        &mut self,
        callback: F,
    ) -> &mut Self;

    /// 设置响应错误的回调函数
    fn on_response_error<F: Fn(&mut ResponseError) -> AnyResult<()> + Send + Sync + 'static>(
        &mut self,
        callback: F,
    ) -> &mut Self;
}

/// 分片上传回调函数提供者
pub trait MultiPartsUploaderWithCallbacks: UploaderWithCallbacks {
    /// 设置分片上传回调函数
    fn on_part_uploaded<F: Fn(&dyn UploadedPart) -> AnyResult<()> + Send + Sync + 'static>(
        &mut self,
        callback: F,
    ) -> &mut Self;
}

#[derive(Default, Clone)]
pub(super) struct Callbacks<'a> {
    before_request_callbacks: Vec<BeforeRequestCallback<'a>>,
    upload_progress_callbacks: Vec<UploadProgressCallback<'a>>,
    part_uploaded_callbacks: Vec<PartUploadedCallback<'a>>,
    after_response_ok_callbacks: Vec<AfterResponseOkCallback<'a>>,
    after_response_error_callbacks: Vec<AfterResponseErrorCallback<'a>>,
}

impl<'a> Callbacks<'a> {
    pub(super) fn insert_before_request_callback(
        &mut self,
        callback: impl Fn(&mut RequestBuilderParts<'_>) -> AnyResult<()> + Send + Sync + 'a,
    ) -> &mut Self {
        self.before_request_callbacks.push(Arc::new(callback));
        self
    }

    pub(super) fn insert_upload_progress_callback(
        &mut self,
        callback: impl Fn(&UploadingProgressInfo) -> AnyResult<()> + Send + Sync + 'a,
    ) -> &mut Self {
        self.upload_progress_callbacks.push(Arc::new(callback));
        self
    }

    pub(super) fn insert_part_uploaded_callback(
        &mut self,
        callback: impl Fn(&dyn UploadedPart) -> AnyResult<()> + Send + Sync + 'a,
    ) -> &mut Self {
        self.part_uploaded_callbacks.push(Arc::new(callback));
        self
    }

    pub(super) fn insert_after_response_ok_callback(
        &mut self,
        callback: impl Fn(&mut ResponseParts) -> AnyResult<()> + Send + Sync + 'a,
    ) -> &mut Self {
        self.after_response_ok_callbacks.push(Arc::new(callback));
        self
    }

    pub(super) fn insert_after_response_error_callback(
        &mut self,
        callback: impl Fn(&mut ResponseError) -> AnyResult<()> + Send + Sync + 'a,
    ) -> &mut Self {
        self.after_response_error_callbacks.push(Arc::new(callback));
        self
    }

    pub(super) fn before_request(&self, builder_parts: &mut RequestBuilderParts) -> AnyResult<()> {
        for callback in self.before_request_callbacks.iter() {
            callback(builder_parts)?;
        }
        Ok(())
    }

    pub(super) fn upload_progress(&self, progress_info: &UploadingProgressInfo) -> AnyResult<()> {
        for callback in self.upload_progress_callbacks.iter() {
            callback(progress_info)?;
        }
        Ok(())
    }

    pub(super) fn part_uploaded(&self, progress_info: &dyn UploadedPart) -> AnyResult<()> {
        for callback in self.part_uploaded_callbacks.iter() {
            callback(progress_info)?;
        }
        Ok(())
    }

    pub(super) fn after_response<B>(&self, result: &mut Result<Response<B>, ResponseError>) -> AnyResult<()> {
        match result {
            Ok(response) => self.after_response_ok(response.parts_mut()),
            Err(err) => self.after_response_error(err),
        }
    }

    fn after_response_ok(&self, response_parts: &mut ResponseParts) -> AnyResult<()> {
        for callback in self.after_response_ok_callbacks.iter() {
            callback(response_parts)?;
        }
        Ok(())
    }

    fn after_response_error(&self, error: &mut ResponseError) -> AnyResult<()> {
        for callback in self.after_response_error_callbacks.iter() {
            callback(error)?;
        }
        Ok(())
    }
}

impl Debug for Callbacks<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Callbacks").finish()
    }
}

/// 上传进度信息
#[derive(Debug, Clone, Copy)]
pub struct UploadingProgressInfo {
    transferred_bytes: u64,
    total_bytes: Option<u64>,
}

impl UploadingProgressInfo {
    /// 创建上传进度信息
    #[inline]
    pub fn new(transferred_bytes: u64, total_bytes: Option<u64>) -> Self {
        Self {
            transferred_bytes,
            total_bytes,
        }
    }

    /// 获取已传输的字节数
    #[inline]
    pub fn transferred_bytes(&self) -> u64 {
        self.transferred_bytes
    }

    /// 获取总字节数
    #[inline]
    pub fn total_bytes(&self) -> Option<u64> {
        self.total_bytes
    }
}

impl<'a> From<&'a TransferProgressInfo<'a>> for UploadingProgressInfo {
    #[inline]
    fn from(t: &'a TransferProgressInfo<'a>) -> Self {
        Self::new(t.transferred_bytes(), Some(t.total_bytes()))
    }
}

impl From<TransferProgressInfo<'_>> for UploadingProgressInfo {
    #[inline]
    fn from(t: TransferProgressInfo<'_>) -> Self {
        Self::new(t.transferred_bytes(), Some(t.total_bytes()))
    }
}