A tus client
A tus client in pure dart. Resumable uploads using tus protocol
tus is a protocol based on HTTP for resumable file uploads . Resumable
means that an upload can be interrupted at any moment and can be resumed without
re-uploading the previous data again. An interruption may happen willingly, if
the user wants to pause, or by accident in case of a network issue or server
outage.
Usage
import 'package:cross_file/cross_file.dart' show XFile ;
final file = XFile ("/path/to/my/pic.jpg" );
final client = TusClient (
Uri .parse ("https://master.tus.io/files/" ),
file,
store: TusMemoryStore (),
);
await client.upload (
onComplete: () {
print ("Complete!" );
print (client.uploadUrl.toString ());
},
onProgress: (progress) {
print ("Progress: $progress " );
},
);
Using Persistent URL Store
This is only supported on Flutter Android, iOS, desktop and web.
You need to add to your pubspec.yaml:
dependencies:
tus_client: ^1.0.2
tus_client_file_store: ^0.0.3
import 'package:path_provider/path_provider.dart' ;
import 'package:tus_client_file_store/tus_client_file_store.dart' show TusFileStore ;
import 'package:path/path.dart' as p;
final tempDir = (await getTemporaryDirectory ()).path;
final tempDirectory = Directory (p.join (tempDir, "tus-uploads" ));
final client = TusClient (
Uri .parse ("https://example.com/tus" ),
file,
store: TusFileStore (tempDirectory),
);
await client.upload ();
Adding Extra Headers
final client = TusClient (
Uri .parse ("https://master.tus.io/files/" ),
file,
headers: {"Authorization" : "..." },
);
Adding extra data
final client = TusClient (
Uri .parse ("https://master.tus.io/files/" ),
file,
metadata: {"for-gallery" : "..." },
);
Changing chunk size
The file is uploaded in chunks. Default size is 512KB. This should be set considering speed of upload vs device memory constraints
final client = TusClient (
Uri .parse ("https://master.tus.io/files/" ),
file,
maxChunkSize: 10 * 1024 * 1024 ,
);
Pausing upload
Pausing upload can be done after current uploading in chunk is completed.
final client = TusClient (
Uri .parse ("https://master.tus.io/files/" ),
file
);
Future .delayed (Duration (seconds: 5 )).then ((_) =>client.pause ());
await client.upload (
onComplete: () {
print ("Complete!" );
},
onProgress: (progress) {
print ("Progress: $progress " );
},
);
Example
For an example of usage in a Flutter app (using file picker) see: /example
Maintainers