Code coverage report for code/utils/google-drive-io.coffee

Statements: 0% (0 / 79)      Branches: 0% (0 / 26)      Functions: 0% (0 / 19)      Lines: 0% (0 / 61)      Ignored: none     

All files » code/utils/ » google-drive-io.coffee
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112                                                                                                                                                                                                                               
module.exports = class GoogleDriveIO
 
  APP_ID : '1095918012594'
  DEVELOPER_KEY: 'AIzaSyAUobrEXqtbZHBvr24tamdE6JxmPYTRPEA'
  CLIENT_ID: '1095918012594-svs72eqfalasuc4t1p1ps1m8r9b8psso.apps.googleusercontent.com'
  SCOPES: 'https://www.googleapis.com/auth/drive'
 
  authorized: false
 
  token: null
 
  authorize: (immediate, callback) ->
    if @token
      callback null, @token
    else
      args =
        'client_id': @CLIENT_ID
        'scope': @SCOPES
        'immediate': immediate or false
      gapi.auth.authorize args, (token) =>
        if token and not token.error
          @token = token
        if callback
          err = (if not token
            'Unable to authorize'
          else if token.error
            token.error
          else
            null
          )
          @authorized = err is null
          callback err, token
 
  makeMultipartBody: (parts, boundary) ->
    ((for part in parts
      "\r\n--#{boundary}\r\nContent-Type: application/json\r\n\r\n#{part}"
    ).join '') + "\r\n--#{boundary}--"
 
  sendFile: (fileSpec, contents, callback) ->
    boundary = '-------314159265358979323846'
    metadata = JSON.stringify
      title: fileSpec.fileName
      mimeType: 'application/json'
 
    [method, path] = if fileSpec.fileId
      ['PUT', "/upload/drive/v2/files/#{fileSpec.fileId}"]
    else
      ['POST', '/upload/drive/v2/files']
 
    request = gapi.client.request
      path: path
      method: method
      params: {uploadType: 'multipart', alt: 'json'}
      headers: {'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'}
      body: @makeMultipartBody [metadata, contents], boundary
 
    request.execute (file) ->
      if callback
        if file
          callback null, file
        else
          callback 'Unabled to upload file'
 
  upload: (fileSpec, contents, callback) ->
    @authorize @authorized, (err) =>
      if not err
        gapi.client.load 'drive', 'v2', => @sendFile fileSpec, contents, callback
      else
        callback "No authorization. Upload failed for file: #{fileSpec.fileName}"
 
  download: (fileSpec, callback) ->
    @authorize @authorized, (err, token) ->
      if err
        callback err
      else
        gapi.client.load 'drive', 'v2', ->
          request = gapi.client.drive.files.get
            fileId: fileSpec.id
          request.execute (file) ->
            if file?.downloadUrl
              xhr = new XMLHttpRequest()
              xhr.open 'GET', file.downloadUrl
              xhr.setRequestHeader 'Authorization', "Bearer #{token.access_token}"
              xhr.onload = ->
                try
                  json = JSON.parse xhr.responseText
                catch e
                  callback e
                  return
                callback null, json
              xhr.onerror = ->
                callback "Unable to download #{file.downloadUrl}"
              xhr.send()
            else
              callback "Unable to get download url"
 
  filePicker: (callback) ->
    @authorize @authorized, (err, token) ->
      if err
        callback err
      else
        gapi.load 'picker', callback: ->
          pickerCallback = (data, etc) ->
            callback null, if data.action is 'picked' then data.docs[0] else null
          picker = new google.picker.PickerBuilder()
            .addView google.picker.ViewId.DOCS
            .setOAuthToken token.access_token
            .setCallback pickerCallback
            .build()
          picker.setVisible true