Class: Jobs::PullUserProfileHotlinkedImages

Inherits:
PullHotlinkedImages show all
Defined in:
app/jobs/regular/pull_user_profile_hotlinked_images.rb

Instance Method Summary collapse

Methods inherited from PullHotlinkedImages

#attempt_download, #download, #extract_images_from, #initialize, #log, #should_download_image?

Methods inherited from Base

acquire_cluster_concurrency_lock!, clear_cluster_concurrency_lock!, cluster_concurrency, cluster_concurrency_redis_key, delayed_perform, #error_context, get_cluster_concurrency, #last_db_duration, #log, #perform, #perform_immediately

Constructor Details

This class inherits a constructor from Jobs::PullHotlinkedImages

Instance Method Details

#execute(args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/jobs/regular/pull_user_profile_hotlinked_images.rb', line 5

def execute(args)
  @user_id = args[:user_id]
  raise Discourse::InvalidParameters.new(:user_id) if @user_id.blank?

   = UserProfile.find_by(user_id: @user_id)
  return if .blank? || .bio_cooked.nil?

  large_image_urls = []
  broken_image_urls = []
  downloaded_images = {}

  extract_images_from(.bio_cooked).each do |node|
    download_src = original_src = node["src"] || node["href"]
    download_src =
      "#{SiteSetting.force_https ? "https" : "http"}:#{original_src}" if original_src.start_with?(
      "//",
    )
    normalized_src = normalize_src(download_src)

    next if !should_download_image?(download_src)

    begin
      already_attempted_download =
        downloaded_images.include?(normalized_src) ||
          large_image_urls.include?(normalized_src) ||
          broken_image_urls.include?(normalized_src)
      if !already_attempted_download
        downloaded_images[normalized_src] = attempt_download(download_src, @user_id)
      end
    rescue ImageTooLargeError
      large_image_urls << normalized_src
    rescue ImageBrokenError
      broken_image_urls << normalized_src
    end
  rescue => e
    raise e if Rails.env.test?
    log(
      :error,
      "Failed to pull hotlinked image (#{download_src}) user: #{@user_id}\n" + e.message +
        "\n" + e.backtrace.join("\n"),
    )
  end

  .bio_raw =
    InlineUploads.replace_hotlinked_image_urls(raw: .bio_raw) do |match_src|
      normalized_match_src = PostHotlinkedMedia.normalize_src(match_src)
      downloaded_images[normalized_match_src]
    end

  .skip_pull_hotlinked_image = true
  .save!
end