uploaded_file.rb
3.93 KB
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
# Article type that handles uploaded files.
#
# Limitation: only file metadata are versioned. Only the latest version
# of the file itself is kept. (FIXME?)
class UploadedFile < Article
  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? }
  include ShortFilename
  settings_items :title, :type => 'string'
  xss_terminate :only => [ :title ]
  def title_with_default
    title_without_default || short_filename(name, 60)
  end
  alias_method_chain :title, :default
  validates_size_of :title, :maximum => 60, :if => (lambda { |file| !file.title.blank? })
  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(:thumb).gsub(File.join(RAILS_ROOT, 'public'), '') : nil
  end
  def display_title
    title.blank? ? name : title
  end
  def first_paragraph
    ''
  end
  def self.max_size
    UploadedFile.attachment_options[:max_size]
  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], :thumb => '130x130>', :slideshow => '320x240>', :display => '640X480>' },
    :thumbnail_class => Thumbnail,
    :max_size => 5.megabytes # remember to update validate message below
  validates_attachment :size => N_("%{fn} of uploaded file was larger than the maximum size of 5.0 MB")
  delay_attachment_fu_thumbnails
  postgresql_attachment_fu
  def self.icon_name(article = nil)
    if article
      article.image? ? article.public_filename(:icon) : (article.mime_type ? article.mime_type.gsub(/[\/+.]/, '-') : 'upload-file')
    else
      'upload-file'
    end
  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 data
    File.read(self.full_filename)
  end
  def to_html(options = {})
    article = self
    if image?
      lambda do
        if article.gallery? && options[:gallery_view]
          images = article.parent.images
          current_index = images.index(article)
          total_of_images = images.count
          link_to_previous = if current_index >= 1
            link_to(_('« Previous'), images[current_index - 1].view_url, :class => 'left')
          else
            content_tag('span', _('« Previous'), :class => 'left')
          end
          link_to_next = if current_index < total_of_images - 1
            link_to(_('Next »'), images[current_index + 1].view_url, :class => 'right')
          else
            content_tag('span', _('Next »'), :class => 'right')
          end
          content_tag(
            'div',
            link_to_previous + content_tag('span', _('image %d of %d'), :class => 'total-of-images') % [current_index + 1, total_of_images] + link_to_next,
            :class => 'gallery-navigation'
          )
        end.to_s +
        image_tag(article.public_filename(:display), :class => article.css_class_name, :style => 'max-width: 100%') +
          content_tag('p', article.abstract, :class => 'uploaded-file-description')
      end
    else
      lambda do
        content_tag('ul', content_tag('li', link_to(article.name, article.url, :class => article.css_class_name))) +
          content_tag('p', article.abstract, :class => 'uploaded-file-description')
      end
    end
  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
end