From 16d03003488998ae5e64cb80f0a94e8bb5cf6961 Mon Sep 17 00:00:00 2001 From: Leonid Andreev Date: Mon, 24 Jan 2022 15:14:41 -0500 Subject: [PATCH] prevents the file upload api from defaulting to "text/plain" for the mime type, when no Content-Type: header is found in the incoming data part. (#8344) --- .../edu/harvard/iq/dataverse/api/Datasets.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/edu/harvard/iq/dataverse/api/Datasets.java b/src/main/java/edu/harvard/iq/dataverse/api/Datasets.java index f708cd4580e..93f48753908 100644 --- a/src/main/java/edu/harvard/iq/dataverse/api/Datasets.java +++ b/src/main/java/edu/harvard/iq/dataverse/api/Datasets.java @@ -2356,7 +2356,23 @@ public Response addFileToDataset(@PathParam("id") String idSupplied, } } else { newFilename = contentDispositionHeader.getFileName(); - newFileContentType = formDataBodyPart.getMediaType().toString(); + // Let's see if the form data part has the mime (content) type specified. + // Note that we don't want to rely on formDataBodyPart.getMediaType() - + // because that defaults to "text/plain" when no "Content-Type:" header is + // present. Instead we'll go through the headers, and see if "Content-Type:" + // is there. If not, we'll default to "application/octet-stream" - the generic + // unknown type. This will prompt the application to run type detection and + // potentially find something more accurate. + //newFileContentType = formDataBodyPart.getMediaType().toString(); + + for (String header : formDataBodyPart.getHeaders().keySet()) { + if (header.equalsIgnoreCase("Content-Type")) { + newFileContentType = formDataBodyPart.getHeaders().get(header).get(0); + } + } + if (newFileContentType == null) { + newFileContentType = FileUtil.MIME_TYPE_UNDETERMINED_DEFAULT; + } }