Class: UploadRecovery

Inherits:
Object
  • Object
show all
Defined in:
lib/upload_recovery.rb

Instance Method Summary collapse

Constructor Details

#initialize(dry_run: false, stop_on_error: false) ⇒ UploadRecovery

Returns a new instance of UploadRecovery.



4
5
6
7
# File 'lib/upload_recovery.rb', line 4

def initialize(dry_run: false, stop_on_error: false)
  @dry_run = dry_run
  @stop_on_error = stop_on_error
end

Instance Method Details

#recover(posts = Post) ⇒ Object



9
10
11
# File 'lib/upload_recovery.rb', line 9

def recover(posts = Post)
  posts.have_uploads.find_each { |post| recover_post post }
end

#recover_post(post) ⇒ Object



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
# File 'lib/upload_recovery.rb', line 13

def recover_post(post)
  begin
    analyzer = PostAnalyzer.new(post.raw, post.topic_id)

    analyzer
      .cooked_stripped
      .css("img", "a")
      .each do |media|
        if media.name == "img" && orig_src = media["data-orig-src"]
          if dom_class = media["class"]
            next if (Post.allowed_image_classes & dom_class.split).count > 0
          end

          if @dry_run
            puts "#{post.full_url} #{orig_src}"
          else
            recover_post_upload(post, Upload.sha1_from_short_url(orig_src))
          end
        elsif url = (media["href"] || media["src"])
          data = Upload.extract_url(url)
          next unless data

          upload = Upload.get_from_url(url)

          if !upload || upload.verification_status == Upload.verification_statuses[:invalid_etag]
            if @dry_run
              puts "#{post.full_url} #{url}"
            else
              sha1 = data[2]
              recover_post_upload(post, sha1)
            end
          end
        end
      end
  rescue => e
    raise e if @stop_on_error
    puts "#{post.full_url} #{e.class}: #{e.message}"
  end
end