Skip to content

Commit 4a4e5ee

Browse files
authored
Rsdev 258 reactivate google drive (#72)
Reactivate the google drive integration by replacing deprecated google apis.
1 parent ab71273 commit 4a4e5ee

File tree

4 files changed

+71
-50
lines changed

4 files changed

+71
-50
lines changed

src/main/java/com/researchspace/webapp/controller/StructuredDocumentController.java

+4
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,10 @@ public ModelAndView openDocument(
581581
model.addAttribute("inventoryAvailable", inventoryEnabled);
582582
model.addAttribute("enforce_ontologies", anyGroupEnforcesOntologies(user));
583583
model.addAttribute("allow_bioOntologies", allGroupsAllowBioOntologies(user));
584+
585+
boolean isGoogleDriveAppEnabled =
586+
integrationsHandler.getIntegration(user, "GOOGLEDRIVE").isEnabled();
587+
model.addAttribute("isGoogleDriveAppEnabled", isGoogleDriveAppEnabled);
584588
String view =
585589
msTeamsDocView
586590
? STRUCTURED_DOCUMENT_MS_TEAMS_SIMPLE_VIEW_NAME

src/main/webapp/common/header.jsp

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<%@ include file="/common/taglibs.jsp"%>
22
<meta name="google-signin-client_id" content="731959816562-p2igqsv375nta4bd0g3c3tkq5r90kpg5.apps.googleusercontent.com">
3+
<c:if test="${isGoogleDriveAppEnabled}">
4+
<script src="https://accounts.google.com/gsi/client" async defer></script>
5+
<script src="https://apis.google.com/js/api.js" async defer></script>
6+
</c:if>
7+
38
<rst:hasDeploymentProperty name="cloud" value="true">
49
<script src="<c:url value='/scripts/pages/signup/google-signin.js'/>"></script>
510
<script src="https://accounts.google.com/gsi/client?onload=onLoad" async defer></script>

src/main/webapp/scripts/externalTinymcePlugins/googledrive/plugin.min.js

+59-46
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,89 @@
1-
/**
2-
* Google Drive plugin Author: dave Date : 16/11/2014
1+
/*
2+
TinyMCE plugin for Google Drive.
3+
4+
Uses the front-end Google API Client (gapi) and Google sign-in (gsi) libraries,
5+
via scripts imported in `header.jsp`.
36
*/
47

5-
var googleDriveEditor;
6-
var googleDriveUrl;
8+
let accessToken;
79

8-
function authorizeGoogleDriveLink() {
9-
if (typeof window.gapi === 'object') {
10-
window.gapi.auth.authorize({
11-
'client_id': gdClientId,
12-
'scope': gdScope,
13-
'immediate': false
14-
}, handleGoogleDriveAuthLinkResult);
10+
async function insertFromGoogleDrive() {
11+
if(!gdClientId || !gdScope){
12+
apprise("Missing required system properties for google drive "
13+
+ "integration. Please speak to your rspace administrator");
1514
} else {
16-
apprise('Unable to contact Google Drive. Please try again later or ensure that your network connection is active and reload the page.');
15+
await gapi.load('client:picker');
16+
17+
const client = await google.accounts.oauth2.initTokenClient({
18+
client_id: gdClientId,
19+
scope: gdScope,
20+
callback: onTokenResponse,
21+
error_callback: onTokenResponseError,
22+
});
23+
24+
if(!accessToken){
25+
// show consent screen for user login and authorisation
26+
client.requestAccessToken({prompt: 'consent'});
27+
} else {
28+
// skip consent screen when user has already consented and has a token
29+
client.requestAccessToken({prompt: ''});
30+
}
1731
}
1832
}
1933

20-
function handleGoogleDriveAuthLinkResult(authResult) {
21-
if (authResult && !authResult.error) {
22-
gdOauthToken = authResult.access_token;
23-
createTinyMCEGoogleDrivePicker(googleDrivePickerLinkCallback);
24-
}
34+
function onTokenResponse(response){
35+
accessToken = response.access_token;
36+
showPicker();
2537
}
2638

27-
function createTinyMCEGoogleDrivePicker(callback) {
28-
if (gdPickerApiLoaded && gdOauthToken) {
29-
var picker = new google.picker.PickerBuilder()
30-
.addView(google.picker.ViewId.DOCS)
31-
.setOAuthToken(gdOauthToken)
32-
.setDeveloperKey(gdDeveloperKey)
33-
.setCallback(callback)
34-
.build();
35-
picker.setVisible(true);
36-
}
39+
function onTokenResponseError(response){
40+
apprise('Unable to contact Google Drive. Please try again later or '
41+
+ 'ensure that your network connection is active and reload the page.');
3742
}
3843

39-
function googleDrivePickerLinkCallback(data) {
40-
var googleDriveLinkUrl = 'nothing';
41-
if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
42-
var doc = data[google.picker.Response.DOCUMENTS][0];
43-
googleDriveLinkUrl = doc[google.picker.Document.EMBEDDABLE_URL] || doc[google.picker.Document.URL];
44+
function showPicker() {
45+
picker = new google.picker.PickerBuilder()
46+
.setDeveloperKey(gdDeveloperKey)
47+
.setAppId(gdClientId)
48+
.setOAuthToken(accessToken)
49+
.addView(google.picker.ViewId.DOCS)
50+
.setCallback(pickerCallback)
51+
.build();
52+
picker.setVisible(true);
53+
}
4454

45-
var name = doc[google.picker.Document.NAME];
55+
/*
56+
When a Google Drive file is picked, grab some metadata and embed a link in the
57+
rspace document.
58+
*/
59+
function pickerCallback(data) {
60+
if (data.action === google.picker.Action.PICKED) {
61+
const doc = data[google.picker.Response.DOCUMENTS][0];
62+
googleDriveLinkUrl = doc[google.picker.Document.EMBEDDABLE_URL] || doc[google.picker.Document.URL];
4663

47-
var extension = RS.getFileExtension(name);
48-
var iconPath = RS.getIconPathForExtension(extension);
49-
var docId = doc.id;
64+
const name = doc[google.picker.Document.NAME];
65+
const extension = RS.getFileExtension(name);
66+
const iconPath = RS.getIconPathForExtension(extension);
67+
const docId = doc.id;
5068

51-
var json = {
69+
const json = {
5270
id: 'googledrive-' + docId,
5371
fileStore: 'Google Drive',
5472
recordURL: googleDriveLinkUrl,
5573
name: name,
5674
iconPath: iconPath,
5775
badgeIconPath: '/images/icons/drive_icon.png'
5876
};
59-
6077
RS.insertTemplateIntoTinyMCE('insertedExternalDocumentTemplate', json);
6178
}
6279
}
6380

6481
tinymce.PluginManager.add('googledrive', function (editor, url) {
65-
66-
googleDriveEditor = editor;
67-
googleDriveUrl = url;
68-
69-
// Add command to open the comment dialog.htm
7082
editor.addCommand('cmdGoogleDrive', function () {
71-
authorizeGoogleDriveLink();
83+
insertFromGoogleDrive();
7284
});
7385

74-
// Add a button that opens a window
86+
// adds editor button
7587
editor.ui.registry.addButton('googledrive', {
7688
tooltip: 'Insert from Google Drive',
7789
icon: 'google_drive',
@@ -80,7 +92,7 @@ tinymce.PluginManager.add('googledrive', function (editor, url) {
8092
}
8193
});
8294

83-
// Adds a menu item to the insert menu
95+
// adds From Google Drive menu item
8496
editor.ui.registry.addMenuItem('optGoogleDrive', {
8597
text: 'From Google Drive',
8698
icon: 'google_drive',
@@ -89,6 +101,7 @@ tinymce.PluginManager.add('googledrive', function (editor, url) {
89101
}
90102
});
91103

104+
// adds menu item to Insert menu
92105
if(!window.insertActions) window.insertActions = new Map();
93106
window.insertActions.set("optGoogleDrive", {
94107
text: 'From Google Drive',

src/main/webapp/scripts/pages/workspace/mediaGalleryManager.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ var isInSearch = false; //This is used to keep track and reapply filters when so
2626
var gdDeveloperKey = '';
2727
// The Client ID obtained from the Google Developers Console. Replace with your own Client ID.
2828
var gdClientId = "";
29-
// Scope to use to access user's photos.
30-
var gdScope = ['https://www.googleapis.com/auth/drive'];
29+
// Scope to use to access user's files.
30+
var gdScope = 'https://www.googleapis.com/auth/drive'
3131
var gdPickerApiLoaded = false;
3232
var gdOauthToken;
3333

@@ -1695,8 +1695,7 @@ function appriseNoConnection(provider) {
16951695
}
16961696

16971697
function onGoogleDriveAPILoad() {
1698-
// gapi.load('auth', {'callback': onAuthApiLoad});
1699-
gapi.load('auth', {});
1698+
gapi.load('auth2', {});
17001699
gapi.load('picker', { 'callback': onGoogleDrivePickerApiLoad });
17011700
}
17021701

0 commit comments

Comments
 (0)