Fixed race condition where images for user avatar or news icons were not blocking execution until after they were downloaded causing a possible crash due to trying to save and load at the same time (sometimes also causing images to be only partly saved making them corrupt causing another crash)

This commit is contained in:
2022-03-20 13:51:42 -05:00
parent 76ca7e369f
commit 8766ed7d86
5 changed files with 11 additions and 8 deletions

View File

@@ -7,16 +7,19 @@ class W3DHub
end
# Fetch a generic uri
def self.fetch(uri, force_fetch = false)
def self.fetch(uri:, force_fetch: false, async: true)
path = path(uri)
if !force_fetch && File.exist?(path)
path
else
elsif async
BackgroundWorker.job(
-> { Async::HTTP::Internet.instance.get(uri, W3DHub::Api::DEFAULT_HEADERS) },
->(response) { response.save(path, "wb") if response.success? }
)
else
response = Async::HTTP::Internet.instance.get(uri, W3DHub::Api::DEFAULT_HEADERS)
response.save(path, "wb") if response.success?
end
end