diff --git a/config/configuration.yml.example b/config/configuration.yml.example index cdfd516b5..7b19b02bc 100644 --- a/config/configuration.yml.example +++ b/config/configuration.yml.example @@ -239,9 +239,28 @@ default: # Preview for Office documents (Microsoft Office / LibreOffice) # + # Redmine uses Pandoc to convert supported Microsoft Office and LibreOffice + # attachments to Markdown for preview. The settings below named + # `markdownized_preview_*` control that Markdown-based preview generation. + # # Absolute path (e.g. /usr/local/bin/pandoc) to the Pandoc command # used to convert supported attachments to Markdown for preview. #pandoc_command: + # + # Timeout when generating Markdown previews using the Pandoc command. + # Timeout is set in seconds. + # Default: 10 + #markdownized_preview_generation_timeout: 10 + # + # Maximum source file size in bytes for Markdown preview generation. + # Files larger than this limit are skipped. + # Default: 10485760 (10 megabytes) + #markdownized_preview_max_source_size: 10485760 + # + # Maximum output size in bytes saved after Markdown conversion. + # Larger output is truncated to this limit. + # Default: 102400 (100 kilobytes) + #markdownized_preview_max_output_size: 102400 # specific configuration options for production environment # that overrides the default ones diff --git a/lib/redmine/configuration.rb b/lib/redmine/configuration.rb index 1c7fdb3a5..1c2426458 100644 --- a/lib/redmine/configuration.rb +++ b/lib/redmine/configuration.rb @@ -27,7 +27,10 @@ module Redmine 'email_delivery' => nil, 'max_concurrent_ajax_uploads' => 2, 'common_mark_enable_hardbreaks' => true, - 'thumbnails_generation_timeout' => 10 + 'thumbnails_generation_timeout' => 10, + 'markdownized_preview_generation_timeout' => 10, + 'markdownized_preview_max_source_size' => 10.megabytes, + 'markdownized_preview_max_output_size' => 100.kilobytes } @config = nil diff --git a/lib/redmine/markdownizer.rb b/lib/redmine/markdownizer.rb index 688a2afaf..ac1c67203 100644 --- a/lib/redmine/markdownizer.rb +++ b/lib/redmine/markdownizer.rb @@ -27,8 +27,9 @@ module Redmine extend Redmine::Utils::Shell COMMAND = (Redmine::Configuration['pandoc_command'] || 'pandoc').freeze - MAX_SOURCE_SIZE = 20.megabytes - MAX_PREVIEW_SIZE = 512.kilobytes + PREVIEW_GENERATION_TIMEOUT = Redmine::Configuration['markdownized_preview_generation_timeout'].to_i + MAX_SOURCE_SIZE = Redmine::Configuration['markdownized_preview_max_source_size'].to_i + MAX_OUTPUT_SIZE = Redmine::Configuration['markdownized_preview_max_output_size'].to_i def self.supports?(filename) markdownizable_extensions.include?(File.extname(filename.to_s).downcase) @@ -50,7 +51,7 @@ module Redmine output = Tempfile.new('markdownized-preview') begin - Timeout.timeout(Redmine::Configuration['thumbnails_generation_timeout'].to_i) do + Timeout.timeout(PREVIEW_GENERATION_TIMEOUT) do pid = Process.spawn(*args, out: output.path) _, status = Process.wait2(pid) unless status.success? @@ -72,8 +73,8 @@ module Redmine output.close end - preview = File.binread(output.path, MAX_PREVIEW_SIZE + 1) || +"" - File.binwrite(target, preview.byteslice(0, MAX_PREVIEW_SIZE)) + preview = File.binread(output.path, MAX_OUTPUT_SIZE + 1) || +"" + File.binwrite(target, preview.byteslice(0, MAX_OUTPUT_SIZE)) target ensure output&.unlink