Skip to content

Commit cbfb75d

Browse files
committed
Using Path instead of File
1 parent 2251d0a commit cbfb75d

File tree

3 files changed

+49
-91
lines changed

3 files changed

+49
-91
lines changed

src/express/http/response/Response.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
import express.utils.Status;
99
import express.utils.Utils;
1010

11-
import java.io.File;
12-
import java.io.FileInputStream;
13-
import java.io.IOException;
14-
import java.io.OutputStream;
11+
import java.io.*;
12+
import java.nio.file.Files;
13+
import java.nio.file.Path;
14+
import java.nio.file.StandardOpenOption;
1515
import java.util.List;
1616

1717
/**
@@ -180,10 +180,12 @@ public void send(String s) {
180180
*
181181
* @param file The file.
182182
*/
183-
public void send(@NotNull File file) {
184-
if (checkIfClosed()) return;
183+
public void send(@NotNull Path file) {
184+
if (checkIfClosed())
185+
return;
186+
185187
try {
186-
this.contentLength = file.length();
188+
this.contentLength = Files.size(file);
187189

188190
// Detect content type
189191
MediaType mediaType = Utils.getContentType(file);
@@ -193,7 +195,7 @@ public void send(@NotNull File file) {
193195
sendHeaders();
194196

195197
// Send file
196-
FileInputStream fis = new FileInputStream(file);
198+
InputStream fis = Files.newInputStream(file, StandardOpenOption.READ);
197199
byte[] buffer = new byte[1024];
198200
int n;
199201
while ((n = fis.read(buffer)) != -1) {

src/express/middleware/FileProvider.java

Lines changed: 31 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
import java.io.File;
1010
import java.io.IOException;
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
import java.nio.file.Paths;
1114
import java.util.Date;
1215

1316
/**
@@ -32,21 +35,29 @@ final class FileProvider implements HttpRequest {
3235
@Override
3336
public void handle(Request req, Response res) {
3437
String path = req.getURI().getPath();
35-
File reqFile = new File(ROOT + "\\" + path);
38+
39+
if (path.length() <= 1)
40+
path = "index.html";
41+
42+
Path reqFile = Paths.get(ROOT + "\\" + path);
3643

3744
/*
3845
* If the file wasn't found, it will search in the target-directory for
3946
* the file by the raw-name without extension.
4047
*/
41-
if (OPTIONS.isFallBackSearching() && !reqFile.exists() || !reqFile.isFile()) {
42-
File dir = new File(ROOT + "\\" + getURIPath(path));
43-
String name = getURIFilename(path);
44-
reqFile = getFirst(dir, name);
48+
if (OPTIONS.isFallBackSearching() && !Files.exists(reqFile) && !Files.isDirectory(reqFile)) {
49+
String name = reqFile.getFileName().toString();
50+
51+
try {
52+
reqFile = Files.walk(reqFile.getParent()).filter(sub -> getBaseName(sub).equals(name)).findFirst().get();
53+
} catch (IOException e) {
54+
// TODO: Handle error
55+
}
4556
}
4657

47-
if (reqFile != null && reqFile.exists()) {
58+
if (Files.exists(reqFile) && Files.isRegularFile(reqFile)) {
4859

49-
if (reqFile.getName().charAt(0) == '.') {
60+
if (reqFile.getFileName().toString().charAt(0) == '.') {
5061
switch (OPTIONS.getDotFiles()) {
5162
case IGNORE:
5263
res.setStatus(Status._404);
@@ -77,84 +88,25 @@ public void handle(Request req, Response res) {
7788
}
7889
}
7990

80-
private void finish(File file, Request req, Response res) {
91+
private void finish(Path file, Request req, Response res) {
8192
if (OPTIONS.getHandler() != null)
8293
OPTIONS.getHandler().handle(req, res);
8394

84-
85-
// Apply header
86-
if (OPTIONS.isLastModified())
87-
res.setHeader("Last-Modified", Utils.getGMTDate(new Date(file.lastModified())));
88-
89-
res.setHeader("Cache-Control", String.valueOf(OPTIONS.getMaxAge()));
90-
res.send(file);
91-
}
92-
93-
/**
94-
* Returns the first file which has this specific name.
95-
* If no file where found, null will be returned.
96-
*
97-
* @param dir The root directory.
98-
* @param name The target name.
99-
* @return The first file which will be found, null otherwise.
100-
*/
101-
private File getFirst(File dir, String name) {
102-
File[] fs = dir.listFiles();
103-
104-
if (fs == null)
105-
return null;
106-
107-
for (File f : fs) {
108-
if (f.isFile() && getBaseName(f.getName()).equals(name)) {
109-
return f;
110-
}
95+
try {
96+
// Apply header
97+
if (OPTIONS.isLastModified())
98+
res.setHeader("Last-Modified", Utils.getGMTDate(new Date(Files.getLastModifiedTime(file).toMillis())));
99+
} catch (IOException e) {
100+
// TODO: Handle error
111101
}
112102

113-
return null;
114-
}
115-
116-
private String getBaseName(String path) {
117-
int index = path.lastIndexOf('.');
118-
return index == -1 ? path : path.substring(0, index);
119-
}
120-
121-
/**
122-
* Extract the filename from an URI-Path.
123-
*
124-
* @param path The path.
125-
* @return The file name.
126-
*/
127-
private String getURIFilename(String path) {
128-
int s = path.lastIndexOf('/');
129-
int e = path.lastIndexOf('.');
130-
131-
if (e == -1 && s == -1)
132-
return "index";
133-
134-
if (e == -1)
135-
e = path.length();
136-
137-
if (s == -1)
138-
s = 0;
139-
140-
String name = path.substring(s + 1, e);
141-
142-
return name.isEmpty() ? "index" : name;
103+
res.setHeader("Cache-Control", String.valueOf(OPTIONS.getMaxAge()));
104+
res.send(file); // TODO: FIX
143105
}
144106

145-
/**
146-
* Extract the directory path from an URI-Path.
147-
*
148-
* @param path The path
149-
* @return The path (without file name)
150-
*/
151-
private String getURIPath(String path) {
152-
int s = path.indexOf('/');
153-
int e = path.lastIndexOf('/');
154-
155-
if (e == s || s == -1 || e == -1)
156-
return "";
157-
158-
return path.substring(s, e);
107+
private String getBaseName(Path path) {
108+
String name = path.getFileName().toString();
109+
int index = name.lastIndexOf('.');
110+
return index == -1 ? name : name.substring(0, index);
159111
}
160112
}

src/express/utils/Utils.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
import com.sun.istack.internal.NotNull;
44

5-
import java.io.*;
5+
import java.io.BufferedReader;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.io.InputStreamReader;
69
import java.math.BigInteger;
710
import java.net.Inet4Address;
811
import java.net.UnknownHostException;
12+
import java.nio.file.Path;
913
import java.security.SecureRandom;
1014
import java.text.SimpleDateFormat;
1115
import java.util.Date;
@@ -42,7 +46,7 @@ public static String streamToString(InputStream is) {
4246
* @param file The file.
4347
* @return The MIME-Type.
4448
*/
45-
public static MediaType getContentType(File file) {
49+
public static MediaType getContentType(Path file) {
4650
String ex = getExtension(file);
4751
MediaType contentType = MediaType.getForExtension(ex);
4852

@@ -79,8 +83,8 @@ public static String getYourIp() throws UnknownHostException {
7983
* @param file The file.
8084
* @return The extension.
8185
*/
82-
public static String getExtension(@NotNull File file) {
83-
String path = file.getAbsolutePath();
86+
public static String getExtension(@NotNull Path file) {
87+
String path = file.getFileName().toString();
8488
int indx = path.lastIndexOf('.') + 1;
8589
if (indx == 0)
8690
return null;

0 commit comments

Comments
 (0)