require 'dropbox_sdk' require 'JenkinsUtil/logger_util' require 'JenkinsUtil/version' require 'pathname' module DropboxUtil include LoggerUtil attr_reader :uploaded_files 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 ||= DropboxClient.new(ENV['DROPBOX_ACCESS_TOKEN']) @sources = sources @destination = destination @flatten = flatten @root_path = Pathname.new(root) unless root.nil? upload_files end def self.delete(dropbox_location) @dropbox_client ||= DropboxClient.new(JenkinsUtil::DROPBOX_ACCESS_TOKEN) LoggerUtil.fatal('Location passed is nil') if dropbox_location.nil? end def self.upload_files LoggerUtil.log.debug(@dropbox_client.account_info.inspect) # Check auth @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 DropboxAuthError LoggerUtil.fatal('User is not authenticated, please verify access token') end def self.dropbox_file_destination(file) if @flatten name_and_extension = File.basename(file) dropbox_destination = File.join(@destination, name_and_extension) else file_path = Pathname.new(file) dropbox_destination = File.join(@destination, file_path.relative_path_from(@root_path)) end dropbox_destination end def self.upload_and_share(file) @uploaded_files ||= [] dropbox_file = @dropbox_client.put_file(dropbox_file_destination(file), open(file), false) share = @dropbox_client.shares(dropbox_file['path']) LoggerUtil.log.info("path: #{dropbox_file['path']}") LoggerUtil.log.info("url: #{share['url']}") end private_class_method :upload_files, :dropbox_file_destination, :upload_and_share end