class Carraway::FileRepository

Public Instance Methods

all() click to toggle source
# File lib/carraway/file_repository.rb, line 3
    def all
      query = { table_name: Config.backend['table_name'] }
      query[:filter_expression] = <<~FILTER
          record_type = :type
        FILTER
      query[:expression_attribute_values] = {
        ':type' => 'file'
      }

      client.scan(query).items.map do |item|
        Carraway::File.new(
          uid: item['uid'],
          ext: item['ext'],
          title: item['title'],
          created: item['created'],
          labels: item['labels'],
          published: item['published'],
          category: Carraway::Category.find(item['category'])
        )
      end
    end
destroy(file) click to toggle source
# File lib/carraway/file_repository.rb, line 79
def destroy(file)
  s3_client.delete_object(
    bucket: Config.file_backend['bucket'],
    key: file.path
  )
  client.delete_item(
    table_name: Config.backend['table_name'],
    key: {
      uid: file.uid
    }
  )
end
drop() click to toggle source
# File lib/carraway/file_repository.rb, line 98
def drop
  s3_client.list_objects(
    bucket: Config.file_backend['bucket']
  ).contents.each do |content|
    s3_client.delete_object(
      bucket: Config.file_backend['bucket'],
      key: content.key
    )
  end
  s3_client.delete_bucket(
    bucket: Config.file_backend['bucket']
  )
end
find(uid) click to toggle source
# File lib/carraway/file_repository.rb, line 25
def find(uid)
  item = client.get_item(
    key: {
      uid: uid
    },
    table_name: Config.backend['table_name'],
  ).item
  if item && item['record_type'] == 'file'
    Carraway::File.new(
      uid: item['uid'],
      ext: item['ext'],
      title: item['title'],
      created: item['created'],
      labels: item['labels'],
      published: item['published'],
      category: Carraway::Category.find(item['category'])
    )
  end
end
persisted?(file) click to toggle source
# File lib/carraway/file_repository.rb, line 45
def persisted?(file)
  s3_client.head_object(
    bucket: Config.file_backend['bucket'],
    key: file.path
  )
  true
rescue Aws::S3::Errors::NotFound
  false
end
save(file, at: Time.now) click to toggle source
# File lib/carraway/file_repository.rb, line 55
def save(file, at: Time.now)
  client.put_item(
    table_name: Config.backend['table_name'],
    item: {
      uid: file.uid,
      record_type: 'file',
      ext: file.ext,
      title: file.title,
      created: file.created || at.to_i,
      labels: file.labels,
      published: file.published,
      category: file.category.key
    }
  )
  if file.file
    s3_client.put_object(
      body: file.file[:tempfile],
      bucket: Config.file_backend['bucket'],
      acl: 'public-read',
      key: file.path
    )
  end
end
setup() click to toggle source
# File lib/carraway/file_repository.rb, line 92
def setup
  s3_client.create_bucket(
    bucket: Config.file_backend['bucket']
  )
end

Private Instance Methods

client() click to toggle source
# File lib/carraway/file_repository.rb, line 114
def client
  if Config.backend['endpoint']
    Aws::DynamoDB::Client.new(
      endpoint: Config.backend['endpoint'],
      region: Config.backend['region'],
      access_key_id: 'dummy',
      secret_access_key: 'dummy'
    )
  else
    Aws::DynamoDB::Client.new
  end
end
s3_client() click to toggle source
# File lib/carraway/file_repository.rb, line 127
def s3_client
  if Config.file_backend['endpoint']
    Aws::S3::Client.new(
      endpoint: Config.file_backend['endpoint'],
      region: Config.file_backend['region'],
      access_key_id: 'dummy',
      secret_access_key: 'dummy_secret',
      force_path_style: true,
    )
  else
    Aws::S3::Client.new
  end
end