Skip to content

Commit

Permalink
TRUNK-5027 - Added MIME types support for Data handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
tmarzeion committed Feb 7, 2017
1 parent f91198c commit 5762cbd
Show file tree
Hide file tree
Showing 12 changed files with 555 additions and 90 deletions.
94 changes: 68 additions & 26 deletions api/src/main/java/org/openmrs/obs/handler/AbstractHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@
*/
package org.openmrs.obs.handler;

import java.io.File;
import java.io.IOException;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Arrays;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand All @@ -25,6 +19,15 @@
import org.openmrs.util.OpenmrsConstants;
import org.openmrs.util.OpenmrsUtil;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;

/**
* Abstract handler for some convenience methods Files are stored in the location specified by the
* global property: "obs.complex_obs_dir"
Expand Down Expand Up @@ -67,37 +70,47 @@ public File getOutputFileToWrite(Obs obs) throws IOException {

File dir = OpenmrsUtil.getDirectoryInApplicationDataDirectory(Context.getAdministrationService().getGlobalProperty(
OpenmrsConstants.GLOBAL_PROPERTY_COMPLEX_OBS_DIR));
File outputfile = null;
File outputFile;

// Get the output stream
if (null == title) {
String now = longfmt.format(new Date());
outputfile = new File(dir, now);
outputFile = new File(dir, now);
} else {
title = title.replace("." + extension, "");
outputfile = new File(dir, title + "." + extension);
// outputfile = new File(dir, title);
outputFile = new File(dir, title + "." + extension);
}

int i = 0;
String tmp = null;
String tmp;

// If the Obs does not exist, but the File does, append a two-digit
// count number to the filename and save it.
while (obs.getObsId() == null && outputfile.exists() && i < 100) {
tmp = null;
if (outputFile.exists() || StringUtils.isNotBlank(obs.getComplexData().getMimeType())) {
// Remove the extension from the filename.
tmp = String.valueOf(outputfile.getAbsolutePath().replace("." + extension, ""));
outputfile = null;
// Append two-digit count number to the filename.
String filename = (i < 1) ? tmp + "_" + nf.format(Integer.valueOf(++i)) : tmp.replace(nf.format(Integer
.valueOf(i)), nf.format(Integer.valueOf(++i)));
tmp = String.valueOf(outputFile.getAbsolutePath().replace("." + extension, ""));

// Get MIME type
String mimetype = "";
if (StringUtils.isNotBlank(obs.getComplexData().getMimeType())) {
mimetype = "_" + obs.getComplexData().getMimeType().replace("/", "-slash-");
}

// If the Obs does not exist, but the File does, append a two-digit
// count number to the filename and save it.
String twoDigitNumber = "";
if (new File(tmp + mimetype + "." + extension).exists()) {
for (int i = 1; obs.getObsId() == null && i < 100; i++) {
// create two-digit count number for the filename.
twoDigitNumber = "_" + nf.format(Integer.valueOf(i));
if (!new File(tmp + mimetype + twoDigitNumber + "." + extension).exists()) {
break;
}
}
}

String filename = tmp + mimetype + twoDigitNumber;
// Append the extension to the filename.
outputfile = new File(filename + "." + extension);
outputFile = new File(filename + "." + extension);
}

return outputfile;

return outputFile;
}

/**
Expand All @@ -119,6 +132,35 @@ public String getExtension(String filename) {
return extension;
}

public String getMimeTypeFromFile(File file) {
if (file == null) {
return null;
}

String filenameWithoutExtension = FilenameUtils.removeExtension(file.getName());

String mimeType = null;
// Try to parse MIME type from filename
if (filenameWithoutExtension.contains("-slash-")) {
HashSet<String> fileParams = new HashSet<>(Arrays.asList(filenameWithoutExtension.split("_")));
for (String param : fileParams) {
if (param.contains("-slash-")) {
return param.replace("-slash-","/");
}
}
}
else {
// Try to guess MIME type
try {
mimeType = Files.probeContentType(file.toPath());
} catch (IOException e) {
log.error("Trying to probe content type: " + file.getAbsolutePath(), e);
}
}
//Default MIME type value
return mimeType != null ? mimeType : "application/octet-stream";
}

/**
* @see org.openmrs.obs.ComplexObsHandler#getObs(Obs, String)
*/
Expand Down
19 changes: 8 additions & 11 deletions api/src/main/java/org/openmrs/obs/handler/BinaryDataHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,19 @@
*/
package org.openmrs.obs.handler;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.util.Assert;

import org.openmrs.Obs;
import org.openmrs.api.APIException;
import org.openmrs.obs.ComplexData;
import org.openmrs.obs.ComplexObsHandler;
import org.openmrs.util.OpenmrsUtil;
import org.springframework.util.Assert;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
* Handler for storing files for complex obs to the file system. Files are stored in the location
Expand Down Expand Up @@ -72,16 +70,15 @@ public Obs getObs(Obs obs, String view) {
catch (IOException e) {
log.error("Trying to read file: " + file.getAbsolutePath(), e);
}

obs.setComplexData(complexData);

} else {
// No other view supported
// NOTE: if adding support for another view, don't forget to update supportedViews list above
return null;
}

