async-graphql/src/types/upload.rs

133 lines
3.8 KiB
Rust
Raw Normal View History

2020-03-14 03:46:20 +00:00
use std::borrow::Cow;
2020-10-10 02:32:43 +00:00
use std::fs::File;
2020-05-11 09:13:50 +00:00
use std::io::Read;
2020-03-14 03:46:20 +00:00
2020-11-30 05:47:00 +00:00
#[cfg(feature = "unblock")]
2020-10-16 06:49:22 +00:00
use futures_util::io::AsyncRead;
2020-10-15 06:38:10 +00:00
use crate::{registry, Context, InputType, InputValueError, InputValueResult, Type, Value};
2020-10-15 06:38:10 +00:00
2020-10-10 02:32:43 +00:00
/// A file upload value.
pub struct UploadValue {
/// The name of the file.
pub filename: String,
/// The content type of the file.
pub content_type: Option<String>,
/// The file data.
pub content: File,
}
impl UploadValue {
/// Attempt to clone the upload value. This type's `Clone` implementation simply calls this and
/// panics on failure.
///
/// # Errors
///
/// Fails if cloning the inner `File` fails.
pub fn try_clone(&self) -> std::io::Result<Self> {
Ok(Self {
filename: self.filename.clone(),
content_type: self.content_type.clone(),
content: self.content.try_clone()?,
})
}
/// Convert to a `Read`.
///
/// **Note**: this is a *synchronous/blocking* reader.
pub fn into_read(self) -> impl Read + Sync + Send + 'static {
self.content
}
#[cfg(feature = "unblock")]
#[cfg_attr(feature = "nightly", doc(cfg(feature = "unblock")))]
/// Convert to a `AsyncRead`.
pub fn into_async_read(self) -> impl AsyncRead + Sync + Send + 'static {
blocking::Unblock::new(self.content)
}
/// Returns the size of the file, in bytes.
pub fn size(&self) -> std::io::Result<u64> {
self.content.metadata().map(|meta| meta.len())
}
}
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
2020-04-28 07:41:31 +00:00
/// *[Full Example](<https://github.com/async-graphql/examples/blob/master/models/files/src/lib.rs>)*
2020-03-30 14:02:43 +00:00
///
/// ```
/// use async_graphql::*;
2020-03-30 14:02:43 +00:00
///
/// struct MutationRoot;
///
/// #[Object]
2020-03-30 14:02:43 +00:00
/// impl MutationRoot {
2020-10-10 02:32:43 +00:00
/// async fn upload(&self, ctx: &Context<'_>, file: Upload) -> bool {
/// println!("upload: filename={}", file.value(ctx).unwrap().filename);
2020-03-30 14:02:43 +00:00
/// true
/// }
/// }
///
/// ```
/// # Example Curl Request
2020-03-30 14:36:27 +00:00
/// Assuming you have defined your MutationRoot like in the example above,
2020-03-30 14:31:38 +00:00
/// you can now upload a file `myFile.txt` with the below curl command:
2020-03-30 14:02:43 +00:00
///
/// ```curl
2020-03-30 14:47:17 +00:00
/// curl 'localhost:8000' \
2020-03-30 14:02:43 +00:00
/// --form 'operations={
/// "query": "mutation ($file: Upload!) { upload(file: $file) }",
/// "variables": { "file": null }}' \
/// --form 'map={ "0": ["variables.file"] }' \
/// --form '0=@myFile.txt'
/// ```
2020-10-10 02:32:43 +00:00
pub struct Upload(usize);
2020-05-11 09:13:50 +00:00
impl Upload {
2020-10-10 02:32:43 +00:00
/// Get the upload value.
pub fn value(&self, ctx: &Context<'_>) -> std::io::Result<UploadValue> {
ctx.query_env.uploads[self.0].try_clone()
}
2020-03-14 03:46:20 +00:00
}
2020-09-06 05:38:31 +00:00
impl 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::MetaType::Scalar {
2020-03-14 03:46:20 +00:00
name: Self::type_name().to_string(),
description: None,
2020-10-10 02:32:43 +00:00
is_valid: |value| matches!(value, Value::String(_)),
2020-03-14 03:46:20 +00:00
})
}
}
impl InputType for Upload {
fn parse(value: Option<Value>) -> InputValueResult<Self> {
2020-10-10 02:32:43 +00:00
const PREFIX: &str = "#__graphql_file__:";
let value = value.unwrap_or_default();
2020-10-10 02:32:43 +00:00
if let Value::String(s) = &value {
2020-10-16 10:37:59 +00:00
if let Some(filename) = s.strip_prefix(PREFIX) {
return Ok(Upload(filename.parse::<usize>().unwrap()));
2020-10-10 02:32:43 +00:00
}
2020-03-14 03:46:20 +00:00
}
2020-10-10 02:32:43 +00:00
Err(InputValueError::expected_type(value))
2020-03-14 03:46:20 +00:00
}
fn to_value(&self) -> Value {
Value::Null
}
2020-03-14 03:46:20 +00:00
}