exceptionfactory commented on code in PR #8011:
URL: https://github.com/apache/nifi/pull/8011#discussion_r1393154825
##########
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/IdentifyMimeType.java:
##########
@@ -233,53 +239,62 @@ public void onTrigger(final ProcessContext context, final
ProcessSession session
}
final ComponentLog logger = getLogger();
- final AtomicReference<String> mimeTypeRef = new
AtomicReference<>(null);
- final String filename =
flowFile.getAttribute(CoreAttributes.FILENAME.key());
-
- session.read(flowFile, new InputStreamCallback() {
- @Override
- public void process(final InputStream stream) throws IOException {
- try (final InputStream in = new BufferedInputStream(stream);
- final TikaInputStream tikaStream =
TikaInputStream.get(in)) {
- Metadata metadata = new Metadata();
-
- if (filename != null &&
context.getProperty(USE_FILENAME_IN_DETECTION).asBoolean()) {
- metadata.add(TikaCoreProperties.RESOURCE_NAME_KEY,
filename);
- }
- // Get mime type
- MediaType mediatype = detector.detect(tikaStream,
metadata);
- mimeTypeRef.set(mediatype.toString());
- }
+
+ final String mediaTypeString;
+ final String extension;
+ final Charset charset;
+
+ try (final InputStream flowFileStream = session.read(flowFile);
+ final TikaInputStream tikaStream =
TikaInputStream.get(flowFileStream)) {
+ final String filename =
flowFile.getAttribute(CoreAttributes.FILENAME.key());
+
+ Metadata metadata = new Metadata();
+ if (filename != null &&
context.getProperty(USE_FILENAME_IN_DETECTION).asBoolean()) {
+ metadata.add(TikaCoreProperties.RESOURCE_NAME_KEY, filename);
}
- });
- String mimeType = mimeTypeRef.get();
+ final MediaType mediaType = detector.detect(tikaStream, metadata);
+ mediaTypeString = mediaType.getBaseType().toString();
+ extension = lookupExtension(mediaTypeString, logger);
+ charset = identifyCharset(tikaStream, metadata, mediaType);
+ } catch (IOException e) {
+ throw new ProcessException("IOException thrown identifying
mime-type of FlowFile content", e);
+ }
+
+ flowFile = session.putAttribute(flowFile,
CoreAttributes.MIME_TYPE.key(), mediaTypeString);
+ flowFile = session.putAttribute(flowFile, "mime.extension", extension);
+ flowFile = session.putAttribute(flowFile, "mime.charset", charset ==
null ? "" : charset.name());
Review Comment:
It seems better to avoid setting this attribute at all, as opposed to
setting an empty value when the detected character set is null.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]