Module: Uploader::Models::Upload

Extended by:
ActiveSupport::Concern
Defined in:
lib/uploader/models/upload.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#calculate_sizes(style) ⇒ Object



183
184
185
186
187
188
189
190
191
192
# File 'lib/uploader/models/upload.rb', line 183

def calculate_sizes(style)
  if image_ratio > 1
    @image_width ||= width > max_dimension(style) ? max_dimension(style) : width
    @image_height ||= (@image_width / image_ratio).round
  else
    @image_height ||= height > max_dimension(style) ? max_dimension(style) : height
    @image_width ||= (@image_height * image_ratio).round
  end
  @image_size ||= "#{@image_width.to_i}x#{@image_height.to_i}"
end

#can_edit?(check_user) ⇒ Boolean

Returns:

  • (Boolean)


147
148
149
150
# File 'lib/uploader/models/upload.rb', line 147

def can_edit?(check_user)
  return false if check_user.blank?
  check_user == self.creator
end

#determine_immediate_send_to_remoteObject



53
54
55
56
57
# File 'lib/uploader/models/upload.rb', line 53

def determine_immediate_send_to_remote
  if Uploader.configuration.s3_no_wait
    self.remote = local.to_file # This will result in the file being sent to S3
  end
end

#display_nameObject



143
144
145
# File 'lib/uploader/models/upload.rb', line 143

def display_name
  CGI::escapeHTML(self.local_file_name)
end

#fileObject



45
46
47
# File 'lib/uploader/models/upload.rb', line 45

def file
  local_file_name ? local : remote
end

#file_nameObject



49
50
51
# File 'lib/uploader/models/upload.rb', line 49

def file_name
  remote_file_name || local_file_name
end

#height(style = :default) ⇒ Object



161
162
163
164
165
166
# File 'lib/uploader/models/upload.rb', line 161

def height(style = :default)
  return nil unless self[:height]
  return self[:height] if style == :default
  calculate_sizes(style.to_sym)
  return @image_height.to_i
end

#iconObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/uploader/models/upload.rb', line 118

def icon
  if self.is_pdf?
    '/images/file_icons/file_pdf.gif'
  elsif self.is_word?
    '/images/file_icons/file_doc.gif'
  elsif self.is_image?
    self.file.url(:icon)
  elsif self.is_mp3?
    '/images/file_icons/file_mp3.gif'
  elsif self.is_excel?
    '/images/file_icons/file_xls.gif'
  elsif self.is_text?
    '/images/file_icons/file_txt.gif'
  else
    '/images/file_icons/file_raw.gif'
  end
end

#image_ratioObject



179
180
181
# File 'lib/uploader/models/upload.rb', line 179

def image_ratio
  @image_ratio ||= width.to_f / height.to_f
end

#is_excel?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/uploader/models/upload.rb', line 84

def is_excel?
  Uploader::MimeTypeGroups::EXCEL_TYPES.include?(self.local_content_type)
end

#is_image?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/uploader/models/upload.rb', line 76

def is_image?
  Uploader::MimeTypeGroups::IMAGE_TYPES.include?(self.local_content_type)
end

#is_mp3?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/uploader/models/upload.rb', line 80

def is_mp3?
  Uploader::MimeTypeGroups::MP3_TYPES.include?(self.local_content_type)
end

#is_pdf?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/uploader/models/upload.rb', line 88

def is_pdf?
  Uploader::MimeTypeGroups::PDF_TYPES.include?(self.local_content_type)
end

#is_text?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/uploader/models/upload.rb', line 96

def is_text?
  Uploader::MimeTypeGroups::TEXT_TYPES.include?(self.local_content_type)
end

#is_word?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/uploader/models/upload.rb', line 92

def is_word?
  Uploader::MimeTypeGroups::WORD_TYPES.include?(self.local_content_type)
end

#max_dimension(style) ⇒ Object



175
176
177
# File 'lib/uploader/models/upload.rb', line 175

def max_dimension(style)
  @max_dimension ||= Paperclip::Geometry.parse(self.local.styles[style][:geometry]).width.to_f
end

#multiupload_local=(filedata) ⇒ Object



71
72
73
74
# File 'lib/uploader/models/upload.rb', line 71

def multiupload_local=(filedata)
  filedata.content_type = MIME::Types.type_for(filedata.original_filename)[0].to_s
  self.local = filedata
end

#send_to_remoteObject



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/uploader/models/upload.rb', line 59

def send_to_remote
  if local_file_name
    self.remote = local.to_file
    if self.save and remote.original_filename and remote.exists?
      self.local = nil unless Uploader.configuration.keep_local_file
      self.save
    else
      false
    end
  end
end

#size(style = :default) ⇒ Object



168
169
170
171
172
173
# File 'lib/uploader/models/upload.rb', line 168

def size(style = :default)
  return nil unless width || height
  return "#{width}x#{height}" if style == :default
  calculate_sizes(style.to_sym)
  return @image_size
end

#thumbObject

Only works for images



137
138
139
140
141
# File 'lib/uploader/models/upload.rb', line 137

def thumb
  if self.is_image?
    self.file.url(:thumb)
  end
end

#upload_typeObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/uploader/models/upload.rb', line 100

def upload_type
  if self.is_pdf?
    'Adobe pdf file'
  elsif self.is_word?
    'Word document'
  elsif self.is_image?
    'photo'
  elsif self.is_mp3?
    'mp3'
  elsif self.is_excel?
    'Excel document'
  elsif self.is_text?
    'text file'
  else
    'file'
  end
end

#width(style = :default) ⇒ Object

Image dimension calculations



154
155
156
157
158
159
# File 'lib/uploader/models/upload.rb', line 154

def width(style = :default)
  return nil unless self[:width]
  return self[:width] if style == :default
  calculate_sizes(style.to_sym)
  return @image_width.to_i
end