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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
//! 响应流

use crate::tina::data::binary::{BinaryContent, DataContent, MemoryData};
use crate::tina::data::{api_schema::ApiSchema, app_error::AppError, AppResult};
use crate::tina::file::{FileContent, FileData};
use crate::tina::server::http::request::RequestExt;
use crate::tina::server::http::response::ResponseAttribute;
use crate::tina::server::session::Session;
use crate::tina::util::string::AsStr;
use crate::tina::util::Utility;
use crate::{app_error_from, tina::util::schema::SchemaExt};
use bytes::Bytes;
use futures::Stream;
use http::header::IF_MODIFIED_SINCE;
use http::{HeaderMap, HeaderName, HeaderValue, StatusCode};
use httpdate::HttpDate;
use mime::APPLICATION_OCTET_STREAM;
use serde::{Serialize, Serializer};
use std::ops::DerefMut;
use std::path::PathBuf;
use std::pin::Pin;
use std::str::FromStr;
use std::task::{Context, Poll};
use std::time::SystemTime;
use std::{any::type_name, borrow::Cow};
use utoipa::{
    openapi::{
        path::Parameter, request_body::RequestBody, ContentBuilder, KnownFormat, ObjectBuilder, RefOr, ResponseBuilder, Responses,
        ResponsesBuilder, Schema, SchemaFormat, SchemaType,
    },
    ToSchema,
};

use super::request_metadata::HttpReqMetadata;

/// 流数据
#[derive(Debug)]
pub struct StreamData {
    pub(crate) data: FileContent,
    pub(crate) name: String,
    pub(crate) content_type: mime::Mime,
    pub(crate) size: Option<u64>,
    pub(crate) download: bool,
    pub(crate) last_modified: Option<HttpDate>,
}

/// 流适配器
pub struct BinaryContentStreamAdapter(pub(crate) FileContent);

impl Stream for BinaryContentStreamAdapter {
    type Item = Result<Bytes, AppError>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        match futures::Future::poll(Pin::new(&mut self.0.lock()), cx) {
            Poll::Ready(mut lock) => match Stream::poll_next(Pin::new(lock.deref_mut()), cx) {
                Poll::Ready(opt) => match opt {
                    None => Poll::Ready(None),
                    Some(r) => match r {
                        Ok(data) => match data {
                            DataContent::STATIC(v) => Poll::Ready(Some(Ok(Bytes::from_static(v)))),
                            DataContent::BYTES(v) => Poll::Ready(Some(Ok(Bytes::from_iter(v.into_iter())))),
                            DataContent::VEC(v) => Poll::Ready(Some(Ok(Bytes::from(v)))),
                        },
                        Err(err) => Poll::Ready(Some(Err(err))),
                    },
                },
                Poll::Pending => Poll::Pending,
            },
            Poll::Pending => Poll::Pending,
        }
    }
}

/// 响应流
#[allow(dead_code)]
pub struct ResStream {
    pub(crate) inner: AjaxStreamInner,
    /// Session
    pub session: Session,
    /// headers
    pub headers: HeaderMap,
}

pub(crate) enum AjaxStreamInner {
    Success(StreamData),
    AppError(AppError),
    HttpStatus(StatusCode),
}

