11package org .backmeup .storage .service .resources ;
22
3+ import java .io .File ;
4+ import java .io .FileOutputStream ;
35import java .io .IOException ;
46import java .io .InputStream ;
57import java .nio .file .Files ;
1719import javax .ws .rs .core .Response ;
1820import javax .ws .rs .core .Response .Status ;
1921
22+ import org .apache .commons .io .IOUtils ;
2023import org .backmeup .index .client .UserMappingClientFactory ;
2124import org .backmeup .keyserver .client .KeyserverClient ;
2225import 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