DEV: Refactor helper methods for upload markdown

Follow-up to a61ff167
This commit is contained in:
Gerhard Schlager 2019-07-25 16:34:46 +02:00
parent 2ba4de2d45
commit fd12c414e7
6 changed files with 43 additions and 39 deletions

View File

@ -4,7 +4,7 @@ require 'csv'
require 'zip'
require_dependency 'system_message'
require_dependency 'upload_creator'
require_dependency 'discourse_markdown'
require_dependency 'upload_markdown'
module Jobs
@ -405,7 +405,7 @@ module Jobs
SystemMessage.create_from_system_user(
@current_user,
:csv_export_succeeded,
download_link: DiscourseMarkdown.attachment_markdown(upload),
download_link: UploadMarkdown.new(upload).attachment_markdown,
export_title: export_title
)
else

View File

@ -1,24 +0,0 @@
# frozen_string_literal: true
require_dependency "file_helper"
class DiscourseMarkdown
def self.upload_markdown(upload, display_name: nil)
if FileHelper.is_supported_image?(upload.original_filename)
image_markdown(upload)
else
attachment_markdown(upload, display_name: display_name)
end
end
def self.image_markdown(upload)
"![#{upload.original_filename}|#{upload.width}x#{upload.height}](#{upload.short_url})"
end
def self.attachment_markdown(upload, display_name: nil, with_filesize: true)
human_filesize = with_filesize ? " (#{upload.human_filesize})" : ""
display_name ||= upload.original_filename
"[#{display_name}|attachment](#{upload.short_url})#{human_filesize}"
end
end

View File

@ -5,7 +5,7 @@ require_dependency "new_post_manager"
require_dependency "html_to_markdown"
require_dependency "plain_text_to_markdown"
require_dependency "upload_creator"
require_dependency "discourse_markdown"
require_dependency "upload_markdown"
module Email
@ -1034,16 +1034,16 @@ module Email
InlineUploads.match_img(raw) do |match, src, replacement, _|
if src == upload.url
raw = raw.sub(match, DiscourseMarkdown.image_markdown(upload))
raw = raw.sub(match, UploadMarkdown.new(upload).image_markdown)
end
end
elsif raw[/\[image:.*?\d+[^\]]*\]/i]
raw.sub!(/\[image:.*?\d+[^\]]*\]/i, DiscourseMarkdown.upload_markdown(upload))
raw.sub!(/\[image:.*?\d+[^\]]*\]/i, UploadMarkdown.new(upload).to_markdown)
else
raw << "\n\n#{DiscourseMarkdown.upload_markdown(upload)}\n\n"
raw << "\n\n#{UploadMarkdown.new(upload).to_markdown}\n\n"
end
else
raw << "\n\n#{DiscourseMarkdown.upload_markdown(upload)}\n\n"
raw << "\n\n#{UploadMarkdown.new(upload).to_markdown}\n\n"
end
else
rejected_attachments << upload

28
lib/upload_markdown.rb Normal file
View File

@ -0,0 +1,28 @@
# frozen_string_literal: true
require_dependency "file_helper"
class UploadMarkdown
def initialize(upload)
@upload = upload
end
def to_markdown(display_name: nil)
if FileHelper.is_supported_image?(@upload.original_filename)
image_markdown
else
attachment_markdown(display_name: display_name)
end
end
def image_markdown
"![#{@upload.original_filename}|#{@upload.width}x#{@upload.height}](#{@upload.short_url})"
end
def attachment_markdown(display_name: nil, with_filesize: true)
human_filesize = with_filesize ? " (#{@upload.human_filesize})" : ""
display_name ||= @upload.original_filename
"[#{display_name}|attachment](#{@upload.short_url})#{human_filesize}"
end
end

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true
require_dependency 'url_helper'
require_dependency 'discourse_markdown'
require_dependency 'upload_markdown'
module ImportScripts
class Uploader
@ -40,15 +40,15 @@ module ImportScripts
end
def html_for_upload(upload, display_filename)
DiscourseMarkdown.upload_markdown(upload, display_name: display_filename)
UploadMarkdown.new(upload).to_markdown(display_name: display_filename)
end
def embedded_image_html(upload)
DiscourseMarkdown.image_markdown(upload)
UploadMarkdown.new(upload).image_markdown
end
def attachment_html(upload, display_filename)
DiscourseMarkdown.attachment_markdown(upload, display_name: display_filename)
UploadMarkdown.new(upload).attachment_markdown(display_name: display_filename)
end
private

View File

@ -376,10 +376,10 @@ describe Email::Sender do
fab!(:reply) do
raw = <<~RAW
Hello world!
#{DiscourseMarkdown.attachment_markdown(small_pdf)}
#{DiscourseMarkdown.attachment_markdown(large_pdf)}
#{DiscourseMarkdown.image_markdown(image)}
#{DiscourseMarkdown.attachment_markdown(csv_file)}
#{UploadMarkdown.new(small_pdf).attachment_markdown}
#{UploadMarkdown.new(large_pdf).attachment_markdown}
#{UploadMarkdown.new(image).image_markdown}
#{UploadMarkdown.new(csv_file).attachment_markdown}
RAW
reply = Fabricate(:post, raw: raw, topic: post.topic, user: Fabricate(:user))
reply.link_post_uploads