Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

attachment.rb 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # redMine - project management software
  2. # Copyright (C) 2006 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/md5"
  18. class Attachment < ActiveRecord::Base
  19. belongs_to :container, :polymorphic => true
  20. belongs_to :author, :class_name => "User", :foreign_key => "author_id"
  21. validates_presence_of :filename
  22. def file=(incomming_file)
  23. unless incomming_file.nil?
  24. @temp_file = incomming_file
  25. if @temp_file.size > 0
  26. self.filename = sanitize_filename(@temp_file.original_filename)
  27. self.disk_filename = DateTime.now.strftime("%y%m%d%H%M%S") + "_" + self.filename
  28. self.content_type = @temp_file.content_type
  29. self.filesize = @temp_file.size
  30. end
  31. end
  32. end
  33. # Copy temp file to its final location
  34. def before_save
  35. if @temp_file && (@temp_file.size > 0)
  36. logger.debug("saving '#{self.diskfile}'")
  37. File.open(diskfile, "wb") do |f|
  38. f.write(@temp_file.read)
  39. end
  40. self.digest = Digest::MD5.hexdigest(File.read(diskfile))
  41. end
  42. end
  43. # Deletes file on the disk
  44. def after_destroy
  45. if self.filename?
  46. File.delete(diskfile) if File.exist?(diskfile)
  47. end
  48. end
  49. # Returns file's location on disk
  50. def diskfile
  51. "#{$RDM_STORAGE_PATH}/#{self.disk_filename}"
  52. end
  53. def increment_download
  54. increment!(:downloads)
  55. end
  56. # returns last created projects
  57. def self.most_downloaded
  58. find(:all, :limit => 5, :order => "downloads DESC")
  59. end
  60. private
  61. def sanitize_filename(value)
  62. # get only the filename, not the whole path
  63. just_filename = value.gsub(/^.*(\\|\/)/, '')
  64. # NOTE: File.basename doesn't work right with Windows paths on Unix
  65. # INCORRECT: just_filename = File.basename(value.gsub('\\\\', '/'))
  66. # Finally, replace all non alphanumeric, underscore or periods with underscore
  67. @filename = just_filename.gsub(/[^\w\.\-]/,'_')
  68. end
  69. end