Project

General

Profile

Feature #37530 » 37530-v3.patch

Go MAEDA, 2024-05-22 13:57

View differences:

config/configuration.yml.example
178 178
  # the `gs` binary. Used to generate attachment thumbnails of PDF files.
179 179
  #gs_command:
180 180

  
181
  # Timeout when generating thumbnails using the `convert` or `gs` command.
182
  # Timeout is set in seconds.
183
  #thumbnails_generation_timeout: 10
184

  
181 185
  # Configuration of MiniMagick font.
182 186
  #
183 187
  # Redmine uses MiniMagick in order to export a gantt chart to a PNG image.
lib/redmine/configuration.rb
27 27
      'avatar_server_url' => 'https://www.gravatar.com',
28 28
      'email_delivery' => nil,
29 29
      'max_concurrent_ajax_uploads' => 2,
30
      'common_mark_enable_hardbreaks' => true
30
      'common_mark_enable_hardbreaks' => true,
31
      'thumbnails_generation_timeout' => 10
31 32
    }
32 33

  
33 34
    @config = nil
lib/redmine/thumbnail.rb
18 18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 19

  
20 20
require 'fileutils'
21
require 'timeout'
21 22

  
22 23
module Redmine
23 24
  module Thumbnail
......
51 52
        else
52 53
          cmd = "#{shell_quote CONVERT_BIN} #{shell_quote source} -auto-orient -thumbnail #{shell_quote size_option} #{shell_quote target}"
53 54
        end
54
        unless system(cmd)
55
          logger.error("Creating thumbnail failed (#{$?}):\nCommand: #{cmd}")
55

  
56
        pid = nil
57
        begin
58
          Timeout.timeout(Redmine::Configuration['thumbnails_generation_timeout'].to_i) do
59
            pid = Process.spawn(cmd)
60
            _, status = Process.wait2(pid)
61
            unless status.success?
62
              logger.error("Creating thumbnail failed (#{status.exitstatus}):\nCommand: #{cmd}")
63
              return nil
64
            end
65
          end
66
        rescue Timeout::Error
67
          begin
68
            Process.kill('TERM', pid)
69
            Timeout.timeout(1) { Process.wait(pid) }
70
          rescue Timeout::Error
71
            Process.kill('KILL', pid)
72
          end
73
          logger.error("Creating thumbnail timed out:\nCommand: #{cmd}")
56 74
          return nil
57 75
        end
58 76
      end
test/unit/attachment_test.rb
626 626
    ensure
627 627
      set_tmp_attachments_directory
628 628
    end
629

  
630
    def test_thumbnail_should_timeout
631
      dummy_pid = 37530
632
      Process.stubs(:spawn).returns(dummy_pid)
633
      Process.stubs(:wait2).raises(Timeout::Error)
634
      Process.stubs(:kill).returns(1)
635
      Process.stubs(:wait).returns(dummy_pid)
636
      Rails.logger.expects(:error).with(regexp_matches(/Creating thumbnail timed out/))
637

  
638
      set_fixtures_attachments_directory
639
      Attachment.clear_thumbnails
640

  
641
      attachment = Attachment.find(16)
642
      thumbnail = attachment.thumbnail
643

  
644
      assert_nil thumbnail
645
    ensure
646
      set_tmp_attachments_directory
647
    end
629 648
  else
630 649
    puts '(ImageMagick convert not available)'
631 650
  end
(4-4/5)