cirandas.net

ref: master

app/models/uploaded_file.rb


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 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
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# Article type that handles uploaded files.
#
# Limitation: only file metadata are versioned. Only the latest version
# of the file itself is kept. (FIXME?)

require 'sdbm' unless RUBY_ENGINE == 'jruby'

class UploadedFile < Article

  attr_accessible :uploaded_data, :title

  include Noosfero::Plugin::HotSpot

  def environment
    profile.environment
  end

  def self.type_name
    _('File')
  end

  DBM_PRIVATE_FILE = 'cache/private_files'
  after_save do |uploaded_file|
    if uploaded_file.published_changed?
      dbm = SDBM.open(DBM_PRIVATE_FILE)
      if uploaded_file.published
        dbm.delete(uploaded_file.public_filename)
      else
        dbm.store(uploaded_file.public_filename, uploaded_file.full_path)
      end
      dbm.close
    end
  end unless RUBY_ENGINE == 'jruby'

  track_actions :upload_image, :after_create, :keep_params => ["view_url", "thumbnail_path", "parent.url", "parent.name"], :if => Proc.new { |a| a.published? && a.image? && !a.parent.nil? && a.parent.gallery? }, :custom_target => :parent

  def title
    if self.name.present? then self.name else self.filename end
  end
  def title= value
    self.name = value
  end

  sanitize_filename

  before_create do |uploaded_file|
    uploaded_file.is_image = true if uploaded_file.image?
  end

  def thumbnail_path
    self.image? ? self.full_filename(:display).to_s.gsub(Rails.root.join('public').to_s, '') : nil
  end

  def first_paragraph
    ''
  end

  def self.max_size
    default = 5.megabytes

    multipliers = {
      :KB => :kilobytes,
      :MB => :megabytes,
      :GB => :gigabytes,
      :TB => :terabytes,
    }
    max_upload_size = NOOSFERO_CONF['max_upload_size']

    if max_upload_size =~ /^(\d+(\.\d+)?)\s*(KB|MB|GB|TB)?$/
      number = $1.to_f
      unit = $3 || :MB
      multiplier = multipliers[unit.to_sym]

      number.send(multiplier).to_i
    else
      default
    end
  end

  # FIXME need to define min/max file size
  #
  # default max_size is 1.megabyte to redefine it set options:
  #  :min_size => 2.megabytes
  #  :max_size => 5.megabytes
  has_attachment :storage => :file_system,
    :thumbnails => { :icon => [24,24], :bigicon => [50,50], :thumb => '130x130>', :slideshow => '320x240>', :display => '640X480>' },
    :thumbnail_class => Thumbnail,
    :max_size => self.max_size,
    processor: 'Rmagick'

  validates_attachment :size => N_("{fn} of uploaded file was larger than the maximum size of %{size}").sub('%{size}', self.max_size.to_humanreadable).fix_i18n

  extend DelayedAttachmentFu::ClassMethods
  delay_attachment_fu_thumbnails

  postgresql_attachment_fu

  # Use this method only to get the generic icon for this kind of content.
  # If you want the specific icon for a file type or the iconified version
  # of an image, use FilePresenter.for(uploaded_file).icon_name
  def self.icon_name(article = nil)
    unless article.nil?
      warn = ('='*80) + "\n" +
             'The method `UploadedFile.icon_name(obj)` is deprecated. ' +
             'You must to encapsulate UploadedFile with `FilePresenter.for()`.' +
             "\n" + ('='*80)
      raise NoMethodError, warn if ENV['RAILS_ENV'] == 'test'
      Rails.logger.warn warn if Rails.logger
      puts warn if ENV['RAILS_ENV'] == 'development'
    end
    'upload-file'
  end

  def mime_type
    content_type
  end

  def self.short_description
    _("Uploaded file")
  end

  def self.description
    _('Upload any kind of file you want.')
  end

  alias :orig_set_filename :filename=
  def filename=(value)
    orig_set_filename(value)
    self.name ||= self.filename
  end

  def download_disposition
    case content_type
    when 'application/pdf'
      'inline'
    else
      'attachment'
    end
  end

  def data
    File.read(self.full_filename) rescue nil
  end

  def to_html(options = {})
    warn = ('='*80) + "\n" +
           'The method `UploadedFile#to_html()` is deprecated. ' +
           'You must to encapsulate UploadedFile with `FilePresenter.for()`.' +
           "\n" + ('='*80)
    raise NoMethodError, warn if ENV['RAILS_ENV'] == 'test'
    Rails.logger.warn warn if Rails.logger
    puts warn if ENV['RAILS_ENV'] == 'development'
    article = self
    if image?
      proc do
        image_tag(article.public_filename(:display),
                  :class => article.css_class_name,
                  :style => 'max-width: 100%') +
        content_tag('div', article.abstract, :class => 'uploaded-file-description')
      end
    else
      proc do
        content_tag('div',
                    link_to(article.name, article.url),
                    :class => article.css_class_name) +
        content_tag('div', article.abstract, :class => 'uploaded-file-description')
      end
    end
  end

  def extension
    dotindex = self.filename.rindex('.')
    return nil unless dotindex
    self.filename[(dotindex+1)..-1].downcase
  end

  def allow_children?
    false
  end

  def can_display_hits?
    false
  end

  def gallery?
    self.parent && self.parent.folder? && self.parent.gallery?
  end

  def uploaded_file?
    true
  end

  def image?
    mime_type =~ /^image\//
  end

  def notifiable?
    !image?
  end

  protected

  def sanitize_filename filename
    # let accents and other utf8
    # overwrite vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu.rb
    filename
  end

end