Assert.notNull(complexData, "Complex data must not be null");
complexData.setMimeType("application/octet-stream");
complexData.setMimeType(getMimeTypeFromFile(file));
obs.setComplexData(complexData);

return obs;
Expand Down
22 changes: 10 additions & 12 deletions api/src/main/java/org/openmrs/obs/handler/BinaryStreamHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,21 @@
*/
package org.openmrs.obs.handler;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.util.Assert;

import org.openmrs.Obs;
import org.openmrs.api.APIException;
import org.openmrs.obs.ComplexData;
import org.openmrs.obs.ComplexObsHandler;
import org.openmrs.util.OpenmrsConstants;
import org.openmrs.util.OpenmrsUtil;
import org.springframework.util.Assert;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
* Handler for storing generic binary data for complex obs to the file system.
Expand Down Expand Up @@ -60,11 +58,11 @@ public BinaryStreamHandler() {
@Override
public Obs getObs(Obs obs, String view) {
ComplexData complexData = null;

File file;
// Raw stream
if (ComplexObsHandler.RAW_VIEW.equals(view)) {
try {
File file = getComplexDataFile(obs);
file = getComplexDataFile(obs);
String[] names = obs.getValueComplex().split("\\|");
String originalFilename = names[0];
originalFilename = originalFilename.replace(",", "").replace(" ", "");
Expand All @@ -86,7 +84,7 @@ public Obs getObs(Obs obs, String view) {
}

Assert.notNull(complexData, "Complex data must not be null");
complexData.setMimeType("application/octet-stream");
complexData.setMimeType(getMimeTypeFromFile(file));
obs.setComplexData(complexData);

return obs;
Expand Down
25 changes: 12 additions & 13 deletions api/src/main/java/org/openmrs/obs/handler/ImageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,24 @@
*/
package org.openmrs.obs.handler;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.Obs;
import org.openmrs.api.APIException;
import org.openmrs.obs.ComplexData;
import org.openmrs.obs.ComplexObsHandler;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.FileImageInputStream;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.FileImageInputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.Obs;
import org.openmrs.api.APIException;
import org.openmrs.obs.ComplexData;
import org.openmrs.obs.ComplexObsHandler;
import java.util.Set;

/**
* Handler for storing basic images for complex obs to the file system. The image mime type used is
Expand Down Expand Up @@ -87,7 +86,7 @@ public Obs getObs(Obs obs, String view) {
Iterator<ImageReader> imgReader = ImageIO.getImageReaders(imgStream);
imgStream.close();
if (imgReader.hasNext()) {
complexData.setMimeType("image/" + imgReader.next().getFormatName().toLowerCase());
complexData.setMimeType(getMimeTypeFromFile(file));
} else {
log.warn("MIME type of " + file.getAbsolutePath() + " is not known");
}
Expand Down
20 changes: 10 additions & 10 deletions api/src/main/java/org/openmrs/obs/handler/MediaHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@
*/
package org.openmrs.obs.handler;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.Obs;
Expand All @@ -24,9 +17,16 @@
import org.openmrs.obs.ComplexObsHandler;
import org.openmrs.util.OpenmrsUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
* Handler for storing audio and video for complex obs to the file system. The mime type used is
* taken from the file name. Media are stored in the location specified by the global property: "obs.complex_obs_dir"
* Handler for storing audio and video for complex obs to the file system.
* Media are stored in the location specified by the global property: "obs.complex_obs_dir"
*
* @see org.openmrs.util.OpenmrsConstants#GLOBAL_PROPERTY_COMPLEX_OBS_DIR
* @since 1.12
Expand Down Expand Up @@ -60,7 +60,7 @@ public Obs getObs(Obs obs, String view) {
FileInputStream mediaStream = new FileInputStream(file);
ComplexData complexData = new ComplexData(originalFilename, mediaStream);

complexData.setMimeType(OpenmrsUtil.getFileMimeType(file));
complexData.setMimeType(getMimeTypeFromFile(file));

complexData.setLength(file.length());

Expand Down
22 changes: 10 additions & 12 deletions api/src/main/java/org/openmrs/obs/handler/TextHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,23 @@
*/
package org.openmrs.obs.handler;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.util.Assert;

import org.openmrs.Obs;
import org.openmrs.api.APIException;
import org.openmrs.obs.ComplexData;
import org.openmrs.obs.ComplexObsHandler;
import org.openmrs.util.OpenmrsUtil;
import org.springframework.util.Assert;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

/**
* Handler for storing files for complex obs to the file system. Files are stored in the location
Expand Down Expand Up @@ -85,7 +83,7 @@ public Obs getObs(Obs obs, String view) {
}

Assert.notNull(complexData, "Complex data must not be null");
complexData.setMimeType("text/plain");
complexData.setMimeType(getMimeTypeFromFile(file));
obs.setComplexData(complexData);

return obs;
Expand Down
Loading

0 comments on commit 5762cbd

Please sign in to comment.