Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate TUS client #438

Open
Vinzent03 opened this issue Apr 12, 2023 · 15 comments
Open

Integrate TUS client #438

Vinzent03 opened this issue Apr 12, 2023 · 15 comments
Labels
enhancement New feature or request storage This issue or pull request is related to storage

Comments

@Vinzent03
Copy link
Collaborator

Is your feature request related to a problem? Please describe.
As proposed here it seems like it's not planned to integrate the tus client into supabase-js, because there is already the great library uppy.js.
Though the best TUS client for Dart I found was this one, which doesn't support Web for persistent storage and is a fork from an older package.

Describe the solution you'd like
I think it feels more in Supabase integrated if the existing supabase-dart client would support TUS as well. So my proposal would be to integrate it in storage-dart.

@Vinzent03 Vinzent03 added the enhancement New feature or request label Apr 12, 2023
@dshukertjr
Copy link
Member

There actually is a plan to integrate tus client on js library. We can wait to see how that plays out, and bring it over to the Flutter library!

@prafulfillment
Copy link

What's the status on this for Flutter?

@dshukertjr
Copy link
Member

@prafulfillment
We are still evaluating the optimal developer experience for the client side implementation of resumable uploads across all client libraries.

@dshukertjr dshukertjr added the storage This issue or pull request is related to storage label May 12, 2023
@theShreyans
Copy link

Correct me if I'm wrong. After the launch week, it seems like resumable uploads are still not available for all projects and are made available per project basis. In that case is there any workaround as of now for flutter-related projects? I'm working on a project that lets the user upload max 100 MB of files but without upload progress or cancelling uploads or resuming them, it would be impossible. Also, I tried integrating a custom Backblaze storage option but was too much work.

@dshukertjr
Copy link
Member

@theShreyans
You can contact Supabase support if you want resumable uploads turned on for your Supabase project.

Implementing TUS protocol on the client library is in progress. We ask to patiently wait for it to be complete. In the mean while, you may explorer third party TUS client libraries such as this one
https://pub.dev/packages/tus_client_dart

@rlee1990
Copy link

@dshukertjr any updates on this for flutter?

@rlee1990
Copy link

Any updates on this?

@dshukertjr
Copy link
Member

@rlee1990
Will add this to supabase-flutter once they add it to the js client. supabase/storage-js#189

@JEndler
Copy link

JEndler commented Feb 21, 2024

I'd also be interested in this.

In the meantime, I've been trying to get the resumable upload to work with https://pub.dev/packages/tus_client_dart, here's a version that works for me :) Might save someone some time:

// Function to pick a file
Future<XFile> pickFile(List<String> allowedExtensions) async {
  // Function to pick files
  FilePickerResult? result = await FilePicker.platform.pickFiles(
    type: FileType.custom,
    allowedExtensions: allowedExtensions
  );
  if (result != null) {
    XFile file = XFile(result.files.single.path!);
    return file;
  } else {
    // User canceled the picker
    return XFile('');
  }
}

// Function to pick files and upload to Supabase Storage using TUS
Future<void> pickFilesAndUploadToSupabaseWithTUS() async {
  try {
    // Function to pick files, only allows zip files
    XFile? file = await pickFile(['zip']);

    String uid = Supabase.instance.client.auth.currentUser!.id;
    
    // Create a new TusClient instance
    final tusClient = TusClient(file, store: TusMemoryStore());

    // function to print progress
    void printOnProgress(double progress, Duration duration) {
      print('Progress: $progress% and $duration time');
    }

    // Starts the upload
    await tusClient.upload(
      uri: Uri.parse(
        // This resolves to our Supabase Storage URL
          '${dotenv.env['SUPABASE_URL']!}/storage/v1/upload/resumable'),
      headers: {
        'Authorization': 'Bearer ${Supabase.instance.client.auth.currentSession?.accessToken}',
        // Set to true to enable overwrite
        'x-upsert': 'true'
      },
      metadata: {
        'bucketName': 'bucketname',
        'objectName': '$uid/${file.path.split('/').last}',
        'contentType': 'application/zip',
      },
      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(tusClient.uploadUrl.toString());
      },
      onProgress: printOnProgress,
      // Set this to true if you want to measure upload speed at the start of the upload
      measureUploadSpeed: true,
    );
   }

@iampopal
Copy link

iampopal commented Jun 2, 2024

I'd also be interested in this.

In the meantime, I've been trying to get the resumable upload to work with https://pub.dev/packages/tus_client_dart, here's a version that works for me :) Might save someone some time:

// Function to pick a file
Future<XFile> pickFile(List<String> allowedExtensions) async {
  // Function to pick files
  FilePickerResult? result = await FilePicker.platform.pickFiles(
    type: FileType.custom,
    allowedExtensions: allowedExtensions
  );
  if (result != null) {
    XFile file = XFile(result.files.single.path!);
    return file;
  } else {
    // User canceled the picker
    return XFile('');
  }
}

// Function to pick files and upload to Supabase Storage using TUS
Future<void> pickFilesAndUploadToSupabaseWithTUS() async {
  try {
    // Function to pick files, only allows zip files
    XFile? file = await pickFile(['zip']);

    String uid = Supabase.instance.client.auth.currentUser!.id;
    
    // Create a new TusClient instance
    final tusClient = TusClient(file, store: TusMemoryStore());

    // function to print progress
    void printOnProgress(double progress, Duration duration) {
      print('Progress: $progress% and $duration time');
    }

    // Starts the upload
    await tusClient.upload(
      uri: Uri.parse(
        // This resolves to our Supabase Storage URL
          '${dotenv.env['SUPABASE_URL']!}/storage/v1/upload/resumable'),
      headers: {
        'Authorization': 'Bearer ${Supabase.instance.client.auth.currentSession?.accessToken}',
        // Set to true to enable overwrite
        'x-upsert': 'true'
      },
      metadata: {
        'bucketName': 'bucketname',
        'objectName': '$uid/${file.path.split('/').last}',
        'contentType': 'application/zip',
      },
      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(tusClient.uploadUrl.toString());
      },
      onProgress: printOnProgress,
      // Set this to true if you want to measure upload speed at the start of the upload
      measureUploadSpeed: true,
    );
   }

Does this example works to check progress of uploading?

@iampopal
Copy link

iampopal commented Jun 2, 2024

I am looking and need to check progress of uploading files, when this could be included?

@iampopal
Copy link

iampopal commented Jun 2, 2024

@dshukertjr When will Uploading progress be added in flutter storage?

@iampopal
Copy link

iampopal commented Jun 2, 2024

As resumable uploads are added in JS Library, will be great to add steps for adding it to flutter too.

https://supabase.com/docs/guides/storage/uploads/resumable-uploads?queryGroups=language&language=js

@dshukertjr
Copy link
Member

@iampopal I Love the enthusiasm, but I would appreciate it if you could refrain from spamming the same issue with four consecutive comments. supabase_flutter will add resumable upload support when JS SDK gets the resumable upload feature. You can check this PR to monitor it. In the meantime, you can implement resumable uploads using a third-party library like this lovely example.

@itsatifsiddiqui
Copy link

Hi everyone,

I encountered the need to upload files and track their progress while using the Supabase Flutter SDK and realized others might be facing the same issue. To address this, I’ve developed and published a package: supabase_progress_uploads.

This package supports uploading files with real-time progress updates and can handle multiple file uploads simultaneously. It’s based on a code snippet provided by @JEndler, to whom I’m very grateful.

Feel free to check it out and see if it fits your needs!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request storage This issue or pull request is related to storage
Projects
None yet
Development

No branches or pull requests

8 participants