Class: FileUploadObject

Inherits:
Object
  • Object
show all
Defined in:
lib/notion_ruby_mapping/objects/file_upload_object.rb

Constant Summary collapse

MAX_SIZE =

10 MB

10 * 1024 * 1024

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fname:, external_url: nil) ⇒ FileUploadObject

Returns a new instance of FileUploadObject.

Parameters:

  • id (String)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/notion_ruby_mapping/objects/file_upload_object.rb', line 6

def initialize(fname:, external_url: nil)
  @fname = fname
  if external_url
    payload = {mode: "external_url", external_url: external_url, filename: fname}
    create payload
  else
    raise StandardError, "FileUploadObject requires a valid file name: #{fname}" unless File.exist?(fname)

    @file_size = File.size fname
    @number_of_parts = (@file_size - 1) / MAX_SIZE + 1
    payload = if @number_of_parts == 1
                {}
              else
                {number_of_parts: @number_of_parts, mode: "multi_part",
                 filename: File.basename(@fname)}
              end
    create payload
    if @number_of_parts == 1
      single_file_upload
    else
      @temp_files = FileUploadObject.split_to_small_files(@fname, MAX_SIZE)
      @temp_files.each_with_index do |temp_file, i|
        single_file_upload temp_file.path, i + 1
        temp_file.close
        temp_file.unlink
      end
      NotionRubyMapping::NotionCache.instance.complete_a_file_upload_request @id
    end
  end
end

Instance Attribute Details

#fnameObject (readonly)

Returns the value of attribute fname.



36
37
38
# File 'lib/notion_ruby_mapping/objects/file_upload_object.rb', line 36

def fname
  @fname
end

#idObject (readonly)

Returns the value of attribute id.



36
37
38
# File 'lib/notion_ruby_mapping/objects/file_upload_object.rb', line 36

def id
  @id
end

#statusObject (readonly)

Returns the value of attribute status.



36
37
38
# File 'lib/notion_ruby_mapping/objects/file_upload_object.rb', line 36

def status
  @status
end

Class Method Details

.split_to_small_files(org_file, max_size = MAX_SIZE) ⇒ Object

Raises:

  • (StandardError)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/notion_ruby_mapping/objects/file_upload_object.rb', line 70

def self.split_to_small_files(org_file, max_size = MAX_SIZE)
  raise StandardError, "File does not exist: #{org_file}" unless File.exist?(org_file)

  temp_files = []
  File.open(org_file, "rb") do |file|
    until file.eof?
      chunk = file.read(max_size)
      temp_file = Tempfile.new("part_")
      temp_file.binmode
      temp_file.write(chunk)
      temp_file.rewind
      temp_files << temp_file
    end
  end
  temp_files
end

Instance Method Details

#create(payload) ⇒ FileUploadObject

Returns:



39
40
41
42
43
44
# File 'lib/notion_ruby_mapping/objects/file_upload_object.rb', line 39

def create(payload)
  nc = NotionRubyMapping::NotionCache.instance
  response = nc.create_file_upload_request(payload)
  @id = nc.hex_id response["id"]
  @status = response["status"]
end

#reloadObject



46
47
48
49
50
51
# File 'lib/notion_ruby_mapping/objects/file_upload_object.rb', line 46

def reload
  nc = NotionRubyMapping::NotionCache.instance
  response = nc.file_upload_request @id
  @status = response["status"]
  self
end

#single_file_upload(fname = @fname, part_number = 0) ⇒ Object

Parameters:

  • fname (String) (defaults to: @fname)
  • part_number (Integer, NilClass) (defaults to: 0)

Raises:

  • (StandardError)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/notion_ruby_mapping/objects/file_upload_object.rb', line 55

def single_file_upload(fname = @fname, part_number = 0)
  if @number_of_parts > 1
    options = {"part_number" => part_number}
    status = "pending"
  else
    options = {}
    status = "uploaded"
  end
  nc = NotionRubyMapping::NotionCache.instance
  response = nc.send_file_upload_request fname, @id, options
  return if nc.hex_id(response["id"]) == @id && response["status"] == status

  raise StandardError, "File upload failed: #{response}"
end