Skip to content

Commit

Permalink
Merge branch 'master' of git@github.com:technoweenie/attachment_fu
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Jan 27, 2009
2 parents 42a37fa + 5b16d05 commit 95bc6bc
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 16 deletions.
4 changes: 4 additions & 0 deletions lib/technoweenie/attachment_fu.rb
Expand Up @@ -50,7 +50,11 @@ module ActMethods
# * <tt>:thumbnail_class</tt> - Set what class to use for thumbnails. This attachment class is used by default.
# * <tt>:path_prefix</tt> - path to store the uploaded files. Uses public/#{table_name} by default for the filesystem, and just #{table_name}
# for the S3 backend. Setting this sets the :storage to :file_system.

# * <tt>:storage</tt> - Use :file_system to specify the attachment data is stored with the file system. Defaults to :db_system.
# * <tt>:bucket_key</tt> - Use this to specify a different bucket key other than :bucket_name in the amazon_s3.yml file. This allows you to use
# different buckets for different models. An example setting would be :image_bucket and the you would need to define the name of the corresponding
# bucket in the amazon_s3.yml file.

# * <tt>:keep_profile</tt> By default image EXIF data will be stripped to minimize image size. For small thumbnails this proivides important savings. Picture quality is not affected. Set to false if you want to keep the image profile as is. ImageScience will allways keep EXIF data.
#
Expand Down
71 changes: 55 additions & 16 deletions lib/technoweenie/attachment_fu/backends/s3_backend.rb
Expand Up @@ -18,17 +18,17 @@ module Backends
# You can sign up for S3 and get access keys by visiting http://aws.amazon.com/s3.
#
# Example configuration (RAILS_ROOT/config/amazon_s3.yml)
#
#
# development:
# bucket_name: appname_development
# access_key_id: <your key>
# secret_access_key: <your key>
#
#
# test:
# bucket_name: appname_test
# access_key_id: <your key>
# secret_access_key: <your key>
#
#
# production:
# bucket_name: appname
# access_key_id: <your key>
Expand Down Expand Up @@ -81,10 +81,43 @@ module Backends
#
# Which would result in URLs like <tt>http(s)://:server/:bucket_name/my/custom/path/:id/:filename.</tt>
#
# === Using different bucket names on different models
#
# By default the bucket name that the file will be stored to is the one specified by the
# <tt>:bucket_name</tt> key in the amazon_s3.yml file. You can use the <tt>:bucket_key</tt> option
# to overide this behavior on a per model basis. For instance if you want a bucket that will hold
# only Photos you can do this:
#
# class Photo < ActiveRecord::Base
# has_attachment :storage => :s3, :bucket_key => :photo_bucket_name
# end
#
# And then your amazon_s3.yml file needs to look like this.
#
# development:
# bucket_name: appname_development
# access_key_id: <your key>
# secret_access_key: <your key>
#
# test:
# bucket_name: appname_test
# access_key_id: <your key>
# secret_access_key: <your key>
#
# production:
# bucket_name: appname
# photo_bucket_name: appname_photos
# access_key_id: <your key>
# secret_access_key: <your key>
#
# If the bucket_key you specify is not there in a certain environment then attachment_fu will
# default to the <tt>bucket_name</tt> key. This way you only have to create special buckets
# this can be helpful if you only need special buckets in certain environments.
#
# === Permissions
#
# By default, files are stored on S3 with public access permissions. You can customize this using
# the <tt>:s3_access</tt> option to <tt>has_attachment</tt>. Available values are
# the <tt>:s3_access</tt> option to <tt>has_attachment</tt>. Available values are
# <tt>:private</tt>, <tt>:public_read_write</tt>, and <tt>:authenticated_read</tt>.
#
# === Other options
Expand Down Expand Up @@ -123,7 +156,7 @@ class ConfigFileNotFoundError < StandardError; end

def self.included(base) #:nodoc:
mattr_reader :bucket_name, :s3_config

