Class: FileUploadObject
- Inherits:
-
Object
- Object
- FileUploadObject
- 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
-
#fname ⇒ Object
readonly
Returns the value of attribute fname.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Class Method Summary collapse
Instance Method Summary collapse
- #create(payload) ⇒ FileUploadObject
-
#initialize(fname:, external_url: nil) ⇒ FileUploadObject
constructor
A new instance of FileUploadObject.
- #reload ⇒ Object
- #single_file_upload(fname = @fname, part_number = 0) ⇒ Object
Constructor Details
#initialize(fname:, external_url: nil) ⇒ FileUploadObject
Returns a new instance of FileUploadObject.
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
#fname ⇒ Object (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 |
#id ⇒ Object (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 |
#status ⇒ Object (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
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
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 |
#reload ⇒ Object
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
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 = {"part_number" => part_number} status = "pending" else = {} status = "uploaded" end nc = NotionRubyMapping::NotionCache.instance response = nc.send_file_upload_request fname, @id, return if nc.hex_id(response["id"]) == @id && response["status"] == status raise StandardError, "File upload failed: #{response}" end |