gdrive.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * Copyright 2010-2019 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. this.GDrive = this.GDrive || (() => {
  25. "use strict";
  26. const TOKEN_URL = "https://oauth2.googleapis.com/token";
  27. const AUTH_URL = "https://accounts.google.com/o/oauth2/v2/auth";
  28. const REVOKE_ACCESS_URL = "https://accounts.google.com/o/oauth2/revoke";
  29. const GDRIVE_URL = "https://www.googleapis.com/drive/v3/files";
  30. const GDRIVE_UPLOAD_URL = "https://www.googleapis.com/upload/drive/v3/files";
  31. class GDrive {
  32. constructor(clientId, scopes) {
  33. this.clientId = clientId;
  34. this.scopes = scopes;
  35. this.folderIds = new Map();
  36. setInterval(() => this.folderIds.clear(), 60 * 1000);
  37. }
  38. async auth(options = {}) {
  39. try {
  40. await browser.permissions.request({ permissions: ["identity"] });
  41. }
  42. catch (error) {
  43. // ignored;
  44. }
  45. if (this.managedToken()) {
  46. const token = await browser.identity.getAuthToken({ interactive: options.interactive });
  47. if (token) {
  48. this.accessToken = token;
  49. return { accessToken: this.accessToken };
  50. }
  51. } else {
  52. this.getAuthURL(options);
  53. return options.code ? authFromCode(this, options) : initAuth(this, options);
  54. }
  55. }
  56. managedToken() {
  57. return Boolean(browser.identity.getAuthToken);
  58. }
  59. setAuthInfo(authInfo) {
  60. if (!browser.identity.getAuthToken) {
  61. this.accessToken = authInfo.accessToken;
  62. this.refreshToken = authInfo.refreshToken;
  63. this.expirationDate = authInfo.expirationDate;
  64. }
  65. }
  66. getAuthURL(options = {}) {
  67. this.redirectURI = encodeURIComponent("urn:ietf:wg:oauth:2.0:oob" + (options.auto ? ":auto" : ""));
  68. this.authURL = AUTH_URL +
  69. "?client_id=" + this.clientId +
  70. "&response_type=code" +
  71. "&access_type=offline" +
  72. "&redirect_uri=" + this.redirectURI +
  73. "&scope=" + this.scopes.join(" ");
  74. return this.authURL;
  75. }
  76. async refreshAuthToken() {
  77. if (this.clientId && this.refreshToken) {
  78. const httpResponse = await fetch(TOKEN_URL, {
  79. method: "POST",
  80. headers: { "Content-Type": "application/x-www-form-urlencoded" },
  81. body: "client_id=" + this.clientId +
  82. "&refresh_token=" + this.refreshToken +
  83. "&grant_type=refresh_token"
  84. });
  85. const response = await getJSON(httpResponse);
  86. this.accessToken = response.access_token;
  87. this.refreshToken = response.refresh_token;
  88. this.expirationDate = Date.now() + (response.expires_in * 1000);
  89. return { accessToken: this.accessToken, refreshToken: this.refreshToken, expirationDate: this.expirationDate };
  90. }
  91. }
  92. async revokeAuthToken(accessToken) {
  93. if (accessToken) {
  94. if (this.managedToken()) {
  95. await browser.identity.removeCachedAuthToken({ token: accessToken });
  96. }
  97. const httpResponse = await fetch(REVOKE_ACCESS_URL, {
  98. method: "POST",
  99. headers: { "Content-Type": "application/x-www-form-urlencoded" },
  100. body: "token=" + accessToken
  101. });
  102. try {
  103. await getJSON(httpResponse);
  104. }
  105. catch (error) {
  106. if (error.message != "invalid_token") {
  107. throw error;
  108. }
  109. }
  110. finally {
  111. delete this.accessToken;
  112. delete this.refreshToken;
  113. delete this.expirationDate;
  114. }
  115. }
  116. }
  117. async upload(fullFilename, blob, retry = true) {
  118. const parentFolderId = await getParentFolderId(this, fullFilename);
  119. const fileParts = fullFilename.split("/");
  120. const filename = fileParts.pop();
  121. const uploader = new MediaUploader({
  122. token: this.accessToken,
  123. file: blob,
  124. parents: [parentFolderId],
  125. filename
  126. });
  127. try {
  128. return await uploader.upload();
  129. }
  130. catch (error) {
  131. if (error.message == "path_not_found" && retry) {
  132. this.folderIds.clear();
  133. return this.upload(fullFilename, blob, false);
  134. } else {
  135. throw error;
  136. }
  137. }
  138. }
  139. }
  140. class MediaUploader {
  141. constructor(options) {
  142. this.file = options.file;
  143. this.contentType = this.file.type || "application/octet-stream";
  144. this.metadata = {
  145. name: options.filename,
  146. mimeType: this.contentType,
  147. parents: options.parents || ["root"]
  148. };
  149. this.token = options.token;
  150. this.offset = 0;
  151. this.chunkSize = options.chunkSize || 5 * 1024 * 1024;
  152. }
  153. async upload() {
  154. const httpResponse = getResponse(await fetch(GDRIVE_UPLOAD_URL + "?uploadType=resumable", {
  155. method: "POST",
  156. headers: {
  157. "Authorization": "Bearer " + this.token,
  158. "Content-Type": "application/json",
  159. "X-Upload-Content-Length": this.file.size,
  160. "X-Upload-Content-Type": this.contentType
  161. },
  162. body: JSON.stringify(this.metadata)
  163. }));
  164. const location = httpResponse.headers.get("Location");
  165. this.url = location;
  166. return sendFile(this);
  167. }
  168. }
  169. return GDrive;
  170. async function authFromCode(gdrive, options) {
  171. const httpResponse = await fetch(TOKEN_URL, {
  172. method: "POST",
  173. headers: { "Content-Type": "application/x-www-form-urlencoded" },
  174. body: "client_id=" + gdrive.clientId +
  175. "&grant_type=authorization_code" +
  176. "&code=" + options.code +
  177. "&redirect_uri=" + gdrive.redirectURI
  178. });
  179. const response = await getJSON(httpResponse);
  180. gdrive.accessToken = response.access_token;
  181. gdrive.refreshToken = response.refresh_token;
  182. gdrive.expirationDate = Date.now() + (response.expires_in * 1000);
  183. return { accessToken: gdrive.accessToken, refreshToken: gdrive.refreshToken, expirationDate: gdrive.expirationDate };
  184. }
  185. async function initAuth(gdrive, options) {
  186. try {
  187. return browser.identity.launchWebAuthFlow({
  188. interactive: options.interactive,
  189. url: gdrive.authURL
  190. });
  191. }
  192. catch (error) {
  193. if (error.message && error.message.includes("access")) {
  194. throw new Error("code_required");
  195. } else {
  196. throw error;
  197. }
  198. }
  199. }
  200. async function getParentFolderId(gdrive, filename, retry = true) {
  201. const fileParts = filename.split("/");
  202. fileParts.pop();
  203. const folderId = gdrive.folderIds.get(fileParts.join("/"));
  204. if (folderId) {
  205. return folderId;
  206. }
  207. let parentFolderId = "root";
  208. if (fileParts.length) {
  209. let fullFolderName = "";
  210. for (const folderName of fileParts) {
  211. if (fullFolderName) {
  212. fullFolderName += "/";
  213. }
  214. fullFolderName += folderName;
  215. const folderId = gdrive.folderIds.get(fullFolderName);
  216. if (folderId) {
  217. parentFolderId = folderId;
  218. } else {
  219. try {
  220. parentFolderId = await getOrCreateFolder(gdrive, folderName, parentFolderId);
  221. gdrive.folderIds.set(fullFolderName, parentFolderId);
  222. } catch (error) {
  223. if (error.message == "path_not_found" && retry) {
  224. gdrive.folderIds.clear();
  225. return getParentFolderId(gdrive, filename, false);
  226. } else {
  227. throw error;
  228. }
  229. }
  230. }
  231. }
  232. }
  233. return parentFolderId;
  234. }
  235. async function getOrCreateFolder(gdrive, folderName, parentFolderId) {
  236. const response = await getFolder(gdrive, folderName, parentFolderId);
  237. if (response.files.length) {
  238. return response.files[0].id;
  239. } else {
  240. const response = await createFolder(gdrive, folderName, parentFolderId);
  241. return response.id;
  242. }
  243. }
  244. async function getFolder(gdrive, folderName, parentFolderId) {
  245. const httpResponse = await fetch(GDRIVE_URL + "?q=mimeType = 'application/vnd.google-apps.folder' and name = '" + folderName + "' and trashed != true and '" + parentFolderId + "' in parents", {
  246. headers: {
  247. "Authorization": "Bearer " + gdrive.accessToken
  248. }
  249. });
  250. return getJSON(httpResponse);
  251. }
  252. async function createFolder(gdrive, folderName, parentFolderId) {
  253. const httpResponse = await fetch(GDRIVE_URL, {
  254. method: "POST",
  255. headers: {
  256. "Authorization": "Bearer " + gdrive.accessToken,
  257. "Content-Type": "application/json"
  258. },
  259. body: JSON.stringify({
  260. name: folderName,
  261. parents: [parentFolderId],
  262. mimeType: "application/vnd.google-apps.folder"
  263. })
  264. });
  265. return getJSON(httpResponse);
  266. }
  267. async function sendFile(mediaUploader) {
  268. let content = mediaUploader.file, end = mediaUploader.file.size;
  269. if (mediaUploader.offset || mediaUploader.chunkSize) {
  270. if (mediaUploader.chunkSize) {
  271. end = Math.min(mediaUploader.offset + mediaUploader.chunkSize, mediaUploader.file.size);
  272. }
  273. content = content.slice(mediaUploader.offset, end);
  274. }
  275. const httpResponse = await fetch(mediaUploader.url, {
  276. method: "PUT",
  277. headers: {
  278. "Authorization": "Bearer " + mediaUploader.token,
  279. "Content-Type": mediaUploader.contentType,
  280. "Content-Range": "bytes " + mediaUploader.offset + "-" + (end - 1) + "/" + mediaUploader.file.size,
  281. "X-Upload-Content-Type": mediaUploader.contentType
  282. },
  283. body: content
  284. });
  285. if (httpResponse.status == 200 || httpResponse.status == 201) {
  286. return httpResponse.json();
  287. } else if (httpResponse.status == 308) {
  288. const range = httpResponse.headers.get("Range");
  289. if (range) {
  290. mediaUploader.offset = parseInt(range.match(/\d+/g).pop(), 10) + 1;
  291. }
  292. sendFile(mediaUploader);
  293. } else {
  294. getResponse(httpResponse);
  295. }
  296. }
  297. async function getJSON(httpResponse) {
  298. httpResponse = getResponse(httpResponse);
  299. const response = await httpResponse.json();
  300. if (response.error) {
  301. throw new Error(response.error);
  302. } else {
  303. return response;
  304. }
  305. }
  306. function getResponse(httpResponse) {
  307. if (httpResponse.status == 200) {
  308. return httpResponse;
  309. } else if (httpResponse.status == 404) {
  310. throw new Error("path_not_found");
  311. } else if (httpResponse.status == 401) {
  312. throw new Error("invalid_token");
  313. } else {
  314. throw new Error("unknown_error (" + httpResponse.status + ")");
  315. }
  316. }
  317. })();