-
Notifications
You must be signed in to change notification settings - Fork 54
/
save.js
114 lines (100 loc) · 4.16 KB
/
save.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
var clientId = '464627085914-lfj4ile380lp6nd6ikttbsgtrclq3m56.apps.googleusercontent.com';
var apiKey = 'AIzaSyBnTLTORenEukV07j7vOzen3YqQUcP3ZuU';
var scopes = 'https://www.googleapis.com/auth/drive.file';
function handleClientLoad() {
gapi.client.setApiKey(apiKey);
window.setTimeout(checkAuth, 1);
}
function checkAuth() {
gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true}, handleAuthResult);
}
function handleAuthResult(authResult) {
var authorizeButton = document.getElementById('authorize-button');
if (authResult) {
authorizeButton.style.visibility = 'hidden';
makeApiCall(authResult);
} else {
authorizeButton.style.visibility = '';
authorizeButton.onclick = handleAuthClick;
}
}
function handleAuthClick(event) {
// TODO: find out why this window doesn't close after the OAuth flow is complete
gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult);
return false;
}
function processResponse(data) {
var success = document.getElementById("success");
var url = document.getElementById("url");
success.style.display = "block";
}
function makeApiCall(authResult) {
gapi.client.load('drive', 'v1', function() {
if(window.webkitIntent) {
var data = window.webkitIntent.data;
if(data.constructor.name == "Blob") {
insertFileData(data, authResult, processResponse);
}
else if(typeof(data) == "string" ) {
if(data.indexOf("http") == 0) {
// Data is at a URL so fetch it.
loadFile(data, function(fileData) {
insertFileData(fileData, authResult, processResponse);
}, true);
}
else {
var saveImg = document.getElementById("save-img");
saveImg.src = window.webkitIntent.data;
var meta = {
'title': "Test Image " + (new Date()).toJSON(),
'mimeType': window.webkitIntent.type
};
insertBase64Data(data.replace(/data:image\/([^;]*);base64,/,""), window.webkitIntent.type, meta, authResult, processResponse);
}
}
}
});
}
const boundary = '-------314159265358979323846';
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";
function insertBase64Data(data, contentType, metadata, authRequest, callback) {
var multipartRequestBody =
delimiter +
'Content-Type: application/json\r\n\r\n' +
JSON.stringify(metadata) +
delimiter +
'Content-Type: ' + contentType + '\r\n' +
'Content-Transfer-Encoding: base64\r\n' +
'\r\n' +
data +
close_delim;
var request = gapi.client.request({
'path': '/upload/drive/v1/files',
'method': 'POST',
'params': {'uploadType': 'multipart', "access_token": authRequest.access_token },
'headers': {
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
},
'body': multipartRequestBody});
if (!callback) {
callback = function(file) {
console.log(file.id);
};
}
request.execute(callback);
}
function insertFileData(fileData, authRequest, callback) {
var reader = new FileReader();
reader.readAsBinaryString(fileData);
reader.onload = function(e) {
// TODO: how to set the correct values to these fields?
var contentType = fileData.type || 'application/octet-stream';
var metadata = {
'title': fileData.fileName || "Test Image " + (new Date()).toJSON(),
'mimeType': contentType
};
var base64Data = btoa(reader.result);
insertBase64Data(base64Data, contentType, metadata, authRequest, callback);
}
}