uploaded_file.rb
4.36 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# 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
attr_accessible :uploaded_data, :title
def self.type_name
_('File')
end
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
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
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_headers
{
'Content-Disposition' => "attachment; filename=\"#{self.filename}\"",
}
end
def data
File.read(self.full_filename)
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
end