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


155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'app/models/attachment.rb', line 155

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

#activity_typeObject

This method is called on save by the AttachmentJournal in order to decide which kind of activity we are dealing with. When that activity is retrieved later, we don't need to check the container_type in SQL anymore as that will be just the one we have specified here.


47
48
49
50
51
52
53
54
55
56
# File 'app/models/attachment.rb', line 47

def activity_type
  case container_type
  when "Document"
    "documents"
  when "Version"
    "files"
  else
    super
  end
end

#after_destroyObject

Deletes file on the disk


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

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


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/models/attachment.rb', line 88

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)

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

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

#diskfileObject

Returns file's location on disk


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

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

#fileObject


82
83
84
# File 'app/models/attachment.rb', line 82

def file
  nil
end

#file=(incoming_file) ⇒ Object


67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/attachment.rb', line 67

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)

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

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

#increment_downloadObject


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

def increment_download
  increment!(:downloads)
end

#is_diff?Boolean

Returns:

  • (Boolean)

141
142
143
# File 'app/models/attachment.rb', line 141

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

#is_text?Boolean

Returns:

  • (Boolean)

137
138
139
# File 'app/models/attachment.rb', line 137

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

#projectObject


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

def project
  container.project
end

#readable?Boolean

Returns true if the file is readable

Returns:

  • (Boolean)

146
147
148
# File 'app/models/attachment.rb', line 146

def readable?
  File.readable?(diskfile)
end

#validateObject


61
62
63
64
65
# File 'app/models/attachment.rb', line 61

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)

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

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