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

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