Class: Attachment

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

Constant Summary collapse

@@storage_path =
Redmine::Configuration['attachments_storage_path'] || "#{RAILS_ROOT}/files"

Class Method Summary collapse

Instance Method Summary collapse

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, attachments)
  attached = []
  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)

      if a.new_record?
        obj.unsaved_attachments ||= []
        obj.unsaved_attachments << a
      else
        attached << a
      end
    end
  end
  {:files => attached, :unsaved => obj.unsaved_attachments}
end

Instance Method Details

#after_destroyObject

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_saveObject

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

Returns:

  • (Boolean)


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

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

#diskfileObject

Returns file’s location on disk



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

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

#fileObject



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

Returns:

  • (Boolean)


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

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

#increment_downloadObject



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

def increment_download
  increment!(:downloads)
end

#is_diff?Boolean

Returns:

  • (Boolean)


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

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

#is_text?Boolean

Returns:

  • (Boolean)


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

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

#projectObject



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

def project
  container.project
end

#readable?Boolean

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

#validateObject



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

#visible?(user = User.current) ⇒ Boolean

Returns:

  • (Boolean)


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

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