Skip to content

Commit 1855167

Browse files
committed
Add automatic content type detection.
1 parent 1eb06af commit 1855167

File tree

7 files changed

+347
-433
lines changed

7 files changed

+347
-433
lines changed

.idea/workspace.xml

+256-429
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ server.ip = The IP which the server will bind to. Defaults to: 0.0.0.0
3636
master.serverListFile = The location of the server list. Defaults to: WLSServers.json in the working directory
3737
master.listDirectories = Whether or not the master server will list directories (the slaves still will). Defaults to: true
3838
slave.rootDir = The location of the directory from where the files will be served from. Defaults to the working directory
39+
slave.useSlowTypeDetection = Whether or not to use slow file type detection (true/false). Will also increase memory usage. Defaults to: false
3940
```
4041

4142
## Server List File ##
4243
The server list is stored in the file specified by: `master.serverListFile`
43-
It is in JSON format (in a weird way), here is an example:
44+
It is in JSON format, here is an example:
4445
```
4546
{
4647
"servers": [

src/main/java/xyz/nulldev/wls/routes/slave/GetFileRoute.java

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import spark.Response;
77
import spark.Route;
88
import xyz.nulldev.wls.server.SlaveServerImpl;
9+
import xyz.nulldev.wls.utils.TypeDetector;
910

1011
import javax.servlet.ServletOutputStream;
1112
import java.io.BufferedInputStream;
@@ -20,6 +21,7 @@
2021
* Author: nulldev
2122
*/
2223
public class GetFileRoute implements Route {
24+
private TypeDetector typeDetector = new TypeDetector();
2325

2426
private Logger logger = LoggerFactory.getLogger(GetFileRoute.class);
2527

@@ -83,12 +85,17 @@ public Object handle(Request request, Response response) throws Exception {
8385
response.status(statusCode);
8486
response.header("Content-Length", String.valueOf(fileEnd - fileStart + 1));
8587
response.header("Content-Range", "bytes " + fileStart + "-" + fileEnd + "/" + length);
88+
8689
//Allow download forcing
8790
if(request.queryParams().contains("force-download")
8891
&& Objects.equals(request.queryParams("force-download").toUpperCase(), "TRUE")) {
8992
response.type("application/force-download");
9093
response.header("Content-Transfer-Encoding", "binary");
9194
response.header("Content-Disposition","attachment; filename=\"" + resolvedFile.getName() + "\"");
95+
} else {
96+
String detectedType = typeDetector.detectType(resolvedFile);
97+
if(detectedType != null)
98+
response.type(detectedType);
9299
}
93100
try (BufferedInputStream stream
94101
= new BufferedInputStream(new FileInputStream(resolvedFile));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package xyz.nulldev.wls.utils;
2+
3+
import org.apache.tika.detect.DefaultDetector;
4+
import org.apache.tika.detect.Detector;
5+
import org.apache.tika.io.TikaInputStream;
6+
import org.apache.tika.metadata.Metadata;
7+
import org.apache.tika.mime.MediaType;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
import xyz.nulldev.wls.WebLinkedServer;
11+
12+
import java.io.*;
13+
14+
public class TypeDetector {
15+
private Detector detector;
16+
private boolean useSlowDetection = false;
17+
18+
private Logger logger = LoggerFactory.getLogger(TypeDetector.class);
19+
20+
public TypeDetector() {
21+
String useSlowDetectionString
22+
= WebLinkedServer
23+
.getCONFIGURATION()
24+
.getConfig()
25+
.getProperty("slave.useSlowTypeDetection", "false")
26+
.trim();
27+
detector = new DefaultDetector();
28+
if(useSlowDetectionString.equalsIgnoreCase("true")
29+
|| useSlowDetectionString.equalsIgnoreCase("yes")) {
30+
useSlowDetection = true;
31+
}
32+
}
33+
34+
public boolean isUseSlowDetection() {
35+
return useSlowDetection;
36+
}
37+
38+
public void setUseSlowDetection(boolean useSlowDetection) {
39+
this.useSlowDetection = useSlowDetection;
40+
}
41+
42+
public String detectType(File file) {
43+
InputStream stream = null;
44+
Metadata metadata = null;
45+
46+
//Use TikaInputStream if we want slow detection
47+
if(useSlowDetection) {
48+
metadata = new Metadata();
49+
try {
50+
stream = TikaInputStream.get(file.toPath(), metadata);
51+
} catch (IOException e) {
52+
logger.warn("Cannot load slow detection engine, using fast detection!", e);
53+
metadata = null;
54+
}
55+
}
56+
57+
if(stream == null) {
58+
try {
59+
stream = new BufferedInputStream(new FileInputStream(file));
60+
} catch (FileNotFoundException e) {
61+
logger.warn("File was removed while detecting it's type!", e);
62+
return null;
63+
}
64+
}
65+
if(metadata == null) {
66+
metadata = new Metadata();
67+
metadata.set(Metadata.RESOURCE_NAME_KEY, file.getName());
68+
}
69+
70+
MediaType type;
71+
try(InputStream theStream = stream) {
72+
type = detector.detect(theStream, metadata);
73+
} catch (IOException e) {
74+
logger.warn("Could not detect type of '" + file.getName() + "'!", e);
75+
return null;
76+
}
77+
return type.toString();
78+
}
79+
}

test/master/WebLinkedServer.config

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#-=[WebLinkedServer Configuration]=-
2-
#Mon Jan 30 14:01:43 EST 2017
2+
#Mon Jan 30 15:22:20 EST 2017
33
server.role=MASTER

test/slave/WebLinkedServer.config

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#-=[WebLinkedServer Configuration]=-
2-
#Mon Jan 30 13:57:08 EST 2017
2+
#Mon Jan 30 15:22:22 EST 2017
33
server.port=1918
44
server.role=SLAVE

test/slave2/WebLinkedServer.config

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#-=[WebLinkedServer Configuration]=-
2-
#Mon Jan 30 13:57:06 EST 2017
2+
#Mon Jan 30 15:09:25 EST 2017
33
server.port=1917
44
server.role=SLAVE

0 commit comments

Comments
 (0)