impl ResStream {
    /// 成功的响应流
    pub fn success(
        session: &Session,
        data: impl BinaryContent + 'static,
        name: &str,
        content_type: mime::Mime,
        download: bool,
    ) -> ResStream {
        let size = data.get_size();
        Self {
            inner: AjaxStreamInner::Success(StreamData {
                data: FileContent::from_content(data),
                name: name.to_owned(),
                content_type,
                size,
                download,
                last_modified: None,
            }),
            session: session.clone(),
            headers: HeaderMap::new(),
        }
    }
    /// 成功的响应流
    pub fn from_file_content(session: &Session, data: FileContent, name: &str, content_type: mime::Mime, download: bool) -> ResStream {
        let size = data.get_size();
        Self {
            inner: AjaxStreamInner::Success(StreamData {
                data,
                name: name.to_owned(),
                content_type,
                size,
                download,
                last_modified: None,
            }),
            session: session.clone(),
            headers: HeaderMap::new(),
        }
    }
    /// 设置修改时间
    pub fn last_modified(mut self, date_time: HttpDate) -> Self {
        if let AjaxStreamInner::Success(v) = &mut self.inner {
            v.last_modified = Some(date_time);
        }
        self
    }
    /// 从文件构建
    pub fn from_file_data(req: &HttpReqMetadata, file_data: FileData, download: bool) -> Self {
        let mut last_modified = file_data.get_last_modified_time();
        if let Ok(Some(if_modified_since)) = req.get_request_header(IF_MODIFIED_SINCE.as_str()) {
            if let Ok(http_data) = HttpDate::from_str(if_modified_since.as_ref()) {
                let mut if_modified_since = http_data;
                if last_modified < if_modified_since {
                    std::mem::swap(&mut last_modified, &mut if_modified_since);
                }
                if let Ok(duration) = SystemTime::from(last_modified).duration_since(SystemTime::from(if_modified_since)) {
                    if duration.as_secs() < 1 {
                        return Self::with_status_code(&req.session, StatusCode::NOT_MODIFIED);
                    }
                }
            }
        }

        let file_name = match file_data.get_original_filename() {
            None => file_data.get_name(),
            Some(v) => v,
        };
        let mime = new_mime_guess::from_path(file_name).first_or(APPLICATION_OCTET_STREAM);
        let data = file_data.content.clone();
        ResStream::from_file_content(&req.session, data, file_name, mime, download).last_modified(last_modified)
    }
    /// 从内嵌资源构建
    pub fn from_embedded_resource<Dir: rust_embed::RustEmbed>(req: &mut HttpReqMetadata, path_buf: PathBuf, download: bool) -> Self {
        let path = path_buf.as_path();
        let accept_encoding = req.get_request_header("Accept-Encoding").ok().unwrap_or_default();
        let accept_gz = accept_encoding.as_str().to_lowercase().contains("gzip");
        let mut is_gz = false;
        let file = match accept_gz {
            true => {
                let gz_path = format!("{}.gz", path.to_string_lossy());
                match Dir::get(gz_path.as_str()) {
                    None => Dir::get(path.to_string_lossy().as_ref()),
                    Some(gz_file) => {
                        is_gz = true;
                        Some(gz_file)
                    }
                }
            }
            false => Dir::get(path.to_string_lossy().as_ref()),
        };
        match (file, path.file_name()) {
            (Some(file), Some(file_name)) => {
                let mut cur_exe_last_modified = Utility::get_current_exe_last_modified();
                if let Ok(Some(if_modified_since)) = req.get_request_header(IF_MODIFIED_SINCE.as_str()) {
                    if let Ok(http_data) = HttpDate::from_str(if_modified_since.as_ref()) {
                        let mut if_modified_since = SystemTime::from(http_data);
                        if cur_exe_last_modified < if_modified_since {
                            std::mem::swap(&mut cur_exe_last_modified, &mut if_modified_since);
                        }
                        if let Ok(duration) = cur_exe_last_modified.duration_since(if_modified_since) {
                            if duration.as_secs() < 1 {
                                return Self::with_status_code(&req.session, StatusCode::NOT_MODIFIED);
                            }
                        }
                    }
                }

                let file_name = file_name.to_string_lossy();
                let path = path.to_string_lossy();
                let path = path.as_ref();
                let mime = new_mime_guess::from_path(path).first_or(APPLICATION_OCTET_STREAM);
                let content = file.data;
                let data = match content {
                    Cow::Borrowed(v) => MemoryData::new(DataContent::STATIC(v)),
                    Cow::Owned(v) => MemoryData::new(DataContent::VEC(v)),
                };
                let mut v = Self::success(&req.session, data, file_name.as_ref(), mime, download)
                    .last_modified(HttpDate::from(cur_exe_last_modified));
                if is_gz {
                    if let Err(err) = v.header("Content-Encoding", "gzip") {
                        tracing::error!("add content-encoding failed for file: {}, reason: {:?}", path, err);
                    }
                }
                v
            }
            _ => Self::with_status_code(&req.session, StatusCode::NOT_FOUND),
        }
    }
}

