Sha256: 12226e599442d3adc36eaae96e8508ded122b1be7bdf2763b919ee743e0fca15

Contents?: true

Size: 1.99 KB

Versions: 1

Compression:

Stored size: 1.99 KB

Contents

const developerKey = 'AIzaSyDr30lsukCywJXYYPSn5prs6FwvmgqQkxg'
const clientId =
  '762288849553-blsuhbaecksbh4tsi5blkhvp2hq9beo5.apps.googleusercontent.com'
const appId = '762288849553'
const scope = [
  'https://www.googleapis.com/auth/drive.file',
  'https://www.googleapis.com/auth/drive.appdata',
]
let pickerApiLoaded = false
let oauthToken
const loadPicker = () => {
  gapi.load('auth', { callback: onAuthApiLoad })
  gapi.load('picker', { callback: onPickerApiLoad })
}
const onAuthApiLoad = () => {
  window.gapi.auth.authorize(
    {
      client_id: clientId,
      scope: scope,
      immediate: false,
    },
    handleAuthResult
  )
}
let onPickerApiLoad = () => {
  pickerApiLoaded = true
  createPicker()
}
const handleAuthResult = (authResult) => {
  if (authResult && !authResult.error) {
    oauthToken = authResult.access_token
    createPicker()
  }
}
const createPicker = () => {
  if (pickerApiLoaded && oauthToken) {
    const view = new google.picker.View(google.picker.ViewId.DOCS)
    view.setMimeTypes('application/pdf,image/jpeg,image/png,image/jpg')
    const picker = new google.picker.PickerBuilder()
      .enableFeature(google.picker.Feature.NAV_HIDDEN)
      .enableFeature(google.picker.Feature.MULTISELECT_ENABLED)
      .setAppId(appId)
      .setOAuthToken(oauthToken)
      .addView(view)
      .addView(new google.picker.DocsUploadView())
      .setDeveloperKey(developerKey)
      .setCallback(pickerCallback)
      .build()
    picker.setVisible(true)
  }
}
const pickerCallback = async (data) => {
  if (data.action == google.picker.Action.PICKED) {
    const res = await fetch(
      `https://www.googleapis.com/drive/v2/files/${data.docs[0].id}?key=${developerKey}`
    ).then((res) => res.json())
    const response = await fetch(res.downloadUrl).then((res) => res.blob())
    let metadata = {
      type: data.docs[0].mimeType,
    }
    let file = new File([response], data.docs[0].name, metadata)
    getFile(file)
  }
}

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
appscms-tools-theme-0.8.7 assets/js/googledrive.js