Class: Attachment

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

Constant Summary

@@storage_path =
"#{RAILS_ROOT}/files"

Class Method Summary

Instance Method Summary

Methods inherited from ActiveRecord::Base

quoted_table_name

Class Method Details

+ (Object) attach_files(obj, attachments)

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
# File 'app/models/attachment.rb', line 142

def self.attach_files(obj, attachments)
  attached = []
  unsaved = []
  if attachments && attachments.is_a?(Hash)
    attachments.each_value do |attachment|
      file = attachment['file']
      next unless file && file.size > 0
      a = Attachment.create(:container => obj, 
                            :file => file,
                            :description => attachment['description'].to_s.strip,
                            :author => User.current)
      a.new_record? ? (obj.unsaved_attachments << a) : (attached << a)
    end
  end
  {:files => attached, :unsaved => obj.unsaved_attachments}
end

Instance Method Details

- (Object) after_destroy

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

- (Object) before_save

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

- (Boolean) deletable?(user = User.current)

Returns:

  • (Boolean)


116
117
118
# File 'app/models/attachment.rb', line 116

def deletable?(user=User.current)
  container.attachments_deletable?(user)
end

- (Object) diskfile

Returns file’s location on disk



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

def diskfile
  "#{@@storage_path}/#{self.disk_filename}"
end

- (Object) file



69
70
71
# File 'app/models/attachment.rb', line 69

def file
  nil
end

- (Object) file=(incoming_file)



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

- (Boolean) image?

Returns:

  • (Boolean)


120
121
122
# File 'app/models/attachment.rb', line 120

def image?
  self.filename =~ /\.(jpe?g|gif|png)$/i
end

- (Object) increment_download



104
105
106
# File 'app/models/attachment.rb', line 104

def increment_download
  increment!(:downloads)
end

- (Boolean) is_diff?

Returns:

  • (Boolean)


128
129
130
# File 'app/models/attachment.rb', line 128

def is_diff?
  self.filename =~ /\.(patch|diff)$/i
end

- (Boolean) is_text?

Returns:

  • (Boolean)


124
125
126
# File 'app/models/attachment.rb', line 124

def is_text?
  Redmine::MimeType.is_type?('text', filename)
end

- (Object) project



108
109
110
# File 'app/models/attachment.rb', line 108

def project
  container.project
end

- (Boolean) readable?

Returns true if the file is readable

Returns:

  • (Boolean)


133
134
135
# File 'app/models/attachment.rb', line 133

def readable?
  File.readable?(diskfile)
end

- (Object) validate



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

def validate
  if self.filesize > Setting.attachment_max_size.to_i.kilobytes
    errors.add(:base, :too_long, :count => Setting.attachment_max_size.to_i.kilobytes)
  end
end

- (Boolean) visible?(user = User.current)

Returns:

  • (Boolean)


112
113
114
# File 'app/models/attachment.rb', line 112

def visible?(user=User.current)
  container.attachments_visible?(user)
end