1
1
package org .backmeup .storage .service .resources ;
2
2
3
+ import java .io .File ;
4
+ import java .io .FileOutputStream ;
3
5
import java .io .IOException ;
4
6
import java .io .InputStream ;
5
7
import java .nio .file .Files ;
17
19
import javax .ws .rs .core .Response ;
18
20
import javax .ws .rs .core .Response .Status ;
19
21
22
+ import org .apache .commons .io .IOUtils ;
20
23
import org .backmeup .index .client .UserMappingClientFactory ;
21
24
import org .backmeup .keyserver .client .KeyserverClient ;
22
25
import org .backmeup .keyserver .model .Token .Kind ;
@@ -68,11 +71,17 @@ public Response getFile(//
68
71
throw new WebApplicationException (Status .NOT_FOUND );
69
72
}
70
73
71
- java .nio .file .Path p = Paths .get (filePath );
72
74
try {
75
+ java .nio .file .Path p = Paths .get (filePath );
73
76
String mediaType = Files .probeContentType (p );
74
77
75
78
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
76
85
mediaType = MediaType .APPLICATION_OCTET_STREAM ;
77
86
}
78
87
@@ -85,6 +94,29 @@ public Response getFile(//
85
94
}
86
95
}
87
96
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
+
88
120
protected StorageUser getUserFromAccessToken (String accessToken ) {
89
121
if ("" .equals (accessToken )) {
90
122
this .logger .debug ("unable to retrieve StorageUser, invalid accessToken" );
0 commit comments