begin
require 'aws/s3'
include AWS::S3
Expand All @@ -138,7 +171,13 @@ def self.included(base) #:nodoc:
# raise ConfigFileNotFoundError.new('File %s not found' % @@s3_config_path)
end

@@bucket_name = s3_config[:bucket_name]
bucket_key = base.attachment_options[:bucket_key]

if bucket_key and s3_config[bucket_key.to_sym]
@@bucket_name = s3_config[bucket_key.to_sym]
else
@@bucket_name = s3_config[:bucket_name]
end

Base.establish_connection!(s3_config.slice(:access_key_id, :secret_access_key, :server, :port, :use_ssl, :persistent, :proxy))

Expand All @@ -150,11 +189,11 @@ def self.included(base) #:nodoc:
def self.protocol
@protocol ||= s3_config[:use_ssl] ? 'https://' : 'http://'
end

def self.hostname
@hostname ||= s3_config[:server] || AWS::S3::DEFAULT_HOST
end

def self.port_string
@port_string ||= (s3_config[:port].nil? || s3_config[:port] == (s3_config[:use_ssl] ? 443 : 80)) ? '' : ":#{s3_config[:port]}"
end
Expand All @@ -163,11 +202,11 @@ module ClassMethods
def s3_protocol
Technoweenie::AttachmentFu::Backends::S3Backend.protocol
end

def s3_hostname
Technoweenie::AttachmentFu::Backends::S3Backend.hostname
end

def s3_port_string
Technoweenie::AttachmentFu::Backends::S3Backend.port_string
end
Expand Down Expand Up @@ -196,7 +235,7 @@ def full_filename(thumbnail = nil)
File.join(base_path, thumbnail_name_for(thumbnail))
end

# All public objects are accessible via a GET request to the S3 servers. You can generate a
# All public objects are accessible via a GET request to the S3 servers. You can generate a
# url for an object using the s3_url method.
#
# @photo.s3_url
Expand All @@ -211,7 +250,7 @@ def s3_url(thumbnail = nil)
end
alias :public_filename :s3_url

# All private objects are accessible via an authenticated GET request to the S3 servers. You can generate an
# All private objects are accessible via an authenticated GET request to the S3 servers. You can generate an
# authenticated url for an object like this:
#
# @photo.authenticated_s3_url
Expand All @@ -223,7 +262,7 @@ def s3_url(thumbnail = nil)
#
# # Absolute expiration date (October 13th, 2025)
# @photo.authenticated_s3_url(:expires => Time.mktime(2025,10,13).to_i)
#
#
# # Expiration in five hours from now
# @photo.authenticated_s3_url(:expires_in => 5.hours)
#
Expand Down Expand Up @@ -252,11 +291,11 @@ def current_data
def s3_protocol
Technoweenie::AttachmentFu::Backends::S3Backend.protocol
end

def s3_hostname
Technoweenie::AttachmentFu::Backends::S3Backend.hostname
end

def s3_port_string
Technoweenie::AttachmentFu::Backends::S3Backend.port_string
end
Expand All @@ -269,7 +308,7 @@ def destroy_file

def rename_file
return unless @old_filename && @old_filename != filename

old_full_filename = File.join(base_path, @old_filename)

S3Object.rename(
Expand Down
Expand Up @@ -42,6 +42,9 @@ def resize_image(img, size)
if size.is_a?(Fixnum) || (size.is_a?(Array) && size.first.is_a?(Fixnum))
size = [size, size] if size.is_a?(Fixnum)
img.thumbnail!(*size)
elsif size.is_a?(String) && size =~ /^c.*$/ # Image cropping - example geometry string: c75x75
dimensions = size[1..size.size].split("x")
img.crop_resized!(dimensions[0].to_i, dimensions[1].to_i)
else
img.change_geometry(size.to_s) { |cols, rows, image| image.resize!(cols<1 ? 1 : cols, rows<1 ? 1 : rows) }
end
Expand Down

0 comments on commit 95bc6bc

Please sign in to comment.