1
0

gdrive.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * Copyright 2010-2020 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * The code in this file is free software: you can redistribute it and/or
  8. * modify it under the terms of the GNU Affero General Public License
  9. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  10. * of the License, or (at your option) any later version.
  11. *
  12. * The code in this file is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  15. * General Public License for more details.
  16. *
  17. * As additional permission under GNU AGPL version 3 section 7, you may
  18. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  19. * AGPL normally required by section 4, provided you include this license
  20. * notice and a URL through which recipients can access the Corresponding
  21. * Source.
  22. */
  23. /* global browser, fetch, setInterval */
  24. const TOKEN_URL = "https://oauth2.googleapis.com/token";
  25. const AUTH_URL = "https://accounts.google.com/o/oauth2/v2/auth";
  26. const REVOKE_ACCESS_URL = "https://accounts.google.com/o/oauth2/revoke";
  27. const GDRIVE_URL = "https://www.googleapis.com/drive/v3/files";
  28. const GDRIVE_UPLOAD_URL = "https://www.googleapis.com/upload/drive/v3/files";
  29. let requestPermissionIdentityNeeded = true;
  30. class GDrive {
  31. constructor(clientId, scopes) {
  32. this.clientId = clientId;
  33. this.scopes = scopes;
  34. this.folderIds = new Map();
  35. setInterval(() => this.folderIds.clear(), 60 * 1000);
  36. }
  37. async auth(options = { interactive: true, auto: true }) {
  38. if (options.requestPermissionIdentity && requestPermissionIdentityNeeded) {
  39. try {
  40. await browser.permissions.request({ permissions: ["identity"] });
  41. requestPermissionIdentityNeeded = false;
  42. }
  43. catch (error) {
  44. // ignored;
  45. }
  46. }
  47. if (nativeAuth(options)) {
  48. this.accessToken = await browser.identity.getAuthToken({ interactive: options.interactive });
  49. return { revokableAccessToken: this.accessToken };
  50. } else {
  51. getAuthURL(this, options);
  52. return options.code ? authFromCode(this, options) : initAuth(this, options);
  53. }
  54. }
  55. setAuthInfo(authInfo, options) {
  56. if (!nativeAuth(options)) {
  57. if (authInfo) {
  58. this.accessToken = authInfo.accessToken;
  59. this.refreshToken = authInfo.refreshToken;
  60. this.expirationDate = authInfo.expirationDate;
  61. } else {
  62. delete this.accessToken;
  63. delete this.refreshToken;
  64. delete this.expirationDate;
  65. }
  66. }
  67. }
  68. async refreshAuthToken() {
  69. if (this.refreshToken) {
  70. const httpResponse = await fetch(TOKEN_URL, {
  71. method: "POST",
  72. headers: { "Content-Type": "application/x-www-form-urlencoded" },
  73. body: "client_id=" + this.clientId +
  74. "&refresh_token=" + this.refreshToken +
  75. "&grant_type=refresh_token"
  76. });
  77. if (httpResponse.status == 400) {
  78. throw new Error("unknown_token");
  79. }
  80. const response = await getJSON(httpResponse);
  81. this.accessToken = response.access_token;
  82. if (response.refresh_token) {
  83. this.refreshToken = response.refresh_token;
  84. }
  85. if (response.expires_in) {
  86. this.expirationDate = Date.now() + (response.expires_in * 1000);
  87. }
  88. return { accessToken: this.accessToken, refreshToken: this.refreshToken, expirationDate: this.expirationDate };
  89. }
  90. }
  91. async revokeAuthToken(accessToken) {
  92. if (accessToken) {
  93. if (browser.identity && browser.identity.removeCachedAuthToken) {
  94. try {
  95. await browser.identity.removeCachedAuthToken({ token: accessToken });
  96. } catch (error) {
  97. // ignored
  98. }
  99. }
  100. const httpResponse = await fetch(REVOKE_ACCESS_URL, {
  101. method: "POST",
  102. headers: { "Content-Type": "application/x-www-form-urlencoded" },
  103. body: "token=" + accessToken
  104. });
  105. try {
  106. await getJSON(httpResponse);
  107. }
  108. catch (error) {
  109. if (error.message != "invalid_token") {
  110. throw error;
  111. }
  112. }
  113. finally {
  114. delete this.accessToken;
  115. delete this.refreshToken;
  116. delete this.expirationDate;
  117. }
  118. }
  119. }
  120. async upload(fullFilename, blob, options, retry = true) {
  121. const parentFolderId = await getParentFolderId(this, fullFilename);
  122. const fileParts = fullFilename.split("/");
  123. const filename = fileParts.pop();
  124. const uploader = new MediaUploader({
  125. token: this.accessToken,
  126. file: blob,
  127. parents: [parentFolderId],
  128. filename,
  129. onProgress: options.onProgress
  130. });
  131. try {
  132. return {
  133. cancelUpload: () => uploader.cancelled = true,
  134. uploadPromise: uploader.upload()
  135. };
  136. }
  137. catch (error) {
  138. if (error.message == "path_not_found" && retry) {
  139. this.folderIds.clear();
  140. return this.upload(fullFilename, blob, options, false);
  141. } else {
  142. throw error;
  143. }
  144. }
  145. }
  146. }
  147. class MediaUploader {
  148. constructor(options) {
  149. this.file = options.file;
  150. this.onProgress = options.onProgress;
  151. this.contentType = this.file.type || "application/octet-stream";
  152. this.metadata = {
  153. name: options.filename,
  154. mimeType: this.contentType,
  155. parents: options.parents || ["root"]
  156. };
  157. this.token = options.token;
  158. this.offset = 0;
  159. this.chunkSize = options.chunkSize || 512 * 1024;
  160. }
  161. async upload() {
  162. const httpResponse = getResponse(await fetch(GDRIVE_UPLOAD_URL + "?uploadType=resumable", {
  163. method: "POST",
  164. headers: {
  165. "Authorization": "Bearer " + this.token,
  166. "Content-Type": "application/json",
  167. "X-Upload-Content-Length": this.file.size,
  168. "X-Upload-Content-Type": this.contentType
  169. },
  170. body: JSON.stringify(this.metadata)
  171. }));
  172. const location = httpResponse.headers.get("Location");
  173. this.url = location;
  174. if (!this.cancelled) {
  175. if (this.onProgress) {
  176. this.onProgress(0, this.file.size);
  177. }
  178. return sendFile(this);
  179. }
  180. }
  181. }
  182. export { GDrive };
  183. async function authFromCode(gdrive, options) {
  184. const httpResponse = await fetch(TOKEN_URL, {
  185. method: "POST",
  186. headers: { "Content-Type": "application/x-www-form-urlencoded" },
  187. body: "client_id=" + gdrive.clientId +
  188. "&grant_type=authorization_code" +
  189. "&code=" + options.code +
  190. "&redirect_uri=" + gdrive.redirectURI
  191. });
  192. const response = await getJSON(httpResponse);
  193. gdrive.accessToken = response.access_token;
  194. gdrive.refreshToken = response.refresh_token;
  195. gdrive.expirationDate = Date.now() + (response.expires_in * 1000);
  196. return { accessToken: gdrive.accessToken, refreshToken: gdrive.refreshToken, expirationDate: gdrive.expirationDate };
  197. }
  198. async function initAuth(gdrive, options) {
  199. let code;
  200. if (options.extractAuthCode) {
  201. options.extractAuthCode(getAuthURL(gdrive, options))
  202. .then(authCode => code = authCode)
  203. .catch(() => { /* ignored */ });
  204. }
  205. try {
  206. if (browser.identity && browser.identity.launchWebAuthFlow && !options.forceWebAuthFlow) {
  207. return await browser.identity.launchWebAuthFlow({
  208. interactive: options.interactive,
  209. url: gdrive.authURL
  210. });
  211. } else if (options.launchWebAuthFlow) {
  212. return await options.launchWebAuthFlow({ url: gdrive.authURL });
  213. } else {
  214. throw new Error("auth_not_supported");
  215. }
  216. }
  217. catch (error) {
  218. if (error.message && (error.message == "code_required" || error.message.includes("access"))) {
  219. if (!options.auto && !code && options.promptAuthCode) {
  220. code = await options.promptAuthCode();
  221. }
  222. if (code) {
  223. options.code = code;
  224. return await authFromCode(gdrive, options);
  225. } else {
  226. throw new Error("code_required");
  227. }
  228. } else {
  229. throw error;
  230. }
  231. }
  232. }
  233. function getAuthURL(gdrive, options = {}) {
  234. gdrive.redirectURI = encodeURIComponent("urn:ietf:wg:oauth:2.0:oob" + (options.auto ? ":auto" : ""));
  235. gdrive.authURL = AUTH_URL +
  236. "?client_id=" + gdrive.clientId +
  237. "&response_type=code" +
  238. "&access_type=offline" +
  239. "&redirect_uri=" + gdrive.redirectURI +
  240. "&scope=" + gdrive.scopes.join(" ");
  241. return gdrive.authURL;
  242. }
  243. function nativeAuth(options = {}) {
  244. return Boolean(browser.identity && browser.identity.getAuthToken) && !options.forceWebAuthFlow;
  245. }
  246. async function getParentFolderId(gdrive, filename, retry = true) {
  247. const fileParts = filename.split("/");
  248. fileParts.pop();
  249. const folderId = gdrive.folderIds.get(fileParts.join("/"));
  250. if (folderId) {
  251. return folderId;
  252. }
  253. let parentFolderId = "root";
  254. if (fileParts.length) {
  255. let fullFolderName = "";
  256. for (const folderName of fileParts) {
  257. if (fullFolderName) {
  258. fullFolderName += "/";
  259. }
  260. fullFolderName += folderName;
  261. const folderId = gdrive.folderIds.get(fullFolderName);
  262. if (folderId) {
  263. parentFolderId = folderId;
  264. } else {
  265. try {
  266. parentFolderId = await getOrCreateFolder(gdrive, folderName, parentFolderId);
  267. gdrive.folderIds.set(fullFolderName, parentFolderId);
  268. } catch (error) {
  269. if (error.message == "path_not_found" && retry) {
  270. gdrive.folderIds.clear();
  271. return getParentFolderId(gdrive, filename, false);
  272. } else {
  273. throw error;
  274. }
  275. }
  276. }
  277. }
  278. }
  279. return parentFolderId;
  280. }
  281. async function getOrCreateFolder(gdrive, folderName, parentFolderId) {
  282. const response = await getFolder(gdrive, folderName, parentFolderId);
  283. if (response.files.length) {
  284. return response.files[0].id;
  285. } else {
  286. const response = await createFolder(gdrive, folderName, parentFolderId);
  287. return response.id;
  288. }
  289. }
  290. async function getFolder(gdrive, folderName, parentFolderId) {
  291. const httpResponse = await fetch(GDRIVE_URL + "?q=mimeType = 'application/vnd.google-apps.folder' and name = '" + folderName + "' and trashed != true and '" + parentFolderId + "' in parents", {
  292. headers: {
  293. "Authorization": "Bearer " + gdrive.accessToken
  294. }
  295. });
  296. return getJSON(httpResponse);
  297. }
  298. async function createFolder(gdrive, folderName, parentFolderId) {
  299. const httpResponse = await fetch(GDRIVE_URL, {
  300. method: "POST",
  301. headers: {
  302. "Authorization": "Bearer " + gdrive.accessToken,
  303. "Content-Type": "application/json"
  304. },
  305. body: JSON.stringify({
  306. name: folderName,
  307. parents: [parentFolderId],
  308. mimeType: "application/vnd.google-apps.folder"
  309. })
  310. });
  311. return getJSON(httpResponse);
  312. }
  313. async function sendFile(mediaUploader) {
  314. let content = mediaUploader.file, end = mediaUploader.file.size;
  315. if (mediaUploader.offset || mediaUploader.chunkSize) {
  316. if (mediaUploader.chunkSize) {
  317. end = Math.min(mediaUploader.offset + mediaUploader.chunkSize, mediaUploader.file.size);
  318. }
  319. content = content.slice(mediaUploader.offset, end);
  320. }
  321. const httpResponse = await fetch(mediaUploader.url, {
  322. method: "PUT",
  323. headers: {
  324. "Authorization": "Bearer " + mediaUploader.token,
  325. "Content-Type": mediaUploader.contentType,
  326. "Content-Range": "bytes " + mediaUploader.offset + "-" + (end - 1) + "/" + mediaUploader.file.size,
  327. "X-Upload-Content-Type": mediaUploader.contentType
  328. },
  329. body: content
  330. });
  331. if (mediaUploader.onProgress && !mediaUploader.cancelled) {
  332. mediaUploader.onProgress(mediaUploader.offset + mediaUploader.chunkSize, mediaUploader.file.size);
  333. }
  334. if (httpResponse.status == 200 || httpResponse.status == 201) {
  335. return httpResponse.json();
  336. } else if (httpResponse.status == 308) {
  337. const range = httpResponse.headers.get("Range");
  338. if (range) {
  339. mediaUploader.offset = parseInt(range.match(/\d+/g).pop(), 10) + 1;
  340. }
  341. if (mediaUploader.cancelled) {
  342. throw new Error("upload_cancelled");
  343. } else {
  344. return sendFile(mediaUploader);
  345. }
  346. } else {
  347. getResponse(httpResponse);
  348. }
  349. }
  350. async function getJSON(httpResponse) {
  351. httpResponse = getResponse(httpResponse);
  352. const response = await httpResponse.json();
  353. if (response.error) {
  354. throw new Error(response.error);
  355. } else {
  356. return response;
  357. }
  358. }
  359. function getResponse(httpResponse) {
  360. if (httpResponse.status == 200) {
  361. return httpResponse;
  362. } else if (httpResponse.status == 404) {
  363. throw new Error("path_not_found");
  364. } else if (httpResponse.status == 401) {
  365. throw new Error("invalid_token");
  366. } else {
  367. throw new Error("unknown_error (" + httpResponse.status + ")");
  368. }
  369. }