forked from fsprojects/FSharp.Data.GraphQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpload.fs
More file actions
40 lines (32 loc) · 1.5 KB
/
Copy pathUpload.fs
File metadata and controls
40 lines (32 loc) · 1.5 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
30
31
32
33
34
35
36
37
38
39
40
/// The MIT License (MIT)
/// Copyright (c) 2016 Bazinga Technologies Inc
namespace FSharp.Data.GraphQL
open System
open System.IO
open FSharp.Data.GraphQL.Client
/// The base type for all GraphQLProvider upload types.
/// Upload types are used in GraphQL multipart request spec, mostly for file uploading features.
type Upload (stream : Stream, fileName : string, ?contentType : string, ?ownsStream : bool) =
new(bytes : byte [], fileName, ?contentType) =
let stream = new MemoryStream(bytes)
match contentType with
| Some ct -> new Upload(stream, fileName, ct, true)
| None -> new Upload(stream, fileName, ownsStream = true)
/// Gets the stream associated to this Upload type.
member __.Stream = stream
/// Gets the content type of this Upload type.
member __.ContentType =
match contentType with
| Some ct -> ct
| None ->
let ext = Path.GetExtension(fileName)
match MimeTypes.dict.Force().TryGetValue(ext) with
| (true, mime) -> mime
| _ -> "application/octet-stream"
/// Gets the name of the file which contained on the stream.
member __.FileName = fileName
/// Gets a boolean value indicating if this Upload type owns the stream associated with it.
/// If true, it will dispose the stream when this Upload type is disposed.
member __.OwnsStream = defaultArg ownsStream false
interface IDisposable with
member x.Dispose() = if x.OwnsStream then x.Stream.Dispose()