Merge branch 'BFP-1784' of /home/git/repositories/brainfood/java-nginx-upload-processor
Showing
1 changed file
with
16 additions
and
15 deletions
... | @@ -18,15 +18,17 @@ import org.apache.commons.fileupload.servlet.ServletFileUpload; | ... | @@ -18,15 +18,17 @@ import org.apache.commons.fileupload.servlet.ServletFileUpload; |
18 | import org.ofbiz.base.util.HttpClient; | 18 | import org.ofbiz.base.util.HttpClient; |
19 | 19 | ||
20 | public class NGINXUploadProcessor extends HttpServlet { | 20 | public class NGINXUploadProcessor extends HttpServlet { |
21 | class ProgressRecord { | 21 | final class ProgressRecord { |
22 | long bytesRead; | 22 | final boolean complete; |
23 | long contentLength; | 23 | final long bytesRead; |
24 | int items; | 24 | final long contentLength; |
25 | final int items; | ||
25 | 26 | ||
26 | public ProgressRecord(long bytesRead, long contentLength, int items) { | 27 | public ProgressRecord(long bytesRead, long contentLength, int items, boolean complete) { |
27 | this.bytesRead = bytesRead; | 28 | this.bytesRead = bytesRead; |
28 | this.contentLength = contentLength; | 29 | this.contentLength = contentLength; |
29 | this.items = items; | 30 | this.items = items; |
31 | this.complete = complete; | ||
30 | } | 32 | } |
31 | } | 33 | } |
32 | 34 | ||
... | @@ -65,12 +67,8 @@ public class NGINXUploadProcessor extends HttpServlet { | ... | @@ -65,12 +67,8 @@ public class NGINXUploadProcessor extends HttpServlet { |
65 | System.err.println("QUERY PROGRESS: [" + xProgressId + "] - " + progress.get(xProgressId)); | 67 | System.err.println("QUERY PROGRESS: [" + xProgressId + "] - " + progress.get(xProgressId)); |
66 | if (xProgressId != null && progress.get(xProgressId) != null) { | 68 | if (xProgressId != null && progress.get(xProgressId) != null) { |
67 | ProgressRecord record = progress.get(xProgressId); | 69 | ProgressRecord record = progress.get(xProgressId); |
68 | String status = "done"; | 70 | out.println("({ \"state\" : \""+ (record.complete ? "done" : "uploading") +"\", \"received\" : " + |
69 | if (record.bytesRead < record.contentLength) { | 71 | record.bytesRead + ", \"size\" : " + record.contentLength + " });"); |
70 | status = "uploading"; | ||
71 | } | ||
72 | out.println("({ \"state\" : \""+ status +"\", \"received\" : " + | ||
73 | record.bytesRead + ", \"size\" : " + record.contentLength + " });"); | ||
74 | } else { | 72 | } else { |
75 | out.println("({ \"error\" : \"Invalid X-Progress-ID\" });"); | 73 | out.println("({ \"error\" : \"Invalid X-Progress-ID\" });"); |
76 | } | 74 | } |
... | @@ -78,7 +76,7 @@ public class NGINXUploadProcessor extends HttpServlet { | ... | @@ -78,7 +76,7 @@ public class NGINXUploadProcessor extends HttpServlet { |
78 | } else { | 76 | } else { |
79 | String xProgressId = request.getParameter("X-Progress-ID"); | 77 | String xProgressId = request.getParameter("X-Progress-ID"); |
80 | System.err.println("STORE: [" + xProgressId + "]"); | 78 | System.err.println("STORE: [" + xProgressId + "]"); |
81 | progress.put(xProgressId, new ProgressRecord(-1, 0, 1)); | 79 | progress.put(xProgressId, new ProgressRecord(-1, 0, 1, false)); |
82 | 80 | ||
83 | if (ServletFileUpload.isMultipartContent(request)) { | 81 | if (ServletFileUpload.isMultipartContent(request)) { |
84 | // Create a factory for disk-based file items | 82 | // Create a factory for disk-based file items |
... | @@ -108,9 +106,9 @@ public class NGINXUploadProcessor extends HttpServlet { | ... | @@ -108,9 +106,9 @@ public class NGINXUploadProcessor extends HttpServlet { |
108 | this.progress = progress; | 106 | this.progress = progress; |
109 | } | 107 | } |
110 | @Override | 108 | @Override |
111 | public void update(long sent, long length, int items) { | 109 | public void update(long bytesRead, long contentLength, int items) { |
112 | System.err.println("PROGRESS: " + sent + " " + length + " " + items); | 110 | System.err.println("PROGRESS: " + bytesRead + " " + contentLength + " " + items); |
113 | progress.put(xProgressId, new ProgressRecord(sent, length, items)); | 111 | progress.put(xProgressId, new ProgressRecord(bytesRead, contentLength, items, false)); |
114 | } | 112 | } |
115 | }; | 113 | }; |
116 | 114 | ||
... | @@ -132,6 +130,9 @@ public class NGINXUploadProcessor extends HttpServlet { | ... | @@ -132,6 +130,9 @@ public class NGINXUploadProcessor extends HttpServlet { |
132 | String pResponse = http.post(); | 130 | String pResponse = http.post(); |
133 | System.err.println("FINISHED:" + pResponse); | 131 | System.err.println("FINISHED:" + pResponse); |
134 | out.print(pResponse); | 132 | out.print(pResponse); |
133 | |||
134 | ProgressRecord oldRecord = progress.get(xProgressId); | ||
135 | progress.put(xProgressId, new ProgressRecord(oldRecord.bytesRead, oldRecord.contentLength, oldRecord.items, true)); | ||
135 | } catch (Exception ex) { | 136 | } catch (Exception ex) { |
136 | ex.printStackTrace(); | 137 | ex.printStackTrace(); |
137 | } | 138 | } | ... | ... |
-
Please register or sign in to post a comment