Skip to content
This repository has been archived by the owner on Apr 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #185 from mezis/update-gems
Browse files Browse the repository at this point in the history
Update gems
  • Loading branch information
mezis committed Jan 1, 2014
2 parents dd9d29a + e929565 commit cb273f9
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 49 deletions.
29 changes: 15 additions & 14 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ GEM
descendants_tracker (~> 0.0.1)
ice_nine (~> 0.9)
bcrypt-ruby (3.1.2)
better_errors (1.0.1)
better_errors (1.1.0)
coderay (>= 1.0.0)
erubis (>= 2.6.6)
binding_of_caller (0.7.2)
Expand Down Expand Up @@ -116,7 +116,7 @@ GEM
dotenv (0.9.0)
dotenv-rails (0.9.0)
dotenv (= 0.9.0)
dragonfly (0.9.15)
dragonfly (1.0.2)
multi_json (~> 1.0)
rack
em-http-request (1.0.3)
Expand Down Expand Up @@ -199,6 +199,7 @@ GEM
jquery-turbolinks (2.0.1)
railties (>= 3.1.0)
turbolinks
json (1.8.1)
jwt (0.1.8)
multi_json (>= 1.5)
kgio (2.8.1)
Expand Down Expand Up @@ -230,9 +231,9 @@ GEM
multi_test (0.0.2)
multi_xml (0.5.5)
multipart-post (1.2.0)
newrelic_rpm (3.6.9.171)
newrelic_rpm (3.7.1.180)
nifty-generators (0.4.6)
nokogiri (1.6.0)
nokogiri (1.6.1)
mini_portile (~> 0.5.0)
oauth2 (0.8.1)
faraday (~> 0.8)
Expand All @@ -253,7 +254,7 @@ GEM
oauth2 (~> 0.8.0)
omniauth (~> 1.0)
orm_adapter (0.5.0)
pg (0.17.0)
pg (0.17.1)
poltergeist (1.1.2)
capybara (~> 2.0.1)
faye-websocket (~> 0.4.4)
Expand All @@ -274,8 +275,8 @@ GEM
httpclient (~> 2.3.0)
multi_json (~> 1.0)
signature (~> 0.1.6)
pusher-fake (0.10.0)
em-http-request (~> 1.0.0)
pusher-fake (0.12.0)
em-http-request (>= 1.0.0, < 1.2.0)
em-websocket (~> 0.5)
multi_json (~> 1.6)
thin (~> 1.5)
Expand Down Expand Up @@ -323,7 +324,7 @@ GEM
redcarpet (3.0.0)
rest-client (1.6.7)
mime-types (>= 1.16)
roadie (2.4.2)
roadie (2.4.3)
actionmailer (> 3.0.0, < 5.0.0)
css_parser (~> 1.3.4)
nokogiri (> 1.5.0)
Expand Down Expand Up @@ -357,7 +358,7 @@ GEM
multi_json (~> 1.0)
rubyzip (< 1.0.0)
websocket (~> 1.0.4)
sentry-raven (0.6.0)
sentry-raven (0.7.1)
faraday (>= 0.7.6)
hashie (>= 1.1.0)
uuidtools
Expand Down Expand Up @@ -393,25 +394,25 @@ GEM
tins (~> 0.8)
terminal-notifier-guard (1.5.3)
text (1.2.3)
thin (1.5.1)
thin (1.6.1)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
eventmachine (>= 1.0.0)
rack (>= 1.0.0)
thor (0.18.1)
thread_safe (0.1.3)
atomic
tilt (1.4.1)
timecop (0.6.3)
timecop (0.7.1)
tins (0.9.0)
treetop (1.4.15)
polyglot
polyglot (>= 0.3.1)
turbolinks (2.1.0)
coffee-rails
tzinfo (0.3.38)
uglifier (2.2.1)
uglifier (2.4.0)
execjs (>= 0.3.0)
multi_json (~> 1.0, >= 1.0.2)
json (>= 1.8.0)
unicorn (4.6.3)
kgio (~> 2.6)
rack
Expand Down
3 changes: 2 additions & 1 deletion app/models/attachment.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
class Attachment < ActiveRecord::Base
extend Dragonfly::Model
# attr_accessible :mime_type, :size, :name, :owner, :uploader, :file

belongs_to :owner, :polymorphic => true, :counter_cache => true
belongs_to :uploader, :class_name => 'User'

file_accessor :file
dragonfly_accessor :file

delegate :format, to: :file

Expand Down
8 changes: 5 additions & 3 deletions app/models/storage/data_store.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Storage::DataStore

# +temp_object+ should respond to +data+ and +meta+
def store(temp_object, opts={})
def write(temp_object, opts={})
::Storage::File.transaction do
file = ::Storage::File.create!(metadata: temp_object.meta)
file.data = temp_object.data
Expand All @@ -12,13 +12,15 @@ def store(temp_object, opts={})
end
end

