Skip to content

Commit

Permalink
Attach base64 files from window.plugins.socialsharing.share in Window…
Browse files Browse the repository at this point in the history
…s platform
  • Loading branch information
Juan David Nicholls Cardona authored and Juan David Nicholls Cardona committed Jan 17, 2016
1 parent 4aaf78a commit 8fd3fa1
Showing 1 changed file with 68 additions and 14 deletions.
82 changes: 68 additions & 14 deletions src/windows/SocialSharingProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,36 @@ module.exports = {
var fileOrFileArray = args[2];
//Web link
var url = args[3];

var folder = Windows.Storage.ApplicationData.current.temporaryFolder;

var getExtension = function (strBase64) {
return strBase64.substring(strBase64.indexOf("/") + 1, strBase64.indexOf(";base64"));
};

var replaceAll = function (str, find, replace) {
return str.replace(new RegExp(find, 'g'), replace);
};

var sanitizeFilename = function (name) {
return replaceAll(name, "[:\\\\/*?|<> ]", "_");
};

var getFileName = function (position, fileExtension) {
var fileName = (subject ? sanitizeFilename(subject) : "file") + (position == 0 ? "" : "_" + position) + "." + fileExtension;
return fileName;
};

var createTemporalFile = function (fileName, buffer) {

var filePath = "";
return folder.createFileAsync(fileName, Windows.Storage.CreationCollisionOption.replaceExisting).then(function (file) {
filePath = file.path;
return Windows.Storage.FileIO.writeBufferAsync(file, buffer);
}).then(function(){
return Windows.Storage.StorageFile.getFileFromPathAsync(filePath);
});
};

var doShare = function (e) {
e.request.data.properties.title = subject?subject: "Sharing";
Expand All @@ -19,22 +49,46 @@ module.exports = {
var deferral = e.request.getDeferral();
var storageItems = [];
var filesCount = fileOrFileArray.length;

var completeFile = function () {
if (!--filesCount) {
storageItems.length && e.request.data.setStorageItems(storageItems);
deferral.complete();
}
};

for (var i = 0; i < fileOrFileArray.length; i++) {
Windows.Storage.StorageFile.getFileFromPathAsync(fileOrFileArray[i]).done(
function (file) {
storageItems.push(file);
if (!--filesCount) {
e.request.data.setStorageItems(storageItems);
deferral.complete();
}
},
function() {
if (!--filesCount) {
e.request.data.setStorageItems(storageItems);
deferral.complete();

var file = fileOrFileArray[i];
if (file.indexOf("data:") >= 0) {
var fileName = getFileName(i, getExtension(file));
var buffer = Windows.Security.Cryptography.CryptographicBuffer.decodeFromBase64String(file.split(',')[1]);
if (buffer) {
createTemporalFile(fileName, buffer).done(
function (file) {
storageItems.push(file);
completeFile();
},
function () {
completeFile();
}
);
}
else {
completeFile();
}
}
else {
Windows.Storage.StorageFile.getFileFromPathAsync(file).done(
function (file) {
storageItems.push(file);
completeFile();
},
function () {
completeFile();
}
}
);
);
}
}
}
};
Expand Down

0 comments on commit 8fd3fa1

Please sign in to comment.