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 file:// upload using cordova file-transfer #144

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
89 changes: 78 additions & 11 deletions lib/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

Slingshot.Upload = function (directive, metaData) {

if (!window.File || !window.FormData) {
throw new Error("Browser does not support HTML5 uploads");
if (!window.File || !window.FormData ) {
if(!cordova || !cordova.file || !FileTransfer)
throw new Error("Browser does not support HTML5 uploads and cordova file transfer is not available");
}

var self = this,
Expand All @@ -34,6 +35,54 @@ Slingshot.Upload = function (directive, metaData) {
return formData;
}

function cordova_transfer(callback){
status.set("transferring");
loaded.set(0);

var ft = new FileTransfer();

ft.onprogress = function(progressEvent){
if(progressEvent.lengthComputable){
loaded.set(progressEvent.loaded);
total.set(progressEvent.total);
}
};

var options = new FileUploadOptions();
options.headers = {};
_.each(self.instructions.headers, function (value, key) {
options.headers[key] = value;
});
options.headers['Content-Length'] = self.file.size;


options.params = {};
_.each(self.instructions.postData, function (value) {
options.params[value.name] = value.value;
});

var fileUrl = self.file.nativeURL;
options.fileKey = "file";
options.fileName = self.file.name;
options.mimeType = self.file.type;
//options.chunkMode = false;
options.httpMethod = 'post';


ft.upload(fileUrl,encodeURI(self.instructions.upload),function(result){
status.set("done");
loaded.set(total.get());
callback(null, self.instructions.download);

},function(err){
status.set("failed");
callback(new Meteor.Error(err.http_status,"Failed to upload file to cloud storage",err.exception));
},options);

self.xhr = ft;

return self;
}
_.extend(self, {

/**
Expand Down Expand Up @@ -86,20 +135,34 @@ Slingshot.Upload = function (directive, metaData) {
*/

send: function (file, callback) {
if (! (file instanceof window.File) && ! (file instanceof window.Blob))
if (! (file instanceof window.File) && ! (file instanceof window.Blob) && !( typeof file==='string' && /^file:\/\/.*$/i.test(file)) )
Copy link

Choose a reason for hiding this comment

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

This checks only for Android and BB based file path. https://github.com/apache/cordova-plugin-file#file-system-layouts

throw new Error("Not a file");

self.file = file;
function _send(){
self.request(function (error, instructions) {
if (error) {
return callback(error);
}

self.request(function (error, instructions) {
if (error) {
return callback(error);
}
self.instructions = instructions;

self.instructions = instructions;
self.transfer(callback);
});
}

if(/^file:\/\/.*$/i.test(file)) {
window.resolveLocalFileSystemURL(file, function (fileEntry) {
fileEntry.file(function (f) {
self.file = f;
self.file.nativeURL = fileEntry.nativeURL;
_send();
})
})
}else{
self.file = file;
_send();
}

self.transfer(callback);
});

return self;
},
Expand Down Expand Up @@ -146,6 +209,10 @@ Slingshot.Upload = function (directive, metaData) {
throw new Error("Cannot transfer file at upload status: " +
status.curValue);
}
if(self.file.hasOwnProperty('nativeURL'))//cordova File
{
return cordova_transfer(callback);
}

status.set("transferring");
loaded.set(0);
Expand Down
2 changes: 1 addition & 1 deletion package.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package.describe({
name: "edgee:slingshot",
summary: "Directly post files to cloud storage services, such as AWS-S3.",
version: "0.7.1",
version: "0.7.1-test",
git: "https://github.com/CulturalMe/meteor-slingshot"
});

Expand Down