-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathFileController.java
More file actions
199 lines (153 loc) · 6.28 KB
/
Copy pathFileController.java
File metadata and controls
199 lines (153 loc) · 6.28 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package org.cellocad.api;
import org.apache.commons.codec.binary.Base64;
import org.apache.tools.ant.DirectoryScanner;
import org.cellocad.MIT.dnacompiler.Util;
import org.json.simple.JSONObject;
import org.springframework.web.bind.annotation.*;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;
/**
* Created by Bryan Der on 7/3/15.
*/
@RestController
public class FileController extends BaseController {
@RequestMapping(value="/results", method= RequestMethod.GET, produces = "application/json")
public @ResponseBody
JSONObject getDirectoryList(
@RequestHeader("Authorization") String basic
) throws IOException {
if(!auth.login(basic)) {
throw new CelloUnauthorizedException("invalid username/password");
}
String username = auth.getUsername(basic);
String directory = _resultPath + "/" + username;
DirectoryScanner scanner = new DirectoryScanner();
scanner.setBasedir(directory);
scanner.scan();
String[] directory_names = scanner.getIncludedDirectories();
JSONObject response = new JSONObject();
response.put("folders", directory_names);
return response;
}
@RequestMapping(value="/results/{jobid}",method= RequestMethod.GET, produces = "application/json")
public @ResponseBody
JSONObject getResultFiles(
@RequestHeader("Authorization") String basic,
@PathVariable("jobid") String jobid,
@RequestParam Map<String, String> params
) throws IOException {
if(!auth.login(basic)) {
throw new CelloUnauthorizedException("invalid username/password");
}
String username = auth.getUsername(basic);
String keyword = "";
String extension = "";
if(params.containsKey("keyword")) {
keyword = params.get("keyword");
}
if(params.containsKey("extension")) {
extension = params.get("extension");
}
String filePath = _resultPath + "/" + username + "/" + jobid;
File f = new File(filePath);
if (f.exists()) {
DirectoryScanner scanner = new DirectoryScanner();
scanner.setIncludes(new String[]{"*" + keyword + "*" + extension});
scanner.setBasedir(filePath);
scanner.setCaseSensitive(false);
scanner.scan();
String[] files = scanner.getIncludedFiles();
//return files;
JSONObject response = new JSONObject();
response.put("files", files);
return response;
} else {
throw new CelloNotFoundException("files not found.");
}
}
@RequestMapping(value="/results/{jobid}",method= RequestMethod.DELETE, produces = "application/json")
public @ResponseBody
JSONObject deleteDirectory(
@RequestHeader("Authorization") String basic,
@PathVariable("jobid") String jobid
) throws IOException {
if(!auth.login(basic)) {
throw new CelloUnauthorizedException("invalid username/password");
}
String username = auth.getUsername(basic);
String directory = _resultPath + "/" + username + "/" + jobid;
File f = new File(directory);
if(!f.exists()) {
throw new CelloNotFoundException("directory " + jobid + " not found.");
}
//trying to do this safely, in other words, not deleting subdirectories or recursive delete
Util.deleteFilesInDirectory(new File(directory));
Util.deleteEmptyDirectory(new File(directory));
JSONObject response = new JSONObject();
response.put("message", "deleted " + jobid);
return response;
}
@RequestMapping(value="/results/{jobid}/{filename:.+}", method = RequestMethod.GET)
public @ResponseBody
String getResultFile(
@RequestHeader("Authorization") String basic,
@PathVariable("jobid") String jobid,
@PathVariable("filename") String filename
) {
if(!auth.login(basic)) {
throw new CelloUnauthorizedException("invalid username/password");
}
String username = auth.getUsername(basic);
if(filename.endsWith(".png") || filename.endsWith(".pdf")) {
String filePath = _resultPath + "/" + username + "/" + jobid + "/" + filename;
try {
//Convert binary image file to byte array to base64 encoded string
FileInputStream mFileInputStream = new FileInputStream(filePath);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int bytesRead = 0;
while ((bytesRead = mFileInputStream.read(b)) != -1) {
bos.write(b, 0, bytesRead);
}
byte[] ba = bos.toByteArray();
String imgstr = Base64.encodeBase64String(ba);
mFileInputStream.close();
bos.close();
return imgstr;
} catch (java.io.IOException e) {
throw new CelloNotFoundException("file " + filename + " not found.");
}
}
else {
String filePath = _resultPath + "/" + username + "/" + jobid + "/" + filename;
String fileContents = "";
try {
fileContents = readFile(filePath, Charset.defaultCharset());
} catch (IOException e) {
throw new CelloNotFoundException("file " + filename + " not found.");
}
return fileContents;
}
}
private static String readFile(String path, Charset encoding) throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
@RequestMapping(value="/resultsroot",method= RequestMethod.GET)
public @ResponseBody
String getResultRootPath(
@RequestHeader("Authorization") String basic
) throws IOException {
if (!auth.login(basic)) {
throw new CelloUnauthorizedException("invalid username/password");
}
return _resultPath;
}
}