[英]Extracting attachment name from eml file using Content-Type header
I'm using Tika-server to parse bunch of eml files. 我正在使用Tika-server解析一堆eml文件。 Extracting both content and metadata of emls and attachments works fine while using /rmeta
endpoint. 使用/rmeta
端点时,提取emls和附件的内容以及元数据都可以正常工作。
Problem occurs with proper attachment file name. 正确的附件文件名出现问题。 When attachment part in raw eml file has got a following structure: 当原始eml文件中的附件部分具有以下结构时:
Content-Type: application/pdf; name="filename_a.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="filename_a.pdf"
everything works fine: extracted filename path in metadata object (in api response) is: 一切工作正常:元数据对象中的提取文件名路径(在api响应中)为:
"X-TIKA:embedded_resource_path": "/filename_a.pdf"
However some of my emails have got malformed header structure (missing filename in Content-Disposition) ie: 但是我的一些电子邮件的标头结构格式错误(Content-Disposition中缺少文件名),即:
Content-Type: application/pdf; name="filename_a.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
Then after parsing the whole eml I obtain: 然后在解析整个eml之后,我得到:
"X-TIKA:embedded_resource_path": "/embedded-1"
I checked in Tika's source code that filename meta is defined in \\org\\apache\\tika\\parser\\RecursiveParserWrapper.class here: 我在Tika的源代码中检查了\\ org \\ apache \\ tika \\ parser \\ RecursiveParserWrapper.class中定义的文件名元:
private String getResourceName(Metadata metadata, RecursiveParserWrapper.ParserState state) {
String objectName = "";
if (metadata.get("resourceName") != null) {
objectName = metadata.get("resourceName");
} else if (metadata.get("embeddedRelationshipId") != null) {
objectName = metadata.get("embeddedRelationshipId");
} else {
objectName = "embedded-" + ++state.unknownCount;
}
objectName = FilenameUtils.getName(objectName);
return objectName;
}
I was trying to access somehow mentioned filename attribute by inspecting Content-Type key in metadata object but it's not there. 我试图通过检查元数据对象中的Content-Type键来以某种方式访问提到的filename属性,但是它不存在。 (I assume that Tika assess Content-type key not just by looking into proper header hence needed filename is absent) (我认为Tika不仅通过查看适当的标头来评估Content-type密钥,因此缺少所需的文件名)
Therefore my question (since I'm not able to figure it out) is there a way to modify Tika source code to force filename extraction from Content-Type header when proper filename attribute in Content-Disposition header is missing? 因此,我的问题(由于我无法弄清楚)是否有一种方法可以修改Tika源代码,以在缺少Content-Disposition标头中的正确文件名属性时从Content-Type标头中强制提取文件名?
Ok, so I managed on my own. 好吧,所以我自己做。 The workaround is preety simple and straightforward. 解决方法非常简单明了。
One has to extend one of the conditions in \\org\\apache\\tika\\parser\\mail\\MailContentHandler.class . 必须扩展\\ org \\ apache \\ tika \\ parser \\ mail \\ MailContentHandler.class中的条件之一 。 In line 129 we have: 在第129行,我们有:
if (contentDispositionFileName != null) {
submd.set("resourceName", contentDispositionFileName);
}
By extending with additional else block: 通过扩展其他else块:
if (contentDispositionFileName != null) {
submd.set("resourceName", contentDispositionFileName);
} else {
Map<String, String> contentTypeParameters = ((MaximalBodyDescriptor)body).getContentTypeParameters();
String contentTypeFilename = (String)contentTypeParameters.get("name");
submd.set("resourceName", contentTypeFilename);
}
we enforce the handler to look for an additional filename property in content type parameters. 我们强制处理程序在内容类型参数中寻找其他文件名属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.