[Notice] Announcing the End of Demo Server [Read me]

git,svn: Fix 500 error when uploading big file.
BodyParser.Raw stores request body as a file if it is larger than 100KB.
@14d9252aa75892048bf264139a554af5c7d30687
--- app/playRepository/RepositoryService.java
+++ app/playRepository/RepositoryService.java
... | ... | @@ -3,7 +3,9 @@ |
3 | 3 |
import java.io.ByteArrayInputStream; |
4 | 4 |
import java.io.ByteArrayOutputStream; |
5 | 5 |
import java.io.File; |
6 |
+import java.io.FileInputStream; |
|
6 | 7 |
import java.io.IOException; |
8 |
+import java.io.InputStream; |
|
7 | 9 |
import java.util.Enumeration; |
8 | 10 |
import java.util.HashMap; |
9 | 11 |
import java.util.Map; |
... | ... | @@ -28,6 +30,7 @@ |
28 | 30 |
import org.tmatesoft.svn.core.internal.server.dav.DAVServlet; |
29 | 31 |
|
30 | 32 |
import play.Logger; |
33 |
+import play.mvc.Http.RawBuffer; |
|
31 | 34 |
import play.mvc.Http.Response; |
32 | 35 |
import play.mvc.Http.Request; |
33 | 36 |
|
... | ... | @@ -169,8 +172,18 @@ |
169 | 172 |
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
170 | 173 |
|
171 | 174 |
// FIXME 스트림으로.. |
172 |
- byte[] buf = request.body().asRaw().asBytes(); |
|
173 |
- ByteArrayInputStream in = new ByteArrayInputStream(buf); |
|
175 |
+ RawBuffer raw = request.body().asRaw(); |
|
176 |
+ byte[] buf = raw.asBytes(); |
|
177 |
+ InputStream in; |
|
178 |
+ |
|
179 |
+ // If the content size is bigger than memoryThreshold, |
|
180 |
+ // which is defined as 100 * 1024 in play.api.mvc.BodyParsers trait, |
|
181 |
+ // the content is stored as a file. |
|
182 |
+ if (buf != null) { |
|
183 |
+ in = new ByteArrayInputStream(buf); |
|
184 |
+ } else { |
|
185 |
+ in = new FileInputStream(raw.asFile()); |
|
186 |
+ } |
|
174 | 187 |
|
175 | 188 |
Repository repository = createGitRepository(userName, projectName); |
176 | 189 |
|
--- app/utils/PlayServletRequest.java
+++ app/utils/PlayServletRequest.java
... | ... | @@ -1,7 +1,10 @@ |
1 | 1 |
package utils; |
2 | 2 |
|
3 | 3 |
import java.io.BufferedReader; |
4 |
+import java.io.ByteArrayInputStream; |
|
5 |
+import java.io.FileInputStream; |
|
4 | 6 |
import java.io.IOException; |
7 |
+import java.io.InputStream; |
|
5 | 8 |
import java.io.UnsupportedEncodingException; |
6 | 9 |
import java.net.URI; |
7 | 10 |
import java.net.URISyntaxException; |
... | ... | @@ -108,36 +111,29 @@ |
108 | 111 |
|
109 | 112 |
@Override |
110 | 113 |
public ServletInputStream getInputStream() throws IOException { |
114 |
+ RawBuffer raw = request.body().asRaw(); |
|
115 |
+ byte[] buf = raw.asBytes(); |
|
116 |
+ final InputStream in; |
|
117 |
+ |
|
118 |
+ // If the content size is bigger than memoryThreshold, |
|
119 |
+ // which is defined as 100 * 1024 in play.api.mvc.BodyParsers trait, |
|
120 |
+ // the content is stored as a file. |
|
121 |
+ if (buf != null) { |
|
122 |
+ in = new ByteArrayInputStream(buf); |
|
123 |
+ } else { |
|
124 |
+ in = new FileInputStream(raw.asFile()); |
|
125 |
+ } |
|
126 |
+ |
|
111 | 127 |
return new ServletInputStream() { |
112 |
- |
|
113 |
- int offset = 0; |
|
114 |
- byte[] bytes = null; |
|
115 |
- |
|
116 | 128 |
@Override |
117 | 129 |
public int read() throws IOException { |
118 |
- if (bytes == null) { |
|
119 |
- RawBuffer raw = request.body().asRaw(); |
|
120 |
- if (raw == null) { |
|
121 |
- return -1; |
|
122 |
- } |
|
123 |
- bytes = raw.asBytes(); |
|
124 |
- } |
|
125 |
- |
|
126 |
- if (isFinished()) { |
|
127 |
- return -1; |
|
128 |
- } |
|
129 |
- |
|
130 |
- return bytes[offset++]; |
|
130 |
+ return in.read(); |
|
131 | 131 |
} |
132 | 132 |
|
133 |
- public boolean isFinished() { |
|
134 |
- if (bytes != null && bytes.length <= offset) { |
|
135 |
- return true; |
|
136 |
- } |
|
137 |
- |
|
138 |
- return false; |
|
133 |
+ @Override |
|
134 |
+ protected void finalize() throws IOException { |
|
135 |
+ in.close(); |
|
139 | 136 |
} |
140 |
- |
|
141 | 137 |
}; |
142 | 138 |
} |
143 | 139 |
|
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?