Class: BaiduNetDisk::Uploader

Inherits:
Object
  • Object
show all
Defined in:
lib/baidu-netdisk/uploader.rb

Constant Summary collapse

SLICE_SIZE =
4 * 1024 * 1024

Instance Method Summary collapse

Constructor Details

#initialize(source_path, target_path, options = {}) ⇒ Uploader

Returns a new instance of Uploader.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/baidu-netdisk/uploader.rb', line 7

def initialize(source_path, target_path, options = {})
  @is_dir = File.directory?(source_path) ? true : false
  raise BaiduNetDisk::Exception::UploadDirectoryNotSupported, 'Uploading directory is not supported at present.' if @is_dir

  @source_path = source_path
  @target_path = target_path

  @verbose = options[:verbose]
  @slice_prefix = ".tmp_#{Random.hex(6)}_"

  @rtype = options[:overwrite] ? 3 : 1
  @file_size = File.size @source_path
  @content_md5  = Digest::MD5.hexdigest(File.read @source_path)
  @upload_id = nil
  @slices = []
  @refresh_token = options[:refresh_token] || BaiduNetDisk.refresh_token
  @access_token = options[:access_token] || BaiduNetDisk.access_token
end

Instance Method Details

#executeObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/baidu-netdisk/uploader.rb', line 26

def execute
  prepare
  pre_upload
  upload_by_slices
  create_file
rescue BaiduNetDisk::Exception::PermissionDenied
  $stderr.puts 'You are not authorised to upload files to your target path.'
  return false
rescue BaiduNetDisk::Exception::AuthenticationFailed, BaiduNetDisk::Exception::AccessTokenExpired => e
  raise e if @tried_refresh_token
  retry if refresh_token!
ensure
  clear_up
end