2018-09-25 00:08:55 +00:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import { Dropbox } from 'dropbox';
|
|
|
|
|
|
|
|
import {
|
|
|
|
getJitsiMeetGlobalNS,
|
2020-05-07 22:26:37 +00:00
|
|
|
parseStandardURIString,
|
|
|
|
parseURLParams
|
2018-09-25 00:08:55 +00:00
|
|
|
} from '../base/util';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes the oauth flow.
|
|
|
|
*
|
|
|
|
* @param {string} authUrl - The URL to oauth service.
|
|
|
|
* @returns {Promise<string>} - The URL with the authorization details.
|
|
|
|
*/
|
|
|
|
function authorize(authUrl: string): Promise<string> {
|
|
|
|
const windowName = `oauth${Date.now()}`;
|
|
|
|
const gloabalNS = getJitsiMeetGlobalNS();
|
|
|
|
|
|
|
|
gloabalNS.oauthCallbacks = gloabalNS.oauthCallbacks || {};
|
|
|
|
|
|
|
|
return new Promise(resolve => {
|
|
|
|
const popup = window.open(authUrl, windowName);
|
|
|
|
|
2019-02-27 15:29:41 +00:00
|
|
|
gloabalNS.oauthCallbacks[windowName] = url => {
|
2018-09-25 00:08:55 +00:00
|
|
|
popup.close();
|
|
|
|
delete gloabalNS.oauthCallbacks.windowName;
|
2019-02-27 15:29:41 +00:00
|
|
|
resolve(url);
|
2018-09-25 00:08:55 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Action to authorize the Jitsi Recording app in dropbox.
|
|
|
|
*
|
2018-09-26 17:57:58 +00:00
|
|
|
* @param {string} appKey - The Jitsi Recorder dropbox app key.
|
2018-09-25 00:08:55 +00:00
|
|
|
* @param {string} redirectURI - The return URL.
|
|
|
|
* @returns {Promise<string>}
|
|
|
|
*/
|
|
|
|
export function _authorizeDropbox(
|
2018-09-26 17:57:58 +00:00
|
|
|
appKey: string,
|
2018-09-25 00:08:55 +00:00
|
|
|
redirectURI: string
|
|
|
|
): Promise<string> {
|
2018-09-26 17:57:58 +00:00
|
|
|
const dropboxAPI = new Dropbox({ clientId: appKey });
|
2018-09-25 00:08:55 +00:00
|
|
|
const url = dropboxAPI.getAuthenticationUrl(redirectURI);
|
|
|
|
|
|
|
|
return authorize(url).then(returnUrl => {
|
|
|
|
const params
|
|
|
|
= parseURLParams(parseStandardURIString(returnUrl), true) || {};
|
|
|
|
|
|
|
|
return params.access_token;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:57:58 +00:00
|
|
|
/**
|
|
|
|
* Returns the display name for the current dropbox account.
|
|
|
|
*
|
|
|
|
* @param {string} token - The dropbox access token.
|
|
|
|
* @param {string} appKey - The Jitsi Recorder dropbox app key.
|
|
|
|
* @returns {Promise<string>}
|
|
|
|
*/
|
|
|
|
export function getDisplayName(token: string, appKey: string) {
|
|
|
|
const dropboxAPI = new Dropbox({
|
|
|
|
accessToken: token,
|
|
|
|
clientId: appKey
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
dropboxAPI.usersGetCurrentAccount()
|
|
|
|
.then(account => account.name.display_name));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns information about the space usage for the current dropbox account.
|
|
|
|
*
|
|
|
|
* @param {string} token - The dropbox access token.
|
|
|
|
* @param {string} appKey - The Jitsi Recorder dropbox app key.
|
|
|
|
* @returns {Promise<Object>}
|
|
|
|
*/
|
|
|
|
export function getSpaceUsage(token: string, appKey: string) {
|
|
|
|
const dropboxAPI = new Dropbox({
|
|
|
|
accessToken: token,
|
|
|
|
clientId: appKey
|
|
|
|
});
|
|
|
|
|
|
|
|
return dropboxAPI.usersGetSpaceUsage().then(space => {
|
|
|
|
const { allocation, used } = space;
|
|
|
|
const { allocated } = allocation;
|
|
|
|
|
|
|
|
return {
|
|
|
|
allocated,
|
|
|
|
used
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-25 00:08:55 +00:00
|
|
|
/**
|
|
|
|
* Returns <tt>true</tt> if the dropbox features is enabled and <tt>false</tt>
|
|
|
|
* otherwise.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The redux state.
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
export function isEnabled(state: Object) {
|
|
|
|
const { dropbox = {} } = state['features/base/config'];
|
|
|
|
|
2018-09-26 17:57:58 +00:00
|
|
|
return typeof dropbox.appKey === 'string';
|
2018-09-25 00:08:55 +00:00
|
|
|
}
|