Skip to content

Commit

Permalink
Merge 3db8799 into 344433d
Browse files Browse the repository at this point in the history
  • Loading branch information
atz committed Sep 10, 2016
2 parents 344433d + 3db8799 commit d491cc1
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 90 deletions.
17 changes: 8 additions & 9 deletions app/controllers/browse_everything_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,30 @@ class BrowseEverythingController < ActionController::Base
layout 'browse_everything'
helper BrowseEverythingHelper

after_filter {session["#{provider_name}_token"] = provider.token unless provider.nil? }
after_filter { session["#{provider_name}_token"] = provider.token unless provider.nil? }

def index
render :layout => !request.xhr?
end

def show
render :layout => !request.xhr?
end

def auth
code = params[:code]
session["#{provider_name}_token"] = provider.connect(params,session["#{provider_name}_data"])
session["#{provider_name}_token"] = provider.connect(params, session["#{provider_name}_data"])
end

def resolve
selected_files = params[:selected_files] || []
@links = selected_files.collect { |file|
p,f = file.split(/:/)
@links = selected_files.collect { |file|
p,f = file.split(/:/)
(url,extra) = browser.providers[p].link_for(f)
result = { url: url }
result.merge!(extra) unless extra.nil?
result
}
respond_to do |format|
respond_to do |format|
format.html { render :layout => false }
format.json { render :json => @links }
end
Expand All @@ -40,7 +39,7 @@ def auth_link
@auth_link ||= if provider.present?
link, data = provider.auth_link
session["#{provider_name}_data"] = data
link = "#{link}&state=#{provider.key}" unless link.include?("state")
link = "#{link}&state=#{provider.key}" unless link.to_s.include?('state')
link
else
nil
Expand Down
3 changes: 2 additions & 1 deletion browse-everything.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ Gem::Specification.new do |spec|
spec.add_dependency "sass-rails"
spec.add_dependency "bootstrap-sass"
spec.add_dependency "font-awesome-rails"
spec.add_dependency "google-api-client", "~> 0.8.6"
spec.add_dependency "google-api-client", "~> 0.9"
spec.add_dependency "signet"
spec.add_dependency "httparty"
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "rspec-rails"
Expand Down
3 changes: 2 additions & 1 deletion lib/browse_everything/driver/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Base

attr_reader :config, :name
attr_accessor :token

def initialize(config,session_info={})
@config = config
validate_config
Expand Down Expand Up @@ -42,6 +42,7 @@ def authorized?
false
end

# @return [Array{URI,Object}] 2 elements: the URI, and session data to store under "#{provider_name}_data"
def auth_link
[]
end
Expand Down
131 changes: 52 additions & 79 deletions lib/browse_everything/driver/google_drive.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module BrowseEverything
module Driver
class GoogleDrive < Base

require 'google/api_client'
require 'google/apis/drive_v3'
require 'signet'

def icon
'google-plus-sign'
Expand All @@ -17,114 +17,87 @@ def validate_config
end
end

def contents(path='')
default_params = { }
def contents(path = '')
return to_enum(:contents, path) unless block_given?
default_params = {}
page_token = nil
files = []
begin
unless path.blank?
default_params[:q] = "'#{path}' in parents"
end
unless page_token.blank?
default_params[:pageToken] = page_token
end
api_result = oauth_client.execute( api_method: drive.files.list, parameters: default_params )
response = JSON.parse(api_result.response.body)
page_token = response["nextPageToken"]
response["items"].select do |file|
path.blank? ? (file["parents"].blank? or file["parents"].any?{|p| p["isRoot"] }) : true
default_params[:q] = "'#{path}' in parents" unless path.blank?
default_params[:page_token] = page_token unless page_token.blank?
response = drive.list_files(default_params)
page_token = response.next_page_token
response.items.select do |file|
path.blank? ? (file['parents'].blank? || file['parents'].any?{|p| p['isRoot'] }) : true
end.each do |file|
files << details(file, path)
d = details(file, path)
yield d if d
end
end while !page_token.blank?
files.compact
end

def details(file, path='')
if file["downloadUrl"] or file["mimeType"] == "application/vnd.google-apps.folder"
BrowseEverything::FileEntry.new(
file["id"],
"#{self.key}:#{file["id"]}",
file["title"],
(file["fileSize"] || 0),
Time.parse(file["modifiedDate"]),
file["mimeType"] == "application/vnd.google-apps.folder",
file["mimeType"] == "application/vnd.google-apps.folder" ?
"directory" :
file["mimeType"]
)
end
def details(file, path = '')
mime_folder = file.mime_type == 'application/vnd.google-apps.folder'
return unless file.web_content_link || mime_folder
BrowseEverything::FileEntry.new(
file.id,
"#{self.key}:#{file.id}",
file.name,
file.size.to_i,
file.modified_time,
mime_folder,
mime_folder ? 'directory' : file.mime_type
)
end

def link_for(id)
api_method = drive.files.get
api_result = oauth_client.execute(api_method: api_method, parameters: {fileId: id})
download_url = JSON.parse(api_result.response.body)["downloadUrl"]
auth_header = {'Authorization' => "Bearer #{oauth_client.authorization.access_token.to_s}"}
extras = {
file = drive.get_file(id)
auth_header = {'Authorization' => "Bearer #{client.authorization.access_token.to_s}"}
extras = {
auth_header: auth_header,
expires: 1.hour.from_now,
file_name: api_result.data.title,
file_size: api_result.data.fileSize.to_i
expires: 1.hour.from_now,
file_name: file.name,
file_size: file.size.to_i
}
[download_url, extras]
[file.web_content_link, extras]
end

def auth_link
oauth_client.authorization.authorization_uri.to_s
auth_client.authorization_uri
end

def authorized?
@token.present?
token.present?
end

def connect(params, data)
oauth_client.authorization.code = params[:code]
@token = oauth_client.authorization.fetch_access_token!
auth_client.code = params[:code]
self.token = auth_client.fetch_access_token!
end

def drive
oauth_client.discovered_api('drive', 'v2')
@drive ||= Google::Apis::DriveV3::DriveService.new.tap do |s|
s.authorization = authorization
end
end

private

#As per issue http://stackoverflow.com/questions/12572723/rails-google-client-api-unable-to-exchange-a-refresh-token-for-access-token

#patch start
def token_expired?(token)
client=@client
result = client.execute( api_method: drive.files.list, parameters: {} )
(result.status != 200)
def authorization
return @auth_client unless @auth_client.nil?
return nil unless token.present?
auth_client.update_token!(token)
self.token = auth_client.fetch_access_token! if auth_client.expired?
auth_client
end

def exchange_refresh_token( refresh_token )
client=oauth_client
client.authorization.grant_type = 'refresh_token'
client.authorization.refresh_token = refresh_token
client.authorization.fetch_access_token!
client.authorization
client
def auth_client
@auth_client ||= Signet::OAuth2::Client.new token_credential_uri: 'https://www.googleapis.com/oauth2/v3/token',
authorization_uri: 'https://accounts.google.com/o/oauth2/auth',
scope: 'https://www.googleapis.com/auth/drive',
client_id: config[:client_id],
client_secret: config[:client_secret],
redirect_uri: callback
end
#patch end

def oauth_client
if @client.nil?
@client = Google::APIClient.new
@client.authorization.client_id = config[:client_id]
@client.authorization.client_secret = config[:client_secret]
@client.authorization.scope = "https://www.googleapis.com/auth/drive"
@client.authorization.redirect_uri = callback
@client.authorization.update_token!(@token) if @token.present?
#Patch start
@client = exchange_refresh_token(@token["refresh_token"]) if @token.present? && token_expired?(@token)
#Patch end
end
#todo error checking here
@client
end

end

end
end

0 comments on commit d491cc1

Please sign in to comment.