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.

utils.rb 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 File.dirname(__FILE__) + '/utils/date_calculation'
  19. require File.dirname(__FILE__) + '/utils/shell'
  20. require 'fileutils'
  21. module Redmine
  22. module Utils
  23. class << self
  24. # Returns the relative root url of the application
  25. def relative_url_root
  26. if ActionController::Base.respond_to?(:relative_url_root)
  27. ActionController::Base.relative_url_root.to_s
  28. else
  29. ActionController::Base.config.relative_url_root.to_s
  30. end
  31. end
  32. # Sets the relative root url of the application
  33. def relative_url_root=(arg)
  34. if ActionController::Base.respond_to?(:relative_url_root=)
  35. ActionController::Base.relative_url_root=arg
  36. else
  37. ActionController::Base.config.relative_url_root = arg
  38. end
  39. end
  40. # Generates a n bytes random hex string
  41. # Example:
  42. # random_hex(4) # => "89b8c729"
  43. def random_hex(n)
  44. SecureRandom.hex(n)
  45. end
  46. def save_upload(upload, path)
  47. directory = File.dirname(path)
  48. FileUtils.mkdir_p directory
  49. File.open(path, "wb") do |f|
  50. if upload.respond_to?(:read)
  51. buffer = ""
  52. while (buffer = upload.read(8192))
  53. f.write(buffer)
  54. yield buffer if block_given?
  55. end
  56. else
  57. f.write(upload)
  58. yield upload if block_given?
  59. end
  60. end
  61. end
  62. end
  63. end
  64. end