Module: Attacheable::Uploading
- Included in:
- Attacheable
- Defined in:
- lib/attacheable/uploading.rb
Instance Method Summary collapse
- #accepts_file_type_for_upload?(file_type) ⇒ Boolean
- #handle_uploaded_file ⇒ Object
- #identify_image_properties(path) ⇒ Object
- #identify_uploaded_file_type ⇒ Object
- #prepare_merb_uploaded_file(file_data) ⇒ Object
- #prepare_uploaded_file(file_data) ⇒ Object
- #save_to_replicas ⇒ Object
-
#save_to_storage ⇒ Object
Saves the file to the file system.
Instance Method Details
#accepts_file_type_for_upload?(file_type) ⇒ Boolean
62 63 64 65 66 |
# File 'lib/attacheable/uploading.rb', line 62 def accepts_file_type_for_upload?(file_type) return false unless @tempfile return true if [:valid_filetypes] == :all return true if [:valid_filetypes].include?(file_type) end |
#handle_uploaded_file ⇒ Object
68 69 70 71 |
# File 'lib/attacheable/uploading.rb', line 68 def handle_uploaded_file @save_new_attachment = true @valid_filetype = true end |
#identify_image_properties(path) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/attacheable/uploading.rb', line 47 def identify_image_properties(path) return [nil,nil,nil] if path.blank? output = nil silence_stderr do output = `identify "#{path}"` end if output && match_data = / (\w+) (\d+)x(\d+) /.match(output) file_type = match_data[1].to_s.downcase width = match_data[2] height = match_data[3] return [file_type, width, height] end end |
#identify_uploaded_file_type ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/attacheable/uploading.rb', line 32 def identify_uploaded_file_type return unless @tempfile self.content_type = @tempfile.content_type if @tempfile.respond_to?(:content_type) if content_type.blank? || content_type =~ /image\// || content_type == "application/octet-stream" file_type, width, height = identify_image_properties(@tempfile.path) if file_type self.width = width if(respond_to?(:width=)) self.height = height if(respond_to?(:height=)) self.content_type = "image/#{file_type}" file_type end end end |
#prepare_merb_uploaded_file(file_data) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/attacheable/uploading.rb', line 23 def prepare_merb_uploaded_file(file_data) return nil if file_data["tempfile"].blank? || file_data["filename"].blank? self.filename = file_data["filename"] self.size = file_data["size"] if respond_to?(:size=) @tempfile = file_data["tempfile"] @save_new_attachment = true @valid_filetype = false end |
#prepare_uploaded_file(file_data) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/attacheable/uploading.rb', line 3 def prepare_uploaded_file(file_data) return prepare_merb_uploaded_file(file_data) if file_data.is_a?(Hash) && file_data["tempfile"] return nil if file_data.blank? || !file_data.respond_to?(:original_filename) || !respond_to?(:filename=) self.filename = file_data.original_filename if file_data.is_a?(StringIO) file_data.rewind @tempfile = Tempfile.new(filename) @tempfile.write(file_data.read) @tempfile.close else @tempfile = file_data end if respond_to?(:size=) self.size = file_data.respond_to?(:size) ? file_data.size : @tempfile.respond_to?(:size) ? @tempfile.size : File.size(@tempfile.path) end @save_new_attachment = true @valid_filetype = false end |
#save_to_replicas ⇒ Object
86 87 88 89 90 91 |
# File 'lib/attacheable/uploading.rb', line 86 def save_to_replicas [:replicas].each do |replica| system("ssh #{replica[:user]}@#{replica[:host]} mkdir -p \"#{File.dirname(full_filename)}\"") system("scp \"#{full_filename}\" \"#{replica[:user]}@#{replica[:host]}:#{full_filename}\"") end if [:replicas] end |
#save_to_storage ⇒ Object
Saves the file to the file system
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/attacheable/uploading.rb', line 74 def save_to_storage if @save_new_attachment FileUtils.mkdir_p(File.dirname(full_filename)) FileUtils.cp(@tempfile.path, full_filename) File.chmod(0644, full_filename) save_to_replicas end @save_new_attachment = false @tempfile = nil true end |