|
1 |
| -use poem::{error::StaticFileError, Body}; |
| 1 | +use poem::{web::StaticFileResponse, Body}; |
2 | 2 |
|
3 | 3 | use crate::{
|
4 |
| - payload::{Binary, PlainText}, |
| 4 | + payload::{Binary, Payload}, |
| 5 | + registry::{MetaHeader, MetaMediaType, MetaResponse, MetaResponses, Registry}, |
| 6 | + types::Type, |
5 | 7 | ApiResponse,
|
6 | 8 | };
|
7 | 9 |
|
8 |
| -/// A static file response. |
9 |
| -#[cfg_attr(docsrs, doc(cfg(feature = "static-files")))] |
10 |
| -#[derive(ApiResponse)] |
11 |
| -#[oai(internal)] |
12 |
| -pub enum StaticFileResponse { |
13 |
| - /// Ok |
14 |
| - #[oai(status = 200)] |
15 |
| - Ok( |
16 |
| - Binary<Body>, |
17 |
| - /// The ETag (or entity tag) HTTP response header is an identifier for a |
18 |
| - /// specific version of a resource. It lets caches be more efficient and |
19 |
| - /// save bandwidth, as a web server does not need to resend a full |
20 |
| - /// response if the content was not changed. Additionally, etags help to |
21 |
| - /// prevent simultaneous updates of a resource from overwriting each |
22 |
| - /// other ("mid-air collisions"). |
23 |
| - /// |
24 |
| - /// Reference: <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag> |
25 |
| - #[oai(header = "etag")] |
26 |
| - Option<String>, |
27 |
| - /// The Last-Modified response HTTP header contains a date and time when |
28 |
| - /// the origin server believes the resource was last modified. It is |
29 |
| - /// used as a validator to determine if the resource is the same as the |
30 |
| - /// previously stored one. Less accurate than an ETag header, it is a |
31 |
| - /// fallback mechanism. Conditional requests containing |
32 |
| - /// If-Modified-Since or If-Unmodified-Since headers make use of this |
33 |
| - /// field. |
34 |
| - /// |
35 |
| - /// Reference: <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Last-Modified> |
36 |
| - #[oai(header = "last-modified")] |
37 |
| - Option<String>, |
38 |
| - /// The Content-Type representation header is used to indicate the |
39 |
| - /// original media type of the resource (prior to any content encoding |
40 |
| - /// applied for sending). |
41 |
| - /// |
42 |
| - /// Reference: <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type> |
43 |
| - #[oai(header = "content-type")] |
44 |
| - Option<String>, |
45 |
| - ), |
46 |
| - /// Not modified |
47 |
| - /// |
48 |
| - /// Reference: <https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/304> |
49 |
| - #[oai(status = 304)] |
50 |
| - NotModified, |
51 |
| - /// Bad request |
52 |
| - /// |
53 |
| - /// Reference: <https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400> |
54 |
| - #[oai(status = 400)] |
55 |
| - BadRequest, |
56 |
| - /// Resource was not found |
57 |
| - #[oai(status = 404)] |
58 |
| - NotFound, |
59 |
| - /// Precondition failed |
60 |
| - /// |
61 |
| - /// Reference: <https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/412> |
62 |
| - #[oai(status = 412)] |
63 |
| - PreconditionFailed, |
64 |
| - /// Range not satisfiable |
65 |
| - /// |
66 |
| - /// Reference: <https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/416> |
67 |
| - #[oai(status = 416)] |
68 |
| - RangeNotSatisfiable( |
69 |
| - /// The Content-Range response HTTP header indicates where in a full |
70 |
| - /// body message a partial message belongs. |
71 |
| - /// |
72 |
| - /// Reference: <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Range> |
73 |
| - #[oai(header = "content-range")] |
74 |
| - String, |
75 |
| - ), |
76 |
| - /// Internal server error |
77 |
| - #[oai(status = 500)] |
78 |
| - InternalServerError(PlainText<String>), |
79 |
| -} |
| 10 | +const ETAG_DESCRIPTION: &str = r#"The ETag (or entity tag) HTTP response header is an identifier for a specific version of a resource. It lets caches be more efficient and save bandwidth, as a web server does not need to resend a full response if the content was not changed. Additionally, etags help to prevent simultaneous updates of a resource from overwriting each other ("mid-air collisions")."#; |
| 11 | +const LAST_MODIFIED_DESCRIPTION: &str = r#"The Last-Modified response HTTP header contains a date and time when the origin server believes the resource was last modified. It is used as a validator to determine if the resource is the same as the previously stored one. Less accurate than an ETag header, it is a fallback mechanism. Conditional requests containing If-Modified-Since or If-Unmodified-Since headers make use of this field."#; |
| 12 | +const CONTENT_TYPE_DESCRIPTION: &str = r#"The Content-Type representation header is used to indicate the original media type of the resource (prior to any content encoding applied for sending)."#; |
80 | 13 |
|
81 |
| -impl StaticFileResponse { |
82 |
| - /// Create a static file response. |
83 |
| - pub fn new(res: Result<poem::web::StaticFileResponse, StaticFileError>) -> Self { |
84 |
| - res.into() |
85 |
| - } |
86 |
| -} |
87 |
| - |
88 |
| -impl From<Result<poem::web::StaticFileResponse, StaticFileError>> for StaticFileResponse { |
89 |
| - fn from(res: Result<poem::web::StaticFileResponse, StaticFileError>) -> Self { |
90 |
| - match res { |
91 |
| - Ok(poem::web::StaticFileResponse::Ok { |
92 |
| - body, |
93 |
| - etag, |
94 |
| - last_modified, |
95 |
| - content_type, |
96 |
| - .. |
97 |
| - }) => StaticFileResponse::Ok(Binary(body), etag, last_modified, content_type), |
98 |
| - Ok(poem::web::StaticFileResponse::NotModified) => StaticFileResponse::NotModified, |
99 |
| - Err( |
100 |
| - StaticFileError::MethodNotAllowed(_) |
101 |
| - | StaticFileError::NotFound |
102 |
| - | StaticFileError::InvalidPath |
103 |
| - | StaticFileError::Forbidden(_), |
104 |
| - ) => StaticFileResponse::NotFound, |
105 |
| - Err(StaticFileError::PreconditionFailed) => StaticFileResponse::PreconditionFailed, |
106 |
| - Err(StaticFileError::RangeNotSatisfiable { size }) => { |
107 |
| - StaticFileResponse::RangeNotSatisfiable(format!("*/{}", size)) |
108 |
| - } |
109 |
| - Err(StaticFileError::Io(_)) => StaticFileResponse::BadRequest, |
| 14 | +impl ApiResponse for StaticFileResponse { |
| 15 | + fn meta() -> MetaResponses { |
| 16 | + MetaResponses { |
| 17 | + responses: vec![ |
| 18 | + MetaResponse { |
| 19 | + description: "", |
| 20 | + status: Some(200), |
| 21 | + content: vec![MetaMediaType { |
| 22 | + content_type: Binary::<Body>::CONTENT_TYPE, |
| 23 | + schema: Binary::<Body>::schema_ref(), |
| 24 | + }], |
| 25 | + headers: vec![MetaHeader { |
| 26 | + name: "etag".to_string(), |
| 27 | + description: Some(ETAG_DESCRIPTION.to_string()), |
| 28 | + required: false, |
| 29 | + deprecated: false, |
| 30 | + schema: String::schema_ref(), |
| 31 | + }, MetaHeader { |
| 32 | + name: "last-modified".to_string(), |
| 33 | + description: Some(LAST_MODIFIED_DESCRIPTION.to_string()), |
| 34 | + required: false, |
| 35 | + deprecated: false, |
| 36 | + schema: String::schema_ref(), |
| 37 | + }, MetaHeader { |
| 38 | + name: "content-type".to_string(), |
| 39 | + description: Some(CONTENT_TYPE_DESCRIPTION.to_string()), |
| 40 | + required: false, |
| 41 | + deprecated: false, |
| 42 | + schema: String::schema_ref(), |
| 43 | + }], |
| 44 | + }, |
| 45 | + MetaResponse { |
| 46 | + description: "Not modified", |
| 47 | + status: Some(304), |
| 48 | + content: vec![], |
| 49 | + headers: vec![], |
| 50 | + }, |
| 51 | + MetaResponse { |
| 52 | + description: "Bad request", |
| 53 | + status: Some(400), |
| 54 | + content: vec![], |
| 55 | + headers: vec![], |
| 56 | + }, |
| 57 | + MetaResponse { |
| 58 | + description: "Resource was not found", |
| 59 | + status: Some(404), |
| 60 | + content: vec![], |
| 61 | + headers: vec![], |
| 62 | + }, |
| 63 | + MetaResponse { |
| 64 | + description: "Precondition failed", |
| 65 | + status: Some(412), |
| 66 | + content: vec![], |
| 67 | + headers: vec![], |
| 68 | + }, |
| 69 | + MetaResponse { |
| 70 | + description: "The Content-Range response HTTP header indicates where in a full body message a partial message belongs.", |
| 71 | + status: Some(416), |
| 72 | + content: vec![], |
| 73 | + headers: vec![], |
| 74 | + }, MetaResponse { |
| 75 | + description: "Internal server error", |
| 76 | + status: Some(500), |
| 77 | + content: vec![], |
| 78 | + headers: vec![], |
| 79 | + }, |
| 80 | + ], |
110 | 81 | }
|
111 | 82 | }
|
| 83 | + |
| 84 | + fn register(_registry: &mut Registry) {} |
112 | 85 | }
|
0 commit comments