gpt4 book ai didi

c# - Urlmon.dll 中具有更多 MIME 类型的 FindMimeFromData 方法的替代方法

转载 作者:太空狗 更新时间:2023-10-29 20:54:51 28 4
gpt4 key购买 nike

FindMimeFromData可通过 Windows DLL Urlmon.dll 访问的方法能够确定存储在内存中的给定数据的 MIME 类型,考虑存储此类数据的字节数组的前 256 个字节。

但是在阅读了它的文档之后,我被引导到 MIME Type Detection in Windows Internet Explorer我在哪里可以找到此方法能够识别的 MIME 类型。参见 list .如您所见,此方法仅限于 26 种 MIME 类型。

所以我想知道是否有人可以向我指出另一种具有更多 MIME 类型的方法,或者另一种方法/类,我是否能够包含我认为合适的 MIME 类型。

最佳答案

更新:@GetoX 已采用此代码并将其包装在 .net 核心的 NuGet 包中!见下文,干杯!

So I was wondering if anyone could point me to another method withmore MIME types, or alternatively another method / class were I wouldbe able to include the MIME types I see fit.

我使用 Winista 和 URLMon 的混合来检测上传文件的真实格式..

Winista MIME 检测

假设有人用 jpg 扩展名重命名了一个 exe,您仍然可以使用二进制分析来确定“真实”文件格式。它不检测 swf 或 flv,但几乎可以检测所有其他众所周知的格式 + 您可以获得一个十六进制编辑器并添加它可以检测的更多文件。

文件魔法

Winista 使用 XML 文件“mime-type.xml”检测真实的 MIME 类型,该文件包含有关文件类型和用于识别内容类型的签名的信息。例如:

<!--
! Audio primary type
! -->

<mime-type name="audio/basic"
description="uLaw/AU Audio File">
<ext>au</ext><ext>snd</ext>
<magic offset="0" type="byte" value="2e736e64000000"/>
</mime-type>

<mime-type name="audio/midi"
description="Musical Instrument Digital Interface MIDI-sequention Sound">
<ext>mid</ext><ext>midi</ext><ext>kar</ext>
<magic offset="0" value="MThd"/>
</mime-type>

<mime-type name="audio/mpeg"
description="MPEG Audio Stream, Layer III">
<ext>mp3</ext><ext>mp2</ext><ext>mpga</ext>
<magic offset="0" value="ID3"/>
</mime-type>

当 Winista 无法检测到真正的文件格式时,我又求助于 URLMon 方法:

public class urlmonMimeDetect
{
[DllImport(@"urlmon.dll", CharSet = CharSet.Auto)]
private extern static System.UInt32 FindMimeFromData(
System.UInt32 pBC,
[MarshalAs(UnmanagedType.LPStr)] System.String pwzUrl,
[MarshalAs(UnmanagedType.LPArray)] byte[] pBuffer,
System.UInt32 cbSize,
[MarshalAs(UnmanagedType.LPStr)] System.String pwzMimeProposed,
System.UInt32 dwMimeFlags,
out System.UInt32 ppwzMimeOut,
System.UInt32 dwReserverd
);

public string GetMimeFromFile(string filename)
{
if (!File.Exists(filename))
throw new FileNotFoundException(filename + " not found");

byte[] buffer = new byte[256];
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
{
if (fs.Length >= 256)
fs.Read(buffer, 0, 256);
else
fs.Read(buffer, 0, (int)fs.Length);
}
try
{
System.UInt32 mimetype;
FindMimeFromData(0, null, buffer, 256, null, 0, out mimetype, 0);
System.IntPtr mimeTypePtr = new IntPtr(mimetype);
string mime = Marshal.PtrToStringUni(mimeTypePtr);
Marshal.FreeCoTaskMem(mimeTypePtr);
return mime;
}
catch (Exception e)
{
return "unknown/unknown";
}
}
}

从 Winista 方法内部,我在这里求助于 URLMon:

   public MimeType GetMimeTypeFromFile(string filePath)
{
sbyte[] fileData = null;
using (FileStream srcFile = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
byte[] data = new byte[srcFile.Length];
srcFile.Read(data, 0, (Int32)srcFile.Length);
fileData = Winista.Mime.SupportUtil.ToSByteArray(data);
}

MimeType oMimeType = GetMimeType(fileData);
if (oMimeType != null) return oMimeType;

//We haven't found the file using Magic (eg a text/plain file)
//so instead use URLMon to try and get the files format
Winista.MimeDetect.URLMONMimeDetect.urlmonMimeDetect urlmonMimeDetect = new Winista.MimeDetect.URLMONMimeDetect.urlmonMimeDetect();
string urlmonMimeType = urlmonMimeDetect.GetMimeFromFile(filePath);
if (!string.IsNullOrEmpty(urlmonMimeType))
{
foreach (MimeType mimeType in types)
{
if (mimeType.Name == urlmonMimeType)
{
return mimeType;
}
}
}

return oMimeType;
}

Wayback Machine link to the Winista utility from netomatix .据我所知,他们发现了一些“开源 Nutch 爬虫系统中的 mime 阅读器实用程序类”,并且他们在 2000 年初进行了 C# 重写。

我已经使用 Winista 托管了我的 MimeDetect 项目,而 URLMon 回退到这里(请使用十六进制编辑器贡献新的文件类型): https://github.com/MeaningOfLights/MimeDetect

您也可以使用注册表方法或 .Net 4.5 methodthis post 中提到由 Paul Zahra 链接,但恕我直言,Winista 是最好的。

享受知道您系统上的文件他们声称的而不是laden with malware !


更新:

对于桌面应用程序,您可能会发现 WindowsAPICodePack 工作得更好:

using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Shell.PropertySystem;

private static string GetFilePropertyItemTypeTextValueFromShellFile(string filePathWithExtension)
{
var shellFile = ShellFile.FromFilePath(filePathWithExtension);
var prop = shellFile.Properties.GetProperty(PItemTypeTextCanonical);
return prop.FormatForDisplay(PropertyDescriptionFormatOptions.None);
}

关于c# - Urlmon.dll 中具有更多 MIME 类型的 FindMimeFromData 方法的替代方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15300567/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com