This repository has been archived by the owner on Jan 28, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEATURE: Setting for populating posts with images (#4)
This adds the new `include_images_in_posts` setting to `dev.yml` (disabled by default). If it's enabled some posts will contain images from https://picsum.photos/. The images get cached locally to speed up subsequent calls of `dev:populate`. It also moves `redis-cli flushall` to the beginning of the `dev:populate` rake task. Otherwise it would delete jobs from the Sidekiq queue that are required for processing new posts. But it deletes the `UserEmail` jobs in order to prevent lots of errors when mailcatcher isn't running.
- Loading branch information
Showing
5 changed files
with
110 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'faker' | ||
require 'net/http' | ||
require 'json' | ||
|
||
module Faker | ||
class DiscourseMarkdown < Markdown | ||
class << self | ||
attr_writer(:user_id) | ||
|
||
def user_id | ||
@user_id || ::Discourse::SYSTEM_USER_ID | ||
end | ||
|
||
def with_user(user_id) | ||
current_user_id = self.user_id | ||
self.user_id = user_id | ||
begin | ||
yield | ||
ensure | ||
self.user_id = current_user_id | ||
end | ||
end | ||
|
||
def image | ||
image = next_image | ||
image_file = load_image(image) | ||
|
||
upload = ::UploadCreator.new( | ||
image_file, | ||
image[:filename], | ||
origin: image[:url] | ||
).create_for(user_id) | ||
|
||
::UploadMarkdown.new(upload).to_markdown | ||
end | ||
|
||
private | ||
|
||
def next_image | ||
if @images.blank? | ||
@next_page = (@next_page || 0) + 1 | ||
url = URI("https://picsum.photos/v2/list?page=#{@next_page}&limit=100") | ||
response = Net::HTTP.get(url) | ||
json = JSON.parse(response) | ||
@images = json.sort_by { |image| image["id"] } | ||
end | ||
|
||
image = @images.pop | ||
{ filename: "#{image['id']}.jpg", url: "#{image['download_url']}.jpg" } | ||
end | ||
|
||
def image_cache_dir | ||
@image_cache_dir ||= ::File.join(Rails.root, "tmp", "discourse_dev", "images") | ||
end | ||
|
||
def load_image(image) | ||
cache_path = ::File.join(image_cache_dir, image[:filename]) | ||
|
||
if !::File.exists?(cache_path) | ||
FileUtils.mkdir_p(image_cache_dir) | ||
temp_file = ::FileHelper.download( | ||
image[:url], | ||
max_file_size: [SiteSetting.max_image_size_kb.kilobytes, 10.megabytes].max, | ||
tmp_file_name: "image", | ||
follow_redirect: true | ||
) | ||
FileUtils.cp(temp_file, cache_path) | ||
end | ||
|
||
::File.open(cache_path) | ||
end | ||
|
||
def available_methods | ||
methods = super | ||
methods << :image if ::DiscourseDev.config.include_images_in_posts | ||
methods | ||
end | ||
end | ||
end | ||
end |