Class: AssetSyncDownload::Storage

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/asset_sync_download/storage.rb

Instance Method Summary collapse

Instance Method Details

#download(target = :asset_files) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/asset_sync_download/storage.rb', line 82

def download(target = :asset_files)
  log "AssetSync: Downloading #{target}."
  case target
  when :manifest
    download_manifest
  when :asset_files
    download_asset_files
  else
    raise "Unknown target specified: #{target}. It must be :manifest or :asset_files."
  end
  log "AssetSync: Done."
end

#download_asset_filesObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/asset_sync_download/storage.rb', line 60

def download_asset_files
  asset_paths = get_asset_files_from_manifest
  if asset_paths.nil?
    log "Using: Remote Directory Search"
    asset_paths = get_remote_files
  end

  asset_paths.each do |asset_path|
    local_path = File.join(path, asset_path)
    if File.exists?(local_path)
      log "Skipped: #{asset_path}"
      next
    end

    file = bucket.files.get(asset_path)
    log "Downloaded: #{asset_path} (#{file.content_length} Bytes)"
    local_dir = File.dirname(local_path)
    FileUtils.mkdir_p(local_dir) unless File.directory?(local_dir)
    File.open(local_path, "wb") { |f| f.write(file.body) }
  end
end

#download_manifestObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/asset_sync_download/storage.rb', line 43

def download_manifest
  return unless defined?(Sprockets::ManifestUtils)

  files = get_remote_files
  manifest_key   = files.find { |f| File.basename(f) =~ Sprockets::ManifestUtils::MANIFEST_RE }
  manifest_key ||= files.find { |f| File.basename(f) =~ Sprockets::ManifestUtils::LEGACY_MANIFEST_RE }
  raise "Could not find any manifests. aborted." if manifest_key.nil?

  manifest = bucket.files.get(manifest_key)
  log "Downloaded: #{manifest_key} (#{manifest.content_length} Bytes)"

  manifest_path = File.join(path, manifest_key)
  local_dir = File.dirname(manifest_path)
  FileUtils.mkdir_p(local_dir) unless File.directory?(local_dir)
  File.open(manifest_path, "wb") { |f| f.write(manifest.body) }
end

#get_asset_files_from_manifestObject



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
# File 'lib/asset_sync_download/storage.rb', line 15

def get_asset_files_from_manifest
  if storage.respond_to?(:get_asset_files_from_manifest)
    return storage.get_asset_files_from_manifest
  end

  if self.config.manifest
    if ActionView::Base.respond_to?(:assets_manifest)
      log "Using: Rails 4.0 manifest access"
      manifest = Sprockets::Manifest.new(ActionView::Base.assets_manifest.environment, ActionView::Base.assets_manifest.dir)
      return manifest.assets.values.map { |f| File.join(self.config.assets_prefix, f) }
    elsif File.exist?(self.config.manifest_path)
      log "Using: Manifest #{self.config.manifest_path}"
      yml = YAML.load(IO.read(self.config.manifest_path))

      return yml.map do |original, compiled|
        # Upload font originals and compiled
        if original =~ /^.+(eot|svg|ttf|woff)$/
          [original, compiled]
        else
          compiled
        end
      end.flatten.map { |f| File.join(self.config.assets_prefix, f) }.uniq!
    else
      log "Warning: Manifest could not be found"
    end
  end
end

#storageObject



11
12
13
# File 'lib/asset_sync_download/storage.rb', line 11

def storage
  @storage ||= AssetSync.storage
end