Class: Cms::Attachment
Constant Summary
collapse
- UNKNOWN_MIME_TYPE =
—– Constants —————————————————————-
'application/octet-stream'
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#build_node, #section, #section_id, #section_id=
#node=
#access_status
#ancestors, #cache_parent, #parent, #parent=, #partial_for
Instance Attribute Details
#uploaded_file ⇒ Object
Also known as:
temp_file
Rails 3 uses a new ActionDispatch::Http::UploadedFile. This holds that during the file processing.
18
19
20
|
# File 'app/models/cms/attachment.rb', line 18
def uploaded_file
@uploaded_file
end
|
Class Method Details
.find_live_by_file_path(file_path) ⇒ Object
136
137
138
|
# File 'app/models/cms/attachment.rb', line 136
def self.find_live_by_file_path(file_path)
Cms::Attachment.published.not_archived.first(:conditions => {:file_path => file_path})
end
|
.storage_location ⇒ Object
—– Class Methods ———————————————————
128
129
130
|
# File 'app/models/cms/attachment.rb', line 128
def self.storage_location
@storage_location ||= File.join(Rails.root, "/tmp/uploads")
end
|
.storage_location=(storage_location) ⇒ Object
132
133
134
|
# File 'app/models/cms/attachment.rb', line 132
def self.storage_location=(storage_location)
@storage_location = storage_location
end
|
Instance Method Details
#clear_ivars ⇒ Object
120
121
122
123
124
|
# File 'app/models/cms/attachment.rb', line 120
def clear_ivars
@temp_file = nil
@section = nil
@section_id = nil
end
|
#dirty! ⇒ Object
Forces this record to be changed, even if nothing has changed This is necessary if just the section.id has changed, for example
178
179
180
181
|
# File 'app/models/cms/attachment.rb', line 178
def dirty!
self.updated_at = Time.now
end
|
68
69
70
71
72
|
# File 'app/models/cms/attachment.rb', line 68
def
if file_name && file_name['.']
self.file_extension = file_name.split('.').last.to_s.downcase
end
end
|
85
86
87
88
89
|
# File 'app/models/cms/attachment.rb', line 85
def
unless temp_file.blank?
self.file_size = temp_file.size
end
end
|
74
75
76
77
78
79
80
81
82
83
|
# File 'app/models/cms/attachment.rb', line 74
def
unless temp_file.blank?
self.file_type = temp_file.content_type
if self.file_type == nil
self.file_type = UNKNOWN_MIME_TYPE
end
end
end
|
#file_name ⇒ Object
—– Instance Methods ——————————————————
142
143
144
|
# File 'app/models/cms/attachment.rb', line 142
def file_name
file_path ? file_path.split('/').last : nil
end
|
#full_file_location ⇒ Object
172
173
174
|
# File 'app/models/cms/attachment.rb', line 172
def full_file_location
File.join(Cms::Attachment.storage_location, file_location)
end
|
#icon ⇒ Object
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
# File 'app/models/cms/attachment.rb', line 150
def icon
{
:doc => %w[doc],
:gif => %w[gif jpg jpeg png tiff bmp],
:htm => %w[htm html],
:pdf => %w[pdf],
:ppt => %w[ppt],
:swf => %w[swf],
:txt => %w[txt],
:xls => %w[xls],
:xml => %w[xml],
:zip => %w[zip rar tar gz tgz]
}.each do |icon, extensions|
return icon if extensions.include?(file_extension.to_s)
end
:file
end
|
#make_dirty_if_temp_file ⇒ Object
—– Callbacks Methods —————————————————–
58
59
60
|
# File 'app/models/cms/attachment.rb', line 58
def make_dirty_if_temp_file
dirty! if temp_file
end
|
#name ⇒ Object
146
147
148
|
# File 'app/models/cms/attachment.rb', line 146
def name
file_name
end
|
#prepend_file_path_with_slash ⇒ Object
62
63
64
65
66
|
# File 'app/models/cms/attachment.rb', line 62
def prepend_file_path_with_slash
unless file_path.blank?
self.file_path = "/#{file_path}" unless file_path =~ /^\//
end
end
|
#public? ⇒ Boolean
168
169
170
|
# File 'app/models/cms/attachment.rb', line 168
def public?
section ? section.public? : false
end
|
#section=(section) ⇒ Object
—– Virtual Attributes —————————————————-
51
52
53
54
|
# File 'app/models/cms/attachment.rb', line 51
def section=(section)
dirty! if self.section != section
super(section)
end
|
#set_file_location ⇒ Object
The file will be stored on disk at Attachment.storage_location/year/month/day/sha1 The sha1 is a 40 character hash based on the original_filename of the file uploaded and the current time
95
96
97
98
99
100
|
# File 'app/models/cms/attachment.rb', line 95
def set_file_location
unless temp_file.blank?
sha1 = Digest::SHA1.hexdigest("#{temp_file.original_filename}#{Time.now.to_f}")
self.file_location = "#{Time.now.strftime("%Y/%m/%d")}/#{sha1}"
end
end
|
#write_temp_file_to_storage_location ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'app/models/cms/attachment.rb', line 102
def write_temp_file_to_storage_location
unless temp_file.blank?
actual_temp_file = temp_file.tempfile
Rails.logger.warn "I want to write out #{temp_file.tempfile}"
FileUtils.mkdir_p File.dirname(full_file_location)
if actual_temp_file.path
FileUtils.copy actual_temp_file.path, full_file_location
else
open(full_file_location, 'w') { |f| f << actual_temp_file.read }
end
if Cms.attachment_file_permission
FileUtils.chmod Cms.attachment_file_permission, full_file_location
end
end
end
|