Sha256: cb39ee945eec5a60a4df2e9485f9309c1e041336b6d788e9f71f700a64609f44
Contents?: true
Size: 1.23 KB
Versions: 1
Compression:
Stored size: 1.23 KB
Contents
require "upload_documents_tool/version" module UploadDocumentsTool class Engine < ::Rails::Engine end def initialize(params = {}) @file = params.delete(:file) super if @file self.filename = sanitize_filename(@file.original_filename) self.content_type = @file.content_type self.file_contents = @file.read end end def upload_local path = "#{Rails.root}/public/uploads/document" FileUtils.mkdir_p(path) unless File.exists?(path) FileUtils.copy(@file.tempfile, path) end private def sanitize_filename(filename) File.basename(filename) end def document_file_format unless ['application/pdf','application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'text/plain', 'text/csv', 'application/octet-stream'].include? self.content_type errors.add(:file, 'Invalid file format.') end end NUM_BYTES_IN_MEGABYTE = 1048576 def file_size_under_one_mb if (@file.size.to_f / NUM_BYTES_IN_MEGABYTE) > 1 errors.add(:file, 'File size cannot be over one megabyte.') end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
upload_documents_tool-0.1.0 | lib/upload_documents_tool.rb |