Created
September 21, 2009 01:22
-
-
Save captproton/190010 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Resource < ActiveRecord::Base | |
include PaperclipSupport | |
if CONFIG['s3'] | |
has_attached_file :attachment, :storage => :s3, :path => "files/:filename", | |
:bucket => CONFIG['s3_bucket_name'], # TODO is there a way to share this between models? | |
:s3_credentials => { :access_key_id => CONFIG['s3_access_id'], | |
:secret_access_key => CONFIG['s3_secret_key'] }, | |
:s3_headers => { 'Cache-Control' => 'max-age=315576000', 'Expires' => 10.years.from_now.httpdate } | |
else | |
has_attached_file :attachment, :storage => :filesystem, :url => "/files/:filename" | |
end | |
validates_attachment_size :attachment, :less_than => 100.megabytes | |
def is_mp3? | |
%w(audio/mpeg audio/mpg).include?(attachment_content_type) ? true : false | |
end | |
before_post_process :image? | |
def image? | |
!(attachment_content_type =~ /^image.*/).nil? | |
end | |
acts_as_indexed :fields => [:title, :type_of_content] | |
#def validate | |
# errors.add_to_base("You must choose a file to upload") unless self.filename | |
# | |
# unless self.filename.nil? | |
# [:size].each do |attr_name| | |
# enum = attachment_options[attr_name] | |
# unless enum.nil? || enum.include?(send(attr_name)) | |
# errors.add_to_base("Files should be smaller than 50 MB in size") | |
# end | |
# end | |
# end | |
# | |
# end | |
# used for searching | |
def type_of_content | |
#self.attachment_content_type.split("/").join(" ") | |
"nonimage" | |
end | |
def title | |
self.attachment_file_name | |
##(split_filename = self[:filename].split('.')).pop and return split_filename.join('.').titleize | |
end | |
private | |
def attachment_url_provided? | |
!self.attachment_url.blank? | |
end | |
def download_remote_file | |
self.attachment = do_download_remote_file | |
self.attachment_remote_url = attachment_url | |
end | |
def do_download_remote_file | |
io = open(URI.parse(attachment_url)) | |
def io.original_filename; base_uri.path.split('/').last; end | |
io.original_filename.blank? ? nil : io | |
rescue # catch url errors with validations instead of exceptions (Errno::ENOENT, OpenURI::HTTPError, etc...) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment