Google Drive (second look)

This commit is contained in:
RainLoop Team 2014-07-29 14:28:02 +04:00
parent eea55d4e0a
commit 7107104e1a
18 changed files with 507 additions and 233 deletions

View file

@ -347,7 +347,7 @@ function PopupsComposeViewModel()
return this.dropboxEnabled();
});
this.driveEnabled = ko.observable(!!RL.settingsGet('GoogleApiKey') && !!RL.settingsGet('GoogleClientID'));
this.driveEnabled = ko.observable(false && Globals.bXMLHttpRequestSupported && !!RL.settingsGet('GoogleClientID'));
this.driveVisible = ko.observable(false);
this.driveCommand = Utils.createCommand(this, function () {
@ -992,13 +992,40 @@ PopupsComposeViewModel.prototype.onBuild = function ()
}
};
PopupsComposeViewModel.prototype.driveCallback = function (oData)
PopupsComposeViewModel.prototype.driveCallback = function (sAccessToken, oData)
{
if (oData && window.google && oData[window.google.picker.Response.ACTION] === window.google.picker.Action.PICKED &&
oData[window.google.picker.Response.DOCUMENTS] && oData[window.google.picker.Response.DOCUMENTS][0] &&
oData[window.google.picker.Response.DOCUMENTS][0]['url'])
oData[window.google.picker.Response.DOCUMENTS][0]['id'])
{
this.addDriveAttachment(oData[window.google.picker.Response.DOCUMENTS][0]);
var
self = this,
oRequest = new window.XMLHttpRequest()
;
oRequest.open('GET', 'https://www.googleapis.com/drive/v2/files/' + oData[window.google.picker.Response.DOCUMENTS][0]['id']);
oRequest.setRequestHeader('Authorization', 'Bearer ' + sAccessToken);
oRequest.addEventListener('load', function() {
if (oRequest && oRequest.responseText)
{
var oItem = JSON.parse(oRequest.responseText);
if (oItem && oItem['downloadUrl'])
{
window.console.log(oItem['downloadUrl']);
var oSubRequest = new window.XMLHttpRequest();
oSubRequest.open('GET', oItem['downloadUrl']);
oSubRequest.setRequestHeader('Authorization', 'Bearer ' + sAccessToken);
oSubRequest.addEventListener('load', function() {
window.console.log(oSubRequest);
});
oSubRequest.send();
// self.addDriveAttachment(oItem, sAccessToken);
}
}
});
oRequest.send();
}
};
@ -1018,9 +1045,8 @@ PopupsComposeViewModel.prototype.driveCreatePiker = function (oOauthToken)
.setIncludeFolders(true)
)
.setAppId(RL.settingsGet('GoogleClientID'))
.setDeveloperKey(RL.settingsGet('GoogleApiKey'))
.setOAuthToken(oOauthToken.access_token)
.setCallback(_.bind(self.driveCallback, self))
.setCallback(_.bind(self.driveCallback, self, oOauthToken.access_token))
.enableFeature(window.google.picker.Feature.NAV_HIDDEN)
.build()
;
@ -1044,7 +1070,9 @@ PopupsComposeViewModel.prototype.driveOpenPopup = function ()
{
window.gapi.auth.authorize({
'client_id': RL.settingsGet('GoogleClientID'),
'scope': 'https://www.googleapis.com/auth/drive.readonly',
'scope': [
'https://www.googleapis.com/auth/drive.readonly'
].join(' '),
'immediate': false
}, function (oAuthResult) {
if (oAuthResult && !oAuthResult.error)
@ -1390,12 +1418,64 @@ PopupsComposeViewModel.prototype.addDropboxAttachment = function (oDropboxFile)
/**
* @param {Object} oDriveFile
* @param {string} sAccessToken
* @return {boolean}
*/
PopupsComposeViewModel.prototype.addDriveAttachment = function (oDriveFile)
PopupsComposeViewModel.prototype.addDriveAttachment = function (oDriveFile, sAccessToken)
{
window.console.log(oDriveFile);
return false;
var
self = this,
fCancelFunc = function (sId) {
return function () {
self.attachments.remove(function (oItem) {
return oItem && oItem.id === sId;
});
};
},
iAttachmentSizeLimit = Utils.pInt(RL.settingsGet('AttachmentLimit')),
oAttachment = null,
mSize = Utils.pInt(oDriveFile['fileSize'])
;
oAttachment = new ComposeAttachmentModel(
oDriveFile['downloadUrl'], oDriveFile['originalFilename'], mSize
);
oAttachment.fromMessage = false;
oAttachment.cancel = fCancelFunc(oDriveFile['downloadUrl']);
oAttachment.waiting(false).uploading(true);
this.attachments.push(oAttachment);
if (0 < mSize && 0 < iAttachmentSizeLimit && iAttachmentSizeLimit < mSize)
{
oAttachment.uploading(false);
oAttachment.error(Utils.i18n('UPLOAD/ERROR_FILE_IS_TOO_BIG'));
return false;
}
RL.remote().composeUploadDrive(function (sResult, oData) {
var bResult = false;
oAttachment.uploading(false);
if (Enums.StorageResultType.Success === sResult && oData && oData.Result)
{
if (oData.Result[oAttachment.id])
{
bResult = true;
oAttachment.tempName(oData.Result[oAttachment.id]);
}
}
if (!bResult)
{
oAttachment.error(Utils.getUploadErrorDescByCode(Enums.UploadErrorCode.FileNoUploaded));
}
}, oDriveFile['downloadUrl'], sAccessToken);
return true;
};
/**