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.

thumbnail.rb 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 'fileutils'
  19. require 'mimemagic'
  20. module Redmine
  21. module Thumbnail
  22. extend Redmine::Utils::Shell
  23. CONVERT_BIN = (Redmine::Configuration['imagemagick_convert_command'] || 'convert').freeze
  24. ALLOWED_TYPES = %w(image/bmp image/gif image/jpeg image/png application/pdf)
  25. # Generates a thumbnail for the source image to target
  26. def self.generate(source, target, size, is_pdf = false)
  27. return nil unless convert_available?
  28. return nil if is_pdf && !gs_available?
  29. unless File.exists?(target)
  30. mime_type = File.open(source) {|f| MimeMagic.by_magic(f).try(:type) }
  31. return nil if mime_type.nil?
  32. return nil if !ALLOWED_TYPES.include? mime_type
  33. return nil if is_pdf && mime_type != "application/pdf"
  34. # Make sure we only invoke Imagemagick if the file type is allowed
  35. unless File.open(source) {|f| ALLOWED_TYPES.include? MimeMagic.by_magic(f).try(:type) }
  36. return nil
  37. end
  38. directory = File.dirname(target)
  39. unless File.exists?(directory)
  40. FileUtils.mkdir_p directory
  41. end
  42. size_option = "#{size}x#{size}>"
  43. if is_pdf
  44. cmd = "#{shell_quote CONVERT_BIN} #{shell_quote "#{source}[0]"} -thumbnail #{shell_quote size_option} #{shell_quote "png:#{target}"}"
  45. else
  46. cmd = "#{shell_quote CONVERT_BIN} #{shell_quote source} -auto-orient -thumbnail #{shell_quote size_option} #{shell_quote target}"
  47. end
  48. unless system(cmd)
  49. logger.error("Creating thumbnail failed (#{$?}):\nCommand: #{cmd}")
  50. return nil
  51. end
  52. end
  53. target
  54. end
  55. def self.convert_available?
  56. return @convert_available if defined?(@convert_available)
  57. begin
  58. `#{shell_quote CONVERT_BIN} -version`
  59. @convert_available = $?.success?
  60. rescue
  61. @convert_available = false
  62. end
  63. logger.warn("Imagemagick's convert binary (#{CONVERT_BIN}) not available") unless @convert_available
  64. @convert_available
  65. end
  66. def self.gs_available?
  67. return @gs_available if defined?(@gs_available)
  68. if Redmine::Platform.mswin?
  69. @gs_available = false
  70. else
  71. begin
  72. `gs -version`
  73. @gs_available = $?.success?
  74. rescue
  75. @gs_available = false
  76. end
  77. end
  78. @gs_available
  79. end
  80. def self.logger
  81. Rails.logger
  82. end
  83. end
  84. end