about summary refs log tree commit diff
path: root/app/workers/redownload_media_worker.rb
blob: 0ead9a7a8a2ba7d4f23c79e3b89af50ae20ae7c7 (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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# frozen_string_literal: true

class RedownloadMediaWorker
  include Sidekiq::Worker
  include ExponentialBackoff

  sidekiq_options queue: 'pull', retry: 3

  def perform(id)
    media_attachment = MediaAttachment.find(id)

    return if media_attachment.remote_url.blank?

    orig_small_url = media_attachment.file.url(:small)

    media_attachment.download_file!
    media_attachment.download_thumbnail!

    if media_attachment.save && media_attachment.inline? && media_attachment.status.present?
      if unsupported_media_type?(media_attachment.file.content_type)
        media_attachment.destroy
        true
      else
        media_attachment.status.text.gsub!("#{orig_small_url}##{media_attachment.id}", media_attachment.file.url(:small))
        media_attachment.status.save
      end
    end
  rescue ActiveRecord::RecordNotFound
    true
  end

  private

  def unsupported_media_type?(mime_type)
    mime_type.present? && !MediaAttachment.supported_mime_types.include?(mime_type)
  end
end