From 8fd3fa1fd34ec154719d048496bb51e4fb46bc7f Mon Sep 17 00:00:00 2001 From: Juan David Nicholls Cardona Date: Sun, 17 Jan 2016 14:33:15 -0500 Subject: [PATCH] Attach base64 files from window.plugins.socialsharing.share in Windows platform --- src/windows/SocialSharingProxy.js | 82 +++++++++++++++++++++++++------ 1 file changed, 68 insertions(+), 14 deletions(-) diff --git a/src/windows/SocialSharingProxy.js b/src/windows/SocialSharingProxy.js index b5b6393d..ff257d52 100644 --- a/src/windows/SocialSharingProxy.js +++ b/src/windows/SocialSharingProxy.js @@ -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"; @@ -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(); } - } - ); + ); + } } } };