Skip to content

Commit eb0f6f3

Browse files
committed
Added Saving to file
1 parent 7094bce commit eb0f6f3

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/http_ok_result.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ pub enum HttpOutput {
1818
permanent: bool,
1919
},
2020

21+
File {
22+
file_name: String,
23+
content: Vec<u8>,
24+
},
25+
2126
Raw(Response<Body>),
2227
}
2328

@@ -62,6 +67,19 @@ impl HttpOutput {
6267
HttpOutput::Raw(_) => {
6368
panic!("Raw response can not be turned into Http Fail result")
6469
}
70+
HttpOutput::File { file_name, content } => HttpFailResult {
71+
content_type: if let Some(ct) =
72+
WebContentType::detect_by_extension(file_name.as_str())
73+
{
74+
ct
75+
} else {
76+
WebContentType::Text
77+
},
78+
status_code,
79+
content,
80+
write_telemetry,
81+
write_to_log: false,
82+
},
6583
};
6684

6785
Err(result)
@@ -116,6 +134,11 @@ impl HttpOutput {
116134
}
117135
}
118136

137+
Self::File {
138+
file_name: _,
139+
content: _,
140+
} => 200,
141+
119142
HttpOutput::Raw(body) => body.status().as_u16(),
120143
}
121144
}
@@ -196,6 +219,20 @@ impl Into<Response<Body>> for HttpOkResult {
196219
.unwrap(),
197220

198221
HttpOutput::Raw(body) => body,
222+
HttpOutput::File { file_name, content } => {
223+
let builder = Response::builder().header(
224+
"content-disposition",
225+
format!(
226+
"attachment; filename=\"{file_name}\"; filename*=UTF-8''{file_name}",
227+
file_name = file_name
228+
),
229+
);
230+
231+
builder
232+
.status(status_code)
233+
.body(Body::from(content))
234+
.unwrap()
235+
}
199236
};
200237
}
201238
}

src/web_content_type.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub enum WebContentType {
77
JavaScript,
88
Json,
99
Text,
10+
Raw(String),
1011
}
1112

1213
impl WebContentType {
@@ -19,6 +20,7 @@ impl WebContentType {
1920
WebContentType::Text => "text/plain; charset=utf-8",
2021
WebContentType::Png => "image/png",
2122
WebContentType::Svg => "image/svg+xml",
23+
WebContentType::Raw(content_type) => content_type.as_str(),
2224
}
2325
}
2426

0 commit comments

Comments
 (0)