DEV: Make attachment markdown reusable
This commit is contained in:
parent
6a0787445c
commit
a61ff16740
|
@ -4,12 +4,11 @@ require 'csv'
|
|||
require 'zip'
|
||||
require_dependency 'system_message'
|
||||
require_dependency 'upload_creator'
|
||||
require_dependency 'discourse_markdown'
|
||||
|
||||
module Jobs
|
||||
|
||||
class ExportCsvFile < Jobs::Base
|
||||
include ActionView::Helpers::NumberHelper
|
||||
|
||||
sidekiq_options retry: false
|
||||
|
||||
HEADER_ATTRS_FOR ||= HashWithIndifferentAccess.new(
|
||||
|
@ -406,7 +405,7 @@ module Jobs
|
|||
SystemMessage.create_from_system_user(
|
||||
@current_user,
|
||||
:csv_export_succeeded,
|
||||
download_link: "[#{upload.original_filename}|attachment](#{upload.short_url}) (#{number_to_human_size(upload.filesize)})",
|
||||
download_link: DiscourseMarkdown.attachment_markdown(upload),
|
||||
export_title: export_title
|
||||
)
|
||||
else
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
# 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
|
|
@ -5,12 +5,11 @@ require_dependency "new_post_manager"
|
|||
require_dependency "html_to_markdown"
|
||||
require_dependency "plain_text_to_markdown"
|
||||
require_dependency "upload_creator"
|
||||
require_dependency "discourse_markdown"
|
||||
|
||||
module Email
|
||||
|
||||
class Receiver
|
||||
include ActionView::Helpers::NumberHelper
|
||||
|
||||
# If you add a new error, you need to
|
||||
# * add it to Email::Processor#handle_failure()
|
||||
# * add text to server.en.yml (parent key: "emails.incoming.errors")
|
||||
|
@ -1035,19 +1034,16 @@ module Email
|
|||
|
||||
InlineUploads.match_img(raw) do |match, src, replacement, _|
|
||||
if src == upload.url
|
||||
raw = raw.sub(
|
||||
match,
|
||||
"![#{upload.original_filename}|#{upload.width}x#{upload.height}](#{upload.short_url})"
|
||||
)
|
||||
raw = raw.sub(match, DiscourseMarkdown.image_markdown(upload))
|
||||
end
|
||||
end
|
||||
elsif raw[/\[image:.*?\d+[^\]]*\]/i]
|
||||
raw.sub!(/\[image:.*?\d+[^\]]*\]/i, attachment_markdown(upload))
|
||||
raw.sub!(/\[image:.*?\d+[^\]]*\]/i, DiscourseMarkdown.upload_markdown(upload))
|
||||
else
|
||||
raw << "\n\n#{attachment_markdown(upload)}\n\n"
|
||||
raw << "\n\n#{DiscourseMarkdown.upload_markdown(upload)}\n\n"
|
||||
end
|
||||
else
|
||||
raw << "\n\n#{attachment_markdown(upload)}\n\n"
|
||||
raw << "\n\n#{DiscourseMarkdown.upload_markdown(upload)}\n\n"
|
||||
end
|
||||
else
|
||||
rejected_attachments << upload
|
||||
|
@ -1082,14 +1078,6 @@ module Email
|
|||
Email::Sender.new(client_message, :email_reject_attachment).send
|
||||
end
|
||||
|
||||
def attachment_markdown(upload)
|
||||
if FileHelper.is_supported_image?(upload.original_filename)
|
||||
"![#{upload.original_filename}|#{upload.width}x#{upload.height}](#{upload.short_url})"
|
||||
else
|
||||
"[#{upload.original_filename}|attachment](#{upload.short_url}) (#{number_to_human_size(upload.filesize)})"
|
||||
end
|
||||
end
|
||||
|
||||
def create_post(options = {})
|
||||
options[:via_email] = true
|
||||
options[:raw_email] = @raw_email
|
||||
|
|
|
@ -21,8 +21,6 @@ module ImportScripts; end
|
|||
|
||||
class ImportScripts::Base
|
||||
|
||||
include ActionView::Helpers::NumberHelper
|
||||
|
||||
def initialize
|
||||
preload_i18n
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue