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 17KB

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