Class: S3Website::DiffHelper

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

Defined Under Namespace

Classes: DiffProgressIndicator

Class Method Summary collapse

Class Method Details

.resolve_files_to_upload(s3_bucket, site_dir, config) ⇒ 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 'lib/s3_website/diff_helper.rb', line 5

def self.resolve_files_to_upload(s3_bucket, site_dir, config)
  objects = []
  list = nil
  with_progress_indicator('Downloading list of the objects in a bucket') { |progress_indicator|
    while list.nil? || list[:truncated]
      list = s3_bucket.client.list_objects(:bucket_name => s3_bucket.name, :marker => objects.last ? objects.last.key : '')
      objects.concat(list[:contents].map { |o|
        OpenStruct.new({
          :key => o[:key],
          :etag => o[:etag],
          :last_modified => o[:last_modified],
          :size => o[:size]
        })
      })
      progress_indicator.render_next_step
    end
  }

  # ignore objects in the bucket that match ignore_on_server regex
  # otherwise those objects are requested (sometimes just HEAD sometimes a full GET)
  # only to be ignored later
  regexps_to_ignore = S3Website::Uploader.ignore_regexps(config['ignore_on_server'])
  filtered_bucket = OpenStruct.new({
    :objects => objects.reject { |s3_object|
      regexps_to_ignore.any? do |ignore_regexp|
        Regexp.new(ignore_regexp).match s3_object.key
      end
    }
  })

  with_progress_indicator('Calculating diff') { |progress_indicator|
    s3_data_source = Filey::DataSources::AwsSdkS3.new(filtered_bucket, config) { |filey|
      progress_indicator.render_next_step
    }
    fs_data_source = Filey::DataSources::FileSystem.new(site_dir) { |filey|
      progress_indicator.render_next_step
    }

    changed_local_files = Filey::Comparison.list_changed(
      fs_data_source,
      s3_data_source
    )
    new_local_files = Filey::Comparison.list_missing(
      fs_data_source,
      s3_data_source
    )
    [
      reject_blacklisted(changed_local_files, config),
      reject_blacklisted(new_local_files, config)
    ]
  }
end