app/models/spud_media.rb in spud_media-0.9.5 vs app/models/spud_media.rb in spud_media-0.9.6
- old
+ new
@@ -1,28 +1,33 @@
class SpudMedia < ActiveRecord::Base
has_attached_file :attachment,
:storage => Spud::Media.paperclip_storage,
:s3_credentials => Spud::Media.s3_credentials,
- :s3_permissions => lambda { |attachment, style|
- attachment.instance.is_protected ? 'private' : 'public-read'
+ :s3_permissions => lambda { |attachment, style|
+ attachment.instance.is_protected ? 'private' : 'public-read'
},
- :path => Spud::Media.paperclip_storage == :s3 ? Spud::Media.storage_path : lambda { |attachment|
+ :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,
:styles => lambda { |attachment| attachment.instance.dynamic_styles }
attr_accessible :attachment_content_type,:attachment_file_name,:attachment_file_size,:attachment, :is_protected, :crop_x, :crop_y, :crop_w, :crop_h, :crop_s
validates_numericality_of :crop_x, :crop_y, :crop_w, :crop_h, :crop_s, :allow_nil => true
before_create :rename_file
+ before_update :rename_file
#after_create :validate_permissions
before_update :validate_permissions
def rename_file
+ if self.attachment_file_name_changed? && self.attachment_file_name_was.nil? == false
+ attachment.instance_write :file_name, self.attachment_file_name_was
+ return
+ end
# remove periods and other unsafe characters from file name to make routing easier
extension = File.extname(attachment_file_name)
filename = attachment_file_name.chomp(extension).parameterize
attachment.instance_write :file_name, filename + extension
end
@@ -32,39 +37,39 @@
if self.is_image? || self.is_pdf?
return self.attachment_url(:small)
elsif self.attachment_content_type.blank?
return "spud/admin/files_thumbs/dat_thumb.png"
-
+
elsif self.attachment_content_type.match(/jpeg|jpg/)
return "spud/admin/files_thumbs/jpg_thumb.png"
-
+
elsif self.attachment_content_type.match(/png/)
return "spud/admin/files_thumbs/png_thumb.png"
-
+
elsif self.attachment_content_type.match(/zip|tar|tar\.gz|gz/)
return "spud/admin/files_thumbs/zip_thumb.png"
-
+
elsif self.attachment_content_type.match(/xls|xlsx/)
return "spud/admin/files_thumbs/xls_thumb.png"
-
+
elsif self.attachment_content_type.match(/doc|docx/)
return "spud/admin/files_thumbs/doc_thumb.png"
-
+
elsif self.attachment_content_type.match(/ppt|pptx/)
return "spud/admin/files_thumbs/ppt_thumb.png"
-
+
elsif self.attachment_content_type.match(/txt|text/)
return "spud/admin/files_thumbs/txt_thumb.png"
-
+
elsif self.attachment_content_type.match(/pdf|ps/)
return "spud/admin/files_thumbs/pdf_thumb.png"
-
+
elsif self.attachment_content_type.match(/mp3|wav|aac/)
return "spud/admin/files_thumbs/mp3_thumb.png"
end
-
+
return "spud/admin/files_thumbs/dat_thumb.png"
end
def is_image?
if self.attachment_content_type.match(/jpeg|jpg|png/)
@@ -128,10 +133,10 @@
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')
+ new_path = Paperclip::Interpolations.interpolate(Spud::Media.config.storage_path, attachment, 'original')
end
new_base_dir = File.dirname(File.dirname(new_path))
old_base_dir= File.dirname(File.dirname(old_path))
if File.directory?(old_base_dir)
FileUtils.mv(old_base_dir, new_base_dir)