Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Addresses issue 389 #390

Closed
wants to merge 4 commits into from

3 participants

@whittle

I don’t know if non-Rails support is a valuable objective for paperclip. If so, I would happily start a non-Rails appraisal.

@mike-burns
Owner

Hi @whittle, thank you for this pull request. We'd like to pull it in but it conflicts. Can you rebase first?

Our concern is that it removes the file_size and fingerprint from attachments. Please make sure the resubmitting pull request maintains those.

Thanks again,
-Mike

@sikachu sikachu closed this pull request from a commit
Jason Whittle Make Storage::S3.parse_credentials use of Rails.env optional.
Closes #389, Closes #390
ff0910d
@sikachu sikachu closed this in ff0910d
@sikachu
Owner

I've pulled in the first commit, so this pull request can be closed. Thank you for you contribution :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 10, 2011
Commits on Jan 11, 2011
  1. Add convenience method Attachment#content.

    Jason Whittle authored
Commits on Jan 15, 2011
  1. File size and fingerprint need only be calculated once.

    Jason Whittle authored
  2. Assuming we’re coding defensively, methods should start by rewinding …

    Jason Whittle authored
    …any source files.
This page is out of date. Refresh to see the latest.
View
5 lib/paperclip/attachment.rb
@@ -92,15 +92,12 @@ def assign uploaded_file
@queued_for_write[:original] = to_tempfile(uploaded_file)
instance_write(:file_name, uploaded_file.original_filename.strip)
instance_write(:content_type, uploaded_file.content_type.to_s.strip)
- instance_write(:file_size, uploaded_file.size.to_i)
- instance_write(:fingerprint, generate_fingerprint(uploaded_file))
instance_write(:updated_at, Time.now)
@dirty = true
post_process
- # Reset the file size if the original file was reprocessed.
instance_write(:file_size, @queued_for_write[:original].size.to_i)
instance_write(:fingerprint, generate_fingerprint(@queued_for_write[:original]))
ensure
@@ -198,8 +195,8 @@ def updated_at
end
def generate_fingerprint(source)
- data = source.read
source.rewind if source.respond_to?(:rewind)
+ data = source.read
Digest::MD5.hexdigest(data)
end
View
6 lib/paperclip/storage/filesystem.rb
@@ -33,6 +33,12 @@ def to_file style_name = default_style
@queued_for_write[style_name] || (File.new(path(style_name), 'rb') if exists?(style_name))
end
+ def content style_name = default_style
+ source = to_file(style_name)
+ source.rewind if source.respond_to? :rewind
+ source.read
+ end
+
def flush_writes #:nodoc:
@queued_for_write.each do |style_name, file|
file.close
View
9 lib/paperclip/storage/s3.rb
@@ -108,7 +108,8 @@ def s3_host_alias
def parse_credentials creds
creds = find_credentials(creds).stringify_keys
- (creds[Rails.env] || creds).symbolize_keys
+ env = Object.const_defined?(:Rails) ? Rails.env : nil
+ (creds[env] || creds).symbolize_keys
end
def exists?(style = default_style)
@@ -132,11 +133,15 @@ def to_file style = default_style
basename = File.basename(filename, extname)
file = Tempfile.new([basename, extname])
file.binmode
- file.write(AWS::S3::S3Object.value(path(style), bucket_name))
+ file.write content(style)
file.rewind
return file
end
+ def content style = default_style
+ AWS::S3::S3Object.value(path(style), bucket_name)
+ end
+
def create_bucket
AWS::S3::Bucket.create(bucket_name)
end
View
8 test/attachment_test.rb
@@ -440,7 +440,7 @@ def do_after_all; end
context "Assigning an attachment" do
setup do
rebuild_model :styles => { :something => "100x100#" }
- @file = StringIO.new(".")
+ @file = StringIO.new("Contents of a file.")
@file.stubs(:original_filename).returns("5k.png\n\n")
@file.stubs(:content_type).returns("image/png\n\n")
@file.stubs(:to_tempfile).returns(@file)
@@ -450,6 +450,10 @@ def do_after_all; end
@dummy.avatar = @file
end
+ should "retain the original contents of the file" do
+ assert_equal "Contents of a file.", @dummy.avatar.content(:original)
+ end
+
should "strip whitespace from original_filename field" do
assert_equal "5k.png", @dummy.avatar.original_filename
end
@@ -466,7 +470,6 @@ def do_after_all; end
@not_file = mock("not_file")
@tempfile = mock("tempfile")
@not_file.stubs(:nil?).returns(false)
- @not_file.expects(:size).returns(10)
@tempfile.expects(:size).returns(10)
@not_file.expects(:original_filename).returns("sheep_say_bæ.png\r\n")
@not_file.expects(:content_type).returns("image/png\r\n")
@@ -478,7 +481,6 @@ def do_after_all; end
@attachment.expects(:post_process)
@attachment.expects(:to_tempfile).returns(@tempfile)
@attachment.expects(:generate_fingerprint).with(@tempfile).returns("12345")
- @attachment.expects(:generate_fingerprint).with(@not_file).returns("12345")
@dummy.avatar = @not_file
end
View
4 test/storage_test.rb
@@ -356,6 +356,10 @@ class AWS::S3::NoSuchBucket < AWS::S3::ResponseError
file = @dummy.avatar.to_file
assert_match /^original.*\.png$/, File.basename(file.path)
end
+
+ should "have the same contents as the original file" do
+ assert_equal @file.read, @dummy.avatar.content(:original)
+ end
end
end
end
Something went wrong with that request. Please try again.