|
22 | 22 | import jakarta.servlet.http.HttpServletRequest; |
23 | 23 | import jakarta.servlet.http.HttpServletResponse; |
24 | 24 | import edu.harvard.iq.dataverse.settings.SettingsServiceBean; |
| 25 | +import edu.harvard.iq.dataverse.util.FileUtil; |
25 | 26 | import jakarta.ejb.EJB; |
26 | 27 | import org.apache.commons.io.IOUtils; |
27 | 28 | import org.apache.tika.Tika; |
@@ -55,25 +56,22 @@ protected void processRequest(HttpServletRequest request, HttpServletResponse re |
55 | 56 | String filePath = getFilePath(customFileType); |
56 | 57 |
|
57 | 58 | Path physicalPath = Paths.get(filePath); |
| 59 | + |
58 | 60 | FileInputStream inputStream = null; |
59 | 61 | BufferedReader in = null; |
60 | 62 | try { |
61 | 63 | File fileIn = physicalPath.toFile(); |
62 | 64 | if (fileIn != null) { |
63 | 65 | // Time the MIME type detection |
64 | 66 | long startTime = System.currentTimeMillis(); |
65 | | - Tika tika = new Tika(); |
66 | | - try { |
67 | | - String mimeType = tika.detect(fileIn); |
68 | | - response.setContentType(mimeType); |
69 | | - long endTime = System.currentTimeMillis(); |
70 | | - long duration = endTime - startTime; |
71 | | - logger.info("MIME Type detection for " + filePath + " took " + duration + "ms. Detected type: " + mimeType); |
72 | | - } catch (Exception e) { |
73 | | - long endTime = System.currentTimeMillis(); |
74 | | - long duration = endTime - startTime; |
75 | | - logger.info("Error getting MIME Type for " + filePath + " after " + duration + "ms : " + e.getMessage()); |
76 | | - } |
| 67 | + String filename = physicalPath.getFileName().toString(); |
| 68 | + int dotIndex = filename.lastIndexOf('.'); |
| 69 | + String ext = dotIndex >= 0 ? filename.substring(dotIndex) : ""; |
| 70 | + String mimeType = FileUtil.lookupFileTypeByExtension(ext); |
| 71 | + response.setContentType(mimeType); |
| 72 | + long endTime = System.currentTimeMillis(); |
| 73 | + long duration = endTime - startTime; |
| 74 | + logger.info("MIME Type detection for " + filePath + " took " + duration + "ms. Detected type: " + mimeType); |
77 | 75 | inputStream = new FileInputStream(fileIn); |
78 | 76 |
|
79 | 77 | in = new BufferedReader(new InputStreamReader(inputStream)); |
|
0 commit comments