Merge pull request #7104 from betagouv/US/fix-archive-write-append-binary-with-string

fix(parallel_download_queue): appending content to file in chunk leads to some corrupted files.
This commit is contained in:
mfo 2022-04-01 18:26:32 +02:00 committed by GitHub
commit 09b730b992
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -36,11 +36,12 @@ module DownloadManager
File.write(attachment_path, attachment.file.read, mode: 'wb') File.write(attachment_path, attachment.file.read, mode: 'wb')
else else
request = Typhoeus::Request.new(attachment.url) request = Typhoeus::Request.new(attachment.url)
request.on_body do |chunk|
File.write(attachment_path, chunk, mode: 'a+b')
end
request.on_complete do |response| request.on_complete do |response|
unless response.success? if response.success?
File.open(attachment_path, mode: "wb") do |fd|
fd.write(response.body)
end
else
File.delete(attachment_path) if File.exist?(attachment_path) # -> case of retries failed, must cleanup partialy downloaded file File.delete(attachment_path) if File.exist?(attachment_path) # -> case of retries failed, must cleanup partialy downloaded file
on_error.call(attachment, path_in_download_dir, response.code) on_error.call(attachment, path_in_download_dir, response.code)
end end