[Notice] Announcing the End of Demo Server [Read me]
Yi EungJun 2012-11-09
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
+++ app/playRepository/RepositoryService.java
@@ -3,7 +3,9 @@
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Map;
@@ -28,6 +30,7 @@
 import org.tmatesoft.svn.core.internal.server.dav.DAVServlet;
 
 import play.Logger;
+import play.mvc.Http.RawBuffer;
 import play.mvc.Http.Response;
 import play.mvc.Http.Request;
 
@@ -169,8 +172,18 @@
         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
 
         // FIXME 스트림으로..
-        byte[] buf = request.body().asRaw().asBytes();
-        ByteArrayInputStream in = new ByteArrayInputStream(buf);
+        RawBuffer raw = request.body().asRaw();
+        byte[] buf = raw.asBytes();
+        InputStream in;
+
+        // If the content size is bigger than memoryThreshold,
+        // which is defined as 100 * 1024 in play.api.mvc.BodyParsers trait,
+        // the content is stored as a file.
+        if (buf != null) {
+            in = new ByteArrayInputStream(buf);
+        } else {
+            in = new FileInputStream(raw.asFile());
+        }
 
         Repository repository = createGitRepository(userName, projectName);
 
app/utils/PlayServletRequest.java
--- app/utils/PlayServletRequest.java
+++ app/utils/PlayServletRequest.java
@@ -1,7 +1,10 @@
 package utils;
 
 import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
 import java.net.URI;
 import java.net.URISyntaxException;
@@ -108,36 +111,29 @@
 
     @Override
     public ServletInputStream getInputStream() throws IOException {
+        RawBuffer raw = request.body().asRaw();
+        byte[] buf = raw.asBytes();
+        final InputStream in;
+
+        // If the content size is bigger than memoryThreshold,
+        // which is defined as 100 * 1024 in play.api.mvc.BodyParsers trait,
+        // the content is stored as a file.
+        if (buf != null) {
+            in = new ByteArrayInputStream(buf);
+        } else {
+            in = new FileInputStream(raw.asFile());
+        }
+
         return new ServletInputStream() {
-
-            int offset = 0;
-            byte[] bytes = null;
-
             @Override
             public int read() throws IOException {
-                if (bytes == null) {
-                    RawBuffer raw = request.body().asRaw();
-                    if (raw == null) {
-                        return -1;
-                    }
-                    bytes = raw.asBytes();
-                }
-
-                if (isFinished()) {
-                    return -1;
-                }
-
-                return bytes[offset++];
+                return in.read();
             }
 
-            public boolean isFinished() {
-                if (bytes != null && bytes.length <= offset) {
-                    return true;
-                }
-
-                return false;
+            @Override
+            protected void finalize() throws IOException {
+                in.close();
             }
-
         };
     }
 
Add a comment
List