Class: Sharepoint::File

Inherits:
Object show all
Includes:
Type
Defined in:
lib/sharepoint-files.rb

Instance Attribute Summary

Attributes inherited from Object

#parent

Attributes inherited from ObjectProperties

#data, #site, #updated_data

Instance Method Summary collapse

Methods included from Type

#initialize

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

#upload_from_file(filename) ⇒ Object



56
57
58
59
60
61
62
63
# 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