app/models/spud_media.rb in spud_media-0.8.3 vs app/models/spud_media.rb in spud_media-0.9.0

- old
+ new

@@ -1,23 +1,30 @@ class SpudMedia < ActiveRecord::Base + has_attached_file :attachment, :storage => Spud::Media.paperclip_storage, :s3_credentials => Spud::Media.s3_credentials, - :path => Spud::Media.storage_path, + :s3_permissions => lambda { |attachment, style| + attachment.instance.is_protected ? 'private' : 'public-read' + }, + :path => Spud::Media.paperclip_storage == :s3 ? Spud::Media.storage_path : lambda { |attachment| + attachment.instance.is_protected ? Spud::Media.storage_path_protected : Spud::Media.storage_path + }, :url => Spud::Media.storage_url - attr_accessible :attachment_content_type,:attachment_file_name,:attachment_file_size,:attachment + + before_update :validate_permissions + + attr_accessible :attachment_content_type,:attachment_file_name,:attachment_file_size,:attachment, :is_protected def image_from_type if self.attachment_content_type.blank? return "spud/admin/files_thumbs/dat_thumb.png" end if self.attachment_content_type.match(/jpeg|jpg/) return "spud/admin/files_thumbs/jpg_thumb.png" end - - if self.attachment_content_type.match(/png/) return "spud/admin/files_thumbs/png_thumb.png" end if self.attachment_content_type.match(/zip|tar|tar\.gz|gz/) @@ -43,6 +50,52 @@ return "spud/admin/files_thumbs/mp3_thumb.png" end return "spud/admin/files_thumbs/dat_thumb.png" end + + # if you are using S3, attachment.url will automatically point to the S3 url + # protected files need to hit the rails middle-man first + # this method will provide the correct url for either case + def attachment_url + if Spud::Media.paperclip_storage == :s3 && is_protected + return Paperclip::Interpolations.interpolate(Spud::Media.config.storage_url, attachment, 'original') + else + return attachment.url + end + end + + # If is_protected has changed, we need to make sure we are setting the appropriate permissions + # This means either moving the file in the filesystem or setting the appropriate ACL in S3 + def validate_permissions + if Spud::Media.config.paperclip_storage == :filesystem + validate_permissions_filesystem + elsif Spud::Media.config.paperclip_storage == :s3 + validate_permissions_s3 + end + end + +private + + def validate_permissions_filesystem + if is_protected + old_path = Paperclip::Interpolations.interpolate(Spud::Media.config.storage_path, attachment, 'original') + new_path = Paperclip::Interpolations.interpolate(Spud::Media.config.storage_path_protected, attachment, 'original') + else + old_path = Paperclip::Interpolations.interpolate(Spud::Media.config.storage_path_protected, attachment, 'original') + new_path = Paperclip::Interpolations.interpolate(Spud::Media.config.storage_path, attachment, 'original') + end + new_base_dir = File.dirname(new_path) + old_base_dir= File.dirname(old_path) + if File.directory?(old_base_dir) + FileUtils.mv(old_base_dir, new_base_dir) + end + end + + def validate_permissions_s3 + if is_protected + attachment.s3_object.acl = :private + else + attachment.s3_object.acl = :public_read + end + end end