forked from sergey-s-betke/ITG.WinAPI.UrlMon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathITG.WinAPI.UrlMon.psm1
133 lines (123 loc) · 4.5 KB
/
ITG.WinAPI.UrlMon.psm1
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
Add-Type `
@"
namespace ITG.WinAPI.UrlMon {
using System;
using System.IO;
using System.Runtime.InteropServices;
[Flags]
public enum FMFD {
Default = 0x00000000, // No flags specified. Use default behavior for the function.
URLAsFileName = 0x00000001, // Treat the specified pwzUrl as a file name.
EnableMIMESniffing = 0x00000002, // Internet Explorer 6 for Windows XP SP2 and later. Use MIME-type detection even if FEATURE_MIME_SNIFFING is detected. Usually, this feature control key would disable MIME-type detection.
IgnoreMIMETextPlain = 0x00000004, // Internet Explorer 6 for Windows XP SP2 and later. Perform MIME-type detection if "text/plain" is proposed, even if data sniffing is otherwise disabled. Plain text may be converted to text/html if HTML tags are detected.
ServerMIME = 0x00000008, // Internet Explorer 8. Use the authoritative MIME type specified in pwzMimeProposed. Unless FMFD_IGNOREMIMETEXTPLAIN is specified, no data sniffing is performed.
RespectTextPlain = 0x00000010, // Internet Explorer 9. Do not perform detection if "text/plain" is specified in pwzMimeProposed.
RetrunUpdatedImgMIMEs = 0x00000020 // Internet Explorer 9. Returns image/png and image/jpeg instead of image/x-png and image/pjpeg.
};
public
class API {
// http://msdn.microsoft.com/en-us/library/ms775107(VS.85).aspx
// http://www.rsdn.ru/article/dotnet/netTocom.xml
[DllImport(
"urlmon.dll"
, CharSet=CharSet.Unicode
, ExactSpelling=true
, SetLastError=false
)]
public
static
extern
System.UInt32
FindMimeFromData(
System.UInt32 pBC,
[In, MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pwzUrl,
[In, MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.I1, SizeParamIndex=3)] byte[] pBuffer,
[In, MarshalAs(UnmanagedType.U4)] System.UInt32 cbSize,
[In, MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pwzMimeProposed,
[In, MarshalAs(UnmanagedType.U4)] System.UInt32 dwMimeFlags,
[Out, MarshalAs(UnmanagedType.LPWStr)] out String ppwzMimeOut,
[In, MarshalAs(UnmanagedType.U4)] System.UInt32 dwReserverd
);
}
}
"@;
function Get-MIME {
<#
.Synopsis
Функция определяет MIME тип файла по его содержимому и расширению имени файла.
.Description
Функция определяет MIME тип файла по его содержимому и расширению имени файла
(если файл недоступен).
Обёртка для API FindMimeFromData.
.Link
[FindMimeFromData]: http://msdn.microsoft.com/en-us/library/ms775107.aspx
.Link
Обсуждение неочевидных тонкостей использования
FindMimeFromData - http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvcs/thread/d79e76e3-b8c9-4fce-a97d-94ded18ea4dd
.Example
"logo.jpg" | Get-MIME
#>
[CmdletBinding(
)]
param (
# путь к файлу, MIME для которого необходимо определить
[Parameter(
Mandatory=$true,
Position=0,
ValueFromPipeline=$true
)]
[System.IO.FileInfo]$Path
)
process {
try {
$length = 0;
[Byte[]] $buffer = $null;
if ( [System.IO.File]::Exists( $Path ) ) {
Write-Verbose "Определяем MIME тип файла по его содержимому (файл $($Path.FullName) доступен).";
$fs = New-Object System.IO.FileStream (
$Path,
[System.IO.FileMode]::Open,
[System.IO.FileAccess]::Read,
[system.IO.FileShare]::Read,
256
);
try {
if ( $fs.Length -ge 256) {
$length = 256;
} else {
$length = $fs.Length;
};
if ( $length ) {
$buffer = New-Object Byte[] (256);
$length = $fs.Read( $buffer, 0, $length );
};
} finally {
$fs.Dispose();
};
} else {
Write-Verbose "Определяем MIME тип файла по расширению его имени (файл $($Path.FullName) недоступен).";
};
[string]$mime = '';
$err = [ITG.WinAPI.UrlMon.API]::FindMimeFromData(
0,
$Path.FullName,
$buffer,
$length,
$null,
[ITG.WinAPI.UrlMon.FMFD]::URLAsFileName -bor [ITG.WinAPI.UrlMon.FMFD]::RetrunUpdatedImgMIMEs,
[ref]$mime,
0
);
[System.Runtime.InteropServices.Marshal]::ThrowExceptionForHR( $err );
$mime;
} catch [Exception] {
Write-Error `
-Exception $_.Exception `
-Message "Возникла ошибка при попытке определения MIME типа для файла $($Path.FullName)" `
;
};
}
}
Export-ModuleMember `
Get-MIME `
;