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

Support for native files on Apache Cordova #134

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/browser/isCordova.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const isCordova = typeof window != "undefined" && (
typeof window.PhoneGap != "undefined" ||
typeof window.Cordova != "undefined" ||
typeof window.cordova != "undefined");
const isCordova = () => typeof window != "undefined" && (
typeof window.PhoneGap != "undefined" ||
typeof window.Cordova != "undefined" ||
typeof window.cordova != "undefined");

export default isCordova;
13 changes: 13 additions & 0 deletions lib/browser/readAsByteArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* In cordova FileReader is used to read a slice of the file */
function readAsByteArray(chunk, done) {
let reader = new FileReader();
reader.onload = () => {
done(null, new Uint8Array(reader.result));
};
reader.onerror = (err) => {
done(err);
};
reader.readAsArrayBuffer(chunk);
}

export default readAsByteArray;
29 changes: 0 additions & 29 deletions lib/browser/source.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import isReactNative from "./isReactNative";
import isCordova from "./isCordova";
import uriToBlob from "./uriToBlob";

class FileSource {
Expand All @@ -15,26 +14,6 @@ class FileSource {
close() {}
}

class CordovaFileSource {
constructor(file) {
this._file = file;
this.size = file.size;
}

slice(start, end, callback) {
let reader = new FileReader();
reader.onload = () => {
callback(null, new Uint8Array(reader.result));
};
reader.onerror = (err) => {
callback(err);
};
reader.readAsArrayBuffer(this._file.slice(start, end));
}

close() {}
}

export function getSource(input, chunkSize, callback) {
// In React Native, when user selects a file, instead of a File or Blob,
// you usually get a file object {} with a uri property that contains
Expand All @@ -54,14 +33,6 @@ export function getSource(input, chunkSize, callback) {
return;
}

// In Apache Cordova applications, a FileEntry must be resolved using
// FileReader instances, see
// https://cordova.apache.org/docs/en/8.x/reference/cordova-plugin-file/index.html#read-a-file
if (isCordova) {
callback(null, new CordovaFileSource(input));
return;
}

// Since we emulate the Blob type in our tests (not all target browsers
// support it), we cannot use `instanceof` for testing whether the input value
// can be handled. Instead, we simply check is the slice() function and the
Expand Down
20 changes: 18 additions & 2 deletions lib/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {newRequest, resolveUrl} from "./node/request";
import {getSource} from "./node/source";
import * as Storage from "./node/storage";

import isCordova from "./browser/isCordova";
import readAsByteArray from "./browser/readAsByteArray"

const defaultOptions = {
endpoint: null,
fingerprint,
Expand Down Expand Up @@ -510,14 +513,27 @@ class Upload {
if (end === Infinity || end > this._size) {
end = this._size;
}

this._source.slice(start, end, (error, chunk) => {
if (error) {
this._emitError(new DetailedError(`tus: could not slice file or stream (from ${start} to ${end})`, error));
return;
}

xhr.send(chunk);
// In Apache Cordova applications, a FileEntry must be resolved using
// FileReader instances, see
// https://cordova.apache.org/docs/en/8.x/reference/cordova-plugin-file/index.html#read-a-file
if (isCordova()) {
readAsByteArray(chunk, (error, byteArray) => {
if (error) {
this._emitError(new DetailedError(`tus: could not read file (from ${start} to ${end})`, error));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we still xhr.send in the face of this error or is there a missing return?

Copy link
Author

@hannuniemela hannuniemela Dec 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved that block to source.js in commit 7380fdb since that would be more suitable Could you confirm that is correct. There should be this also corrected

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks OK from what I can tell when looking at it from the git diff.

}
xhr.send(byteArray);
});
} else {
xhr.send(chunk);
}

});

// Emit an progress event when a new chunk begins being uploaded.
Expand Down