Class: Notion::FileUploader

Inherits:
Object
  • Object
show all
Defined in:
lib/notion/file_uploader.rb

Constant Summary collapse

PART_SIZE =
20 * 1024 * 1024

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ FileUploader

Returns a new instance of FileUploader.



10
11
12
# File 'lib/notion/file_uploader.rb', line 10

def initialize(client)
  @client = client
end

Instance Method Details

#import(url:, filename: nil, content_type: nil, wait: true, timeout: 300, interval: 1, sleeper: Kernel.method(:sleep)) ⇒ Object

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/notion/file_uploader.rb', line 29

def import(url:, filename: nil, content_type: nil, wait: true, timeout: 300, interval: 1,
           sleeper: Kernel.method(:sleep))
  uri = URI(url)
  raise ArgumentError, "URL must use http or https" unless %w[http https].include?(uri.scheme)

  upload = @client.file_uploads.create(**{
    mode: "external_url",
    external_url: url,
    filename: filename,
    content_type: content_type
  }.compact)
  wait ? wait_for_upload(upload, timeout, interval, sleeper) : upload
end

#upload(path: nil, io: nil, filename: nil, content_type: "application/octet-stream", on_progress: nil, concurrency: 3) ⇒ Object

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/notion/file_uploader.rb', line 14

def upload(path: nil, io: nil, filename: nil, content_type: "application/octet-stream", on_progress: nil,
           concurrency: 3)
  raise ArgumentError, "concurrency must be positive" unless concurrency.to_i.positive?

  if path
    return File.open(path, "rb") do |file|
      upload_io(file, filename || File.basename(path), content_type, on_progress, concurrency)
    end
  end

  raise ArgumentError, "path or io is required" unless io

  upload_io(io, filename || "upload.bin", content_type, on_progress, concurrency)
end