Browse Source

Move MIME detection into separate class

pull/23/head
Siegfried Pammer 15 years ago
parent
commit
8863e8b820
  1. 1
      src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj
  2. 28
      src/Main/Base/Project/Src/Services/DisplayBinding/AutoDetectDisplayBinding.cs
  3. 37
      src/Main/Base/Project/Src/Services/MimeTypeDetection.cs

1
src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj

@ -341,6 +341,7 @@ @@ -341,6 +341,7 @@
<Compile Include="Src\Internal\Doozers\IOptionPanelDescriptor.cs" />
<Compile Include="Src\Services\File\FileService.cs" />
<Compile Include="Src\Services\File\FileEventArgs.cs" />
<Compile Include="Src\Services\MimeTypeDetection.cs" />
<Compile Include="Src\Services\ParserService\AssemblyParserService.cs" />
<Compile Include="Src\Services\ParserService\LoadSolutionProjects.cs" />
<Compile Include="Src\Services\ParserService\ParserService.cs" />

28
src/Main/Base/Project/Src/Services/DisplayBinding/AutoDetectDisplayBinding.cs

@ -14,17 +14,6 @@ namespace ICSharpCode.SharpDevelop @@ -14,17 +14,6 @@ namespace ICSharpCode.SharpDevelop
/// </summary>
public sealed class AutoDetectDisplayBinding : IDisplayBinding
{
[DllImport("urlmon.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = false)]
static extern unsafe int FindMimeFromData(
IntPtr pBC,
[MarshalAs(UnmanagedType.LPWStr)] string pwzUrl,
byte* pBuffer,
int cbSize,
[MarshalAs(UnmanagedType.LPWStr)] string pwzMimeProposed,
int dwMimeFlags,
out IntPtr ppwzMimeOut,
int dwReserved);
public bool IsPreferredBindingForFile(string fileName)
{
return false;
@ -51,7 +40,7 @@ namespace ICSharpCode.SharpDevelop @@ -51,7 +40,7 @@ namespace ICSharpCode.SharpDevelop
string mime = "text/plain";
if (stream.Length > 0) {
stream.Position = 0;
mime = FindMimeType(new BinaryReader(stream).ReadBytes(BUFFER_LENGTH));
mime = MimeTypeDetection.FindMimeType(new BinaryReader(stream).ReadBytes(BUFFER_LENGTH));
}
foreach (var codon in codons) {
stream.Position = 0;
@ -68,20 +57,5 @@ namespace ICSharpCode.SharpDevelop @@ -68,20 +57,5 @@ namespace ICSharpCode.SharpDevelop
return bestMatch.Binding.CreateContentForFile(file);
}
unsafe string FindMimeType(byte[] buffer)
{
fixed (byte *b = buffer) {
const int FMFD_ENABLEMIMESNIFFING = 0x00000002;
IntPtr mimeout;
int result = FindMimeFromData(IntPtr.Zero, null, b, buffer.Length, null, FMFD_ENABLEMIMESNIFFING, out mimeout, 0);
if (result != 0)
throw Marshal.GetExceptionForHR(result);
string mime = Marshal.PtrToStringUni(mimeout);
Marshal.FreeCoTaskMem(mimeout);
return mime;
}
}
}
}

37
src/Main/Base/Project/Src/Services/MimeTypeDetection.cs

@ -0,0 +1,37 @@ @@ -0,0 +1,37 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Runtime.InteropServices;
namespace ICSharpCode.SharpDevelop
{
public static class MimeTypeDetection
{
[DllImport("urlmon.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = false)]
static extern unsafe int FindMimeFromData(
IntPtr pBC,
[MarshalAs(UnmanagedType.LPWStr)] string pwzUrl,
byte* pBuffer,
int cbSize,
[MarshalAs(UnmanagedType.LPWStr)] string pwzMimeProposed,
int dwMimeFlags,
out IntPtr ppwzMimeOut,
int dwReserved);
public static unsafe string FindMimeType(byte[] buffer)
{
fixed (byte *b = buffer) {
const int FMFD_ENABLEMIMESNIFFING = 0x00000002;
IntPtr mimeout;
int result = FindMimeFromData(IntPtr.Zero, null, b, buffer.Length, null, FMFD_ENABLEMIMESNIFFING, out mimeout, 0);
if (result != 0)
throw Marshal.GetExceptionForHR(result);
string mime = Marshal.PtrToStringUni(mimeout);
Marshal.FreeCoTaskMem(mimeout);
return mime;
}
}
}
}
Loading…
Cancel
Save