Class: Attachment

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/attachment.rb

Constant Summary collapse

UNKNOWN_MIME_TYPE =

—– Constants —————————————————————-

'application/octet-stream'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#uploaded_fileObject 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/attachment.rb', line 18

def uploaded_file
  @uploaded_file
end

Class Method Details

.find_live_by_file_path(file_path) ⇒ Object



163
164
165
# File 'app/models/attachment.rb', line 163

def self.find_live_by_file_path(file_path)
  Attachment.published.not_archived.first(:conditions => {:file_path => file_path})
end

.storage_locationObject

—– Class Methods ———————————————————



155
156
157
# File 'app/models/attachment.rb', line 155

def self.storage_location
  @storage_location ||= File.join(Rails.root, "/tmp/uploads")
end

.storage_location=(storage_location) ⇒ Object



159
160
161
# File 'app/models/attachment.rb', line 159

def self.storage_location=(storage_location)
  @storage_location = storage_location
end

Instance Method Details

#clear_ivarsObject



147
148
149
150
151
# File 'app/models/attachment.rb', line 147

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



205
206
207
208
# File 'app/models/attachment.rb', line 205

def dirty!
  # Seems like a hack, is there a better way?
  self.updated_at = Time.now
end

#extract_file_extension_from_file_nameObject



86
87
88
89
90
# File 'app/models/attachment.rb', line 86

def extract_file_extension_from_file_name
  if file_name && file_name['.']
    self.file_extension = file_name.split('.').last.to_s.downcase
  end    
end

#extract_file_size_from_temp_fileObject



103
104
105
106
107
# File 'app/models/attachment.rb', line 103

def extract_file_size_from_temp_file  
  unless temp_file.blank?
    self.file_size = temp_file.size
  end    
end

#extract_file_type_from_temp_fileObject



92
93
94
95
96
97
98
99
100
101
# File 'app/models/attachment.rb', line 92

def extract_file_type_from_temp_file
  unless temp_file.blank?
    self.file_type = temp_file.content_type

    # Some
    if self.file_type == nil
      self.file_type = UNKNOWN_MIME_TYPE
    end
  end    
end

#file_nameObject

—– Instance Methods ——————————————————



169
170
171
# File 'app/models/attachment.rb', line 169

def file_name
  file_path ? file_path.split('/').last : nil
end

#full_file_locationObject



199
200
201
# File 'app/models/attachment.rb', line 199

def full_file_location
  File.join(Attachment.storage_location, file_location)
end

#iconObject



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'app/models/attachment.rb', line 177

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_fileObject

—– Callbacks Methods —————————————————–



76
77
78
# File 'app/models/attachment.rb', line 76

def make_dirty_if_temp_file
  dirty! if temp_file
end

#nameObject



173
174
175
# File 'app/models/attachment.rb', line 173

def name
  file_name
end

#prepend_file_path_with_slashObject



80
81
82
83
84
# File 'app/models/attachment.rb', line 80

def prepend_file_path_with_slash    
  unless file_path.blank?
    self.file_path = "/#{file_path}" unless file_path =~ /^\//
  end
end

#process_sectionObject



120
121
122
123
124
125
126
127
# File 'app/models/attachment.rb', line 120

def process_section
  #logger.info "processing section, section_id => #{section_id}, section_node => #{section_node.inspect}"
  if section_node && !section_node.new_record? && section_node.section_id != section_id
    section_node.move_to_end(Section.find(section_id))
  else
    build_section_node(:node => self, :section_id => section_id)
  end    
end

#public?Boolean

Returns:

  • (Boolean)


195
196
197
# File 'app/models/attachment.rb', line 195

def public?
  section ? section.public? : false
end

#sectionObject



61
62
63
# File 'app/models/attachment.rb', line 61

def section
  @section ||= section_node ? section_node.section : nil
end

#section=(section) ⇒ Object



65
66
67
68
69
70
71
72
# File 'app/models/attachment.rb', line 65

def section=(section)
  logger.debug {"Attachment#section=(#{section})"}
  if @section != section
    dirty!
    @section_id = section ? section.id : nil
    @section = section
  end
end

#section_idObject

—– Virtual Attributes —————————————————-



50
51
52
# File 'app/models/attachment.rb', line 50

def section_id
  @section_id ||= section_node ? section_node.section_id : nil
end

#section_id=(section_id) ⇒ Object



54
55
56
57
58
59
# File 'app/models/attachment.rb', line 54

def section_id=(section_id)
  if @section_id != section_id 
    dirty!
    @section_id = section_id
  end
end

#set_file_locationObject

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



113
114
115
116
117
118
# File 'app/models/attachment.rb', line 113

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_locationObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/models/attachment.rb', line 129

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