Last active
August 29, 2015 14:06
-
-
Save toburger/759b59612a5af27392d9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if INTERACTIVE | |
#r "System.Net.Http" | |
#endif | |
module MimeTip | |
open System.IO | |
open System.Net.Http | |
open System.Runtime.InteropServices | |
[<DllImport("urlmon.dll", CharSet = CharSet.Auto)>] | |
extern uint32 private FindMimeFromData( | |
uint32 pBC, | |
[<MarshalAs(UnmanagedType.LPStr)>] string pwzUrl, | |
[<MarshalAs(UnmanagedType.LPArray)>] byte[] pBuffer, | |
uint32 cbSize, | |
[<MarshalAs(UnmanagedType.LPStr)>] string pwzMimeProposed, | |
uint32 dwMimeFlags, | |
[<Out>] uint32& ppwzMimeOut, | |
uint32 dwReserved) | |
let smime (stream: Stream) = | |
let buffer = Array.zeroCreate 256 | |
ignore (stream.Read(buffer, 0, 256)) | |
try | |
let mutable mimetype = 0ul | |
match FindMimeFromData(0ul, null, buffer, 256ul, null, 0ul, &mimetype, 0ul) with | |
| 0ul -> let mimeTypePtr = nativeint mimetype | |
let mime = Marshal.PtrToStringUni mimeTypePtr | |
Marshal.FreeCoTaskMem(mimeTypePtr) | |
Some mime | |
| _ -> None | |
with _ -> None | |
let asyncwmime url = async { | |
use client = new HttpClient() | |
use! stream = client.GetStreamAsync(url: string) |> Async.AwaitTask | |
return smime stream | |
} | |
let wmime url = asyncwmime url |> Async.RunSynchronously | |
let mime filename = | |
use stream = File.OpenRead filename | |
smime stream |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usage: