Class: Attachment

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#temp_fileObject

Returns the value of attribute temp_file.



13
14
15
# File 'app/models/attachment.rb', line 13

def temp_file
  @temp_file
end

Class Method Details

.find_live_by_file_path(file_path) ⇒ Object



179
180
181
# File 'app/models/attachment.rb', line 179

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

.storage_locationObject

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



171
172
173
# File 'app/models/attachment.rb', line 171

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

.storage_location=(storage_location) ⇒ Object



175
176
177
# File 'app/models/attachment.rb', line 175

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

Instance Method Details

#clear_ivarsObject



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

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



221
222
223
224
# File 'app/models/attachment.rb', line 221

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

#extract_file_extension_from_file_nameObject



77
78
79
80
81
# File 'app/models/attachment.rb', line 77

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



89
90
91
92
93
# File 'app/models/attachment.rb', line 89

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



83
84
85
86
87
# File 'app/models/attachment.rb', line 83

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

#file_nameObject

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



185
186
187
# File 'app/models/attachment.rb', line 185

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

#full_file_locationObject



215
216
217
# File 'app/models/attachment.rb', line 215

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

#iconObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'app/models/attachment.rb', line 193

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 —————————————————–



67
68
69
# File 'app/models/attachment.rb', line 67

def make_dirty_if_temp_file
  dirty! if temp_file
end

#nameObject



189
190
191
# File 'app/models/attachment.rb', line 189

def name
  file_name
end

#parse_credentials(creds) ⇒ Object



140
141
142
143
# File 'app/models/attachment.rb', line 140

def parse_credentials creds
  creds = find_credentials(creds).stringify_keys
  (creds[RAILS_ENV] || creds).symbolize_keys
end

#prepend_file_path_with_slashObject



71
72
73
74
75
# File 'app/models/attachment.rb', line 71

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

#process_sectionObject



106
107
108
109
110
111
112
113
# File 'app/models/attachment.rb', line 106

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)


211
212
213
# File 'app/models/attachment.rb', line 211

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

#sectionObject



53
54
55
# File 'app/models/attachment.rb', line 53

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

#section=(section) ⇒ Object



57
58
59
60
61
62
63
# File 'app/models/attachment.rb', line 57

def section=(section)
  if @section != section
    dirty!
    @section_id = section ? section.id : nil
    @section = section
  end
end

#section_idObject

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



42
43
44
# File 'app/models/attachment.rb', line 42

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

#section_id=(section_id) ⇒ Object



46
47
48
49
50
51
# File 'app/models/attachment.rb', line 46

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



99
100
101
102
103
104
# File 'app/models/attachment.rb', line 99

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



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/models/attachment.rb', line 115

def write_temp_file_to_storage_location
  unless temp_file.blank?
    FileUtils.mkdir_p File.dirname(full_file_location)  if !Cms.file_storage_on_s3
    if temp_file.local_path

      if Cms.file_storage_on_s3
        credentials = parse_credentials(Cms.s3_options[:s3_credentials])
        debugger
        s3 = RightAws::S3.new(credentials[:access_key_id], credentials[:secret_access_key] )
        bucket = s3.bucket(Cms.s3_options[:s3_bucket], true, 'public-read')
        key = RightAws::S3::Key.create(bucket, file_location)
        key.put(temp_file.read,'public-read')
      else
        FileUtils.copy temp_file.local_path, full_file_location
      end
    else
      open(full_file_location, 'w') {|f| f << temp_file.read }
    end
    
    if Cms.attachment_file_permission  && !Cms.file_storage_on_s3
      FileUtils.chmod Cms.attachment_file_permission, full_file_location
    end
  end
end