Added download_file

This commit is contained in:
G. Gibson 2022-04-17 13:08:29 -07:00
parent 8f87624185
commit 46a8aaddfb
3 changed files with 93 additions and 20 deletions

File diff suppressed because one or more lines are too long

View file

@ -113,7 +113,13 @@ class Node0 < Sinatra::Application
# end # end
# ### Implementation of the verbs: # ### Implementation of the verbs:
get '*' do get '*' do
GxG::SERVICES[:www][:receiver].handle_request(:get, request, session) response = GxG::SERVICES[:www][:receiver].handle_request(:get, request, session)
if response[0] == -255
send_file(response[2][:result][:file], {:disposition => 'attachment', :filename => File.basename(response[2][:result][:file])})
GxG::SERVICES[:core][:resources].destroy(response[2][:result][:vfs_dir], ::GxG::DB[:administrator])
response = [200, {"content-type" => "application/json"}, {:result => true}.to_json()]
end
response
end end
# #
put '*' do put '*' do

View file

@ -1152,22 +1152,89 @@ module GxGwww
begin begin
temp_dir = "/Temporary/#{::GxG::uuid_generate().to_s}" temp_dir = "/Temporary/#{::GxG::uuid_generate().to_s}"
download_path = "#{temp_dir + "/" + (details[:file_name] || "Untitled").to_s}" download_path = "#{temp_dir + "/" + (details[:file_name] || "Untitled").to_s}"
final_path = "#{details[:destination].to_s + "/" + (details[:file_name] || "Untitled").to_s}" #
GxG::SERVICES[:core][:resources].create_directory(temp_dir, ::GxG::DB[:administrator]) if details[:destination].to_s[0..4] == "/User"
GxG::SERVICES[:core][:resources].create(download_path, ::GxG::DB[:administrator]) if @credential == :"00000000-0000-4000-0000-000000000000"
handle = GxG::SERVICES[:core][:resources].open_writable(download_path, ::GxG::DB[:administrator]) details[:destination] = ("/Public/www/" << details[:destination].to_s[5..-1]).gsub("//","/")
handle[:resource].write details[:temp_file].read else
GxG::SERVICES[:core][:resources].close(handle[:token]) details[:destination] = ("/Users/#{@credential.to_s}/" << details[:destination].to_s[5..-1]).gsub("//","/")
GxG::SERVICES[:core][:resources].move(download_path, ::GxG::DB[:administrator], final_path) unless GxG::SERVICES[:core][:resources].home_mounted?(@credential)
GxG::SERVICES[:core][:resources].set_permissions(final_path, @credential, {:execute => false, :rename => true, :move => true, :destroy => true, :create => true, :write => true, :read=>true}) GxG::SERVICES[:core][:resources].home_mount(@credential)
# end
response = [200, {"content-type" => "application/json"}, ({:result => true}).to_json()] end
else
details[:destination] = ("/Public/www/" << details[:destination].to_s).gsub("//","/")
end
if GxG::SERVICES[:core][:resources].permissions(details[:destination], @credential)[:write] == true
#
final_path = "#{details[:destination].to_s + "/" + (details[:file_name] || "Untitled").to_s}".gsub("//","/")
GxG::SERVICES[:core][:resources].create_directory(temp_dir, ::GxG::DB[:administrator])
GxG::SERVICES[:core][:resources].create(download_path, ::GxG::DB[:administrator])
handle = GxG::SERVICES[:core][:resources].open_writable(download_path, ::GxG::DB[:administrator])
handle[:resource].write details[:temp_file].read
GxG::SERVICES[:core][:resources].close(handle[:token])
GxG::SERVICES[:core][:resources].move(download_path, ::GxG::DB[:administrator], final_path)
GxG::SERVICES[:core][:resources].set_permissions(final_path, @credential, {:execute => false, :rename => true, :move => true, :destroy => true, :create => true, :write => true, :read=>true})
GxG::SERVICES[:core][:resources].destroy(temp_dir, ::GxG::DB[:administrator])
#
response = [200, {"content-type" => "application/json"}, ({:result => true}).to_json()]
else
response = [500, {"content-type" => "application/json"}, ({:result => false, :error => "You lack permissions to write."}).to_json()]
end
rescue Exception => the_error rescue Exception => the_error
response = [500, {"content-type" => "application/json"}, ({:result => false, :error => the_error.to_s}).to_json()] response = [500, {"content-type" => "application/json"}, ({:result => false, :error => the_error.to_s}).to_json()]
end end
response response
end end
# #
def download_file(details={})
# {:path => "/path/to/object"}
response = [404, {"content-type" => "application/json"}, ({:result => false, :error => "Object Not Found."}).to_json()]
begin
#
temp_dir = "/Temporary/#{::GxG::uuid_generate().to_s}"
final_path = "#{temp_dir}/#{File.basename(details[:path])}"
#
if details[:path].to_s[0..4] == "/User"
if @credential == :"00000000-0000-4000-0000-000000000000"
details[:path] = ("/Public/www/" << details[:path].to_s[5..-1]).gsub("//","/")
else
details[:path] = ("/Users/#{@credential.to_s}/" << details[:path].to_s[5..-1]).gsub("//","/")
unless GxG::SERVICES[:core][:resources].home_mounted?(@credential)
GxG::SERVICES[:core][:resources].home_mount(@credential)
end
end
else
details[:path] = ("/Public/www/" << details[:path].to_s).gsub("//","/")
end
#
# temp_file = "#{temp_dir}/#{File.basename((details[:path] || 'Untitled').to_s)}"
if GxG::SERVICES[:core][:resources].exist?(details[:path])
profile = GxG::SERVICES[:core][:resources].profile(details[:path], @credential)
if profile
if profile[:permissions][:effective][:read] == true
if GxG::SERVICES[:core][:resources].copy(details[:path], ::GxG::DB[:administrator], final_path)
cache_profile = (GxG::SERVICES[:core][:resources].entries(temp_dir, ::GxG::DB[:administrator]) || []).first
if cache_profile
handle = GxG::SERVICES[:core][:resources].open((temp_dir + "/" + cache_profile[:title]), ::GxG::DB[:administrator])
download_path = handle[:resource].path()
GxG::SERVICES[:core][:resources].close(handle[:token])
response = [-255, {"content-type" => "application/hash"}, {:result => {:file => download_path, :vfs_dir => temp_dir} }]
end
else
response = [500, {"content-type" => "application/json"}, ({:result => false, :error => "Failed to cache file."}).to_json()]
end
end
end
#
end
rescue Exception => the_error
response = [500, {"content-type" => "application/json"}, ({:result => false, :error => the_error.to_s}).to_json()]
end
#
response
end
#
def vfs_mkfile(details={}) def vfs_mkfile(details={})
# Review : find correct cooresponding return codes for errors. # Review : find correct cooresponding return codes for errors.
if self.closed? if self.closed?