2022-04-19 04:25:11 +00:00
|
|
|
use std::{borrow::Cow, fs::File, 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
|
|
|
|
2022-04-19 04:25:11 +00:00
|
|
|
use crate::{
|
|
|
|
registry, registry::MetaTypeId, Context, InputType, InputValueError, InputValueResult, 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 {
|
2022-04-19 04:25:11 +00:00
|
|
|
/// Attempt to clone the upload value. This type's `Clone` implementation
|
|
|
|
/// simply calls this and panics on failure.
|
2020-10-10 02:32:43 +00:00
|
|
|
///
|
|
|
|
/// # 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")]
|
2021-03-22 05:27:24 +00:00
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "unblock")))]
|
2020-10-10 02:32:43 +00:00
|
|
|
/// 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`.
|
2022-04-19 04:25:11 +00:00
|
|
|
/// 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.
|
2020-03-30 14:02:43 +00:00
|
|
|
///
|
|
|
|
///
|
|
|
|
/// # 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
|
|
|
///
|
|
|
|
/// ```
|
2020-09-13 03:41:15 +00:00
|
|
|
/// use async_graphql::*;
|
2020-03-30 14:02:43 +00:00
|
|
|
///
|
2021-11-20 03:16:48 +00:00
|
|
|
/// struct Mutation;
|
2020-03-30 14:02:43 +00:00
|
|
|
///
|
2020-09-18 00:52:13 +00:00
|
|
|
/// #[Object]
|
2021-11-20 03:16:48 +00:00
|
|
|
/// impl Mutation {
|
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
|
2021-11-20 03:16:48 +00:00
|
|
|
///
|
|
|
|
/// Assuming you have defined your Mutation 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-09-18 06:32:00 +00:00
|
|
|
}
|
2020-03-14 03:46:20 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 10:52:10 +00:00
|
|
|
impl InputType for Upload {
|
2021-11-18 07:43:12 +00:00
|
|
|
type RawValueType = Self;
|
|
|
|
|
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 {
|
2022-03-30 12:54:49 +00:00
|
|
|
registry.create_input_type::<Self, _>(MetaTypeId::Scalar, |_| 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-12-11 08:03:28 +00:00
|
|
|
visible: None,
|
2022-08-18 09:40:04 +00:00
|
|
|
inaccessible: false,
|
2022-08-22 09:44:02 +00:00
|
|
|
tags: Default::default(),
|
2021-11-02 11:58:23 +00:00
|
|
|
specified_by_url: Some("https://github.com/jaydenseric/graphql-multipart-request-spec"),
|
2020-03-14 03:46:20 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-28 07:00:55 +00:00
|
|
|
fn parse(value: Option<Value>) -> InputValueResult<Self> {
|
2020-10-10 02:32:43 +00:00
|
|
|
const PREFIX: &str = "#__graphql_file__:";
|
2020-05-28 07:00:55 +00:00
|
|
|
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
|
|
|
}
|
2020-05-26 10:34:43 +00:00
|
|
|
|
|
|
|
fn to_value(&self) -> Value {
|
|
|
|
Value::Null
|
|
|
|
}
|
2021-11-18 07:43:12 +00:00
|
|
|
|
|
|
|
fn as_raw_value(&self) -> Option<&Self::RawValueType> {
|
|
|
|
Some(self)
|
|
|
|
}
|
2020-03-14 03:46:20 +00:00
|
|
|
}
|