forked from fsprojects/FSharp.Data.GraphQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestExecutionContext.fs
More file actions
27 lines (23 loc) · 1.05 KB
/
RequestExecutionContext.fs
File metadata and controls
27 lines (23 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
namespace FSharp.Data.GraphQL.Server.AspNetCore
open System.IO
open FSharp.Data.GraphQL
open Microsoft.AspNetCore.Http
type HttpContextRequestExecutionContext (httpContext : HttpContext) =
interface IInputExecutionContext with
member this.GetFile (key) =
if not httpContext.Request.HasFormContentType then
Error "Request does not have form content type"
else
let form = httpContext.Request.Form
match (form.Files |> Seq.vtryFind (fun f -> f.Name = key)) with
| ValueSome file ->
let memoryStream = new MemoryStream ()
use fileStream = file.OpenReadStream ()
fileStream.CopyTo (memoryStream)
memoryStream.Position <- 0L
Ok {
FileName = file.FileName
Stream = memoryStream
ContentType = file.ContentType
}
| ValueNone -> Error $"File with key '{key}' not found"