DEV: Refactor helper methods for upload markdown

DEV: Refactor helper methods for upload markdown

Follow-up to a61ff167

diff --git a/app/jobs/regular/export_csv_file.rb b/app/jobs/regular/export_csv_file.rb
index 4db9fd2..cea8c1e 100644
--- a/app/jobs/regular/export_csv_file.rb
+++ b/app/jobs/regular/export_csv_file.rb
@@ -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
diff --git a/lib/discourse_markdown.rb b/lib/discourse_markdown.rb
deleted file mode 100644
index 8200372..0000000
--- a/lib/discourse_markdown.rb
+++ /dev/null
@@ -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
diff --git a/lib/email/receiver.rb b/lib/email/receiver.rb
index 04882bb..e02ba46 100644
--- a/lib/email/receiver.rb
+++ b/lib/email/receiver.rb
@@ -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
diff --git a/lib/upload_markdown.rb b/lib/upload_markdown.rb
new file mode 100644
index 0000000..d647641
--- /dev/null
+++ b/lib/upload_markdown.rb
@@ -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
diff --git a/script/import_scripts/base/uploader.rb b/script/import_scripts/base/uploader.rb
index fc5df45..8e38850 100644
--- a/script/import_scripts/base/uploader.rb
+++ b/script/import_scripts/base/uploader.rb
@@ -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
diff --git a/spec/components/email/sender_spec.rb b/spec/components/email/sender_spec.rb
index b2a7a58..59730e9 100644
--- a/spec/components/email/sender_spec.rb
+++ b/spec/components/email/sender_spec.rb
@@ -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

GitHub sha: fd12c414

2 Likes