Sha256: 10b9b56ed7f37e17983977488dc7f29e525161d3a1dfef300ca9af1e39c84cf0
Contents?: true
Size: 1.85 KB
Versions: 1
Compression:
Stored size: 1.85 KB
Contents
module Slightcms class Asset < ActiveRecord::Base # Change default table name set_table_name "slightcms_assets" # Validations validates_presence_of :name validates_presence_of :filename validates_presence_of :size validates_presence_of :content_type # Callbacks after_create :store_file_in_filesystem after_destroy :remove_file_from_file_system def file=(uploaded_file) @uploaded_file = uploaded_file write_attribute(:content_type, @uploaded_file.content_type) write_attribute(:size, File.size(@uploaded_file)) write_attribute(:filename, sanitize_filename(@uploaded_file.original_filename)) end # Method to get the public file path def public_file_path "slightcms/assets/#{self.id}/#{self.filename}" end private # Method to store file in filesystem def store_file_in_filesystem if !File.exists?(File.dirname(absolute_file_path)) Dir.mkdir(File.dirname(absolute_file_path)) end if @uploaded_file.instance_of?(Tempfile) FileUtils.copy(@uploaded_file.local_path, absolute_file_path) else File.open(absolute_file_path, "wb") { |f| f.write(@uploaded_file.read) } end end # Method to remove the file from the filesystem def remove_file_from_file_system if File.exists?(absolute_file_path) File.delete(absolute_file_path) Dir.rmdir(File.dirname(absolute_file_path)) end end # Method to get the absolute path for the file def absolute_file_path File.expand_path("#{RAILS_ROOT}/public/slightcms/assets/#{self.id}/#{self.filename}") end # Replace non standard signs with underscores def sanitize_filename(filename) just_filename = File.basename(filename) just_filename.gsub(/[^\w\.\_]/,'_') end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
slightcms-0.0.11 | lib/app/models/asset.rb |