Skip to content

Commit

Permalink
Merge 0e4bb47 into 4d022a3
Browse files Browse the repository at this point in the history
  • Loading branch information
atz committed Sep 27, 2016
2 parents 4d022a3 + 0e4bb47 commit 59d1f04
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 92 deletions.
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@ before_install:
- "if [ $(phantomjs --version) != '2.1.1' ]; then tar -xvf $PWD/travis_phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2 -C $PWD/travis_phantomjs; fi"
- "phantomjs --version"
rvm:
- 2.2
- "2.2.2"
# rack 2 requires Ruby version >= 2.2.2
env:
global:
- NOKOGIRI_USE_SYSTEM_LIBRARIES=true
matrix:
- "RAILS_VERSION=4.1.10"
- "RAILS_VERSION=4.2.1"
- "RAILS_VERSION=5.0.0.1"

matrix:
include:
- rvm: "2.3.0"
env: "RAILS_VERSION=5.0.0.1"
- rvm: 2.1
env: "RAILS_VERSION=4.2.1"
- rvm: 2.0
Expand Down
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
135 changes: 56 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,91 @@ def validate_config
end
end

def contents(path='')
default_params = { }
def contents(path = '')
return to_enum(:contents, path) unless block_given?
default_params = {
order_by: 'folder,modifiedTime desc,name',
fields: 'nextPageToken,files(name,id,mimeType,size,modifiedTime,parents)'
# page_size: 100
}
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.files.select do |file|
path.blank? ? (file.parents.blank? || file.parents.any?{|p| p == 'root' }) : 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 || DateTime.new,
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)
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 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
#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
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

end

end
end
2 changes: 1 addition & 1 deletion spec/features/select_files_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
it "selects files from the filesystem" do
click_button('Browse')
sleep(5)
click_link("README.rdoc")
click_link("README.md")
within(".modal-footer") do
expect(page).to have_selector("span", text: "1 file selected")
click_button("Submit")
Expand Down

0 comments on commit 59d1f04

Please sign in to comment.