You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

attachment.rb 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require "digest"
  18. require "fileutils"
  19. class Attachment < ActiveRecord::Base
  20. include Redmine::SafeAttributes
  21. belongs_to :container, :polymorphic => true
  22. belongs_to :author, :class_name => "User"
  23. validates_presence_of :filename, :author
  24. validates_length_of :filename, :maximum => 255
  25. validates_length_of :disk_filename, :maximum => 255
  26. validates_length_of :description, :maximum => 255
  27. validate :validate_max_file_size, :validate_file_extension
  28. acts_as_event :title => :filename,
  29. :url => Proc.new {|o| {:controller => 'attachments', :action => 'show', :id => o.id, :filename => o.filename}}
  30. acts_as_activity_provider :type => 'files',
  31. :permission => :view_files,
  32. :author_key => :author_id,
  33. :scope => select("#{Attachment.table_name}.*").
  34. joins("LEFT JOIN #{Version.table_name} ON #{Attachment.table_name}.container_type='Version' AND #{Version.table_name}.id = #{Attachment.table_name}.container_id " +
  35. "LEFT JOIN #{Project.table_name} ON #{Version.table_name}.project_id = #{Project.table_name}.id OR ( #{Attachment.table_name}.container_type='Project' AND #{Attachment.table_name}.container_id = #{Project.table_name}.id )")
  36. acts_as_activity_provider :type => 'documents',
  37. :permission => :view_documents,
  38. :author_key => :author_id,
  39. :scope => select("#{Attachment.table_name}.*").
  40. joins("LEFT JOIN #{Document.table_name} ON #{Attachment.table_name}.container_type='Document' AND #{Document.table_name}.id = #{Attachment.table_name}.container_id " +
  41. "LEFT JOIN #{Project.table_name} ON #{Document.table_name}.project_id = #{Project.table_name}.id")
  42. cattr_accessor :storage_path
  43. @@storage_path = Redmine::Configuration['attachments_storage_path'] || File.join(Rails.root, "files")
  44. cattr_accessor :thumbnails_storage_path
  45. @@thumbnails_storage_path = File.join(Rails.root, "tmp", "thumbnails")
  46. before_create :files_to_final_location
  47. after_rollback :delete_from_disk, :on => :create
  48. after_commit :delete_from_disk, :on => :destroy
  49. after_commit :reuse_existing_file_if_possible, :on => :create
  50. safe_attributes 'filename', 'content_type', 'description'
  51. # Returns an unsaved copy of the attachment
  52. def copy(attributes=nil)
  53. copy = self.class.new
  54. copy.attributes = self.attributes.dup.except("id", "downloads")
  55. copy.attributes = attributes if attributes
  56. copy
  57. end
  58. def validate_max_file_size
  59. if @temp_file && self.filesize > Setting.attachment_max_size.to_i.kilobytes
  60. errors.add(:base, l(:error_attachment_too_big, :max_size => Setting.attachment_max_size.to_i.kilobytes))
  61. end
  62. end
  63. def validate_file_extension
  64. if @temp_file
  65. extension = File.extname(filename)
  66. unless self.class.valid_extension?(extension)
  67. errors.add(:base, l(:error_attachment_extension_not_allowed, :extension => extension))
  68. end
  69. end
  70. end
  71. def file=(incoming_file)
  72. unless incoming_file.nil?
  73. @temp_file = incoming_file
  74. if @temp_file.respond_to?(:original_filename)
  75. self.filename = @temp_file.original_filename
  76. self.filename.force_encoding("UTF-8")
  77. end
  78. if @temp_file.respond_to?(:content_type)
  79. self.content_type = @temp_file.content_type.to_s.chomp
  80. end
  81. self.filesize = @temp_file.size
  82. end
  83. end
  84. def file
  85. nil
  86. end
  87. def filename=(arg)
  88. write_attribute :filename, sanitize_filename(arg.to_s)
  89. filename
  90. end
  91. # Copies the temporary file to its final location
  92. # and computes its MD5 hash
  93. def files_to_final_location
  94. if @temp_file
  95. self.disk_directory = target_directory
  96. self.disk_filename = Attachment.disk_filename(filename, disk_directory)
  97. logger.info("Saving attachment '#{self.diskfile}' (#{@temp_file.size} bytes)") if logger
  98. path = File.dirname(diskfile)
  99. unless File.directory?(path)
  100. FileUtils.mkdir_p(path)
  101. end
  102. sha = Digest::SHA256.new
  103. File.open(diskfile, "wb") do |f|
  104. if @temp_file.respond_to?(:read)
  105. buffer = ""
  106. while (buffer = @temp_file.read(8192))
  107. f.write(buffer)
  108. sha.update(buffer)
  109. end
  110. else
  111. f.write(@temp_file)
  112. sha.update(@temp_file)
  113. end
  114. end
  115. self.digest = sha.hexdigest
  116. end
  117. @temp_file = nil
  118. if content_type.blank? && filename.present?
  119. self.content_type = Redmine::MimeType.of(filename)
  120. end
  121. # Don't save the content type if it's longer than the authorized length
  122. if self.content_type && self.content_type.length > 255
  123. self.content_type = nil
  124. end
  125. end
  126. # Deletes the file from the file system if it's not referenced by other attachments
  127. def delete_from_disk
  128. if Attachment.where("disk_filename = ? AND id <> ?", disk_filename, id).empty?
  129. delete_from_disk!
  130. end
  131. end
  132. # Returns file's location on disk
  133. def diskfile
  134. File.join(self.class.storage_path, disk_directory.to_s, disk_filename.to_s)
  135. end
  136. def title
  137. title = filename.dup
  138. if description.present?
  139. title << " (#{description})"
  140. end
  141. title
  142. end
  143. def increment_download
  144. increment!(:downloads)
  145. end
  146. def project
  147. container.try(:project)
  148. end
  149. def visible?(user=User.current)
  150. if container_id
  151. container && container.attachments_visible?(user)
  152. else
  153. author == user
  154. end
  155. end
  156. def editable?(user=User.current)
  157. if container_id
  158. container && container.attachments_editable?(user)
  159. else
  160. author == user
  161. end
  162. end
  163. def deletable?(user=User.current)
  164. if container_id
  165. container && container.attachments_deletable?(user)
  166. else
  167. author == user
  168. end
  169. end
  170. def image?
  171. !!(self.filename =~ /\.(bmp|gif|jpg|jpe|jpeg|png)$/i)
  172. end
  173. def thumbnailable?
  174. image?
  175. end
  176. # Returns the full path the attachment thumbnail, or nil
  177. # if the thumbnail cannot be generated.
  178. def thumbnail(options={})
  179. if thumbnailable? && readable?
  180. size = options[:size].to_i
  181. if size > 0
  182. # Limit the number of thumbnails per image
  183. size = (size / 50.0).ceil * 50
  184. # Maximum thumbnail size
  185. size = 800 if size > 800
  186. else
  187. size = Setting.thumbnails_size.to_i
  188. end
  189. size = 100 unless size > 0
  190. target = thumbnail_path(size)
  191. begin
  192. Redmine::Thumbnail.generate(self.diskfile, target, size)
  193. rescue => e
  194. logger.error "An error occured while generating thumbnail for #{disk_filename} to #{target}\nException was: #{e.message}" if logger
  195. return nil
  196. end
  197. end
  198. end
  199. # Deletes all thumbnails
  200. def self.clear_thumbnails
  201. Dir.glob(File.join(thumbnails_storage_path, "*.thumb")).each do |file|
  202. File.delete file
  203. end
  204. end
  205. def is_text?
  206. Redmine::MimeType.is_type?('text', filename)
  207. end
  208. def is_image?
  209. Redmine::MimeType.is_type?('image', filename)
  210. end
  211. def is_diff?
  212. self.filename =~ /\.(patch|diff)$/i
  213. end
  214. def is_pdf?
  215. Redmine::MimeType.of(filename) == "application/pdf"
  216. end
  217. def is_video?
  218. Redmine::MimeType.is_type?('video', filename)
  219. end
  220. def is_audio?
  221. Redmine::MimeType.is_type?('audio', filename)
  222. end
  223. def previewable?
  224. is_text? || is_image? || is_video? || is_audio?
  225. end
  226. # Returns true if the file is readable
  227. def readable?
  228. disk_filename.present? && File.readable?(diskfile)
  229. end
  230. # Returns the attachment token
  231. def token
  232. "#{id}.#{digest}"
  233. end
  234. # Finds an attachment that matches the given token and that has no container
  235. def self.find_by_token(token)
  236. if token.to_s =~ /^(\d+)\.([0-9a-f]+)$/
  237. attachment_id, attachment_digest = $1, $2
  238. attachment = Attachment.find_by(:id => attachment_id, :digest => attachment_digest)
  239. if attachment && attachment.container.nil?
  240. attachment
  241. end
  242. end
  243. end
  244. # Bulk attaches a set of files to an object
  245. #
  246. # Returns a Hash of the results:
  247. # :files => array of the attached files
  248. # :unsaved => array of the files that could not be attached
  249. def self.attach_files(obj, attachments)
  250. result = obj.save_attachments(attachments, User.current)
  251. obj.attach_saved_attachments
  252. result
  253. end
  254. # Updates the filename and description of a set of attachments
  255. # with the given hash of attributes. Returns true if all
  256. # attachments were updated.
  257. #
  258. # Example:
  259. # Attachment.update_attachments(attachments, {
  260. # 4 => {:filename => 'foo'},
  261. # 7 => {:filename => 'bar', :description => 'file description'}
  262. # })
  263. #
  264. def self.update_attachments(attachments, params)
  265. params = params.transform_keys {|key| key.to_i}
  266. saved = true
  267. transaction do
  268. attachments.each do |attachment|
  269. if p = params[attachment.id]
  270. attachment.filename = p[:filename] if p.key?(:filename)
  271. attachment.description = p[:description] if p.key?(:description)
  272. saved &&= attachment.save
  273. end
  274. end
  275. unless saved
  276. raise ActiveRecord::Rollback
  277. end
  278. end
  279. saved
  280. end
  281. def self.latest_attach(attachments, filename)
  282. attachments.sort_by(&:created_on).reverse.detect do |att|
  283. filename.casecmp(att.filename) == 0
  284. end
  285. end
  286. def self.prune(age=1.day)
  287. Attachment.where("created_on < ? AND (container_type IS NULL OR container_type = '')", Time.now - age).destroy_all
  288. end
  289. # Moves an existing attachment to its target directory
  290. def move_to_target_directory!
  291. return unless !new_record? & readable?
  292. src = diskfile
  293. self.disk_directory = target_directory
  294. dest = diskfile
  295. return if src == dest
  296. if !FileUtils.mkdir_p(File.dirname(dest))
  297. logger.error "Could not create directory #{File.dirname(dest)}" if logger
  298. return
  299. end
  300. if !FileUtils.mv(src, dest)
  301. logger.error "Could not move attachment from #{src} to #{dest}" if logger
  302. return
  303. end
  304. update_column :disk_directory, disk_directory
  305. end
  306. # Moves existing attachments that are stored at the root of the files
  307. # directory (ie. created before Redmine 2.3) to their target subdirectories
  308. def self.move_from_root_to_target_directory
  309. Attachment.where("disk_directory IS NULL OR disk_directory = ''").find_each do |attachment|
  310. attachment.move_to_target_directory!
  311. end
  312. end
  313. # Updates digests to SHA256 for all attachments that have a MD5 digest
  314. # (ie. created before Redmine 3.4)
  315. def self.update_digests_to_sha256
  316. Attachment.where("length(digest) < 64").find_each do |attachment|
  317. attachment.update_digest_to_sha256!
  318. end
  319. end
  320. # Updates attachment digest to SHA256
  321. def update_digest_to_sha256!
  322. if readable?
  323. sha = Digest::SHA256.new
  324. File.open(diskfile, 'rb') do |f|
  325. while buffer = f.read(8192)
  326. sha.update(buffer)
  327. end
  328. end
  329. update_column :digest, sha.hexdigest
  330. end
  331. end
  332. # Returns true if the extension is allowed regarding allowed/denied
  333. # extensions defined in application settings, otherwise false
  334. def self.valid_extension?(extension)
  335. denied, allowed = [:attachment_extensions_denied, :attachment_extensions_allowed].map do |setting|
  336. Setting.send(setting)
  337. end
  338. if denied.present? && extension_in?(extension, denied)
  339. return false
  340. end
  341. if allowed.present? && !extension_in?(extension, allowed)
  342. return false
  343. end
  344. true
  345. end
  346. # Returns true if extension belongs to extensions list.
  347. def self.extension_in?(extension, extensions)
  348. extension = extension.downcase.sub(/\A\.+/, '')
  349. unless extensions.is_a?(Array)
  350. extensions = extensions.to_s.split(",").map(&:strip)
  351. end
  352. extensions = extensions.map {|s| s.downcase.sub(/\A\.+/, '')}.reject(&:blank?)
  353. extensions.include?(extension)
  354. end
  355. # Returns true if attachment's extension belongs to extensions list.
  356. def extension_in?(extensions)
  357. self.class.extension_in?(File.extname(filename), extensions)
  358. end
  359. # returns either MD5 or SHA256 depending on the way self.digest was computed
  360. def digest_type
  361. digest.size < 64 ? "MD5" : "SHA256" if digest.present?
  362. end
  363. private
  364. def reuse_existing_file_if_possible
  365. original_diskfile = nil
  366. reused = with_lock do
  367. if existing = Attachment
  368. .where(digest: self.digest, filesize: self.filesize)
  369. .where('id <> ? and disk_filename <> ?',
  370. self.id, self.disk_filename)
  371. .first
  372. existing.with_lock do
  373. original_diskfile = self.diskfile
  374. existing_diskfile = existing.diskfile
  375. if File.readable?(original_diskfile) &&
  376. File.readable?(existing_diskfile) &&
  377. FileUtils.identical?(original_diskfile, existing_diskfile)
  378. self.update_columns disk_directory: existing.disk_directory,
  379. disk_filename: existing.disk_filename
  380. end
  381. end
  382. end
  383. end
  384. if reused
  385. File.delete(original_diskfile)
  386. end
  387. rescue ActiveRecord::StatementInvalid, ActiveRecord::RecordNotFound
  388. # Catch and ignore lock errors. It is not critical if deduplication does
  389. # not happen, therefore we do not retry.
  390. # with_lock throws ActiveRecord::RecordNotFound if the record isnt there
  391. # anymore, thats why this is caught and ignored as well.
  392. end
  393. # Physically deletes the file from the file system
  394. def delete_from_disk!
  395. if disk_filename.present? && File.exist?(diskfile)
  396. File.delete(diskfile)
  397. end
  398. Dir[thumbnail_path("*")].each do |thumb|
  399. File.delete(thumb)
  400. end
  401. end
  402. def thumbnail_path(size)
  403. File.join(self.class.thumbnails_storage_path,
  404. "#{digest}_#{filesize}_#{size}.thumb")
  405. end
  406. def sanitize_filename(value)
  407. # get only the filename, not the whole path
  408. just_filename = value.gsub(/\A.*(\\|\/)/m, '')
  409. # Finally, replace invalid characters with underscore
  410. just_filename.gsub(/[\/\?\%\*\:\|\"\'<>\n\r]+/, '_')
  411. end
  412. # Returns the subdirectory in which the attachment will be saved
  413. def target_directory
  414. time = created_on || DateTime.now
  415. time.strftime("%Y/%m")
  416. end
  417. # Returns an ASCII or hashed filename that do not
  418. # exists yet in the given subdirectory
  419. def self.disk_filename(filename, directory=nil)
  420. timestamp = DateTime.now.strftime("%y%m%d%H%M%S")
  421. ascii = ''
  422. if filename =~ %r{^[a-zA-Z0-9_\.\-]*$} && filename.length <= 50
  423. ascii = filename
  424. else
  425. ascii = Digest::MD5.hexdigest(filename)
  426. # keep the extension if any
  427. ascii << $1 if filename =~ %r{(\.[a-zA-Z0-9]+)$}
  428. end
  429. while File.exist?(File.join(storage_path, directory.to_s, "#{timestamp}_#{ascii}"))
  430. timestamp.succ!
  431. end
  432. "#{timestamp}_#{ascii}"
  433. end
  434. end