Class: Pcloud::FileHandler
- Inherits:
-
Object
- Object
- Pcloud::FileHandler
- Defined in:
- lib/pcloud/files/file_handler.rb
Instance Method Summary collapse
- #download(params) ⇒ Object
- #download_folder(params) ⇒ Object
-
#initialize(client) ⇒ FileHandler
constructor
A new instance of FileHandler.
- #upload(params, payload) ⇒ Object
Constructor Details
#initialize(client) ⇒ FileHandler
7 8 9 |
# File 'lib/pcloud/files/file_handler.rb', line 7 def initialize(client) @client = client end |
Instance Method Details
#download(params) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/pcloud/files/file_handler.rb', line 15 def download(params) # destination = params[:destination] ? params[:destination] : '.' # data = request(:get, "getfilelink", {fileid: params[:fileid], forcedownload: 1}) # url = "https://#{data[:hosts][0]+data[:path]}" # IO.copy_stream(URI.open(url), "#{destination}/#{url.split("/").last}") # File.write("#{destination}/#{url.split("/").last}", URI.open(url).read) destination = params[:destination] || '.' filename = params[:filename] || nil if !filename stat = @client.request(:get, "stat", {fileid: params[:fileid]}) raise NotFoundError.new(:fileid, params[:fileid]) if stat[:error] filename = stat[:metadata][:name] end File.write("#{destination}/#{filename}", @client.request(:get, "gettextfile", {fileid: params[:fileid], raw: true})) end |
#download_folder(params) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/pcloud/files/file_handler.rb', line 32 def download_folder(params) data = @client.request(:get, "listfolder", {folderid: params[:folderid]}) raise NotFoundError.new(:folderid, params[:folderid]) if data[:error] destination = params[:destination] || '.' folder_name = params[:filename] || data[:metadata][:name] << ".zip" stringio = Zip::OutputStream::write_buffer do |zio| data[:metadata][:contents].each do |content| zio.put_next_entry(content[:name]) zio.write(@client.request(:get, "gettextfile", {fileid: content[:fileid], raw: true})) end end stringio.rewind #reposition buffer pointer to the beginning File.new("#{destination}/#{folder_name}","wb").write(stringio.sysread) #write buffer to zipfile end |
#upload(params, payload) ⇒ Object
11 12 13 |
# File 'lib/pcloud/files/file_handler.rb', line 11 def upload(params, payload) @client.request(:post, 'uploadfile', params, payload) end |