Skip to content
This repository has been archived by the owner on Sep 17, 2019. It is now read-only.

Commit

Permalink
Refactors Resource.
Browse files Browse the repository at this point in the history
  • Loading branch information
joegatt committed Jun 17, 2013
1 parent 9ce9f4d commit 7182206
Showing 1 changed file with 23 additions and 28 deletions.
51 changes: 23 additions & 28 deletions app/models/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,40 +94,35 @@ def sync_binary

increment_attempts

if Settings.evernote.stream_binaries
Settings.evernote.stream_binaries ? stream_binary : download_binary

# Stream binary. NOT WORKING YET

require 'net/http'
require 'uri'

uri = URI.parse("#{ cloud_service.evernote_url_prefix }/res/#{ cloud_resource_identifier }")
# We check that the resource has been downloaded correctly, if so we unflag the resource.
undirtify if Digest::MD5.file(raw_location).digest == data_hash
end
end

connection = Net::HTTP.new(uri.host)
connection.use_ssl = true if uri.scheme == 'https'
connection.start do |http|
response = connection.post_form(uri.path, { 'auth' => oauth_token })
File.open(raw_location, 'wb') do |file|
file.write(response.body)
end
end
else
def stream_binary
require 'net/http'
require 'uri'

# Download binary
uri = URI.parse("#{ cloud_service.evernote_url_prefix }/res/#{ cloud_resource_identifier }")
connection = Net::HTTP.new(uri.host)
connection.use_ssl = true if uri.scheme == 'https'

# To get the binary data via the API use this method (however, this way the whole file is downloaded into memory -
# see http://dev.evernote.com/start/core/resources.php#downloading )
cloud_resource_data = note_store.getResourceData(oauth_token, cloud_resource_identifier)
File.open(raw_location, 'wb') do |file|
file.write(cloud_resource_data)
end
connection.start do |http|
response = connection.post_form(uri.path, { 'auth' => oauth_token })
File.open(raw_location, 'wb') do |file|
file.write(response.body)
end
end
end

# Also how do we delete images when a resource is deleted?
# (A method in resource - checks file is not used by other resources first...?)

# We check that the resource has been downloaded correctly, if so we unflag the resource.
undirtify if Digest::MD5.file(raw_location).digest == data_hash
def download_binary
# This way the whole file is downloaded into memory -
# see http://dev.evernote.com/start/core/resources.php#downloading
cloud_resource_data = note_store.getResourceData(oauth_token, cloud_resource_identifier)
File.open(raw_location, 'wb') do |file|
file.write(cloud_resource_data)
end
end

Expand Down

0 comments on commit 7182206

Please sign in to comment.