Skip to content

Commit 99ce12a

Browse files
committed
TIKA-4808 -- fix extension calculations for embedded files, further refinements
1 parent df4b9af commit 99ce12a

3 files changed

Lines changed: 50 additions & 16 deletions

File tree

tika-core/src/main/java/org/apache/tika/extractor/EmbeddedDocumentUtil.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -148,23 +148,27 @@ public String getExtension(TikaInputStream is, Metadata metadata) {
148148
//use the buffered mimetypes as default
149149
MimeTypes localMimeTypes = getMimeTypes();
150150

151-
MimeType mimeType = null;
152-
if (mimeString != null) {
153-
mimeType = getRegisteredMimeType(localMimeTypes, mimeString);
151+
//a parseable declared type wins, even if we have no glob for it. Don't
152+
//detect just because the registry lookup came back empty -- that would
153+
//overwrite a type the calling parser set deliberately.
154+
if (mimeString != null && MediaType.parse(mimeString) != null) {
155+
return extensionOf(getRegisteredMimeType(localMimeTypes, mimeString));
154156
}
155-
if (mimeType == null) {
156-
try {
157-
MediaType mediaType = getDetector().detect(is, metadata, context);
158-
is.reset();
159-
//set or correct the mime type. Record what was detected, not the
160-
//registry match, which may have fallen back to the base type.
161-
metadata.set(Metadata.CONTENT_TYPE, mediaType.toString());
162-
mimeType = getRegisteredMimeType(localMimeTypes, mediaType.toString());
163-
} catch (IOException e) {
164-
//swallow
165-
}
157+
try {
158+
MediaType mediaType = getDetector().detect(is, metadata, context);
159+
is.reset();
160+
//set or correct the mime type. Record what was detected, not the
161+
//registry match, which may have fallen back to the base type.
162+
metadata.set(Metadata.CONTENT_TYPE, mediaType.toString());
163+
return extensionOf(getRegisteredMimeType(localMimeTypes, mediaType.toString()));
164+
} catch (IOException e) {
165+
//swallow
166166
}
167-
return mimeType == null ? ".bin" : mimeType.getExtension();
167+
return ".bin";
168+
}
169+
170+
private static String extensionOf(MimeType mimeType) {
171+
return mimeType == null ? "" : mimeType.getExtension();
168172
}
169173

170174
/**

tika-core/src/test/java/org/apache/tika/extractor/EmbeddedDocumentUtilExtensionTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@
1616
*/
1717
package org.apache.tika.extractor;
1818

19+
import static java.nio.charset.StandardCharsets.UTF_8;
1920
import static org.junit.jupiter.api.Assertions.assertEquals;
2021

2122
import org.junit.jupiter.api.Test;
2223

24+
import org.apache.tika.io.TikaInputStream;
25+
import org.apache.tika.metadata.Metadata;
2326
import org.apache.tika.mime.MediaType;
2427
import org.apache.tika.mime.MimeTypes;
28+
import org.apache.tika.parser.ParseContext;
2529

2630
/**
2731
* TIKA-4808 -- a media type carrying parameters must still resolve to the
@@ -107,6 +111,32 @@ public void testGeneratedResourceNameKeepsExtension() {
107111
EmbeddedDocumentUtil.EmbeddedResourcePrefix.IMAGE, 0, "image/png"));
108112
}
109113

114+
/**
115+
* A declared type that simply has no glob must not trigger detection -- that would
116+
* overwrite a CONTENT_TYPE the calling parser set deliberately.
117+
*/
118+
@Test
119+
public void testDeclaredUnregisteredTypeIsNotOverwritten() throws Exception {
120+
Metadata metadata = new Metadata();
121+
metadata.set(Metadata.CONTENT_TYPE, "application/tika-bogus-xyz");
122+
EmbeddedDocumentUtil util = new EmbeddedDocumentUtil(new ParseContext());
123+
try (TikaInputStream tis = TikaInputStream.get("%PDF-1.4\n".getBytes(UTF_8))) {
124+
assertEquals("", util.getExtension(tis, metadata));
125+
assertEquals("application/tika-bogus-xyz", metadata.get(Metadata.CONTENT_TYPE));
126+
}
127+
}
128+
129+
@Test
130+
public void testDeclaredParameterizedTypeResolvesAndIsPreserved() throws Exception {
131+
Metadata metadata = new Metadata();
132+
metadata.set(Metadata.CONTENT_TYPE, "text/plain; charset=UTF-8");
133+
EmbeddedDocumentUtil util = new EmbeddedDocumentUtil(new ParseContext());
134+
try (TikaInputStream tis = TikaInputStream.get("hello".getBytes(UTF_8))) {
135+
assertEquals(".txt", util.getExtension(tis, metadata));
136+
assertEquals("text/plain; charset=UTF-8", metadata.get(Metadata.CONTENT_TYPE));
137+
}
138+
}
139+
110140
/**
111141
* Guard the assumption the fix rests on: normalize() deliberately preserves
112142
* parameters, which is why forName() misses the registry for parameterized names.

tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-mail-module/src/test/java/org/apache/tika/parser/mail/RFC822ParserTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ public void testExtractAttachments() throws Exception {
404404
assertEquals(null, metadataList.get(1).get(Metadata.CONTENT_DISPOSITION));
405405
assertEquals("attachment; filename=\"testPNG.png\"",
406406
metadataList.get(2).get(Metadata.CONTENT_DISPOSITION));
407-
assertEquals("/Test Attachment Email.eml/embedded-1",
407+
assertEquals("/Test Attachment Email.eml/embedded-1.txt",
408408
metadataList.get(1).get(TikaCoreProperties.FINAL_EMBEDDED_RESOURCE_PATH));
409409
assertEquals("/Test Attachment Email.eml/testPNG.png",
410410
metadataList.get(2).get(TikaCoreProperties.FINAL_EMBEDDED_RESOURCE_PATH));

0 commit comments

Comments
 (0)