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-2023 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 < ApplicationRecord
  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|webp)$/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. converted = {}
  305. params.each {|key, val| converted[key.to_i] = val}
  306. saved = true
  307. transaction do
  308. attachments.each do |attachment|
  309. if file = converted[attachment.id]
  310. attachment.filename = file[:filename] if file.key?(:filename)
  311. attachment.description = file[:description] if file.key?(:description)
  312. saved &&= attachment.save
  313. end
  314. end
  315. unless saved
  316. raise ActiveRecord::Rollback
  317. end
  318. end
  319. saved
  320. end
  321. def self.latest_attach(attachments, filename)
  322. return unless filename.valid_encoding?
  323. attachments.sort_by{|attachment| [attachment.created_on, attachment.id]}.reverse.detect do |att|
  324. filename.casecmp?(att.filename)
  325. end
  326. end
  327. def self.prune(age=1.day)
  328. Attachment.where("created_on < ? AND (container_type IS NULL OR container_type = '')", Time.now - age).destroy_all
  329. end
  330. def self.archive_attachments(attachments)
  331. attachments = attachments.select(&:readable?)
  332. return nil if attachments.blank?
  333. Zip.unicode_names = true
  334. archived_file_names = []
  335. buffer = Zip::OutputStream.write_buffer do |zos|
  336. attachments.each do |attachment|
  337. filename = attachment.filename
  338. # rename the file if a file with the same name already exists
  339. dup_count = 0
  340. while archived_file_names.include?(filename)
  341. dup_count += 1
  342. extname = File.extname(attachment.filename)
  343. basename = File.basename(attachment.filename, extname)
  344. filename = "#{basename}(#{dup_count})#{extname}"
  345. end
  346. zos.put_next_entry(filename)
  347. zos << IO.binread(attachment.diskfile)
  348. archived_file_names << filename
  349. end
  350. end
  351. buffer.string
  352. ensure
  353. buffer&.close
  354. end
  355. # Moves an existing attachment to its target directory
  356. def move_to_target_directory!
  357. return unless !new_record? & readable?
  358. src = diskfile
  359. self.disk_directory = target_directory
  360. dest = diskfile
  361. return if src == dest
  362. if !FileUtils.mkdir_p(File.dirname(dest))
  363. logger.error "Could not create directory #{File.dirname(dest)}" if logger
  364. return
  365. end
  366. if !FileUtils.mv(src, dest)
  367. logger.error "Could not move attachment from #{src} to #{dest}" if logger
  368. return
  369. end
  370. update_column :disk_directory, disk_directory
  371. end
  372. # Moves existing attachments that are stored at the root of the files
  373. # directory (ie. created before Redmine 2.3) to their target subdirectories
  374. def self.move_from_root_to_target_directory
  375. Attachment.where("disk_directory IS NULL OR disk_directory = ''").find_each do |attachment|
  376. attachment.move_to_target_directory!
  377. end
  378. end
  379. # Updates digests to SHA256 for all attachments that have a MD5 digest
  380. # (ie. created before Redmine 3.4)
  381. def self.update_digests_to_sha256
  382. Attachment.where("length(digest) < 64").find_each do |attachment|
  383. attachment.update_digest_to_sha256!
  384. end
  385. end
  386. # Updates attachment digest to SHA256
  387. def update_digest_to_sha256!
  388. if readable?
  389. sha = Digest::SHA256.new
  390. File.open(diskfile, 'rb') do |f|
  391. while buffer = f.read(8192)
  392. sha.update(buffer)
  393. end
  394. end
  395. update_column :digest, sha.hexdigest
  396. end
  397. end
  398. # Returns true if the extension is allowed regarding allowed/denied
  399. # extensions defined in application settings, otherwise false
  400. def self.valid_extension?(extension)
  401. denied, allowed = [:attachment_extensions_denied, :attachment_extensions_allowed].map do |setting|
  402. Setting.send(setting)
  403. end
  404. if denied.present? && extension_in?(extension, denied)
  405. return false
  406. end
  407. if allowed.present? && !extension_in?(extension, allowed)
  408. return false
  409. end
  410. true
  411. end
  412. # Returns true if extension belongs to extensions list.
  413. def self.extension_in?(extension, extensions)
  414. extension = extension.downcase.sub(/\A\.+/, '')
  415. unless extensions.is_a?(Array)
  416. extensions = extensions.to_s.split(",").map(&:strip)
  417. end
  418. extensions = extensions.map {|s| s.downcase.sub(/\A\.+/, '')}.reject(&:blank?)
  419. extensions.include?(extension)
  420. end
  421. # Returns true if attachment's extension belongs to extensions list.
  422. def extension_in?(extensions)
  423. self.class.extension_in?(File.extname(filename), extensions)
  424. end
  425. # returns either MD5 or SHA256 depending on the way self.digest was computed
  426. def digest_type
  427. digest.size < 64 ? "MD5" : "SHA256" if digest.present?
  428. end
  429. private
  430. def reuse_existing_file_if_possible
  431. original_diskfile = diskfile
  432. original_filename = disk_filename
  433. reused = with_lock do
  434. if existing = Attachment
  435. .where(digest: self.digest, filesize: self.filesize)
  436. .where.not(disk_filename: original_filename)
  437. .order(:id)
  438. .last
  439. existing.with_lock do
  440. existing_diskfile = existing.diskfile
  441. if File.readable?(original_diskfile) &&
  442. File.readable?(existing_diskfile) &&
  443. FileUtils.identical?(original_diskfile, existing_diskfile)
  444. self.update_columns disk_directory: existing.disk_directory,
  445. disk_filename: existing.disk_filename
  446. end
  447. end
  448. end
  449. end
  450. if reused && Attachment.where(disk_filename: original_filename).none?
  451. File.delete(original_diskfile)
  452. end
  453. rescue ActiveRecord::StatementInvalid, ActiveRecord::RecordNotFound
  454. # Catch and ignore lock errors. It is not critical if deduplication does
  455. # not happen, therefore we do not retry.
  456. # with_lock throws ActiveRecord::RecordNotFound if the record isnt there
  457. # anymore, thats why this is caught and ignored as well.
  458. end
  459. # Physically deletes the file from the file system
  460. def delete_from_disk!
  461. FileUtils.rm_f(diskfile) if disk_filename.present?
  462. Dir[thumbnail_path("*")].each do |thumb|
  463. File.delete(thumb)
  464. end
  465. end
  466. def thumbnail_path(size)
  467. File.join(self.class.thumbnails_storage_path,
  468. "#{digest}_#{filesize}_#{size}.thumb")
  469. end
  470. def sanitize_filename(value)
  471. # get only the filename, not the whole path
  472. just_filename = value.gsub(/\A.*(\\|\/)/m, '')
  473. # Finally, replace invalid characters with underscore
  474. just_filename.gsub(/[\/\?\%\*\:\|\"\'<>\n\r]+/, '_')
  475. end
  476. # Returns the subdirectory in which the attachment will be saved
  477. def target_directory
  478. time = created_on || DateTime.now
  479. time.strftime("%Y/%m")
  480. end
  481. # Singleton class method is public
  482. class << self
  483. # Claims a unique ASCII or hashed filename, yields the open file handle
  484. def create_diskfile(filename, directory=nil, &block)
  485. timestamp = DateTime.now.strftime("%y%m%d%H%M%S")
  486. ascii = ''
  487. if %r{^[a-zA-Z0-9_\.\-]*$}.match?(filename) && filename.length <= 50
  488. ascii = filename
  489. else
  490. ascii = Digest::MD5.hexdigest(filename)
  491. # keep the extension if any
  492. ascii << $1 if filename =~ %r{(\.[a-zA-Z0-9]+)$}
  493. end
  494. path = File.join storage_path, directory.to_s
  495. FileUtils.mkdir_p(path) unless File.directory?(path)
  496. begin
  497. name = "#{timestamp}_#{ascii}"
  498. File.open(
  499. File.join(path, name),
  500. flags: File::CREAT | File::EXCL | File::WRONLY,
  501. binmode: true,
  502. &block
  503. )
  504. rescue Errno::EEXIST
  505. timestamp.succ!
  506. retry
  507. end
  508. end
  509. end
  510. end