Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/development storage #21

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

home :: https://github.com/supportify/paperclip-azure
code :: https://github.com/supportify/paperclip-azure
rdoc :: https://www.rubydoc.info/docs/paperclip-azure
rdoc :: http://www.rubydoc.info/github/supportify/paperclip-azure/master/
bugs :: https://github.com/supportify/paperclip-azure/issues

== DESCRIPTION:
Expand Down
9 changes: 7 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,23 @@ Hoe.spec "paperclip-azure" do
license "MIT" # this should match the license in the README

extra_deps << ['azure', '~> 0.7']
extra_deps << ['azure-storage', '~> 0.12']
extra_deps << ['azure-storage', '~> 0.15.0.preview']
extra_deps << ['hashie', '~> 3.5']
extra_deps << ['addressable', '~> 2.5']

extra_dev_deps << ['paperclip', '>= 4.3.6']
extra_dev_deps << ['sqlite3', '~> 1.3.8']
extra_dev_deps << ['rspec', '~> 3.0']
extra_dev_deps << ['simplecov', '~> 0.14']
extra_dev_deps << ['simplecov', '~> 0.16']
extra_dev_deps << ['activerecord', '>= 4.2.0']
extra_dev_deps << ['activerecord-import', '~> 0.19']
extra_dev_deps << ['activemodel', '>= 4.2.0']
extra_dev_deps << ['activesupport', '>= 4.2.0']
extra_dev_deps << ['hoe']
extra_dev_deps << ['hoe-bundler']
extra_dev_deps << ['hoe-gemspec']
extra_dev_deps << ['hoe-git']
extra_dev_deps << ['hoe-rubygems']
end

# vim: syntax=ruby
2 changes: 1 addition & 1 deletion lib/paperclip/azure.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Paperclip; end

class Paperclip::Azure
VERSION = "1.0.3"
VERSION = "1.0.4"
end
29 changes: 16 additions & 13 deletions lib/paperclip/storage/azure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,16 @@ def self.extended base

def expiring_url(time = 3600, style_name = default_style)
if path(style_name)
uri = URI azure_uri(style_name)
uri = URI "#{container_name}/#{path(style_name).gsub(%r{\A/}, '')}"
generator = ::Azure::Storage::Core::Auth::SharedAccessSignature.new azure_account_name,
azure_credentials[:storage_access_key]
azure_storage_client.storage_access_key

generator.signed_uri uri, false, service: 'b',
uri = generator.signed_uri uri, false, service: 'b',
resource: 'b',
permissions: 'r',
start: (Time.now - (5 * 60)).utc.iso8601,
expiry: (Time.now + time).utc.iso8601
azure_interface.generate_uri(uri.path, CGI::parse(uri.query || "")).to_s
else
url(style_name)
end
Expand All @@ -107,10 +108,7 @@ def azure_credentials
end

def azure_account_name
account_name = @options[:azure_storage_account_name] || azure_credentials[:storage_account_name]
account_name = account_name.call(self) if account_name.is_a?(Proc)

account_name
azure_storage_client.storage_account_name
end

def container_name
Expand All @@ -132,13 +130,17 @@ def azure_interface
end

def azure_storage_client
config = {}
@azure_storage_client ||= begin
config = {}

[:storage_account_name, :storage_access_key].each do |opt|
config[opt] = azure_credentials[opt] if azure_credentials[opt]
end
[:storage_account_name, :storage_access_key, :use_development_storage].each do |opt|
config[opt] = azure_credentials[opt] if azure_credentials[opt]
end

config[:storage_blob_host] = "https://#{Environment.url_for azure_credentials[:storage_account_name], azure_credentials[:region]}" if azure_credentials[:region]

@azure_storage_client ||= ::Azure::Storage::Client.create config
::Azure::Storage::Client.create config
end
end

def obtain_azure_instance_for(options)
Expand All @@ -152,7 +154,8 @@ def obtain_azure_instance_for(options)
end

def azure_uri(style_name = default_style)
"https://#{azure_base_url}/#{container_name}/#{path(style_name).gsub(%r{\A/}, '')}"
uri = URI "#{container_name}/#{path(style_name).gsub(%r{\A/}, '')}"
azure_interface.generate_uri uri.path, CGI::parse(uri.query || "")
end

def azure_base_url
Expand Down
2 changes: 1 addition & 1 deletion lib/paperclip/storage/azure/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Environment
}

def self.url_for(account_name, region = nil)
"#{account_name}.blob.#{ENVIRONMENT_SUFFIX[region || :global]}"
"#{account_name}.blob.#{ENVIRONMENT_SUFFIX[(region || :global).to_sym]}"
end
end
end
Expand Down
16 changes: 8 additions & 8 deletions spec/paperclip/storage/azure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@
container: "container",
path: ":attachment/:basename:dotextension",
azure_credentials: {
'access_key_id' => "12345",
'secret_access_key' => "54321"
storage_account_name: 'storage',
storage_access_key: storage_access_key
}

File.open(fixture_file('5k.png'), 'rb') do |file|
Expand All @@ -136,7 +136,8 @@
styles: { large: ["500x500#", :jpg] },
container: "container",
azure_credentials: {
"storage_access_key" => "54321"
storage_account_name: 'storage',
storage_access_key: storage_access_key
}

File.open(fixture_file("spaced file.png"), "rb") do |file|
Expand All @@ -162,18 +163,17 @@
styles: { large: ['500x500#', :jpg] },
container: "container",
azure_credentials: {
'storage_access_key' => "54321"
storage_account_name: 'storage',
storage_access_key: storage_access_key
}

stringio = stringy_file
class << stringio
file = stringy_file
class << file
def original_filename
"question?mark.png"
end
end

file = Paperclip.io_adapters.for(stringio)

@dummy = Dummy.new
@dummy.avatar = file
@dummy.save
Expand Down