Project

General

Profile

Feature #8959 » preview-limits-configurable.patch

Go MAEDA, 2026-03-20 03:43

View differences:

config/configuration.yml.example
239 239

  
240 240
  # Preview for Office documents (Microsoft Office / LibreOffice)
241 241
  #
242
  # Redmine uses Pandoc to convert supported Microsoft Office and LibreOffice
243
  # attachments to Markdown for preview. The settings below named
244
  # `markdownized_preview_*` control that Markdown-based preview generation.
245
  #
242 246
  # Absolute path (e.g. /usr/local/bin/pandoc) to the Pandoc command
243 247
  # used to convert supported attachments to Markdown for preview.
244 248
  #pandoc_command:
249
  #
250
  # Timeout when generating Markdown previews using the Pandoc command.
251
  # Timeout is set in seconds.
252
  # Default: 10
253
  #markdownized_preview_generation_timeout: 10
254
  #
255
  # Maximum source file size in bytes for Markdown preview generation.
256
  # Files larger than this limit are skipped.
257
  # Default: 10485760 (10 megabytes)
258
  #markdownized_preview_max_source_size: 10485760
259
  #
260
  # Maximum output size in bytes saved after Markdown conversion.
261
  # Larger output is truncated to this limit.
262
  # Default: 102400 (100 kilobytes)
263
  #markdownized_preview_max_output_size: 102400
245 264

  
246 265
# specific configuration options for production environment
247 266
# that overrides the default ones
lib/redmine/configuration.rb
27 27
      'email_delivery' => nil,
28 28
      'max_concurrent_ajax_uploads' => 2,
29 29
      'common_mark_enable_hardbreaks' => true,
30
      'thumbnails_generation_timeout' => 10
30
      'thumbnails_generation_timeout' => 10,
31
      'markdownized_preview_generation_timeout' => 10,
32
      'markdownized_preview_max_source_size' => 10.megabytes,
33
      'markdownized_preview_max_output_size' => 100.kilobytes
31 34
    }
32 35

  
33 36
    @config = nil
lib/redmine/markdownizer.rb
27 27
    extend Redmine::Utils::Shell
28 28

  
29 29
    COMMAND = (Redmine::Configuration['pandoc_command'] || 'pandoc').freeze
30
    MAX_SOURCE_SIZE = 20.megabytes
31
    MAX_PREVIEW_SIZE = 512.kilobytes
30
    PREVIEW_GENERATION_TIMEOUT = Redmine::Configuration['markdownized_preview_generation_timeout'].to_i
31
    MAX_SOURCE_SIZE = Redmine::Configuration['markdownized_preview_max_source_size'].to_i
32
    MAX_OUTPUT_SIZE = Redmine::Configuration['markdownized_preview_max_output_size'].to_i
32 33

  
33 34
    def self.supports?(filename)
34 35
      markdownizable_extensions.include?(File.extname(filename.to_s).downcase)
......
50 51
      output = Tempfile.new('markdownized-preview')
51 52

  
52 53
      begin
53
        Timeout.timeout(Redmine::Configuration['thumbnails_generation_timeout'].to_i) do
54
        Timeout.timeout(PREVIEW_GENERATION_TIMEOUT) do
54 55
          pid = Process.spawn(*args, out: output.path)
55 56
          _, status = Process.wait2(pid)
56 57
          unless status.success?
......
72 73
        output.close
73 74
      end
74 75

  
75
      preview = File.binread(output.path, MAX_PREVIEW_SIZE + 1) || +""
76
      File.binwrite(target, preview.byteslice(0, MAX_PREVIEW_SIZE))
76
      preview = File.binread(output.path, MAX_OUTPUT_SIZE + 1) || +""
77
      File.binwrite(target, preview.byteslice(0, MAX_OUTPUT_SIZE))
77 78
      target
78 79
    ensure
79 80
      output&.unlink
(9-9/9)