-
Notifications
You must be signed in to change notification settings - Fork 68
/
google-drive.js
187 lines (172 loc) · 6.7 KB
/
google-drive.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
"use strict";
import _ from "underscore";
import * as utils from "./utils.js";
import { discFor } from "./fdc.js";
export function GoogleDriveLoader() {
const self = this;
const MIME_TYPE = "application/vnd.jsbeeb.disc-image";
const CLIENT_ID = "356883185894-bhim19837nroivv18p0j25gecora60r5.apps.googleusercontent.com";
const SCOPES = "https://www.googleapis.com/auth/drive.file";
let gapi = null;
self.initialise = function () {
return new Promise(function (resolve) {
// https://github.com/google/google-api-javascript-client/issues/319
const gapiScript = document.createElement("script");
gapiScript.src = "https://apis.google.com/js/client.js?onload=__onGapiLoad__";
window.__onGapiLoad__ = function onGapiLoad() {
gapi = window.gapi;
gapi.client.load("drive", "v2", function () {
console.log("Google Drive: available");
resolve(true);
});
};
document.body.appendChild(gapiScript);
});
};
self.authorize = function (immediate) {
return new Promise(function (resolve, reject) {
console.log("Authorizing", immediate);
gapi.auth.authorize(
{
client_id: CLIENT_ID,
scope: SCOPES,
immediate: immediate,
},
function (authResult) {
if (authResult && !authResult.error) {
console.log("Google Drive: authorized");
resolve(true);
} else if (authResult && authResult.error && !immediate) {
reject(new Error(authResult.error));
} else {
console.log("Google Drive: Need to auth");
resolve(false);
}
},
);
});
};
const boundary = "-------314159265358979323846";
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";
function listFiles() {
return new Promise(function (resolve) {
const retrievePageOfFiles = function (request, result) {
request.execute(function (resp) {
result = result.concat(resp.items);
const nextPageToken = resp.nextPageToken;
if (nextPageToken) {
request = gapi.client.drive.files.list({
pageToken: nextPageToken,
});
retrievePageOfFiles(request, result);
} else {
resolve(result);
}
});
};
retrievePageOfFiles(
gapi.client.drive.files.list({
q: "mimeType = '" + MIME_TYPE + "'",
}),
[],
);
});
}
function saveFile(name, data, idOrNone) {
const metadata = {
title: name,
parents: ["jsbeeb disc images"], // TODO: parents doesn't work; also should probably prevent overwriting this on every save
mimeType: MIME_TYPE,
};
const str = utils.uint8ArrayToString(data);
const base64Data = btoa(str);
const multipartRequestBody =
delimiter +
"Content-Type: application/json\r\n\r\n" +
JSON.stringify(metadata) +
delimiter +
"Content-Type: " +
MIME_TYPE +
"\r\n" +
"Content-Transfer-Encoding: base64\r\n" +
"\r\n" +
base64Data +
close_delim;
return gapi.client.request({
path: "/upload/drive/v2/files" + (idOrNone ? "/" + idOrNone : ""),
method: idOrNone ? "PUT" : "POST",
params: { uploadType: "multipart", newRevision: false },
headers: {
"Content-Type": 'multipart/mixed; boundary="' + boundary + '"',
},
body: multipartRequestBody,
});
}
function loadMetadata(fileId) {
return gapi.client.drive.files.get({ fileId: fileId });
}
self.create = function (fdc, name) {
console.log("Google Drive: creating disc image: '" + name + "'");
const byteSize = utils.discImageSize(name).byteSize;
const data = new Uint8Array(byteSize);
utils.setDiscName(data, name);
return saveFile(name, data).then(function (response) {
const meta = response.result;
return { fileId: meta.id, disc: makeDisc(fdc, data, meta) };
});
};
function downloadFile(file) {
if (file.downloadUrl) {
return new Promise(function (resolve, reject) {
const accessToken = gapi.auth.getToken().access_token;
const xhr = new XMLHttpRequest();
xhr.open("GET", file.downloadUrl, true);
xhr.setRequestHeader("Authorization", "Bearer " + accessToken);
xhr.overrideMimeType("text/plain; charset=x-user-defined");
xhr.onload = function () {
if (xhr.status !== 200) {
reject(new Error("Unable to load '" + file.title + "', http code " + xhr.status));
} else if (typeof xhr.response !== "string") {
resolve(xhr.response);
} else {
resolve(utils.stringToUint8Array(xhr.response));
}
};
xhr.onerror = function () {
reject(new Error("Error sending request for " + file));
};
xhr.send();
});
} else {
return Promise.resolve(null);
}
}
function makeDisc(fdc, data, meta) {
let flusher = null;
const name = meta.title;
if (meta.editable) {
console.log("Making editable disc");
flusher = _.debounce(function () {
saveFile(this.name, this.data, meta.id).then(function () {
console.log("Saved ok");
});
}, 200);
} else {
console.log("Making read-only disc");
}
return discFor(fdc, name, data, flusher);
}
self.load = function (fdc, fileId) {
let meta = false;
return loadMetadata(fileId)
.then(function (response) {
meta = response.result;
return downloadFile(response.result);
})
.then(function (data) {
return makeDisc(fdc, data, meta);
});
};
self.cat = listFiles;
}