(InputStream is, DocumentFile dfile, int dLimit, int timeLimit, String host, IProgress intf)
| 77 | private static final int TIMEOUT = 20 * 1000; // milliseconds |
| 78 | |
| 79 | public static String upload(InputStream is, DocumentFile dfile, int dLimit, int timeLimit, String host, IProgress intf) throws Throwable { |
| 80 | String result; |
| 81 | SecureRandom rnd = new SecureRandom(); |
| 82 | |
| 83 | byte[] secret = new byte[16]; |
| 84 | rnd.nextBytes(secret); |
| 85 | |
| 86 | JSONObject jupload = getMetadata(dfile, dLimit, timeLimit, secret); |
| 87 | |
| 88 | Uri uri = Uri.parse("wss://" + Uri.parse(host).getHost() + "/api/ws"); |
| 89 | |
| 90 | WebSocket ws = new WebSocketFactory().createSocket(uri.toString(), TIMEOUT); |
| 91 | ws.setFrameQueueSize(32); // 32 x 64KB = 2 MB |
| 92 | |
| 93 | Semaphore sem = new Semaphore(0); |
| 94 | List<String> queue = Collections.synchronizedList(new ArrayList<>()); |
| 95 | |
| 96 | ws.addListener(new WebSocketAdapter() { |
| 97 | @Override |
| 98 | public void onTextMessage(WebSocket ws, String text) { |
| 99 | Log.i("Send text message=" + text); |
| 100 | queue.add(text); |
| 101 | sem.release(); |
| 102 | } |
| 103 | }); |
| 104 | |
| 105 | Log.i("Send connect"); |
| 106 | ws.connect(); |
| 107 | |
| 108 | try { |
| 109 | Log.i("Send upload=" + jupload); |
| 110 | ws.sendText(jupload.toString()); |
| 111 | |
| 112 | Log.i("Send wait reply"); |
| 113 | if (!sem.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS)) |
| 114 | throw new TimeoutException("reply"); |
| 115 | |
| 116 | JSONObject jreply = new JSONObject(queue.remove(0)); |
| 117 | Log.i("Send reply=" + jreply); |
| 118 | |
| 119 | if (jreply.has("error")) { |
| 120 | String error = jreply.getString("error"); |
| 121 | if ("400".equals(error)) |
| 122 | error += " - try lower limits"; |
| 123 | throw new IOException("Error: " + error); |
| 124 | } |
| 125 | |
| 126 | result = jreply.getString("url") + |
| 127 | "#" + Base64.encodeToString(secret, Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP); |
| 128 | Log.i("Send url=" + result); |
| 129 | |
| 130 | // The record sequence number (SEQ) is a 96-bit unsigned integer in network byte order that starts at zero. |
| 131 | // network byte order = transmitting the most significant byte first |
| 132 | // Java = big endian = network byte order |
| 133 | // sizeof(int) = 4 bytes |
| 134 | // sizeof(long) = 8 bytes |
| 135 | long seq = 0; |
| 136 | byte[] buffer = new byte[65536]; |
nothing calls this directly
no test coverage detected