Git Product home page Git Product logo

tus_client's Introduction

A tus client

Pub Version Build Status


A tus client in pure dart. Resumable uploads using tus protocol Forked from tus_client

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;

// File to be uploaded
final file = XFile("/path/to/my/pic.jpg");

// Create a client
final client = TusClient(
    Uri.parse("https://master.tus.io/files/"),
    file,
    store: TusMemoryStore(),
);

// Starts the upload
await client.upload(
    onStart:(TusClient client, Duration? estimate){
        // If estimate is not null, it will provide the estimate time for completion
        // it will only be not null if measuring upload speed
        print('This is the client to be used $client and $estimate time');
    },
    onComplete: () {
        print("Complete!");

        // Prints the uploaded file URL
        print(client.uploadUrl.toString());
    },
    onProgress: (double progress, Duration estimate, TusClient client) {
        print("Progress: $progress, Estimated time: ${estimate.inSeconds}");
    },

    // Set this to true if you want to measure upload speed at the start of the upload
    measureUploadSpeed: true,
);

Using Persistent URL Store

This is only supported on Flutter Android, iOS, desktop and web. You need to add to your pubspec.yaml:

import 'package:path_provider/path_provider.dart';

//creates temporal directory to store the upload progress
final tempDir = await getTemporaryDirectory();
final tempDirectory = Directory('${tempDir.path}/${gameId}_uploads');
if (!tempDirectory.existsSync()) {
    tempDirectory.createSync(recursive: true);
}

// Create a client
final client = TusClient(
    Uri.parse("https://example.com/tus"),
    file,
    store: TusFileStore(tempDirectory),
);

// Start upload
// Don't forget to delete the 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,  // chunk is 10MB
);

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
);

// Pause after 5 seconds
Future.delayed(Duration(seconds: 5)).then((_) =>client.pause());

// Starts the upload
await client.upload(
    onComplete: () {
        print("Complete!");
    },
    onProgress: (double progress, Duration estimate, TusClient client) {
        print("Progress: $progress, Estimated time: ${estimate.inSeconds}");
    },
);

Set up the retry mechanism

It is posible to set up how many times the upload can fail before throw an error to increase robustness of the upload. Just indicate how many retries to set up the number of attempts before fail, the retryInterval (in seconds) to indicate the time between every retry and the retryScale (constant by default) to indicate how this time should increase or not between every retry.

final client = TusClient(
    Uri.parse("https://master.tus.io/files/"),
    file
    retries: 5,
    retryInterval: 2,
    retryScale: RetryScale.exponential,
);

Example

For an example of usage in a Flutter app (using file picker) see: /example

Maintainers

tus_client's People

Contributors

jjmutumi avatar jorger5 avatar nazarenocavazzon avatar tomassasovsky avatar tomasatisocco avatar satng avatar bboykeen avatar mqaa avatar mehul425 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.