Sha256: 487639580a998017e7dcf5715a7586d1645f05177e0a4b61d2ba2c7ec694034e

Contents?: true

Size: 1.41 KB

Versions: 3

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

class SiteMaps::Adapters::AwsSdk::Storage
  attr_reader :config

  def initialize(config)
    @config = config
  end

  def upload(location, **options)
    options[:acl] ||= config.acl if config.acl
    options[:cache_control] ||= config.cache_control if config.cache_control
    options[:content_type] ||= location.gzip? ? "application/gzip" : "application/xml"
    lastmod = options.delete(:last_modified) || Time.now
    options[:metadata] ||= {}
    options[:metadata]["given-last-modified"] = lastmod.utc.strftime("%Y-%m-%dT%H:%M:%S%:z")
    obj = object(location.remote_path)
    obj.upload_file(location.path, **options)
  end

  def read(location)
    obj = object(location.remote_path).get
    metadata = {
      content_type: obj.content_type
    }
    if (raw = obj.metadata["given-last-modified"]) &&
        (time = Time.parse(raw))
      metadata[:last_modified] = time
    end
    [obj.body.read, metadata]
  rescue Aws::S3::Errors::NoSuchKey
    raise SiteMaps::FileNotFoundError, "File not found: #{location.remote_path}"
  end

  def delete(location)
    object(location.remote_path).delete
  rescue Aws::S3::Errors::NoSuchKey
    raise SiteMaps::FileNotFoundError, "File not found: #{location.remote_path}"
  end

  private

  def list_objects(prefix:)
    config.s3_bucket.objects(
      prefix: prefix
    )
  end

  def object(remote_path)
    config.s3_bucket.object(remote_path)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
site_maps-0.0.1.beta3 lib/site_maps/adapters/aws_sdk/storage.rb
site_maps-0.0.1.beta2 lib/site_maps/adapters/aws_sdk/storage.rb
site_maps-0.0.1.beta1 lib/site_maps/adapters/aws_sdk/storage.rb