RobinBobin / react-native-google-drive-api-wrapper

This wrapper facilitates the use of the Google Drive API in React Native projects.
110 stars 43 forks source link

Uploaded sqlite file is 0 byte #58

Closed przbadu closed 2 years ago

przbadu commented 2 years ago

Hi, I am really struggling to upload my sqlite backup file to google drive. If I do local backup, everything works fine, and I can view content of my sqlite backup in SQLite Viewer.

Where as if I try to upload that file to google drive using this library, I always get 0 byte sqlite file uploaded in google drive, which do not have any content in it.

Here is what I am doing right now:

  async uploadToDrive(): Promise<void> {
    try {
      // const mimeType = 'application/x-sqlite3'; // I tried with different mime type
      const mimeType = MimeTypes.BINARY;
      const localDBFilePath = '/data/data/<my-package-name>/demo.db';
      const fileToBackup = RNFS.readFile(localDBFilePath, 'base64');
     // I have also tried, same result.
    // RNFetchBlog.fs.readStream(localDBFilePath, 'base64')
      const fileMetadata = {
        name: 'test.db',
        MimeTypes: mimeType,
      };

      const result = await this.gdrive.files
        .newMultipartUploader()
        .setData(fileToBackup, mimeType)
        .setRequestBody(fileMetadata)
        .execute();

      console.log('file uploaded to Drive: ', result.id);
    } catch (error: any) {
      alert('Error: ' + error.message);
    }
  }

If anyone can help me find, what I am doing wrong would be appreciated.

Screenshots: Screenshot_3 Screenshot_4

RobinBobin commented 2 years ago

I would try converting base64 to binary before uploading. I guess any RN project has the base64-js dependency. And only then upload to GDrive, setting whatever mime type you want.

przbadu commented 2 years ago

Thank you @RobinBobin for quick response, I will try it.

przbadu commented 2 years ago

Thank you so much @RobinBobin , My stupid mistake, RNFS.readFile was missing await call and trying to treat it as base64.

Here is the solution if anyone is wondering:

async uploadToDrive(): Promise<void> {
    try {
      const mimeType = MimeTypes.BINARY;
      const localDBFilePath = '/data/data/<my-package-name>/demo.db';
      const fileToBackup = await RNFS.readFile(localDBFilePath, 'base64'); // I forget to add await call here
      const fileMetadata = {
        name: 'test.db',
        MimeTypes: mimeType,
      };

      const result = await this.gdrive.files
        .newMultipartUploader()
        .setData(fileToBackup, mimeType)
        .setIsBase64(true) // this is base64 content, so set this to true. Or if you want to pass binary content as @RobinBobin suggested, then you don't need to set this.
        .setRequestBody(fileMetadata)
        .execute();

      console.log('file uploaded to Drive: ', result.id);
    } catch (error: any) {
      alert('Error: ' + error.message);
    }
  }
przbadu commented 2 years ago

Related issue: https://github.com/RobinBobin/react-native-google-drive-api-wrapper/issues/3

RobinBobin commented 2 years ago

Glad you got it working :slightly_smiling_face: .

:smile: shame on me for not noticing the missing await and completely forgetting about .setIsBase64() :flushed: