Class: SpudMedia
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- SpudMedia
- Defined in:
- app/models/spud_media.rb
Instance Method Summary collapse
-
#attachment_url(style = nil) ⇒ Object
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.
- #dynamic_styles ⇒ Object
- #has_custom_crop? ⇒ Boolean
- #image_from_type ⇒ Object
- #is_image? ⇒ Boolean
- #is_pdf? ⇒ Boolean
- #rename_file ⇒ Object
-
#validate_permissions ⇒ Object
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.
Instance Method Details
#attachment_url(style = nil) ⇒ Object
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
105 106 107 108 109 110 111 112 113 114 115 |
# File 'app/models/spud_media.rb', line 105 def (style=nil) # defaults to cropped style if that style exists, otherwise use original if !style style = (is_image? && has_custom_crop?) ? 'cropped' : 'original' end if Spud::Media.paperclip_storage == :s3 && is_protected return Paperclip::Interpolations.interpolate(Spud::Media.config.storage_url, , style) else return .url(style) end end |
#dynamic_styles ⇒ Object
91 92 93 94 95 96 97 98 99 100 |
# File 'app/models/spud_media.rb', line 91 def dynamic_styles styles = {} if is_image? #|| is_pdf? styles[:small] = '50' if has_custom_crop? styles[:cropped] = {:geometry => '', :convert_options => "-resize #{crop_s}% -crop #{crop_w}x#{crop_h}+#{crop_x}+#{crop_y}"} end end return styles end |
#has_custom_crop? ⇒ Boolean
87 88 89 |
# File 'app/models/spud_media.rb', line 87 def has_custom_crop? return (crop_x && crop_y && crop_w && crop_h && crop_s) end |
#image_from_type ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'app/models/spud_media.rb', line 32 def image_from_type if self.is_image? return self.(:small) elsif self..blank? return "admin/files_thumbs/dat_thumb.png" elsif self..match(/jpeg|jpg/) return "admin/files_thumbs/jpg_thumb.png" elsif self..match(/png/) return "admin/files_thumbs/png_thumb.png" elsif self..match(/zip|tar|tar\.gz|gz/) return "admin/files_thumbs/zip_thumb.png" elsif self..match(/xls|xlsx/) return "admin/files_thumbs/xls_thumb.png" elsif self..match(/doc|docx/) return "admin/files_thumbs/doc_thumb.png" elsif self..match(/ppt|pptx/) return "admin/files_thumbs/ppt_thumb.png" elsif self..match(/txt|text/) return "admin/files_thumbs/txt_thumb.png" elsif self..match(/pdf|ps/) return "admin/files_thumbs/pdf_thumb.png" elsif self..match(/mp3|wav|aac/) return "admin/files_thumbs/mp3_thumb.png" end return "admin/files_thumbs/dat_thumb.png" end |
#is_image? ⇒ Boolean
71 72 73 74 75 76 77 |
# File 'app/models/spud_media.rb', line 71 def is_image? if self..match(/jpeg|jpg|png/) return true else return false end end |
#is_pdf? ⇒ Boolean
79 80 81 82 83 84 85 |
# File 'app/models/spud_media.rb', line 79 def is_pdf? if self..match(/pdf/) return true else return false end end |
#rename_file ⇒ Object
25 26 27 28 29 30 |
# File 'app/models/spud_media.rb', line 25 def rename_file # remove periods and other unsafe characters from file name to make routing easier extension = File.extname() filename = .chomp(extension).parameterize .instance_write :file_name, filename + extension end |
#validate_permissions ⇒ Object
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
119 120 121 122 123 124 125 |
# File 'app/models/spud_media.rb', line 119 def if Spud::Media.config.paperclip_storage == :filesystem elsif Spud::Media.config.paperclip_storage == :s3 end end |