summaryrefslogtreecommitdiff
path: root/scripts/audio_tuning/frontend/google_drive_picker.js
blob: 1a375debb06ac577a5220d5f629fcddb00cb9ed6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* Copyright 2015 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/* Client id obtained from API console credential page should be set before
 * running this script. */
if (!CLIENT_ID) {
  console.error.log('CLIENT_ID is not set');
}

/* Initialize a Google Drive picker after Google client API is loaded. */
function onGoogleClientApiLoad() {
  var picker = new Picker({
      clientId: CLIENT_ID,
      button: document.getElementById('google_drive_pick_file')
  });
}

/* Initializes a Google Drive picker and loads drive API and picker API.*/
var Picker = function(options) {
  this.clientId = options.clientId;
  this.button = options.button;
  this.button.addEventListener('click', this.open.bind(this));

  gapi.client.load('drive', 'v2');
  gapi.load('picker', {callback: this.onPickerApiLoaded.bind(this) });
}

/* Enable the button after picker API is loaded. */
Picker.prototype.onPickerApiLoaded = function() {
  this.button.disabled = false;
};

/* Let user authenticate and show file picker. */
Picker.prototype.open = function(){
  if (gapi.auth.getToken()) {
    this.showFilePicker();
  } else {
    this.authenticate(this.showFilePicker.bind(this));
  }
};

/* Run authenticate using auth API. */
Picker.prototype.authenticate = function(callback) {
  gapi.auth.authorize(
      {
          client_id: this.clientId + '.apps.googleusercontent.com',
          scope: 'https://www.googleapis.com/auth/drive.readonly',
          immediate: false
      },
      callback);
};

/* Create a picker using picker API. */
Picker.prototype.showFilePicker = function() {
  var accessToken = gapi.auth.getToken().access_token;
  this.picker = new google.picker.PickerBuilder().
                addView(google.picker.ViewId.DOCS).
                setAppId(this.clientId).
                setOAuthToken(accessToken).
                setCallback(this.onFilePicked.bind(this)).
                build().
                setVisible(true);
};

/* Request the file using drive API once a file is picked. */
Picker.prototype.onFilePicked = function(data) {
  if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
    var file = data[google.picker.Response.DOCUMENTS][0];
    var id = file[google.picker.Document.ID];
    var request = gapi.client.drive.files.get({fileId: id});
    request.execute(this.onFileGet.bind(this));
  }
};

/* Retrieve the file URL and access token and set it as audio source. */
Picker.prototype.onFileGet = function(file) {
  var accessToken = gapi.auth.getToken().access_token;
  var authedUrl = file.downloadUrl + "&access_token="
      + encodeURIComponent(accessToken);
  audio_source_set(authedUrl);
};