mirror of
https://github.com/discourse/discourse.git
synced 2025-03-25 16:48:55 +08:00
DEV: Make attachment markdown reusable
This commit is contained in:
parent
6a0787445c
commit
a61ff16740
@ -4,12 +4,11 @@ require 'csv'
|
|||||||
require 'zip'
|
require 'zip'
|
||||||
require_dependency 'system_message'
|
require_dependency 'system_message'
|
||||||
require_dependency 'upload_creator'
|
require_dependency 'upload_creator'
|
||||||
|
require_dependency 'discourse_markdown'
|
||||||
|
|
||||||
module Jobs
|
module Jobs
|
||||||
|
|
||||||
class ExportCsvFile < Jobs::Base
|
class ExportCsvFile < Jobs::Base
|
||||||
include ActionView::Helpers::NumberHelper
|
|
||||||
|
|
||||||
sidekiq_options retry: false
|
sidekiq_options retry: false
|
||||||
|
|
||||||
HEADER_ATTRS_FOR ||= HashWithIndifferentAccess.new(
|
HEADER_ATTRS_FOR ||= HashWithIndifferentAccess.new(
|
||||||
@ -406,7 +405,7 @@ module Jobs
|
|||||||
SystemMessage.create_from_system_user(
|
SystemMessage.create_from_system_user(
|
||||||
@current_user,
|
@current_user,
|
||||||
:csv_export_succeeded,
|
: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
|
export_title: export_title
|
||||||
)
|
)
|
||||||
else
|
else
|
||||||
|
24
lib/discourse_markdown.rb
Normal file
24
lib/discourse_markdown.rb
Normal file
@ -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)
|
||||||
|
""
|
||||||
|
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 "html_to_markdown"
|
||||||
require_dependency "plain_text_to_markdown"
|
require_dependency "plain_text_to_markdown"
|
||||||
require_dependency "upload_creator"
|
require_dependency "upload_creator"
|
||||||
|
require_dependency "discourse_markdown"
|
||||||
|
|
||||||
module Email
|
module Email
|
||||||
|
|
||||||
class Receiver
|
class Receiver
|
||||||
include ActionView::Helpers::NumberHelper
|
|
||||||
|
|
||||||
# If you add a new error, you need to
|
# If you add a new error, you need to
|
||||||
# * add it to Email::Processor#handle_failure()
|
# * add it to Email::Processor#handle_failure()
|
||||||
# * add text to server.en.yml (parent key: "emails.incoming.errors")
|
# * 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, _|
|
InlineUploads.match_img(raw) do |match, src, replacement, _|
|
||||||
if src == upload.url
|
if src == upload.url
|
||||||
raw = raw.sub(
|
raw = raw.sub(match, DiscourseMarkdown.image_markdown(upload))
|
||||||
match,
|
|
||||||
""
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
elsif raw[/\[image:.*?\d+[^\]]*\]/i]
|
elsif raw[/\[image:.*?\d+[^\]]*\]/i]
|
||||||
raw.sub!(/\[image:.*?\d+[^\]]*\]/i, attachment_markdown(upload))
|
raw.sub!(/\[image:.*?\d+[^\]]*\]/i, DiscourseMarkdown.upload_markdown(upload))
|
||||||
else
|
else
|
||||||
raw << "\n\n#{attachment_markdown(upload)}\n\n"
|
raw << "\n\n#{DiscourseMarkdown.upload_markdown(upload)}\n\n"
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
raw << "\n\n#{attachment_markdown(upload)}\n\n"
|
raw << "\n\n#{DiscourseMarkdown.upload_markdown(upload)}\n\n"
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
rejected_attachments << upload
|
rejected_attachments << upload
|
||||||
@ -1082,14 +1078,6 @@ module Email
|
|||||||
Email::Sender.new(client_message, :email_reject_attachment).send
|
Email::Sender.new(client_message, :email_reject_attachment).send
|
||||||
end
|
end
|
||||||
|
|
||||||
def attachment_markdown(upload)
|
|
||||||
if FileHelper.is_supported_image?(upload.original_filename)
|
|
||||||
""
|
|
||||||
else
|
|
||||||
"[#{upload.original_filename}|attachment](#{upload.short_url}) (#{number_to_human_size(upload.filesize)})"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def create_post(options = {})
|
def create_post(options = {})
|
||||||
options[:via_email] = true
|
options[:via_email] = true
|
||||||
options[:raw_email] = @raw_email
|
options[:raw_email] = @raw_email
|
||||||
|
@ -21,8 +21,6 @@ module ImportScripts; end
|
|||||||
|
|
||||||
class ImportScripts::Base
|
class ImportScripts::Base
|
||||||
|
|
||||||
include ActionView::Helpers::NumberHelper
|
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
preload_i18n
|
preload_i18n
|
||||||
|
|
||||||
|
@ -1,12 +1,10 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require_dependency 'url_helper'
|
require_dependency 'url_helper'
|
||||||
require_dependency 'file_helper'
|
require_dependency 'discourse_markdown'
|
||||||
|
|
||||||
module ImportScripts
|
module ImportScripts
|
||||||
class Uploader
|
class Uploader
|
||||||
include ActionView::Helpers::NumberHelper
|
|
||||||
|
|
||||||
# Creates an upload.
|
# Creates an upload.
|
||||||
# Expects path to be the full path and filename of the source file.
|
# Expects path to be the full path and filename of the source file.
|
||||||
# @return [Upload]
|
# @return [Upload]
|
||||||
@ -42,22 +40,15 @@ module ImportScripts
|
|||||||
end
|
end
|
||||||
|
|
||||||
def html_for_upload(upload, display_filename)
|
def html_for_upload(upload, display_filename)
|
||||||
if FileHelper.is_supported_image?(upload.url)
|
DiscourseMarkdown.upload_markdown(upload, display_name: display_filename)
|
||||||
embedded_image_html(upload)
|
|
||||||
else
|
|
||||||
attachment_html(upload, display_filename)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def embedded_image_html(upload)
|
def embedded_image_html(upload)
|
||||||
image_width = [upload.width, SiteSetting.max_image_width].compact.min
|
DiscourseMarkdown.image_markdown(upload)
|
||||||
image_height = [upload.height, SiteSetting.max_image_height].compact.min
|
|
||||||
upload_name = upload.short_url || upload.url
|
|
||||||
%Q~~
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def attachment_html(upload, display_filename)
|
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
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
Loading…
x
Reference in New Issue
Block a user