Class: Sharepoint::File
- Inherits:
-
Object
- Object
- ObjectProperties
- Object
- Sharepoint::File
- Includes:
- Type
- Defined in:
- lib/sharepoint-files.rb
Instance Attribute Summary
Attributes inherited from Object
Attributes inherited from ObjectProperties
Instance Method Summary collapse
- #download_to_file(filename) ⇒ Object
- #upload_file_via_streaming(file) ⇒ Object
- #upload_from_file(filename) ⇒ Object
Methods included from Type
Methods inherited from Object
#copy, #destroy, #guid, #initialize, #reload, #save
Methods inherited from ObjectProperties
#add_properties, #add_property, #available_properties, #initialize
Instance Method Details
#download_to_file(filename) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/sharepoint-files.rb', line 65 def download_to_file filename content = download ::File.open filename, "w:#{content.encoding.name}" do |file| file.write content end end |
#upload_file_via_streaming(file) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/sharepoint-files.rb', line 72 def upload_file_via_streaming file uuid = SecureRandom.uuid bytes_written = 0 ::File.open(file) do |fh| while data = fh.read(10 * 1024 * 1024) do uri = (bytes_written == 0) ? "#{['uri']}/startupload(uploadId=guid'#{uuid}')" : "#{['uri']}/continueupload(uploadId=guid'#{uuid}',fileOffset=#{bytes_written})" result = @site.query :post, uri, data, skip_json: true new_position = (JSON.parse(result).dig('d', 'ContinueUpload') || JSON.parse(result).dig('d', 'StartUpload')).to_i bytes_written += data.size if new_position != bytes_written raise Sharepoint::Error.new("Streamed #{bytes_written} bytes data, but sharepoint reports position #{new_position}") end end end uri = "#{['uri']}/finishupload(uploadId=guid'#{uuid}',fileOffset=#{bytes_written})" result = @site.query :post, uri, nil, skip_json: true end |