Class: Attachment
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Attachment
- Defined in:
- app/models/attachment.rb
Constant Summary collapse
- @@storage_path =
Redmine::Configuration['attachments_storage_path'] || "#{RAILS_ROOT}/files"
Class Method Summary collapse
-
.attach_files(obj, attachments) ⇒ Object
Bulk attaches a set of files to an object.
Instance Method Summary collapse
-
#after_destroy ⇒ Object
Deletes file on the disk.
-
#before_save ⇒ Object
Copies the temporary file to its final location and computes its MD5 hash.
- #deletable?(user = User.current) ⇒ Boolean
-
#diskfile ⇒ Object
Returns file’s location on disk.
- #file ⇒ Object
- #file=(incoming_file) ⇒ Object
- #image? ⇒ Boolean
- #increment_download ⇒ Object
- #is_diff? ⇒ Boolean
- #is_text? ⇒ Boolean
- #project ⇒ Object
-
#readable? ⇒ Boolean
Returns true if the file is readable.
- #validate ⇒ Object
- #visible?(user = User.current) ⇒ Boolean
Class Method Details
.attach_files(obj, attachments) ⇒ Object
Bulk attaches a set of files to an object
Returns a Hash of the results: :files => array of the attached files :unsaved => array of the files that could not be attached
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'app/models/attachment.rb', line 142 def self.attach_files(obj, ) attached = [] if && .is_a?(Hash) .each_value do || file = ['file'] next unless file && file.size > 0 a = Attachment.create(:container => obj, :file => file, :description => ['description'].to_s.strip, :author => User.current) if a.new_record? obj. ||= [] obj. << a else attached << a end end end {:files => attached, :unsaved => obj.} end |
Instance Method Details
#after_destroy ⇒ Object
Deletes file on the disk
95 96 97 |
# File 'app/models/attachment.rb', line 95 def after_destroy File.delete(diskfile) if !filename.blank? && File.exist?(diskfile) end |
#before_save ⇒ Object
Copies the temporary file to its final location and computes its MD5 hash
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'app/models/attachment.rb', line 75 def before_save if @temp_file && (@temp_file.size > 0) logger.debug("saving '#{self.diskfile}'") md5 = Digest::MD5.new File.open(diskfile, "wb") do |f| buffer = "" while (buffer = @temp_file.read(8192)) f.write(buffer) md5.update(buffer) end end self.digest = md5.hexdigest end # Don't save the content type if it's longer than the authorized length if self.content_type && self.content_type.length > 255 self.content_type = nil end end |
#deletable?(user = User.current) ⇒ Boolean
116 117 118 |
# File 'app/models/attachment.rb', line 116 def deletable?(user=User.current) container.(user) end |
#diskfile ⇒ Object
Returns file’s location on disk
100 101 102 |
# File 'app/models/attachment.rb', line 100 def diskfile "#{@@storage_path}/#{self.disk_filename}" end |
#file ⇒ Object
69 70 71 |
# File 'app/models/attachment.rb', line 69 def file nil end |
#file=(incoming_file) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'app/models/attachment.rb', line 54 def file=(incoming_file) unless incoming_file.nil? @temp_file = incoming_file if @temp_file.size > 0 self.filename = sanitize_filename(@temp_file.original_filename) self.disk_filename = Attachment.disk_filename(filename) self.content_type = @temp_file.content_type.to_s.chomp if content_type.blank? self.content_type = Redmine::MimeType.of(filename) end self.filesize = @temp_file.size end end end |
#image? ⇒ Boolean
120 121 122 |
# File 'app/models/attachment.rb', line 120 def image? self.filename =~ /\.(jpe?g|gif|png)$/i end |
#increment_download ⇒ Object
104 105 106 |
# File 'app/models/attachment.rb', line 104 def increment_download increment!(:downloads) end |
#is_diff? ⇒ Boolean
128 129 130 |
# File 'app/models/attachment.rb', line 128 def is_diff? self.filename =~ /\.(patch|diff)$/i end |
#is_text? ⇒ Boolean
124 125 126 |
# File 'app/models/attachment.rb', line 124 def is_text? Redmine::MimeType.is_type?('text', filename) end |
#project ⇒ Object
108 109 110 |
# File 'app/models/attachment.rb', line 108 def project container.project end |
#readable? ⇒ Boolean
Returns true if the file is readable
133 134 135 |
# File 'app/models/attachment.rb', line 133 def readable? File.readable?(diskfile) end |
#validate ⇒ Object
48 49 50 51 52 |
# File 'app/models/attachment.rb', line 48 def validate if self.filesize > Setting..to_i.kilobytes errors.add(:base, :too_long, :count => Setting..to_i.kilobytes) end end |
#visible?(user = User.current) ⇒ Boolean
112 113 114 |
# File 'app/models/attachment.rb', line 112 def visible?(user=User.current) container.(user) end |