From f965708ad3140e3b8b0ba5e58dc72d28dcf4e6a9 Mon Sep 17 00:00:00 2001 From: Jan Blunck <238088+jblunck@users.noreply.github.com> Date: Sun, 29 Nov 2020 18:29:46 +0100 Subject: [PATCH] Mobile: Fixes #3601: Fix uploading resource files to S3 with RN (#4127) When the mobile application tries to synchronize a note with attached resource files, e.g. from the notebook "Welcome! (Mobile)", readFile() is called to return a Buffer although that isn't implemented in FsDriverRN.readFile(). This is changing the code to return base64 encoded content from react-native and turn it into a Buffer before the S3 putObject() API call is used to create the remote object. Tested with AVD Android 10.0 target. Signed-off-by: Jan Blunck --- packages/lib/file-api-driver-amazon-s3.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/lib/file-api-driver-amazon-s3.js b/packages/lib/file-api-driver-amazon-s3.js index 6e202db26..ddb187b4b 100644 --- a/packages/lib/file-api-driver-amazon-s3.js +++ b/packages/lib/file-api-driver-amazon-s3.js @@ -2,6 +2,7 @@ const { basicDelta } = require('./file-api'); const { basename } = require('./path-utils'); const shim = require('./shim').default; const JoplinError = require('./JoplinError'); +const { Buffer } = require('buffer'); const S3_MAX_DELETES = 1000; @@ -83,12 +84,14 @@ class FileApiDriverAmazonS3 { async s3UploadFileFrom(path, key) { if (!shim.fsDriver().exists(path)) throw new Error('s3UploadFileFrom: file does not exist'); - const body = await shim.fsDriver().readFile(path, 'Buffer'); + const body = await shim.fsDriver().readFile(path, 'base64'); + const fileStat = await shim.fsDriver().stat(path); return new Promise((resolve, reject) => { - this.api().upload({ + this.api().putObject({ Bucket: this.s3_bucket_, Key: key, - Body: body, + Body: Buffer.from(body, 'base64'), + ContentLength: `${fileStat.size}`, }, (err, response) => { if (err) reject(err); else resolve(response);