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

[cross_file] [web] Implement openRead correctly. #91867

Open
ditman opened this issue Oct 15, 2021 · 6 comments · May be fixed by flutter/packages#5158
Open

[cross_file] [web] Implement openRead correctly. #91867

ditman opened this issue Oct 15, 2021 · 6 comments · May be fixed by flutter/packages#5158
Assignees
Labels
c: new feature Nothing broken; request for a new capability p: cross_file The cross_file plugin P1 High-priority issues at the top of the work list package flutter/packages repository. See also p: labels. team-ecosystem Owned by Ecosystem team triaged-ecosystem Triaged by Ecosystem team

Comments

@ditman
Copy link
Member

ditman commented Oct 15, 2021

The current implementation of XFile.openRead is not really streaming the contents of the enclosed Blob as a Stream. It just slices a cached blob and returning that slice, in one go.

People are chunking files by computing the chunks from the outside using the start/end variables, and then listening for the (only) event on the Stream. This is very inefficient, and even though it works, it is not what the Stream is supposed to do!

Users need to get a Stream from the slice they care about in the blob, and then read it until its completion. See an example of the dart:io implementation: _FileStream

cross_file needs a _BlobStream class for its web implementation.

@ditman ditman added c: new feature Nothing broken; request for a new capability plugin p: cross_file The cross_file plugin labels Oct 15, 2021
@ditman ditman added the P3 Issues that are less important to the Flutter project label Oct 15, 2021
@ditman ditman added this to Not Started in Flutter Web Plugins via automation Oct 15, 2021
@rajrushilmakkar
Copy link

rajrushilmakkar commented Jan 10, 2023

I am trying to upload image to google drive on web
Sample code-

uploadFileToGoogleDriveWeb(XFile xFile) async {
    final String serviceAccount =
        await rootBundle.loadString('assets/ServiceAccount.json');
    final accountCredentials =
        ServiceAccountCredentials.fromJson(serviceAccount);
    AuthClient client = await clientViaServiceAccount(
        accountCredentials, ['https://www.googleapis.com/auth/drive']);
    var drive = ga.DriveApi(client);
    ga.File fileToUpload = ga.File();
    fileToUpload.parents = ["parent_id"];
    fileToUpload.name = p.basename(xFile.path);
    var response = await drive.files.create(
      fileToUpload,
      uploadMedia: ga.Media(xFile.openRead(), await xFile.length()),
    );
    return response.id;
  }

I received the xfile using image_picker
I am using googleapis package to upload the file to drive
It’s working fine on android/iOS but for web I get this error-
error: Expected a value of type 'StreamTransformer<Uint8List, String>', but got one of type 'Base64Encoder'
The error is in line

drive.files.create(
      fileToUpload,
      uploadMedia: ga.Media(xFile.openRead(), await xFile.length()),
    ) 

Please let me know if I am doing anything wrong or what's the issue here
Also on mobile devices, I actually used File from 'dart:io' instead of XFile, so file.openRead() didn't have any issue, that's why I assumed that the issue is with xFile.openRead()

@rajrushilmakkar
Copy link

rajrushilmakkar commented Jan 10, 2023

Okay, so I actually needed a Stream<List < int > > instead of Stream
So this worked for me

Stream<List<int>> newStream = xFile.openRead().map((event) {
      //event is uint8list
      return List<int>.from(event);
    });

@stuartmorgan stuartmorgan added package flutter/packages repository. See also p: labels. and removed plugin labels Mar 6, 2023
@flutter-triage-bot flutter-triage-bot bot added team-ecosystem Owned by Ecosystem team triaged-ecosystem Triaged by Ecosystem team labels Jul 8, 2023
@ditman ditman linked a pull request Oct 17, 2023 that will close this issue
11 tasks
@ditman ditman self-assigned this Oct 17, 2023
@ditman ditman moved this from Not Started to Writing Web Implementation in Flutter Web Plugins Oct 17, 2023
@ditman
Copy link
Member Author

ditman commented Oct 17, 2023

I have a very promising demo here:

https://dit-tests.web.app

The app allows selecting files of any size and stream their contents without impacting the memory usage of the browser greatly.

(It's based off of the PR linked above, and a small tweak to package file_selector.)

@yjbanov yjbanov added P1 High-priority issues at the top of the work list and removed P3 Issues that are less important to the Flutter project labels Apr 16, 2024
@rohintoncollins
Copy link

Do we have an ETA for when we think this PR may be merged?

@rohintoncollins
Copy link

rohintoncollins commented May 15, 2024

Please see here for a partial workaround using the File System API: flutter/packages#5158 (comment)

@CosmicPangolin
Copy link

Do we have an ETA for when we think this PR may be merged?

Not one to harass on a package but also curious; I feel like I've patiently watched paint dry on this issue for years. I know Stuart is super busy with platform issues - maybe it makes sense to reassign the factories?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: new feature Nothing broken; request for a new capability p: cross_file The cross_file plugin P1 High-priority issues at the top of the work list package flutter/packages repository. See also p: labels. team-ecosystem Owned by Ecosystem team triaged-ecosystem Triaged by Ecosystem team
Projects
Flutter Web Plugins
  
Writing Web Implementation
Development

Successfully merging a pull request may close this issue.

8 participants