Skip to content
This repository has been archived by the owner on Jul 18, 2022. It is now read-only.

Commit

Permalink
probing content media type from encrypted file does not work on the s…
Browse files Browse the repository at this point in the history
…erver therefore try to probe from temporary file
  • Loading branch information
andrew01ait committed Oct 29, 2015
1 parent abd3616 commit fc1abb6
Showing 1 changed file with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.backmeup.storage.service.resources;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
Expand All @@ -17,6 +19,7 @@
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.apache.commons.io.IOUtils;
import org.backmeup.index.client.UserMappingClientFactory;
import org.backmeup.keyserver.client.KeyserverClient;
import org.backmeup.keyserver.model.Token.Kind;
Expand Down Expand Up @@ -68,11 +71,17 @@ public Response getFile(//
throw new WebApplicationException(Status.NOT_FOUND);
}

java.nio.file.Path p = Paths.get(filePath);
try {
java.nio.file.Path p = Paths.get(filePath);
String mediaType = Files.probeContentType(p);

if (mediaType == null) {
//try to probe the media type from a temp file
mediaType = probeInputStream(filePath, getStorageLogic().getFileAsInputStream(user, owner, filePath));
}

if (mediaType == null) {
//if still missing then set default
mediaType = MediaType.APPLICATION_OCTET_STREAM;
}

Expand All @@ -85,6 +94,29 @@ public Response getFile(//
}
}

/**
* Need to create a temporary file of the encrypted inputstream to properly probe the media's content-type
*
* @param in
* @return
* @throws IOException
*/
private String probeInputStream(String filePath, InputStream in) throws IOException {
String suffix = ".tmp";
if (filePath.lastIndexOf(".") > -1) {
suffix = filePath.substring(filePath.lastIndexOf("."), filePath.length());
}
final File tempFile = File.createTempFile(filePath, suffix);
tempFile.deleteOnExit();
try (FileOutputStream out = new FileOutputStream(tempFile)) {
IOUtils.copy(in, out);
}
java.nio.file.Path p = Paths.get(tempFile.getAbsolutePath());
String mediaType = Files.probeContentType(p);
tempFile.delete();
return mediaType;
}

protected StorageUser getUserFromAccessToken(String accessToken) {
if ("".equals(accessToken)) {
this.logger.debug("unable to retrieve StorageUser, invalid accessToken");
Expand Down

0 comments on commit fc1abb6

Please sign in to comment.