DEV: Make attachment markdown reusable

This commit is contained in:
Gerhard Schlager
2019-07-24 16:07:09 +02:00
parent 6a0787445c
commit a61ff16740
5 changed files with 35 additions and 35 deletions

View File

@ -1,12 +1,10 @@
# frozen_string_literal: true
require_dependency 'url_helper'
require_dependency 'file_helper'
require_dependency 'discourse_markdown'
module ImportScripts
class Uploader
include ActionView::Helpers::NumberHelper
# Creates an upload.
# Expects path to be the full path and filename of the source file.
# @return [Upload]
@ -42,22 +40,15 @@ module ImportScripts
end
def html_for_upload(upload, display_filename)
if FileHelper.is_supported_image?(upload.url)
embedded_image_html(upload)
else
attachment_html(upload, display_filename)
end
DiscourseMarkdown.upload_markdown(upload, display_name: display_filename)
end
def embedded_image_html(upload)
image_width = [upload.width, SiteSetting.max_image_width].compact.min
image_height = [upload.height, SiteSetting.max_image_height].compact.min
upload_name = upload.short_url || upload.url
%Q~![#{upload.original_filename}|#{image_width}x#{image_height}](#{upload_name})~
DiscourseMarkdown.image_markdown(upload)
end
def attachment_html(upload, display_filename)
"[#{display_filename}|attachment](#{upload.short}) (#{number_to_human_size(upload.filesize)})"
DiscourseMarkdown.attachment_markdown(upload, display_name: display_filename)
end
private