From 202966fd08114e18d6e49f2a16e4f23cf9d5488f Mon Sep 17 00:00:00 2001 From: cyberarm Date: Thu, 10 Feb 2022 22:40:51 -0600 Subject: [PATCH] Drafted async-http downloader, fixed not correctly fetching icons at startup --- lib/application_manager/task.rb | 2 -- lib/cache.rb | 39 ++++++++++++++++++++++++++++++++- lib/states/boot.rb | 7 +++--- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/lib/application_manager/task.rb b/lib/application_manager/task.rb index 98f2f4d..fb0ff69 100644 --- a/lib/application_manager/task.rb +++ b/lib/application_manager/task.rb @@ -542,8 +542,6 @@ class W3DHub def package_fetch(package, &block) puts "Downloading: #{package.category}:#{package.subcategory}:#{package.name}-#{package.version}" - internet = Async::HTTP::Internet.instance - Api.package(package) do |chunk, remaining_bytes, total_bytes| block&.call(chunk, remaining_bytes, total_bytes) end diff --git a/lib/cache.rb b/lib/cache.rb index 82cff98..1116870 100644 --- a/lib/cache.rb +++ b/lib/cache.rb @@ -51,6 +51,44 @@ class W3DHub "#{Store.settings[:app_install_dir]}/#{application.category}/#{application.id}/#{channel.id}" end + # Download a W3D Hub package + # TODO: More work needed to make this work reliably + def self._async_fetch_package(package, block) + path = package_path(package.category, package.subcategory, package.name, package.version) + headers = Api::FORM_ENCODED_HEADERS + start_from_bytes = package.custom_partially_valid_at_bytes + + puts " Start from bytes: #{start_from_bytes} of #{package.size}" + + create_directories(path) + + file = File.open(path, start_from_bytes.positive? ? "r+b" : "wb") + + if start_from_bytes.positive? + headers = Api::FORM_ENCODED_HEADERS + [["Range", "bytes=#{start_from_bytes}-"]] + file.pos = start_from_bytes + end + + body = "data=#{JSON.dump({ category: package.category, subcategory: package.subcategory, name: package.name, version: package.version })}" + + response = Api.post("#{Api::ENDPOINT}/apis/launcher/1/get-package", headers, body) + + total_bytes = package.size + remaining_bytes = total_bytes - start_from_bytes + + response.each do |chunk| + file.write(chunk) + + remaining_bytes -= chunk.size + + block.call(chunk, remaining_bytes, total_bytes) + end + + response.success? + ensure + file&.close + end + # Download a W3D Hub package def self.fetch_package(package, block) path = package_path(package.category, package.subcategory, package.name, package.version) @@ -73,7 +111,6 @@ class W3DHub file.write(chunk) block.call(chunk, remaining_bytes, total_bytes) - # puts " Remaining: #{((remaining_bytes.to_f / total_bytes) * 100.0).round}% (#{W3DHub::format_size(total_bytes - remaining_bytes)} / #{W3DHub::format_size(total_bytes)})" end # Create a new connection due to some weirdness somewhere in Excon diff --git a/lib/states/boot.rb b/lib/states/boot.rb index 9660088..ebe1c51 100644 --- a/lib/states/boot.rb +++ b/lib/states/boot.rb @@ -134,9 +134,10 @@ class W3DHub regenerate = false - if File.exist?(path) - regenerate = Digest::SHA256.new.hexdigest(File.binread(path)).upcase != package.checksum.upcase - regenerate ||= !File.exist?(generated_icon_path) + broken_or_out_dated_icon = Digest::SHA256.new.hexdigest(File.binread(path)).upcase != package.checksum.upcase if File.exist?(path) + + if File.exist?(path) && !broken_or_out_dated_icon + regenerate = !File.exist?(generated_icon_path) else Cache.fetch_package(package, proc {}) regenerate = true