class Sharepoint::File

Public Instance Methods

download_to_file(filename) click to toggle source
# 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) click to toggle source
# 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) ? "#{__metadata['uri']}/startupload(uploadId=guid'#{uuid}')" : "#{__metadata['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 = "#{__metadata['uri']}/finishupload(uploadId=guid'#{uuid}',fileOffset=#{bytes_written})"
  result = @site.query :post, uri, nil, skip_json: true
end
upload_from_file(filename) click to toggle source
# File lib/sharepoint-files.rb, line 56
def upload_from_file filename
  content = String.new
  ::File.open filename, 'rb' do |file|
    line = nil
    content += line while line = file.gets
  end
  upload content
end