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



115
116
117
118
119
120
121
122
123
124
125
# File 'app/models/spud_media.rb', line 115

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



101
102
103
104
105
106
107
108
109
110
# File 'app/models/spud_media.rb', line 101

def dynamic_styles
  styles = {}
  if is_image?
    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)


97
98
99
# File 'app/models/spud_media.rb', line 97

def has_custom_crop?
  return (crop_x && crop_y && crop_w && crop_h && crop_s)
end

#image_from_typeObject



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
70
71
72
73
74
75
76
77
78
79
# File 'app/models/spud_media.rb', line 40

def image_from_type
  if self.attachment_content_type.blank?
    return "spud/admin/files_thumbs/dat_thumb.png"
  end
  if self.is_image?
    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

#is_image?Boolean

Returns:

  • (Boolean)


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

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

#is_pdf?Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
95
# File 'app/models/spud_media.rb', line 89

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

#rename_fileObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/models/spud_media.rb', line 27

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
  if self.attachment_file_name.blank? == false
    extension = File.extname(attachment_file_name)
    filename = attachment_file_name.chomp(extension).parameterize
    attachment.instance_write :file_name, filename + extension
  end
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



129
130
131
132
133
134
135
# File 'app/models/spud_media.rb', line 129

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