Skip to content

Commit 62b1e33

Browse files
authored
[CCAP-575] Support for converting docs to pdfs (#647)
1 parent f4d2da8 commit 62b1e33

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/main/java/formflow/library/file/FileValidationService.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@
33
import static java.util.Arrays.stream;
44

55
import java.io.IOException;
6+
import java.io.InputStream;
67
import java.util.List;
78
import java.util.Map;
9+
import java.util.stream.Collectors;
10+
import java.util.zip.ZipEntry;
11+
import java.util.zip.ZipInputStream;
812
import lombok.extern.slf4j.Slf4j;
913
import org.apache.tika.Tika;
1014
import org.springframework.beans.factory.annotation.Value;
1115
import org.springframework.http.MediaType;
1216
import org.springframework.stereotype.Service;
1317
import org.springframework.util.MimeType;
1418
import org.springframework.web.multipart.MultipartFile;
15-
import java.util.stream.Collectors;
1619

1720
/**
1821
* This service is intended to help with miscellaneous file things. This service will help with checking mime types, both proper
@@ -50,6 +53,8 @@ public class FileValidationService {
5053
Map.entry(".odt", new MimeType("application", "vnd.oasis.opendocument.text"))
5154
);
5255

56+
private final MimeType ZIP_MIME_TYPE = new MimeType("application", "zip");
57+
5358
private final List<MimeType> ACCEPTED_MIME_TYPES;
5459

5560
private final List<String> ACCEPTED_FILE_EXTS;
@@ -118,6 +123,22 @@ public Boolean isAcceptedMimeType(MultipartFile file) throws IOException {
118123
if (file.getContentType() == null || file.getContentType().isBlank()) {
119124
return false;
120125
}
126+
127+
MimeType mimeType = MimeType.valueOf(tikaFileValidator.detect(file.getInputStream()));
128+
129+
if (ACCEPTED_MIME_TYPES.contains(FILE_EXT_MIME_TYPE_MAP.get(".docx")) && ZIP_MIME_TYPE.equals(mimeType)) {
130+
// docx files are technically just zip files with xml files inside of them. if the mime type is set to be a
131+
// zip file, and we accept docx files, we can check if the zip is actually a zip... or if it's a docx and return
132+
try (InputStream inputStream = file.getInputStream(); ZipInputStream zipInputStream = new ZipInputStream(inputStream)) {
133+
ZipEntry entry;
134+
while ((entry = zipInputStream.getNextEntry()) != null) {
135+
if (entry.getName().equals("word/document.xml") || entry.getName().equals("[Content_Types].xml")) {
136+
return true;
137+
}
138+
}
139+
}
140+
}
141+
121142
return ACCEPTED_MIME_TYPES.contains(MimeType.valueOf(tikaFileValidator.detect(file.getInputStream())));
122143
}
123144

0 commit comments

Comments
 (0)