Sha256: 5627884e76e9cad97643d7dd9a2a9150dca9364076473d4d8ea08b18983caed5

Contents?: true

Size: 1.05 KB

Versions: 3

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

require 'json'
require 'dropbox_api'
require 'dropbox_content_hasher'

module Riserva::Storage
  class Dropbox < ApplicationStorage
    def upload(local_file, remote_file)
      session.upload(
        File.join('/', remote_file),
        IO.read(local_file)
      )
    end

    def download(remote_file, local_file)
      session.download(File.join('/', remote_file)) do |stream|
        Pathname.new(local_file).write(stream)
      end
    end

    def verify(remote_file, local_file)
      data = session.get_metadata(File.join('/', remote_file))
      checksum = DropboxContentHasher.calculate(local_file)

      data.content_hash == checksum
    end

    def clean
      return unless time_to_keep

      session.list_folder('').entries.each do |file|
        next unless file.client_modified < time_to_keep.ago

        session.delete file.path_lower
      end
    end

    private

    def session
      @session ||= ::DropboxApi::Client.new(token)
    end

    def token
      JSON.parse(File.read(secrets))['bearer']
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
riserva-0.1.7 lib/riserva/storage/dropbox.rb
riserva-0.1.6 lib/riserva/storage/dropbox.rb
riserva-0.1.5 lib/riserva/storage/dropbox.rb