Allow multiple MIME types per extension

This commit is contained in:
danielpetri1 2023-06-06 10:08:14 +00:00
parent ba6b33a83f
commit ffeb8c3acb
8 changed files with 42 additions and 39 deletions

View File

@ -109,5 +109,7 @@ libraryDependencies ++= Seq(
"org.postgresql" % "postgresql" % "42.4.3",
"org.hibernate" % "hibernate-core" % "5.6.1.Final",
"org.flywaydb" % "flyway-core" % "7.8.2",
"com.zaxxer" % "HikariCP" % "4.0.3"
"com.zaxxer" % "HikariCP" % "4.0.3",
"org.apache.tika" % "tika-core" % "2.8.0",
"org.apache.tika" % "tika-parsers-standard-package" % "2.8.0"
)

View File

@ -23,6 +23,7 @@ object Dependencies {
// Office and document conversion
val apachePoi = "5.1.0"
val nuProcess = "2.0.6"
val tika = "2.8.0"
// Server
val servlet = "4.0.1"
@ -56,6 +57,7 @@ object Dependencies {
val poiXml = "org.apache.poi" % "poi-ooxml" % Versions.apachePoi
val nuProcess = "com.zaxxer" % "nuprocess" % Versions.nuProcess
val tika = "org.apache.tika" % "tika-core" % Versions.tika
val servletApi = "javax.servlet" % "javax.servlet-api" % Versions.servlet
@ -93,6 +95,7 @@ object Dependencies {
Compile.apacheHttpAsync,
Compile.poiXml,
Compile.nuProcess,
Compile.tika,
Compile.servletApi,
Compile.apacheLang,
Compile.apacheIo,

View File

@ -11,6 +11,7 @@ public class MimeTypeUtils {
private static final String DOCX = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
private static final String PPT = "application/vnd.ms-powerpoint";
private static final String PPTX = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
private static final String TIKA_MSOFFICE = "application/x-tika-msoffice";
private static final String ODT = "application/vnd.oasis.opendocument.text";
private static final String RTF = "application/rtf";
private static final String TXT = "text/plain";
@ -21,46 +22,45 @@ public class MimeTypeUtils {
private static final String PNG = "image/png";
private static final String SVG = "image/svg+xml";
private static final HashMap<String,String> EXTENSIONS_MIME = new HashMap<String,String>(16) {
private static final HashMap<String, List<String>> EXTENSIONS_MIME = new HashMap<String, List<String>>(16) {
{
// Add all the supported files
put(FileTypeConstants.XLS, XLS);
put(FileTypeConstants.XLSX, XLSX);
put(FileTypeConstants.DOC, DOC);
put(FileTypeConstants.DOCX, DOCX);
put(FileTypeConstants.PPT, PPT);
put(FileTypeConstants.PPTX, PPTX);
put(FileTypeConstants.ODT, ODT);
put(FileTypeConstants.RTF, RTF);
put(FileTypeConstants.TXT, TXT);
put(FileTypeConstants.ODS, ODS);
put(FileTypeConstants.ODP, ODP);
put(FileTypeConstants.PDF, PDF);
put(FileTypeConstants.JPG, JPEG);
put(FileTypeConstants.JPEG, JPEG);
put(FileTypeConstants.PNG, PNG);
put(FileTypeConstants.SVG, SVG);
put(FileTypeConstants.DOC, Arrays.asList(DOC, DOCX, TIKA_MSOFFICE));
put(FileTypeConstants.XLS, Arrays.asList(XLS, XLSX, TIKA_MSOFFICE));
put(FileTypeConstants.PPT, Arrays.asList(PPT, PPTX, TIKA_MSOFFICE));
put(FileTypeConstants.DOCX, Arrays.asList(DOCX));
put(FileTypeConstants.PPTX, Arrays.asList(PPTX));
put(FileTypeConstants.XLSX, Arrays.asList(XLSX));
put(FileTypeConstants.ODT, Arrays.asList(ODT));
put(FileTypeConstants.RTF, Arrays.asList(RTF));
put(FileTypeConstants.TXT, Arrays.asList(TXT));
put(FileTypeConstants.ODS, Arrays.asList(ODS));
put(FileTypeConstants.ODP, Arrays.asList(ODP));
put(FileTypeConstants.PDF, Arrays.asList(PDF));
put(FileTypeConstants.JPG, Arrays.asList(JPEG));
put(FileTypeConstants.JPEG, Arrays.asList(JPEG));
put(FileTypeConstants.PNG, Arrays.asList(PNG));
put(FileTypeConstants.SVG, Arrays.asList(SVG));
}
};
public Boolean extensionMatchMimeType(String mimeType, String finalExtension) {
if(EXTENSIONS_MIME.containsKey(finalExtension.toLowerCase()) &&
EXTENSIONS_MIME.get(finalExtension.toLowerCase()).equalsIgnoreCase(mimeType)) {
return true;
} else if(EXTENSIONS_MIME.containsKey(finalExtension.toLowerCase() + 'x') &&
EXTENSIONS_MIME.get(finalExtension.toLowerCase() + 'x').equalsIgnoreCase(mimeType)) {
//Exception for MS Office files named with old extension but using internally the new mime type
//e.g. a file named with extension `ppt` but has the content of a `pptx`
return true;
}
finalExtension = finalExtension.toLowerCase();
if (EXTENSIONS_MIME.containsKey(finalExtension)) {
for (String validMimeType : EXTENSIONS_MIME.get(finalExtension)) {
if (validMimeType.equalsIgnoreCase(mimeType)) {
return true;
}
}
}
return false;
}
public List<String> getValidMimeTypes() {
List<String> validMimeTypes = Arrays.asList(XLS, XLSX,
DOC, DOCX, PPT, PPTX, ODT, RTF, TXT, ODS, ODP,
PDF, JPEG, PNG, SVG
PDF, JPEG, PNG, SVG, TIKA_MSOFFICE
);
return validMimeTypes;
}

View File

@ -58,7 +58,7 @@ public class SlidesGenerationProgressNotifier {
pres.getTemporaryPresentationId(),
pres.getName(),
pres.getAuthzToken(),
"IVALID_MIME_TYPE",
"INVALID_MIME_TYPE",
fileMime,
fileExtension
);

View File

@ -12,8 +12,8 @@ const PAGE_COUNT_EXCEEDED_KEY = 'PAGE_COUNT_EXCEEDED';
const PDF_HAS_BIG_PAGE_KEY = 'PDF_HAS_BIG_PAGE';
const GENERATED_SLIDE_KEY = 'GENERATED_SLIDE';
const FILE_TOO_LARGE_KEY = 'FILE_TOO_LARGE';
const CONVERSION_TIMEOUT_KEY = "CONVERSION_TIMEOUT";
const IVALID_MIME_TYPE_KEY = "IVALID_MIME_TYPE";
const CONVERSION_TIMEOUT_KEY = 'CONVERSION_TIMEOUT';
const INVALID_MIME_TYPE_KEY = 'INVALID_MIME_TYPE';
const NO_CONTENT = '204';
// const GENERATING_THUMBNAIL_KEY = 'GENERATING_THUMBNAIL';
// const GENERATED_THUMBNAIL_KEY = 'GENERATED_THUMBNAIL';
@ -52,7 +52,7 @@ export default async function handlePresentationConversionUpdate({ body }, meeti
statusModifier['conversion.maxFileSize'] = body.maxFileSize;
case UNSUPPORTED_DOCUMENT_KEY:
case OFFICE_DOC_CONVERSION_FAILED_KEY:
case IVALID_MIME_TYPE_KEY:
case INVALID_MIME_TYPE_KEY:
statusModifier['conversion.error'] = true;
statusModifier['conversion.fileMime'] = body.fileMime;
statusModifier['conversion.fileExtension'] = body.fileExtension;

View File

@ -54,7 +54,7 @@ const intlMessages = defineMessages({
id: 'app.presentationUploder.upload.413',
description: 'error that file exceed the size limit',
},
IVALID_MIME_TYPE: {
INVALID_MIME_TYPE: {
id: 'app.presentationUploder.conversion.invalidMimeType',
description: 'warns user that the file\'s mime type is not supported or it doesn\'t match the extension',
},
@ -151,7 +151,7 @@ function renderPresentationItemStatus(item, intl) {
case 'PDF_HAS_BIG_PAGE':
constraint['0'] = (item.conversion.bigPageSize / 1000 / 1000).toFixed(2);
break;
case 'IVALID_MIME_TYPE':
case 'INVALID_MIME_TYPE':
constraint['0'] = item.conversion.fileExtension;
constraint['1'] = item.conversion.fileMime;
break;

View File

@ -108,7 +108,7 @@ const observePresentationConversion = (
if (doc.temporaryPresentationId !== temporaryPresentationId && doc.id !== tokenId) return;
if (doc.conversion.status === 'FILE_TOO_LARGE' || doc.conversion.status === 'UNSUPPORTED_DOCUMENT'
|| doc.conversion.status === 'CONVERSION_TIMEOUT' || doc.conversion.status === 'IVALID_MIME_TYPE') {
|| doc.conversion.status === 'CONVERSION_TIMEOUT' || doc.conversion.status === 'INVALID_MIME_TYPE') {
Presentations.update(
{ id: tokenId }, { $set: { temporaryPresentationId, renderedInToast: false } },
);

View File

@ -54,8 +54,6 @@ repositories {
dependencies {
runtimeOnly "com.bertramlabs.plugins:asset-pipeline-grails:4.0.0"
runtimeOnly 'org.apache.tika:tika-core:2.8.0'
runtimeOnly 'org.apache.tika:tika-parsers-standard-package:2.8.0'
implementation "org.springframework:spring-core:5.3.21"
implementation "org.springframework:spring-context:5.3.27"