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
29 lines (24 loc) · 1.12 KB
/
RequestExecutionContext.fs
File metadata and controls
29 lines (24 loc) · 1.12 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
28
29
namespace FSharp.Data.GraphQL.Server.AspNetCore
open System.IO
open FSharp.Data.GraphQL
open Microsoft.AspNetCore.Http
type HttpContextRequestExecutionContext (httpContext : IHttpContextAccessor) =
interface IInputExecutionContext with
member this.GetFile (key) =
let context = httpContext.HttpContext
if not context.Request.HasFormContentType then
Error "Request does not have form content type"
else
let form = context.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.Seek (0L, SeekOrigin.Begin) |> ignore
Ok {
FileName = file.FileName
Stream = memoryStream
ContentType = file.ContentType
}
| ValueNone -> Error $"File with key '{key}' not found"