impl ResStream {
    /// 失败的响应流
    pub fn error(session: &Session, err: AppError) -> ResStream {
        Self {
            inner: AjaxStreamInner::AppError(err),
            session: session.clone(),
            headers: HeaderMap::new(),
        }
    }
    /// Http状态的响应
    pub fn with_status_code(session: &Session, status_code: StatusCode) -> ResStream {
        Self {
            inner: AjaxStreamInner::HttpStatus(status_code),
            session: session.clone(),
            headers: HeaderMap::new(),
        }
    }
    /// 设置响应头
    pub fn header<K, V>(&mut self, key: K, value: V) -> AppResult<()>
    where
        K: TryInto<HeaderName>,
        <K as TryInto<HeaderName>>::Error: std::error::Error + Send + Sync + 'static,
        V: TryInto<HeaderValue>,
        <V as TryInto<HeaderValue>>::Error: std::error::Error + Send + Sync + 'static,
    {
        let k: HeaderName = key.try_into().map_err(app_error_from!())?;
        let v: HeaderValue = value.try_into().map_err(app_error_from!())?;
        self.headers.insert(k, v);
        Ok(())
    }
}

impl ResponseAttribute for ResStream {
    fn success(&self) -> bool {
        match &self.inner {
            AjaxStreamInner::Success(_) => true,
            AjaxStreamInner::AppError(_) => false,
            AjaxStreamInner::HttpStatus(status_code) => status_code >= &StatusCode::BAD_REQUEST,
        }
    }

    fn set_success(&mut self, flag: bool) {
        let session = &self.session;
        let inner_success = match &self.inner {
            AjaxStreamInner::Success(_) => true,
            AjaxStreamInner::AppError(_) => false,
            AjaxStreamInner::HttpStatus(status_code) => status_code >= &StatusCode::BAD_REQUEST,
        };
        match flag {
            true => match inner_success {
                true => {}
                false => {
                    (*self) =
                        ResStream::success(session, MemoryData::new(DataContent::VEC(vec![])), "unkown", APPLICATION_OCTET_STREAM, true);
                }
            },
            false => match inner_success {
                true => {
                    (*self) = ResStream::with_status_code(session, StatusCode::INTERNAL_SERVER_ERROR);
                }
                false => {}
            },
        }
    }

    fn get_error_message(&self) -> Option<Cow<str>> {
        match &self.inner {
            AjaxStreamInner::Success(_) => None,
            AjaxStreamInner::AppError(err) => err.get_error_message(),
            AjaxStreamInner::HttpStatus(status_code) => Some(Cow::Borrowed(status_code.as_str())),
        }
    }
}

impl Default for ResStream {
    fn default() -> Self {
        ResStream::success(&Session::default(), MemoryData::new(DataContent::STATIC("".as_bytes())), "", APPLICATION_OCTET_STREAM, true)
    }
}

impl<'a> ToSchema<'a> for ResStream {
    fn schema() -> (&'a str, RefOr<Schema>) {
        (
            type_name::<ResStream>(),
            RefOr::T(Schema::from(
                ObjectBuilder::new().schema_type(SchemaType::String).format(Some(SchemaFormat::KnownFormat(KnownFormat::Binary))),
            )),
        )
    }
}

impl Serialize for ResStream {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str("[ResStream]")
    }
}

impl ApiSchema for ResStream {
    fn get_request_body() -> Option<RequestBody>
    where
        Self: Sized,
    {
        None
    }

    fn get_request_params() -> Option<Vec<Parameter>>
    where
        Self: Sized,
    {
        None
    }

    fn get_responses() -> Responses
    where
        Self: Sized,
    {
        let (_, schema) = Self::schema();
        let description = schema.get_description();
        ResponsesBuilder::new()
            .response(
                "200",
                ResponseBuilder::new()
                    .description(description)
                    .content("application/octet-stream", ContentBuilder::new().schema(schema).build()),
            )
            .build()
    }
}