use std::{borrow::Cow, io::Read, ops::Deref, sync::Arc};
#[cfg(feature = "unblock")]
use futures_util::io::AsyncRead;
use crate::{
Context, InputType, InputValueError, InputValueResult, Value, registry, registry::MetaTypeId,
};
pub struct UploadValue {
pub filename: String,
pub content_type: Option<String>,
#[cfg(feature = "tempfile")]
pub content: std::fs::File,
#[cfg(not(feature = "tempfile"))]
pub content: bytes::Bytes,
}
impl UploadValue {
pub fn try_clone(&self) -> std::io::Result<Self> {
#[cfg(feature = "tempfile")]
{
Ok(Self {
filename: self.filename.clone(),
content_type: self.content_type.clone(),
content: self.content.try_clone()?,
})
}
#[cfg(not(feature = "tempfile"))]
{
Ok(Self {
filename: self.filename.clone(),
content_type: self.content_type.clone(),
content: self.content.clone(),
})
}
}
pub fn into_read(self) -> impl Read + Sync + Send + 'static {
#[cfg(feature = "tempfile")]
{
self.content
}
#[cfg(not(feature = "tempfile"))]
{
std::io::Cursor::new(self.content)
}
}
#[cfg(feature = "unblock")]
#[cfg_attr(docsrs, doc(cfg(feature = "unblock")))]
pub fn into_async_read(self) -> impl AsyncRead + Sync + Send + 'static {
#[cfg(feature = "tempfile")]
{
blocking::Unblock::new(self.content)
}
#[cfg(not(feature = "tempfile"))]
{
futures_util::io::Cursor::new(self.content)
}
}
pub fn size(&self) -> std::io::Result<u64> {
#[cfg(feature = "tempfile")]
{
self.content.metadata().map(|meta| meta.len())
}
#[cfg(not(feature = "tempfile"))]
{
Ok(self.content.len() as u64)
}
}
}
#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct Upload(pub usize);
impl Upload {
pub fn value(&self, ctx: &Context<'_>) -> std::io::Result<UploadValue> {
ctx.query_env.uploads[self.0].try_clone()
}
}
impl Deref for Upload {
type Target = usize;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl InputType for Upload {
type RawValueType = Self;
fn type_name() -> Cow<'static, str> {
Cow::Borrowed("Upload")
}
fn create_type_info(registry: &mut registry::Registry) -> String {
registry.create_input_type::<Self, _>(MetaTypeId::Scalar, |_| registry::MetaType::Scalar {
name: Self::type_name().to_string(),
description: Some("A multipart file upload".to_string()),
is_valid: Some(Arc::new(|value| matches!(value, Value::String(_)))),
visible: None,
inaccessible: false,
tags: Default::default(),
specified_by_url: Some(
"https://github.com/jaydenseric/graphql-multipart-request-spec".to_string(),
),
directive_invocations: Default::default(),
requires_scopes: Default::default(),
})
}
fn parse(value: Option<Value>) -> InputValueResult<Self> {
const PREFIX: &str = "#__graphql_file__:";
let value = value.unwrap_or_default();
if let Value::String(s) = &value
&& let Some(filename) = s.strip_prefix(PREFIX)
{
return Ok(Upload(filename.parse::<usize>().unwrap()));
}
Err(InputValueError::expected_type(value))
}
fn to_value(&self) -> Value {
Value::Null
}
fn as_raw_value(&self) -> Option<&Self::RawValueType> {
Some(self)
}
}