Class: SpudMedia

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/spud_media.rb

Instance Method Summary collapse

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 attachment_url(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, attachment, style)
  else
    return attachment.url(style)
  end
end

#dynamic_stylesObject



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

Returns:

  • (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_typeObject



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.attachment_url(:small)

  elsif self.attachment_content_type.blank?
  	return "admin/files_thumbs/dat_thumb.png"
  
  elsif self.attachment_content_type.match(/jpeg|jpg/)
  	return "admin/files_thumbs/jpg_thumb.png"
  
  elsif self.attachment_content_type.match(/png/)
  	return "admin/files_thumbs/png_thumb.png"
  
  elsif self.attachment_content_type.match(/zip|tar|tar\.gz|gz/)
  	return "admin/files_thumbs/zip_thumb.png"
  
  elsif self.attachment_content_type.match(/xls|xlsx/)
  	return "admin/files_thumbs/xls_thumb.png"
  
  elsif self.attachment_content_type.match(/doc|docx/)
  	return "admin/files_thumbs/doc_thumb.png"
  
  elsif self.attachment_content_type.match(/ppt|pptx/)
  	return "admin/files_thumbs/ppt_thumb.png"
  
  elsif self.attachment_content_type.match(/txt|text/)
  	return "admin/files_thumbs/txt_thumb.png"
  
  elsif self.attachment_content_type.match(/pdf|ps/)
  	return "admin/files_thumbs/pdf_thumb.png"
  
  elsif self.attachment_content_type.match(/mp3|wav|aac/)
  	return "admin/files_thumbs/mp3_thumb.png"
  end
  
  return "admin/files_thumbs/dat_thumb.png"
end

#is_image?Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
77
# File 'app/models/spud_media.rb', line 71

def is_image?
  if self.attachment_content_type.match(/jpeg|jpg|png/)
    return true
  else
    return false
  end
end

#is_pdf?Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
85
# File 'app/models/spud_media.rb', line 79

def is_pdf?
  if self.attachment_content_type.match(/pdf/)
    return true
  else
    return false
  end
end

#rename_fileObject



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(attachment_file_name)
  filename = attachment_file_name.chomp(extension).parameterize
  attachment.instance_write :file_name, filename + extension
end

#validate_permissionsObject

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 validate_permissions
  if Spud::Media.config.paperclip_storage == :filesystem
    validate_permissions_filesystem
  elsif Spud::Media.config.paperclip_storage == :s3
    validate_permissions_s3
  end
end