def retrieve(uid)
def read(uid)
file = ::Storage::File.find(uid.to_i)
file.update_attributes!(accessed_at: Time.now)
[ file.data, file.metadata ]
rescue ActiveRecord::RecordNotFound
nil
end

def destroy(uid)
::Storage::File.destroy(uid.to_i)
end
end
end
4 changes: 2 additions & 2 deletions app/views/attachments/_attachment.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
large = 180
is_image = attachment.mime_type =~ /^image/

image_small = is_image && attachment.file.thumb("#{size}x#{size}#").jpg.url
image_large = is_image && attachment.file.thumb("#{large}x#{large}#").jpg.url
image_small = is_image && attachment.file.thumb("#{size}x#{size}#").encode('jpg').url
image_large = is_image && attachment.file.thumb("#{large}x#{large}#").encode('jpg').url

attachment_title = "%s (%s)" % [ attachment.name, number_to_human_size(attachment.size) ]

Expand Down
18 changes: 18 additions & 0 deletions bin/fix_attachment_metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env ruby
#
# Converts attachment metadata for a format suitable for Dragonfly 1.0
#

# require 'rubygems'
# require 'bundler/setup'

dir = File.expand_path('../..', __FILE__)
$:.unshift(dir) unless $:.include?(dir)

require 'config/environment'

Storage::File.find_each do |file|
file.metadata = JSON.parse(file.metadata.to_json)
file.save!
end

41 changes: 14 additions & 27 deletions config/initializers/dragonfly.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,20 @@
require 'dragonfly'

Dragonfly[:files].tap do |dragonfly_app|
dragonfly_app.configure_with(:imagemagick)
dragonfly_app.configure_with(:rails)
dragonfly_app.define_macro(ActiveRecord::Base, :file_accessor)
dragonfly_app.datastore = Storage::DataStore.new
dragonfly_app.configure do |config|
config.identify_command = "identify -quiet"
end
end
# Configure
Dragonfly.app.configure do
plugin :imagemagick,
identify_command: 'identify -quiet'

protect_from_dos_attacks true
secret '07b23dcdfc0cc6f7ed12540ffdb1e74adc0fd914331c8936881edd9d4b5edd74'

url_format '/media/:job/:name'

memcached_url = case Rails.env
when 'development'
'memcached://127.0.0.1:11211/rack-cache'
when 'test'
'heap:/'
else
"memcached://%<user>s:%<password>s@%<host>s/rack-cache" % {
host: ENV['MEMCACHIER_SERVERS'].split(',').first,
user: ENV['MEMCACHIER_USERNAME'],
password: ENV['MEMCACHIER_PASSWORD']
}
datastore Storage::DataStore.new
end

AppFab::Application.config.middleware.insert 0, 'Rack::Cache', {
:metastore => memcached_url,
:entitystore => URI.encode("file:#{Rails.root}/tmp/dragonfly/cache/body"),
:verbose => false,
}
# Logger
Dragonfly.logger = Rails.logger

AppFab::Application.config.middleware.insert_after 'Rack::Cache',
'Dragonfly::Middleware', :files
# Mount as middleware
AppFab::Application.config.middleware.use Dragonfly::Middleware
20 changes: 20 additions & 0 deletions config/initializers/rack_cache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Rack::Cache configuration
memcached_url = case Rails.env
when 'development'
'memcached://127.0.0.1:11211/rack-cache'
when 'test'
'heap:/'
else
"memcached://%<user>s:%<password>s@%<host>s/rack-cache" % {
host: ENV['MEMCACHIER_SERVERS'].split(',').first,
user: ENV['MEMCACHIER_USERNAME'],
password: ENV['MEMCACHIER_PASSWORD']
}
end

AppFab::Application.config.middleware.insert 0, 'Rack::Cache', {
:metastore => memcached_url,
:entitystore => URI.encode("file:#{Rails.root}/tmp/dragonfly/cache/body"),
:verbose => false,
}

8 changes: 6 additions & 2 deletions spec/models/storage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

share_examples_for 'store and retrieve' do
it 'retrieves the data' do
id = subject.store(fake_file)
returned_data, returned_meta = subject.retrieve(id)
id = subject.write(fake_file)
returned_data, returned_meta = subject.read(id)

returned_data.length.should == data.length
returned_data.should == data
Expand All @@ -31,4 +31,8 @@
let(:data) { SecureRandom.random_bytes(2_000_000) }
it_should_behave_like 'store and retrieve'
end

it 'returns nil for missing files' do
subject.read('1337').should be_nil
end
end

0 comments on commit cb273f9

Please sign in to comment.