-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathNetworkFileProvider.java
More file actions
127 lines (90 loc) · 3.48 KB
/
NetworkFileProvider.java
File metadata and controls
127 lines (90 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.github.kilianB.example.voiceToTextPlayback;
import java.io.File;
import java.util.HashSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import io.undertow.Undertow;
import io.undertow.server.handlers.PathHandler;
import io.undertow.server.handlers.resource.FileResourceManager;
import io.undertow.server.handlers.resource.ResourceHandler;
/**
* Start a file server which maps a local directory to a fixed ip address allowing network devices
* to query data from this place.
* @author Kilian
*
*/
public class NetworkFileProvider {
//Settings
private String host;
private int port;
private String mapPrefix;
//Internal
private final Undertow server;
private final PathHandler pathHandler = new PathHandler();
private HashSet<String> mappedFolders = new HashSet<>();
public NetworkFileProvider(String host, int port, String[] allowedFileExtensions){
this.host = host;
this.port = port;
this.mapPrefix = host+":"+port + "/";
//TODO set safe path so the user can not navigate wherever he likes
//FileResourceManager sharedFolderRessourceManager = new FileResourceManager(globalSharedFolder, 0);
//final ResourceHandler textToSpeechRessourceHandler = new ResourceHandler(textToSpeechRessourceManager);
//TODO MIME Mapping
server = Undertow.builder().addHttpListener(port, host, pathHandler).build();
//Undertow wraps it's exception in a runtime exception.
try {
server.start();
}catch(RuntimeException e) {
Throwable t = e.getCause();
if(t instanceof java.net.BindException) {
LOGGER.severe("Port already bound by another program. Please check if the smart server is running "
+ " twice or swap to a free port.");
}else {
LOGGER.severe(t.toString());
}
}
}
/**
* Maps the given folder to host:port/folderPath allowing network access by quering this address
* @param directory
* @return true if folder was successfully mapped. False otherwise
*/
public synchronized boolean mapFolder(File directory) {
if(directory.isDirectory()) {
//Escape windows path
String prefixPath = directory.getAbsolutePath().replace("\\", "/");
if(mappedFolders.contains(prefixPath)) {
LOGGER.warning("Folder " + prefixPath + " already mapped. Skip request");
return false;
}
FileResourceManager sharedFolderRessourceManager = new FileResourceManager(directory, 0);
ResourceHandler sharedRessourceManager = new ResourceHandler(sharedFolderRessourceManager);
sharedRessourceManager.setDirectoryListingEnabled(true);
pathHandler.addPrefixPath("/"+prefixPath, sharedRessourceManager);
mappedFolders.add(prefixPath);
LOGGER.log(Level.INFO,"map "+directory.getAbsolutePath()+" to " + mapPrefix + prefixPath);
}else {
throw new IllegalArgumentException("Please provide a folder and not a file");
}
return true;
}
public String toMappedPath(String originalLocation) {
return mapPrefix + originalLocation.replace("\\","/");
}
public String toUnmappedPath(String mappedLocation) {
if(mappedLocation.startsWith(mapPrefix)) {
return mappedLocation.substring(mapPrefix.length());
}
return mappedLocation;
}
public void deinit(){
server.stop();
}
private static final Logger LOGGER = Logger.getLogger(NetworkFileProvider.class.getName());
/**
* @return
*/
public String getMapPrefix() {
return mapPrefix;
}
}