change Fropbox clientId

This commit is contained in:
Azgaar 2021-09-08 09:55:54 +03:00
parent 1290c0fea9
commit d19899f0fc

View file

@ -16,126 +16,124 @@ restore(): restore access tokens from storage if possible
*/ */
window.Cloud = (function () { window.Cloud = (function () {
// helpers to use in providers for token handling // helpers to use in providers for token handling
const lSKey = x => `auth-${x}` const lSKey = x => `auth-${x}`;
const setToken = (prov, key) => localStorage.setItem(lSKey(prov), key) const setToken = (prov, key) => localStorage.setItem(lSKey(prov), key);
const getToken = prov => localStorage.getItem(lSKey(prov)) const getToken = prov => localStorage.getItem(lSKey(prov));
/**********************************************************/ /**********************************************************/
/* Dropbox provider */ /* Dropbox provider */
/**********************************************************/ /**********************************************************/
const DBP = { const DBP = {
name: 'dropbox', name: "dropbox",
clientId: 'sp7tzwm27u2w5ns', clientId: "pdr9ae64ip0qno4",
authWindow: undefined, authWindow: undefined,
token: null, // Access token token: null, // Access token
api: null, api: null,
restore() { restore() {
this.token = getToken(this.name) this.token = getToken(this.name);
if (this.token) this.connect(this.token) if (this.token) this.connect(this.token);
}, },
async call(name, param) { async call(name, param) {
try { try {
return await this.api[name](param) return await this.api[name](param);
} catch (e) { } catch (e) {
if (e.name !== "DropboxResponseError") throw(e) if (e.name !== "DropboxResponseError") throw e;
// retry with auth // retry with auth
await this.auth() await this.auth();
return await this.api[name](param) return await this.api[name](param);
} }
}, },
connect(token) { connect(token) {
const clientId = this.clientId const clientId = this.clientId;
const auth = new Dropbox.DropboxAuth({ clientId }) const auth = new Dropbox.DropboxAuth({clientId});
auth.setAccessToken(token) auth.setAccessToken(token);
this.api = new Dropbox.Dropbox({ auth }) this.api = new Dropbox.Dropbox({auth});
}, },
async save(fileName, contents) { async save(fileName, contents) {
if (!this.api) await this.auth() if (!this.api) await this.auth();
const resp = this.call('filesUpload', { path: '/' + fileName, contents }) const resp = this.call("filesUpload", {path: "/" + fileName, contents});
console.log("Dropbox response:", resp) console.log("Dropbox response:", resp);
return true return true;
}, },
async load(path) { async load(path) {
if (!this.api) await this.auth() if (!this.api) await this.auth();
const resp = await this.call('filesDownload', { path }) const resp = await this.call("filesDownload", {path});
const blob = resp.result.fileBlob const blob = resp.result.fileBlob;
if (!blob) throw(new Error('Invalid response from dropbox.')) if (!blob) throw new Error("Invalid response from dropbox.");
return blob return blob;
}, },
async list() { async list() {
if (!this.api) return null if (!this.api) return null;
const resp = await this.call('filesListFolder', { path: '' }) const resp = await this.call("filesListFolder", {path: ""});
return resp.result.entries.map(e => ({ name: e.name, path: e.path_lower })) return resp.result.entries.map(e => ({name: e.name, path: e.path_lower}));
}, },
auth() { auth() {
const url = window.location.origin + window.location.pathname + 'dropbox.html' const url = window.location.origin + window.location.pathname + "dropbox.html";
this.authWindow = window.open(url, 'auth', 'width=640,height=480') this.authWindow = window.open(url, "auth", "width=640,height=480");
// child window expected to call // child window expected to call
// window.opener.Cloud.providers.dropbox.setDropBoxToken (see below) // window.opener.Cloud.providers.dropbox.setDropBoxToken (see below)
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const watchDog = () => { const watchDog = () => {
this.authWindow.close() this.authWindow.close();
reject(new Error("Timeout. No auth for dropbox.")) reject(new Error("Timeout. No auth for dropbox."));
} };
setTimeout(watchDog, 120*1000) setTimeout(watchDog, 120 * 1000);
window.addEventListener('dropboxauth', e => { window.addEventListener("dropboxauth", e => {
clearTimeout(watchDog) clearTimeout(watchDog);
resolve() resolve();
}) });
}) });
}, },
// Callback function for auth window. // Callback function for auth window.
setDropBoxToken(token) { setDropBoxToken(token) {
console.log('Access token got:', token) console.log("Access token got:", token);
setToken(this.name, token) setToken(this.name, token);
this.connect(token) this.connect(token);
this.authWindow.close() this.authWindow.close();
window.dispatchEvent(new Event('dropboxauth')) window.dispatchEvent(new Event("dropboxauth"));
}, },
async getLink(path) { async getLink(path) {
if (!this.api) await this.auth() if (!this.api) await this.auth();
let resp let resp;
// already exists? // already exists?
resp = await this.call('sharingListSharedLinks', { path }) resp = await this.call("sharingListSharedLinks", {path});
if (resp.result.links.length) if (resp.result.links.length) return resp.result.links[0].url;
return resp.result.links[0].url
// create new // create new
resp = await this.call('sharingCreateSharedLinkWithSettings', { resp = await this.call("sharingCreateSharedLinkWithSettings", {
path, path,
settings: { settings: {
require_password: false, require_password: false,
audience: 'public', audience: "public",
access: 'viewer', access: "viewer",
requested_visibility: 'public', requested_visibility: "public",
allow_download: true, allow_download: true
} }
}) });
console.log("dropbox link object:", resp.result) console.log("dropbox link object:", resp.result);
return resp.result.url return resp.result.url;
},
} }
};
// register providers here: // register providers here:
const providers = { const providers = {
dropbox: DBP, dropbox: DBP
} };
// restore all providers at startup // restore all providers at startup
for (const p of Object.values(providers)) p.restore() for (const p of Object.values(providers)) p.restore();
return { providers } return {providers};
})() })();