Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [1.0.3] - Added support for Creation W/ Upload

* Added an optional `uploadDataDuringCreation` options (see [TUS documentation](https://tus.io/protocols/resumable-upload#creation-with-upload))

## [1.0.2] - Fixed issue with not parsing the http port number

* Fixed issue with not parsing the http port number
Expand Down
20 changes: 19 additions & 1 deletion lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class TusClient {
/// Storage used to save and retrieve upload URLs by its fingerprint.
final TusStore? store;

/// Whether to upload the file data during creation
/// (see [create] method)
final bool uploadDataDuringCreation;

final XFile file;

final Map<String, String>? metadata;
Expand Down Expand Up @@ -51,6 +55,7 @@ class TusClient {
this.headers,
this.metadata = const {},
this.maxChunkSize = 512 * 1024,
this.uploadDataDuringCreation = false,
}) {
_fingerprint = generateFingerprint() ?? "";
_uploadMetadata = generateMetadata();
Expand Down Expand Up @@ -83,7 +88,20 @@ class TusClient {
"Upload-Length": "$_fileSize",
});

final response = await client.post(url, headers: createHeaders);
Uint8List? initialChunk;

if (uploadDataDuringCreation) {
createHeaders["Content-Type"] = "application/offset+octet-stream";
createHeaders["Upload-Offset"] = "0";

initialChunk = await _getData();
}

final response = await client.post(
url,
headers: createHeaders,
body: initialChunk,
);
if (!(response.statusCode >= 200 && response.statusCode < 300) &&
response.statusCode != 404) {
throw ProtocolException(
Expand Down