@@ -52,7 +52,7 @@ public class FileUtil {
52
52
/**
53
53
* Default value for suffix of temporary files.
54
54
*/
55
- public static final String DEFAULT_SUFFIX = ".tmp " ;
55
+ public static final String DEFAULT_SUFFIX = ".html " ;
56
56
/**
57
57
* Default value for prefix of temporary files.
58
58
*/
@@ -61,9 +61,9 @@ public class FileUtil {
61
61
* Logger for this class.
62
62
*/
63
63
private static final Logger LOGGER = LoggerFactory .getLogger (FileUtil .class );
64
-
64
+
65
65
private static final int MAX_LENGTH_OF_HEADER = 100 ;
66
-
66
+
67
67
private static final Pattern JSON_FIRST_BYTE = Pattern .compile ("(\\ R\\ s)*\\ s*\\ {\\ s*\" (.|\\ s)*" , Pattern .MULTILINE );//^\\s{\\s*\".*");
68
68
private static final Pattern XML_FIRST_BYTE = Pattern .compile ("((.|\\ s)*<\\ ?xml[^<]*)?\\ s*<\\ s*(\\ w+:)?\\ w+(.|\\ s)*" , Pattern .MULTILINE );
69
69
@@ -101,7 +101,7 @@ public static Optional<Path> downloadResource(URI resourceURL) {
101
101
throw new MappingException ("Error downloading resource from '" + resourceURL + "'!" , tw );
102
102
}
103
103
downloadedFile = fixFileExtension (downloadedFile );
104
-
104
+
105
105
return Optional .ofNullable (downloadedFile );
106
106
}
107
107
@@ -114,14 +114,11 @@ public static Optional<Path> downloadResource(URI resourceURL) {
114
114
public static Path fixFileExtension (Path pathToFile ) {
115
115
Path returnFile = pathToFile ;
116
116
Path renamedFile = pathToFile ;
117
+ LOGGER .trace ("fixFileExtension({})" , pathToFile );
117
118
try {
118
119
if ((pathToFile != null ) && (pathToFile .toFile ().exists ())) {
119
- Tika tika = new Tika ();
120
- String mimeType = tika .detect (pathToFile .toFile ());
121
- MimeTypes allTypes = MimeTypes .getDefaultMimeTypes ();
122
- MimeType estimatedMimeType = allTypes .forName (mimeType );
123
- String newExtension = estimatedMimeType .getExtension (); // .jpg
124
-
120
+ String contentOfFile = FileUtils .readFileToString (pathToFile .toFile (), StandardCharsets .UTF_8 );
121
+ String newExtension = guessFileExtension (contentOfFile .getBytes ());
125
122
if (newExtension != null ) {
126
123
if (!pathToFile .toString ().endsWith (newExtension )) {
127
124
renamedFile = Paths .get (pathToFile + newExtension );
@@ -130,9 +127,10 @@ public static Path fixFileExtension(Path pathToFile) {
130
127
}
131
128
}
132
129
}
133
- } catch (IOException | MimeTypeException ex ) {
130
+ } catch (IOException ex ) {
134
131
LOGGER .error ("Error moving file '{}' to '{}'." , pathToFile , renamedFile );
135
132
}
133
+ LOGGER .trace ("'{}' -> '{}'" , pathToFile , returnFile );
136
134
return returnFile ;
137
135
}
138
136
@@ -177,21 +175,38 @@ public static void removeFile(Path tempFile) {
177
175
* @return Estimated extension. e.g. '.xml'
178
176
*/
179
177
private static String guessFileExtension (byte [] schema ) {
178
+ String returnValue = null ;
180
179
// Cut schema to a maximum of MAX_LENGTH_OF_HEADER characters.
181
180
int length = Math .min (schema .length , MAX_LENGTH_OF_HEADER );
182
181
String schemaAsString = new String (schema , 0 , length );
183
182
LOGGER .trace ("Guess type for '{}'" , schemaAsString );
184
-
183
+
185
184
Matcher m = JSON_FIRST_BYTE .matcher (schemaAsString );
186
185
if (m .matches ()) {
187
- return ".json" ;
186
+ returnValue = ".json" ;
188
187
} else {
189
188
m = XML_FIRST_BYTE .matcher (schemaAsString );
190
189
if (m .matches ()) {
191
- return ".xml" ;
190
+ returnValue = ".xml" ;
192
191
}
193
192
}
194
- return null ;
193
+ if (returnValue == null ) {
194
+ // Use tika library to estimate extension
195
+ LOGGER .trace ("Use tika library to estimate extension." );
196
+ Tika tika = new Tika ();
197
+ String mimeType ;
198
+ mimeType = tika .detect (schema );
199
+ MimeTypes allTypes = MimeTypes .getDefaultMimeTypes ();
200
+ MimeType estimatedMimeType ;
201
+ try {
202
+ estimatedMimeType = allTypes .forName (mimeType );
203
+ returnValue = estimatedMimeType .getExtension (); // .jpg
204
+ LOGGER .trace ("Mimetype: '{}', Extension: '{}'" , mimeType , returnValue );
205
+ } catch (MimeTypeException ex ) {
206
+ LOGGER .error ("Unknown mimetype '{}'" , mimeType );
207
+ }
208
+ }
209
+ return returnValue ;
195
210
}
196
211
197
212
/**
@@ -217,7 +232,7 @@ public static Path cloneGitRepository(String repositoryUrl, String branch) {
217
232
public static Path cloneGitRepository (String repositoryUrl , String branch , String targetFolder ) {
218
233
File target = new File (targetFolder );
219
234
target .mkdirs ();
220
-
235
+
221
236
LOGGER .info ("Cloning branch '{}' of repository '{}' to '{}'" , branch , repositoryUrl , target .getPath ());
222
237
try {
223
238
Git .cloneRepository ().setURI (repositoryUrl ).setBranch (branch ).setDirectory (target ).call ();
@@ -226,7 +241,7 @@ public static Path cloneGitRepository(String repositoryUrl, String branch, Strin
226
241
} catch (GitAPIException ex ) {
227
242
throw new MappingException ("Error cloning git repository '" + repositoryUrl + "' to '" + target + "'!" , ex );
228
243
}
229
-
244
+
230
245
return target .toPath ();
231
246
}
232
247
}
0 commit comments