async-graphql/src/types/upload.rs

105 lines
3.2 KiB
Rust
Raw Normal View History

2020-03-19 09:20:12 +00:00
use crate::{registry, InputValueType, Type, Value};
2020-03-14 03:46:20 +00:00
use std::borrow::Cow;
2020-03-30 14:02:43 +00:00
/// Uploaded file
2020-03-14 03:46:20 +00:00
///
2020-03-30 14:02:43 +00:00
/// **Reference:** <https://github.com/jaydenseric/graphql-multipart-request-spec>
///
///
/// Graphql supports file uploads via `multipart/form-data`.
/// Enable this feature by accepting an argument of type `Upload` (single file) or
/// `Vec<Upload>` (multiple files) in your mutation like in the example blow.
///
///
/// # Example
/// *[Full Example](<https://github.com/sunli829/async-graphql/blob/master/async-graphql-actix-web/examples/upload-file.rs>)*
///
/// ```
/// use async_graphql::Upload;
///
/// struct MutationRoot;
///
/// #[async_graphql::Object]
/// impl MutationRoot {
/// #[field]
/// async fn upload(&self, file: Upload) -> bool {
/// println!(
/// "upload: filename={} size={}",
/// file.filename,
/// file.content.len()
/// );
/// true
/// }
/// }
///
/// ```
/// # Example Curl Request
2020-03-30 14:31:38 +00:00
/// Assuming you have defined your mutation root like in the example above,
/// you can now upload a file `myFile.txt` with the below curl command:
2020-03-30 14:02:43 +00:00
///
/// ```curl
/// curl POST 'localhost:8000' \
/// --form 'operations={
/// "query": "mutation ($file: Upload!) { upload(file: $file) }",
/// "variables": { "file": null }}' \
/// --form 'map={ "0": ["variables.file"] }' \
/// --form '0=@myFile.txt'
/// ```
2020-03-14 03:46:20 +00:00
pub struct Upload {
2020-03-20 03:56:08 +00:00
/// Filename
2020-03-14 03:46:20 +00:00
pub filename: String,
2020-03-20 03:56:08 +00:00
/// Content type, such as `application/json`, `image/jpg` ...
2020-03-14 03:46:20 +00:00
pub content_type: Option<String>,
2020-03-20 03:56:08 +00:00
/// File content
2020-03-14 03:46:20 +00:00
pub content: Vec<u8>,
}
2020-03-19 09:20:12 +00:00
impl<'a> Type for Upload {
2020-03-14 03:46:20 +00:00
fn type_name() -> Cow<'static, str> {
Cow::Borrowed("Upload")
}
fn create_type_info(registry: &mut registry::Registry) -> String {
registry.create_type::<Self, _>(|_| registry::Type::Scalar {
name: Self::type_name().to_string(),
description: None,
is_valid: |value| match value {
Value::String(s) => s.starts_with("file:"),
_ => false,
},
})
}
}
2020-03-19 09:20:12 +00:00
impl<'a> InputValueType for Upload {
2020-03-14 03:46:20 +00:00
fn parse(value: &Value) -> Option<Self> {
if let Value::String(s) = value {
if s.starts_with("file:") {
let s = &s[5..];
2020-03-21 01:32:13 +00:00
if let Some(idx) = s.find('|') {
2020-03-14 03:46:20 +00:00
let name_and_type = &s[..idx];
let content = &s[idx + 1..];
2020-03-21 01:32:13 +00:00
if let Some(type_idx) = name_and_type.find(':') {
2020-03-14 03:46:20 +00:00
let name = &name_and_type[..type_idx];
let mime_type = &name_and_type[type_idx + 1..];
return Some(Self {
filename: name.to_string(),
content_type: Some(mime_type.to_string()),
content: content.as_bytes().to_vec(),
});
} else {
return Some(Self {
filename: name_and_type.to_string(),
content_type: None,
content: content.as_bytes().to_vec(),
});
}
}
}
}
None
}
}