capacitor-background-transfer 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CapacitorBackgroundTransfer.podspec +19 -0
- package/LICENSE +21 -0
- package/Package.swift +31 -0
- package/README.md +552 -2
- package/android/build.gradle +71 -0
- package/android/src/main/AndroidManifest.xml +20 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/AzureBlockUploader.java +148 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/BackgroundTransferPlugin.java +279 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/Downloader.java +244 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/Http.java +238 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/JobRuns.java +40 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/Notifications.java +54 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/Parts.java +25 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/PartsXml.java +199 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/Pause.java +28 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/Progress.java +13 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/PutUploader.java +47 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/Runner.java +369 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/S3MultipartUploader.java +143 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/Scheduler.java +144 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/TransferException.java +65 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/TransferJobService.java +115 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/TransferPaths.java +129 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/TransferRecord.java +163 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/TransferStore.java +121 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/TransferWorker.java +85 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/UploadAdapter.java +11 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/UploadAdapters.java +89 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/UploadOptionsCheck.java +104 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/Uploader.java +225 -0
- package/android/src/main/res/.gitkeep +0 -0
- package/dist/docs.json +527 -0
- package/dist/esm/definitions.d.ts +277 -0
- package/dist/esm/definitions.js +24 -0
- package/dist/esm/definitions.js.map +1 -0
- package/dist/esm/index.d.ts +10 -0
- package/dist/esm/index.js +26 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/validate.d.ts +18 -0
- package/dist/esm/validate.js +113 -0
- package/dist/esm/validate.js.map +1 -0
- package/dist/esm/web.d.ts +17 -0
- package/dist/esm/web.js +27 -0
- package/dist/esm/web.js.map +1 -0
- package/dist/plugin.cjs.js +200 -0
- package/dist/plugin.cjs.js.map +1 -0
- package/dist/plugin.js +203 -0
- package/dist/plugin.js.map +1 -0
- package/ios/Sources/BackgroundTransferCore/Files.swift +166 -0
- package/ios/Sources/BackgroundTransferCore/PartsUpload.swift +327 -0
- package/ios/Sources/BackgroundTransferCore/PartsXML.swift +137 -0
- package/ios/Sources/BackgroundTransferCore/TransferPaths.swift +103 -0
- package/ios/Sources/BackgroundTransferCore/TransferRecord.swift +141 -0
- package/ios/Sources/BackgroundTransferCore/TransferStore.swift +69 -0
- package/ios/Sources/BackgroundTransferCore/UploadOptionsCheck.swift +90 -0
- package/ios/Sources/BackgroundTransferCore/UploadOutcome.swift +96 -0
- package/ios/Sources/BackgroundTransferCore/UploadProtocol.swift +30 -0
- package/ios/Sources/BackgroundTransferPlugin/BackgroundTransferPlugin.swift +168 -0
- package/ios/Sources/BackgroundTransferPlugin/TransferManager.swift +1074 -0
- package/package.json +97 -4
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
package io.github.sandman92.backgroundtransfer;
|
|
2
|
+
|
|
3
|
+
import java.io.File;
|
|
4
|
+
import java.io.IOException;
|
|
5
|
+
import java.io.OutputStream;
|
|
6
|
+
import java.io.RandomAccessFile;
|
|
7
|
+
import java.net.HttpURLConnection;
|
|
8
|
+
import java.net.URL;
|
|
9
|
+
import java.util.Map;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Sends a file to a resumable upload session that the app has already
|
|
13
|
+
* started (Firebase Storage's {@code X-Goog-Upload-*} protocol): asks the
|
|
14
|
+
* server how many bytes it holds, sends the rest in {@link #CHUNK} pieces read
|
|
15
|
+
* from the file in place, and finalizes with the last one. Memory is one chunk
|
|
16
|
+
* buffer whatever the file's size. No {@code Authorization} header is sent
|
|
17
|
+
* unless the caller passes one in {@code headers}.
|
|
18
|
+
*
|
|
19
|
+
* Plain Java (no Android types), so it runs under JUnit against a local
|
|
20
|
+
* server.
|
|
21
|
+
*/
|
|
22
|
+
public final class Uploader implements UploadAdapter {
|
|
23
|
+
|
|
24
|
+
/** A multiple of 256 KiB, as the protocol requires for every chunk but the last. */
|
|
25
|
+
public static final int CHUNK = 4 * 1024 * 1024;
|
|
26
|
+
|
|
27
|
+
private final int chunk;
|
|
28
|
+
private final int retries;
|
|
29
|
+
private final long backoffMillis;
|
|
30
|
+
|
|
31
|
+
public Uploader() {
|
|
32
|
+
this(CHUNK, 3, 2000);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** For tests: a smaller chunk and quicker retries. */
|
|
36
|
+
public Uploader(int chunk, int retries, long backoffMillis) {
|
|
37
|
+
this.chunk = chunk;
|
|
38
|
+
this.retries = retries;
|
|
39
|
+
this.backoffMillis = backoffMillis;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** What the server holds. */
|
|
43
|
+
public static final class Status {
|
|
44
|
+
|
|
45
|
+
public final long received;
|
|
46
|
+
public final boolean isFinal;
|
|
47
|
+
|
|
48
|
+
Status(long received, boolean isFinal) {
|
|
49
|
+
this.received = received;
|
|
50
|
+
this.isFinal = isFinal;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** One chunk: where it starts, how long it is, and whether it finalizes. */
|
|
55
|
+
public static final class Chunk {
|
|
56
|
+
|
|
57
|
+
public final long offset;
|
|
58
|
+
public final int length;
|
|
59
|
+
public final boolean last;
|
|
60
|
+
|
|
61
|
+
Chunk(long offset, int length, boolean last) {
|
|
62
|
+
this.offset = offset;
|
|
63
|
+
this.length = length;
|
|
64
|
+
this.last = last;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** The next chunk to send from {@code offset} of a {@code total}-byte file. */
|
|
69
|
+
public static Chunk nextChunk(long offset, long total, int chunk) {
|
|
70
|
+
if (offset < 0 || offset > total) throw new IllegalArgumentException("offset " + offset + " outside 0.." + total);
|
|
71
|
+
int length = (int) Math.min(chunk, total - offset);
|
|
72
|
+
return new Chunk(offset, length, offset + length >= total);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Uploads {@code file} to {@code url}. Returns the file's size once the
|
|
77
|
+
* server has finalized it. Network errors are retried {@code retries}
|
|
78
|
+
* times from the server's offset before giving up with {@code network}.
|
|
79
|
+
*/
|
|
80
|
+
@Override
|
|
81
|
+
public long run(TransferRecord r, Progress progress) throws TransferException {
|
|
82
|
+
return run(r.url, new File(r.path), r.headers, progress);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public long run(String url, File file, Map<String, String> headers, Progress progress) throws TransferException {
|
|
86
|
+
int attempt = 0;
|
|
87
|
+
while (true) {
|
|
88
|
+
try {
|
|
89
|
+
return attempt(url, file, headers, progress);
|
|
90
|
+
} catch (TransferException e) {
|
|
91
|
+
if (!TransferException.Reason.NETWORK.equals(e.reason) || attempt >= retries) throw e;
|
|
92
|
+
attempt++;
|
|
93
|
+
Pause.sleep(backoffMillis << (attempt - 1), progress);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
private long attempt(String url, File file, Map<String, String> headers, Progress progress) throws TransferException {
|
|
99
|
+
if (!file.isFile()) throw new TransferException(TransferException.Reason.FILE_MISSING, "no file at " + file);
|
|
100
|
+
long total = file.length();
|
|
101
|
+
Status status = query(url, headers);
|
|
102
|
+
if (status.received > total) {
|
|
103
|
+
throw new TransferException(
|
|
104
|
+
TransferException.Reason.SIZE_MISMATCH,
|
|
105
|
+
"server holds " + status.received + " of a " + total + "-byte file"
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
if (status.isFinal) {
|
|
109
|
+
// A closed session isn't proof the object was accepted: the Firebase Storage emulator, for
|
|
110
|
+
// one, closes it even when security rules refuse the finalize. The app checks the object's metadata with its token.
|
|
111
|
+
throw new TransferException(
|
|
112
|
+
TransferException.Reason.SESSION_CLOSED,
|
|
113
|
+
"the session was already closed at " + status.received + " of " + total + " bytes"
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
long offset = status.received;
|
|
117
|
+
progress.onBytes(offset, total);
|
|
118
|
+
byte[] buffer = new byte[(int) Math.min(chunk, Math.max(total - offset, 0))];
|
|
119
|
+
try (RandomAccessFile in = new RandomAccessFile(file, "r")) {
|
|
120
|
+
do {
|
|
121
|
+
Pause.check(progress);
|
|
122
|
+
Chunk next = nextChunk(offset, total, chunk);
|
|
123
|
+
try {
|
|
124
|
+
in.seek(next.offset);
|
|
125
|
+
in.readFully(buffer, 0, next.length);
|
|
126
|
+
} catch (IOException e) {
|
|
127
|
+
throw new TransferException(
|
|
128
|
+
file.isFile() ? TransferException.Reason.UNKNOWN : TransferException.Reason.FILE_MISSING,
|
|
129
|
+
"reading the file: " + e.getMessage(),
|
|
130
|
+
0,
|
|
131
|
+
e
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
Status after = send(url, headers, buffer, next);
|
|
135
|
+
if (next.last) {
|
|
136
|
+
if (!after.isFinal && after.received >= 0) {
|
|
137
|
+
throw new TransferException(
|
|
138
|
+
TransferException.Reason.SIZE_MISMATCH,
|
|
139
|
+
"finalize left the session active at " + after.received
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
offset = total;
|
|
143
|
+
} else {
|
|
144
|
+
// The server says what it kept; carry on from there.
|
|
145
|
+
offset = after.received >= 0 ? after.received : next.offset + next.length;
|
|
146
|
+
}
|
|
147
|
+
progress.onBytes(offset, total);
|
|
148
|
+
} while (offset < total);
|
|
149
|
+
} catch (IOException e) {
|
|
150
|
+
throw new TransferException(TransferException.Reason.FILE_MISSING, "opening the file: " + e.getMessage(), 0, e);
|
|
151
|
+
}
|
|
152
|
+
return total;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** Asks the session how many bytes it holds. */
|
|
156
|
+
public static Status query(String url, Map<String, String> headers) throws TransferException {
|
|
157
|
+
HttpURLConnection c = null;
|
|
158
|
+
try {
|
|
159
|
+
c = open(url, headers);
|
|
160
|
+
c.setRequestProperty("X-Goog-Upload-Command", "query");
|
|
161
|
+
c.setFixedLengthStreamingMode(0);
|
|
162
|
+
c.getOutputStream().close();
|
|
163
|
+
int code = c.getResponseCode();
|
|
164
|
+
if (code != 200) throw TransferException.forStatus(code, "query");
|
|
165
|
+
Status s = statusOf(c);
|
|
166
|
+
if (s.received < 0) throw new TransferException(
|
|
167
|
+
TransferException.Reason.UNKNOWN,
|
|
168
|
+
"query answered without X-Goog-Upload-Size-Received"
|
|
169
|
+
);
|
|
170
|
+
return s;
|
|
171
|
+
} catch (IOException e) {
|
|
172
|
+
throw new TransferException(TransferException.Reason.NETWORK, "query: " + e, 0, e);
|
|
173
|
+
} finally {
|
|
174
|
+
if (c != null) c.disconnect();
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
private static Status send(String url, Map<String, String> headers, byte[] buffer, Chunk chunk) throws TransferException {
|
|
179
|
+
HttpURLConnection c = null;
|
|
180
|
+
try {
|
|
181
|
+
c = open(url, headers);
|
|
182
|
+
c.setRequestProperty("X-Goog-Upload-Command", chunk.last ? "upload, finalize" : "upload");
|
|
183
|
+
c.setRequestProperty("X-Goog-Upload-Offset", String.valueOf(chunk.offset));
|
|
184
|
+
c.setRequestProperty("Content-Type", "application/octet-stream");
|
|
185
|
+
c.setFixedLengthStreamingMode(chunk.length);
|
|
186
|
+
try (OutputStream out = c.getOutputStream()) {
|
|
187
|
+
out.write(buffer, 0, chunk.length);
|
|
188
|
+
}
|
|
189
|
+
int code = c.getResponseCode();
|
|
190
|
+
if (code != 200) throw TransferException.forStatus(code, chunk.last ? "finalize" : "upload");
|
|
191
|
+
return statusOf(c);
|
|
192
|
+
} catch (IOException e) {
|
|
193
|
+
throw new TransferException(TransferException.Reason.NETWORK, "upload at " + chunk.offset + ": " + e, 0, e);
|
|
194
|
+
} finally {
|
|
195
|
+
if (c != null) c.disconnect();
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
private static Status statusOf(HttpURLConnection c) {
|
|
200
|
+
String received = c.getHeaderField("X-Goog-Upload-Size-Received");
|
|
201
|
+
long n = -1;
|
|
202
|
+
if (received != null) {
|
|
203
|
+
try {
|
|
204
|
+
n = Long.parseLong(received.trim());
|
|
205
|
+
} catch (NumberFormatException ignored) {
|
|
206
|
+
n = -1;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return new Status(n, "final".equalsIgnoreCase(c.getHeaderField("X-Goog-Upload-Status")));
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
static HttpURLConnection open(String url, Map<String, String> headers) throws IOException {
|
|
213
|
+
HttpURLConnection c = (HttpURLConnection) new URL(url).openConnection();
|
|
214
|
+
c.setRequestMethod("POST");
|
|
215
|
+
c.setInstanceFollowRedirects(false);
|
|
216
|
+
c.setDoOutput(true);
|
|
217
|
+
c.setUseCaches(false);
|
|
218
|
+
c.setConnectTimeout(30_000);
|
|
219
|
+
c.setReadTimeout(120_000);
|
|
220
|
+
if (headers != null) {
|
|
221
|
+
for (Map.Entry<String, String> h : headers.entrySet()) c.setRequestProperty(h.getKey(), h.getValue());
|
|
222
|
+
}
|
|
223
|
+
return c;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
File without changes
|