|
| 1 | +package io.tus.java.example; |
| 2 | + |
| 3 | +import io.tus.java.client.FingerprintNotFoundException; |
| 4 | +import io.tus.java.client.ProtocolException; |
| 5 | +import io.tus.java.client.ResumingNotEnabledException; |
| 6 | +import io.tus.java.client.TusClient; |
| 7 | +import io.tus.java.client.TusURLFileStore; |
| 8 | +import io.tus.java.client.TusUpload; |
| 9 | +import io.tus.java.client.TusUploader; |
| 10 | +import org.json.JSONObject; |
| 11 | + |
| 12 | +import java.io.ByteArrayInputStream; |
| 13 | +import java.io.File; |
| 14 | +import java.io.IOException; |
| 15 | +import java.net.URL; |
| 16 | + |
| 17 | +public final class Api2DevdockTusFileUrlStorageBackend { |
| 18 | + /** |
| 19 | + * Run the API2 devdock TUS file URL storage example. |
| 20 | + * |
| 21 | + * @param args ignored |
| 22 | + */ |
| 23 | + public static void main(String[] args) { |
| 24 | + try { |
| 25 | + System.setProperty("http.strictPostRedirect", "true"); |
| 26 | + |
| 27 | + final JSONObject scenario = Api2DevdockScenario.loadScenario(); |
| 28 | + final JSONObject createResponse = Api2DevdockScenario.createResponse(scenario); |
| 29 | + final JSONObject result = uploadWithFileStorage(scenario, createResponse); |
| 30 | + Api2DevdockScenario.writeResult(result); |
| 31 | + |
| 32 | + System.out.println( |
| 33 | + "Java TUS SDK devdock scenario " |
| 34 | + + scenario.getString("scenarioId") |
| 35 | + + " resumed with file URL storage " |
| 36 | + + result.getString("uploadUrl") |
| 37 | + ); |
| 38 | + } catch (Exception e) { |
| 39 | + e.printStackTrace(); |
| 40 | + System.exit(1); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private static JSONObject uploadWithFileStorage( |
| 45 | + JSONObject scenario, |
| 46 | + JSONObject createResponse |
| 47 | + ) throws IOException, ProtocolException, FingerprintNotFoundException, |
| 48 | + ResumingNotEnabledException { |
| 49 | + final JSONObject uploadConfig = scenario.getJSONObject("upload"); |
| 50 | + final JSONObject resume = uploadConfig.getJSONObject("resume"); |
| 51 | + final JSONObject urlStorageBackend = uploadConfig.getJSONObject("urlStorageBackend"); |
| 52 | + final byte[] content = Api2DevdockScenario.scenarioBytes(uploadConfig); |
| 53 | + final int chunkSize = Api2DevdockScenario.fixedChunkSizeBytes(uploadConfig); |
| 54 | + final String fingerprint = resume.getString("fingerprint"); |
| 55 | + final File storageFile = File.createTempFile("api2-devdock-tus-url-storage", ".properties"); |
| 56 | + |
| 57 | + try { |
| 58 | + final TusURLFileStore store = new TusURLFileStore(storageFile); |
| 59 | + final TusClient client = new TusClient(); |
| 60 | + client.setUploadCreationURL( |
| 61 | + new URL(Api2DevdockScenario.tusUrl(uploadConfig, scenario, createResponse)) |
| 62 | + ); |
| 63 | + client.enableResuming(store); |
| 64 | + if (resume.getBoolean("removeFingerprintOnSuccess")) { |
| 65 | + client.enableRemoveFingerprintOnSuccess(); |
| 66 | + } |
| 67 | + |
| 68 | + final TusUpload firstUpload = uploadFor( |
| 69 | + scenario, |
| 70 | + createResponse, |
| 71 | + content, |
| 72 | + fingerprint |
| 73 | + ); |
| 74 | + final TusUploader firstUploader = client.createUpload(firstUpload); |
| 75 | + firstUploader.setChunkSize(chunkSize); |
| 76 | + final int firstAcceptedBytes = firstUploader.uploadChunk(); |
| 77 | + firstUploader.finish(false); |
| 78 | + |
| 79 | + if (firstAcceptedBytes != resume.getInt("stopAfterAcceptedBytes")) { |
| 80 | + throw new IllegalStateException( |
| 81 | + "first upload accepted " |
| 82 | + + firstAcceptedBytes |
| 83 | + + " bytes, expected " |
| 84 | + + resume.getInt("stopAfterAcceptedBytes") |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + final String firstUploadUrl = firstUploader.getUploadURL().toString(); |
| 89 | + final int previousUploadCount = store.size(); |
| 90 | + if (previousUploadCount != resume.getInt("expectedPreviousUploadCount")) { |
| 91 | + throw new IllegalStateException( |
| 92 | + "stored upload count " |
| 93 | + + previousUploadCount |
| 94 | + + ", expected " |
| 95 | + + resume.getInt("expectedPreviousUploadCount") |
| 96 | + ); |
| 97 | + } |
| 98 | + final boolean storedUploadKeyPrefixMatched = store.hasKeyWithPrefix( |
| 99 | + urlStorageBackend.getString("expectedStoredUploadKeyPrefix") |
| 100 | + ); |
| 101 | + |
| 102 | + final TusUpload secondUpload = uploadFor( |
| 103 | + scenario, |
| 104 | + createResponse, |
| 105 | + content, |
| 106 | + fingerprint |
| 107 | + ); |
| 108 | + final TusUploader resumedUploader = client.resumeUpload(secondUpload); |
| 109 | + resumedUploader.setChunkSize(content.length); |
| 110 | + int uploadedChunkSize; |
| 111 | + do { |
| 112 | + uploadedChunkSize = resumedUploader.uploadChunk(); |
| 113 | + } while (uploadedChunkSize > -1); |
| 114 | + resumedUploader.finish(); |
| 115 | + |
| 116 | + final String uploadUrl = resumedUploader.getUploadURL().toString(); |
| 117 | + if (!firstUploadUrl.equals(uploadUrl)) { |
| 118 | + throw new IllegalStateException( |
| 119 | + "resumed upload URL " + uploadUrl + ", expected " + firstUploadUrl |
| 120 | + ); |
| 121 | + } |
| 122 | + if (resumedUploader.getOffset() != content.length) { |
| 123 | + throw new IllegalStateException( |
| 124 | + "remote offset " |
| 125 | + + resumedUploader.getOffset() |
| 126 | + + ", expected " |
| 127 | + + content.length |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + final int remainingPreviousUploadCount = store.size(); |
| 132 | + if (remainingPreviousUploadCount != resume.getInt("expectedRemainingPreviousUploadCount")) { |
| 133 | + throw new IllegalStateException( |
| 134 | + "remaining stored upload count " |
| 135 | + + remainingPreviousUploadCount |
| 136 | + + ", expected " |
| 137 | + + resume.getInt("expectedRemainingPreviousUploadCount") |
| 138 | + ); |
| 139 | + } |
| 140 | + |
| 141 | + return new JSONObject() |
| 142 | + .put("firstAcceptedBytes", firstAcceptedBytes) |
| 143 | + .put("firstUploadUrl", firstUploadUrl) |
| 144 | + .put("previousUploadCount", previousUploadCount) |
| 145 | + .put("remainingPreviousUploadCount", remainingPreviousUploadCount) |
| 146 | + .put("storageFileEntryCount", store.size()) |
| 147 | + .put("storedUploadKeyPrefixMatched", storedUploadKeyPrefixMatched) |
| 148 | + .put("uploadUrl", uploadUrl) |
| 149 | + .put("urlStorageBackend", urlStorageBackend.getString("kind")); |
| 150 | + } finally { |
| 151 | + if (storageFile.exists() && !storageFile.delete()) { |
| 152 | + storageFile.deleteOnExit(); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + private static TusUpload uploadFor( |
| 158 | + JSONObject scenario, |
| 159 | + JSONObject createResponse, |
| 160 | + byte[] content, |
| 161 | + String fingerprint |
| 162 | + ) { |
| 163 | + final JSONObject uploadConfig = scenario.getJSONObject("upload"); |
| 164 | + final TusUpload upload = new TusUpload(); |
| 165 | + upload.setInputStream(new ByteArrayInputStream(content)); |
| 166 | + upload.setSize(content.length); |
| 167 | + upload.setFingerprint(fingerprint); |
| 168 | + upload.setMetadata( |
| 169 | + Api2DevdockScenario.uploadMetadata(uploadConfig, scenario, createResponse) |
| 170 | + ); |
| 171 | + return upload; |
| 172 | + } |
| 173 | + |
| 174 | + private Api2DevdockTusFileUrlStorageBackend() { |
| 175 | + throw new IllegalStateException("Utility class"); |
| 176 | + } |
| 177 | +} |
0 commit comments