about summary refs log tree commit diff
path: root/app/workers/fetch_media_worker.rb
blob: 3e5eafb669d593e4a65b676673f4ea2e78285588 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# frozen_string_literal: true

class FetchMediaWorker
  include Sidekiq::Worker

  sidekiq_options queue: 'bulk', retry: 2

  def perform(media_attachment_id, remote_url: nil, force: false)
    object = MediaAttachment.find(media_attachment_id.to_i)
    return if object.blocked?
    return unless force || object.needs_redownload?
    if remote_url.nil?
      return if object.remote_url.nil?
    else
      object.remote_url = remote_url
    end
    object.file_remote_url = object.remote_url
    object.created_at      = Time.now.utc
    object.save!
  rescue ActiveRecord::RecordNotFound, ActiveRecord::RecordInvalid
    true
  end
end