module DropboxUtil

Attributes

uploaded_files[R]

Public Class Methods

delete(file_path) click to toggle source
# File lib/jenkins_util/dropbox_util.rb, line 27
def self.delete(file_path)
  retrier(file_path) { @dropbox_client.delete(file_path) } unless @dropbox_client.nil?
end
download(file_path) click to toggle source
# File lib/jenkins_util/dropbox_util.rb, line 99
def self.download(file_path)
  retrier(file_path) { @dropbox_client.download(file_path) }
end
read_file(file_path) { |buffer| ... } click to toggle source
# File lib/jenkins_util/dropbox_util.rb, line 63
def self.read_file(file_path)
  File.open(file_path) do |file|
    while (buffer = file.read(100_000_000))
      yield buffer
    end
  end
end
retrier(file) { || ... } click to toggle source
# File lib/jenkins_util/dropbox_util.rb, line 103
def self.retrier(file)
  retries = 0
  begin
    response = yield
  rescue DropboxApi::Errors::BasicError, DropboxApi::Errors::HttpError => e
    retries += 1
    if retries <= 10
      # Rate Limit and Namespace Lock Contentions introduced in v2 APIs
      # https://www.dropbox.com/developers/reference/data-ingress-guide
      delay = e.is_a?(DropboxApi::Errors::TooManyWriteOperationsError) ? e.retry_after * retries : retries
      sleep(delay)
      LoggerUtil.log.debug("Attempt #{retries} failed. Retrying #{file} after #{delay} seconds")

      retry
    end

    LoggerUtil.fatal("Exceeded maximum retries. Failed from #{caller_locations[0].label} method with file #{file}")
    raise e
  end

  response unless response.nil?
end
upload(sources, destination, flatten = true, root = nil) click to toggle source
# File lib/jenkins_util/dropbox_util.rb, line 11
def self.upload(sources, destination, flatten = true, root = nil)
  LoggerUtil.fatal('Sources passed is nil') if sources.nil?
  LoggerUtil.fatal('Destination passed is nil') if destination.nil?
  LoggerUtil.fatal('Root must be passed if flatten is false') if !flatten && root.nil?
  LoggerUtil.fatal('Please set environment variable DROPBOX_ACCESS_TOKEN') if ENV['DROPBOX_ACCESS_TOKEN'].nil?

  @dropbox_client ||= DropboxApi::Client.new(ENV['DROPBOX_ACCESS_TOKEN'])
  @sources = sources
  @flatten = flatten
  @root_path = root || Dir.pwd
  @destination = destination
  @destination = "/#{@destination}" unless destination.to_s.start_with?('/')

  upload_files
end

Private Class Methods

dropbox_file_destination(file) click to toggle source
# File lib/jenkins_util/dropbox_util.rb, line 46
def self.dropbox_file_destination(file)
  if @flatten
    name_and_extension = File.basename(file)
    File.join(@destination, name_and_extension)
  else
    file_path = Pathname.new(file)

    if !file_path.to_s.include?(@root_path) || @root_path.nil? || @root_path.empty?
      LoggerUtil.fatal('Please set root dir, file is not a child of current dir')
    end

    destination_path = file_path.to_path.sub(@root_path, '')

    File.join(@destination, destination_path)
  end
end
upload_and_share(file) click to toggle source
# File lib/jenkins_util/dropbox_util.rb, line 71
def self.upload_and_share(file)
  return if File.directory?(file)

  @uploaded_files ||= []
  retrier(file) do
    cursor = @dropbox_client.upload_session_start('')
    read_file(file) do |data|
      @dropbox_client.upload_session_append_v2(cursor, data)
    end

    commit = DropboxApi::Metadata::CommitInfo.new(
      'path' => dropbox_file_destination(file),
      'mode' => :overwrite
    )

    @dropbox_client.upload_session_finish(cursor, commit)

    LoggerUtil.log.debug("Successfully uploaded #{file}")

    # TODO: Fix shared folder
    # @dropbox_client.share_folder(@destination)
    # temp_link = @dropbox_client.get_temporary_link(dropbox_file.path_display)

    # LoggerUtil.log.info("path: #{dropbox_file.path_display}")
    # LoggerUtil.log.info("url: #{temp_link.link}")
  end
end
upload_files() click to toggle source
# File lib/jenkins_util/dropbox_util.rb, line 31
def self.upload_files
  LoggerUtil.log.debug(@dropbox_client.get_current_account.inspect) # Check auth
  @sources = [@sources] unless @sources.respond_to?('each')

  @sources.each do |source|
    source = File.join(source, '**', '*') if File.directory?(source)

    Dir.glob(source).each do |file|
      upload_and_share(file)
    end
  end
rescue DropboxApi::Errors::UploadError
  LoggerUtil.fatal('User is not authenticated, please verify access token')
end