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

Commit fc1abb6

Browse files
committed
probing content media type from encrypted file does not work on the server therefore try to probe from temporary file
1 parent abd3616 commit fc1abb6

File tree

1 file changed

+33
-1
lines changed
  • backmeup-storage-service/src/main/java/org/backmeup/storage/service/resources

1 file changed

+33
-1
lines changed

backmeup-storage-service/src/main/java/org/backmeup/storage/service/resources/Download.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.backmeup.storage.service.resources;
22

3+
import java.io.File;
4+
import java.io.FileOutputStream;
35
import java.io.IOException;
46
import java.io.InputStream;
57
import java.nio.file.Files;
@@ -17,6 +19,7 @@
1719
import javax.ws.rs.core.Response;
1820
import javax.ws.rs.core.Response.Status;
1921

22+
import org.apache.commons.io.IOUtils;
2023
import org.backmeup.index.client.UserMappingClientFactory;
2124
import org.backmeup.keyserver.client.KeyserverClient;
2225
import org.backmeup.keyserver.model.Token.Kind;
@@ -68,11 +71,17 @@ public Response getFile(//
6871
throw new WebApplicationException(Status.NOT_FOUND);
6972
}
7073

71-
java.nio.file.Path p = Paths.get(filePath);
7274
try {
75+
java.nio.file.Path p = Paths.get(filePath);
7376
String mediaType = Files.probeContentType(p);
7477

7578
if (mediaType == null) {
79+
//try to probe the media type from a temp file
80+
mediaType = probeInputStream(filePath, getStorageLogic().getFileAsInputStream(user, owner, filePath));
81+
}
82+
83+
if (mediaType == null) {
84+
//if still missing then set default
7685
mediaType = MediaType.APPLICATION_OCTET_STREAM;
7786
}
7887

@@ -85,6 +94,29 @@ public Response getFile(//
8594
}
8695
}
8796

97+
/**
98+
* Need to create a temporary file of the encrypted inputstream to properly probe the media's content-type
99+
*
100+
* @param in
101+
* @return
102+
* @throws IOException
103+
*/
104+
private String probeInputStream(String filePath, InputStream in) throws IOException {
105+
String suffix = ".tmp";
106+
if (filePath.lastIndexOf(".") > -1) {
107+
suffix = filePath.substring(filePath.lastIndexOf("."), filePath.length());
108+
}
109+
final File tempFile = File.createTempFile(filePath, suffix);
110+
tempFile.deleteOnExit();
111+
try (FileOutputStream out = new FileOutputStream(tempFile)) {
112+
IOUtils.copy(in, out);
113+
}
114+
java.nio.file.Path p = Paths.get(tempFile.getAbsolutePath());
115+
String mediaType = Files.probeContentType(p);
116+
tempFile.delete();
117+
return mediaType;
118+
}
119+
88120
protected StorageUser getUserFromAccessToken(String accessToken) {
89121
if ("".equals(accessToken)) {
90122
this.logger.debug("unable to retrieve StorageUser, invalid accessToken");

0 commit comments

Comments
 (0)