Skip to content

Commit 5fd0dc0

Browse files
committed
release 2.1.1.RELEASE
1 parent 6568190 commit 5fd0dc0

File tree

23 files changed

+214
-168
lines changed

23 files changed

+214
-168
lines changed

blade-core/src/main/java/com/hellokaton/blade/kit/MimeTypeKit.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,25 @@
33
import com.hellokaton.blade.mvc.multipart.MimeType;
44
import lombok.experimental.UtilityClass;
55

6+
import java.net.URLConnection;
7+
68
@UtilityClass
79
public class MimeTypeKit {
810

911
public static String parse(String fileName) {
10-
String ext = fileExt(fileName);
11-
if (null == ext) {
12+
try {
13+
String mimeType = URLConnection.guessContentTypeFromName(fileName);
14+
if (StringKit.isNotEmpty(mimeType)) {
15+
return mimeType;
16+
}
17+
String ext = fileExt(fileName);
18+
if (null == ext) {
19+
return null;
20+
}
21+
return MimeType.get(ext);
22+
} catch (Exception e) {
1223
return null;
1324
}
14-
return MimeType.get(ext);
1525
}
1626

1727
public static String fileExt(String fname) {

blade-core/src/main/java/com/hellokaton/blade/mvc/HttpConst.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ public interface HttpConst {
99
String CONTENT_TYPE_XML = "text/xml; charset=UTF-8";
1010
String CONTENT_TYPE_JSON = "application/json; charset=UTF-8";
1111
String CONTENT_TYPE_TEXT = "text/plain; charset=UTF-8";
12+
String CONTENT_TYPE_STREAM = "application/octet-stream";
1213

1314
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.hellokaton.blade.mvc.http;
22

3-
import io.netty.handler.codec.http.FullHttpResponse;
3+
import io.netty.handler.codec.http.HttpResponse;
44

55
public interface Body {
66

7-
FullHttpResponse write(BodyWriter writer);
7+
HttpResponse write(BodyWriter writer);
88

99
}
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package com.hellokaton.blade.mvc.http;
22

33
import io.netty.buffer.ByteBuf;
4-
import io.netty.handler.codec.http.FullHttpResponse;
4+
import io.netty.handler.codec.http.HttpResponse;
5+
6+
import java.nio.channels.FileChannel;
57

68
public interface BodyWriter {
79

8-
FullHttpResponse onView(ViewBody body);
10+
HttpResponse onView(ViewBody body);
911

10-
FullHttpResponse onRawBody(RawBody body);
12+
HttpResponse onRawBody(RawBody body);
1113

12-
FullHttpResponse onByteBuf(Object byteBuf);
14+
HttpResponse onByteBuf(ByteBuf byteBuf);
1315

14-
FullHttpResponse onByteBuf(ByteBuf byteBuf);
16+
HttpResponse onByteBuf(String fileName, FileChannel channel);
1517

1618
}

blade-core/src/main/java/com/hellokaton/blade/mvc/http/ByteBody.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import io.netty.buffer.ByteBuf;
44
import io.netty.buffer.Unpooled;
5-
import io.netty.handler.codec.http.FullHttpResponse;
5+
import io.netty.handler.codec.http.HttpResponse;
66

77
import java.io.File;
88
import java.io.IOException;
@@ -58,9 +58,9 @@ public static ByteBody of(ByteBuf byteBuf) {
5858
}
5959

6060
@Override
61-
public FullHttpResponse write(BodyWriter writer) {
61+
public HttpResponse write(BodyWriter writer) {
6262
if (null != outputStream) {
63-
return writer.onByteBuf(outputStream);
63+
return writer.onByteBuf((ByteBuf) null);
6464
}
6565
return writer.onByteBuf(byteBuf);
6666
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.hellokaton.blade.mvc.http;
2+
3+
import io.netty.handler.codec.http.HttpResponse;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.nio.channels.FileChannel;
8+
import java.nio.file.StandardOpenOption;
9+
10+
public class ChannelBody implements Body {
11+
12+
private final String fileName;
13+
private final FileChannel content;
14+
15+
public ChannelBody(final String fileName, final FileChannel content) {
16+
this.fileName = fileName;
17+
this.content = content;
18+
}
19+
20+
public static ChannelBody of(File file) throws IOException {
21+
FileChannel fileChannel = FileChannel.open(file.toPath(), StandardOpenOption.READ);
22+
return new ChannelBody(file.getName(), fileChannel);
23+
}
24+
25+
@Override
26+
public HttpResponse write(BodyWriter writer) {
27+
return writer.onByteBuf(fileName, content);
28+
}
29+
30+
}

blade-core/src/main/java/com/hellokaton/blade/mvc/http/EmptyBody.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.hellokaton.blade.mvc.http;
22

33
import io.netty.buffer.Unpooled;
4-
import io.netty.handler.codec.http.FullHttpResponse;
4+
import io.netty.handler.codec.http.HttpResponse;
55
import lombok.NoArgsConstructor;
66

77
@NoArgsConstructor
@@ -14,7 +14,7 @@ public static EmptyBody empty() {
1414
}
1515

1616
@Override
17-
public FullHttpResponse write(BodyWriter writer) {
17+
public HttpResponse write(BodyWriter writer) {
1818
return writer.onByteBuf( Unpooled.buffer(0));
1919
}
2020

blade-core/src/main/java/com/hellokaton/blade/mvc/http/HttpResponse.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.hellokaton.blade.mvc.http;
22

3-
import com.hellokaton.blade.exception.NotFoundException;
4-
import com.hellokaton.blade.kit.MimeTypeKit;
53
import com.hellokaton.blade.mvc.ui.ModelAndView;
64
import com.hellokaton.blade.server.NettyHttpConst;
75
import io.netty.handler.codec.http.cookie.Cookie;
@@ -10,8 +8,10 @@
108
import lombok.extern.slf4j.Slf4j;
119

1210
import java.io.File;
13-
import java.io.FileInputStream;
11+
import java.io.IOException;
12+
import java.nio.channels.FileChannel;
1413
import java.nio.charset.StandardCharsets;
14+
import java.nio.file.StandardOpenOption;
1515
import java.util.*;
1616

1717
/**
@@ -23,8 +23,8 @@
2323
@Slf4j
2424
public class HttpResponse implements Response {
2525

26-
private Map<String, String> headers = new HashMap<>();
27-
private Set<Cookie> cookies = new HashSet<>();
26+
private final Map<String, String> headers = new HashMap<>();
27+
private final Set<Cookie> cookies = new HashSet<>();
2828

2929
private int statusCode = 200;
3030
private Body body;
@@ -138,18 +138,6 @@ public Set<Cookie> cookiesRaw() {
138138
return this.cookies;
139139
}
140140

141-
@Override
142-
public void download(@NonNull String fileName, @NonNull File file) throws Exception {
143-
if (!file.exists() || !file.isFile()) {
144-
throw new NotFoundException("Not found file: " + file.getPath());
145-
}
146-
String contentType = MimeTypeKit.parse(file.getName());
147-
headers.put("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), "ISO8859_1"));
148-
headers.put(NettyHttpConst.CONTENT_LENGTH.toString(), String.valueOf(file.length()));
149-
headers.put(NettyHttpConst.CONTENT_TYPE_STRING, contentType);
150-
this.body = new StreamBody(new FileInputStream(file));
151-
}
152-
153141
@Override
154142
public void render(@NonNull ModelAndView modelAndView) {
155143
this.body = new ViewBody(modelAndView);
@@ -161,6 +149,18 @@ public void redirect(@NonNull String newUri) {
161149
this.status(302);
162150
}
163151

152+
@Override
153+
public void write(File file) throws IOException {
154+
this.body = ChannelBody.of(file);
155+
}
156+
157+
@Override
158+
public void write(String fileName, File file) throws IOException {
159+
FileChannel fileChannel = FileChannel.open(file.toPath(), StandardOpenOption.READ);
160+
headers.put("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
161+
this.body = new ChannelBody(null, fileChannel);
162+
}
163+
164164
@Override
165165
public ModelAndView modelAndView() {
166166
if (this.body instanceof ViewBody) {

blade-core/src/main/java/com/hellokaton/blade/mvc/http/RawBody.java

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

33
import io.netty.handler.codec.http.DefaultHttpResponse;
44
import io.netty.handler.codec.http.FullHttpResponse;
5+
import io.netty.handler.codec.http.HttpResponse;
56

67
public class RawBody implements Body {
78

8-
private FullHttpResponse httpResponse;
9+
private HttpResponse httpResponse;
910
private DefaultHttpResponse defaultHttpResponse;
1011

1112
public RawBody(FullHttpResponse httpResponse) {
@@ -16,7 +17,7 @@ public RawBody(DefaultHttpResponse defaultHttpResponse) {
1617
this.defaultHttpResponse = defaultHttpResponse;
1718
}
1819

19-
public FullHttpResponse httpResponse() {
20+
public HttpResponse httpResponse() {
2021
return httpResponse;
2122
}
2223

@@ -25,7 +26,7 @@ public DefaultHttpResponse defaultHttpResponse() {
2526
}
2627

2728
@Override
28-
public FullHttpResponse write(BodyWriter writer) {
29+
public HttpResponse write(BodyWriter writer) {
2930
return writer.onRawBody(this);
3031
}
3132

blade-core/src/main/java/com/hellokaton/blade/mvc/http/Response.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import com.hellokaton.blade.mvc.ui.ModelAndView;
99

1010
import java.io.File;
11+
import java.io.IOException;
12+
import java.io.InputStream;
1113
import java.util.Map;
1214
import java.util.Objects;
1315
import java.util.Set;
@@ -229,14 +231,6 @@ default void body(String body) {
229231
*/
230232
Response body(Body body);
231233

232-
/**
233-
* download some file to client
234-
*
235-
* @param fileName give client file name
236-
* @param file file storage location
237-
*/
238-
void download(String fileName, File file) throws Exception;
239-
240234
/**
241235
* Render view, can be modified after WebHook
242236
*
@@ -265,6 +259,10 @@ default void render(String view) {
265259
*/
266260
void redirect(String newUri);
267261

262+
void write(File file) throws IOException;
263+
264+
void write(String fileName, File file) throws IOException;
265+
268266
/**
269267
* @return Returns the currently set view, returning an empty Optional type when not set
270268
* @since 2.0.8

0 commit comments

Comments
 (0)