Commit 872942a

mo khan <mo@mokhan.ca>
2015-06-14 17:46:05
download file from google drive.
1 parent 041a998
app/assets/javascripts/models/google.js.coffee
@@ -11,12 +11,7 @@ class Stronglifters.GoogleDrive
 
   initializeDrive: () =>
     @loadClient =>
-      @printFile('StrongLifts/backup.stronglifts')
-
-  showSettingsDialog: () ->
-    @drive = new gapi.drive.share.ShareClient('241601222378')
-    #@drive.setItemIds(['StrongLifts/backup.stronglifts'])
-    @drive.showSettingsDialog()
+      @printFile()
 
   checkAuth: () ->
     gapi.auth.authorize({'client_id': @client_id, 'scope': @scopes, 'immediate': false}, @handleAuthResult)
@@ -31,19 +26,22 @@ class Stronglifters.GoogleDrive
   loadClient: (callback) ->
     gapi.client.load('drive', 'v2', callback)
 
-  printFile: (fileId) =>
-    #request = gapi.client.drive.files.get({ 'fileId': fileId })
+  printFile: () =>
     request = gapi.client.drive.files.list({
       'q': "title contains '.stronglifts' and title contains 'backup'"
     })
     request.execute (response) =>
       console.dir(response)
-      debugger
       if !response.error
         item = response.items[0]
         console.log('Title: ' + item.title)
         console.log('Description: ' + item.description)
         console.log('MIME type: ' + item.mimeType)
+        fileId = item.id
+        downloadUrl = item.downloadUrl
+        $.post( "/training_sessions/drive_upload", item)
+          .done (data) ->
+            console.dir(data)
       else if (item.error.code == 401)
         #// Access token might have expired.
         @checkAuth()
app/controllers/training_sessions_controller.rb
@@ -17,6 +17,11 @@ class TrainingSessionsController < ApplicationController
     redirect_to dashboard_path, notice: t(".success")
   end
 
+  def drive_upload
+    DownloadFromDriveJob.perform_later(params)
+    redirect_to dashboard_path, notice: t(".success")
+  end
+
   private
 
   def storage
app/jobs/download_from_drive_job.rb
@@ -0,0 +1,11 @@
+class DownloadFromDriveJob < ActiveJob::Base
+  queue_as :default
+
+  def perform(params)
+    puts params.inspect
+    Dir.mktmpdir do |dir|
+      download_path = File.join(dir, params[:title])
+      `wget -O #{download_path} #{params[:downloadUrl]}`
+    end
+  end
+end
app/views/training_sessions/index.html.erb
@@ -13,9 +13,6 @@
           </div> <!-- /.small-12 -->
         <% end %>
       </div>
-      <div class="row">
-        <button onclick="googleDriver.showSettingsDialog()">Share</button>
-      </div>
     </div>
     <div class="panel callout radius">
       <h6><%= @training_sessions.count %> training sessions completed</h6>
@@ -31,12 +28,8 @@
 
 <script type="text/javascript" charset="utf-8">
   var googleDriver = new Stronglifters.GoogleDrive();
-  $(function(){
-    window.onload = function() {
-    };
-  });
   function handleClientLoad() {
-    googleDriver.checkAuth()
+    googleDriver.checkAuth();
   }
 </script>
 <script type="text/javascript" src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>
config/routes.rb
@@ -5,6 +5,7 @@ Rails.application.routes.draw do
   resources :training_sessions, only: [:index] do
     collection do
       post :upload
+      post :drive_upload
     end
   end
   resources :programs, only: [:show]
lib/temporary_storage.rb
@@ -1,7 +1,7 @@
 class TemporaryStorage
   def store(file)
-    "#{tmp_dir}/#{file.original_filename.parameterize}".tap do |new_path|
-      Rails.logger.info("Copying... #{file.path} to #{new_path}")
+    "#{tmp_dir}/#{File.basename(file.path).parameterize}".tap do |new_path|
+      puts ("Copying... #{file.path} to #{new_path}")
       FileUtils.mv(file.path, new_path)
     end
   end
spec/jobs/download_from_drive_job_spec.rb
@@ -0,0 +1,5 @@
+require 'rails_helper'
+
+RSpec.describe DownloadFromDriveJob, type: :job do
+  pending "add some examples to (or delete) #{__FILE__}"
+end