From e765ef16fd212dc949c4f24b309e7e5b095608ba Mon Sep 17 00:00:00 2001 From: Martin Date: Fri, 1 Apr 2022 15:49:57 +0200 Subject: [PATCH] fix(parallel_download_queue): appending content to file in those circumstances does not lead to good result. for now write response.body directly to the attachment [may lead to RAM overhead in case of big file... but i do not have another quick fix idea] --- app/lib/download_manager/parallel_download_queue.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app/lib/download_manager/parallel_download_queue.rb b/app/lib/download_manager/parallel_download_queue.rb index 724821606..77295412a 100644 --- a/app/lib/download_manager/parallel_download_queue.rb +++ b/app/lib/download_manager/parallel_download_queue.rb @@ -36,11 +36,12 @@ module DownloadManager File.write(attachment_path, attachment.file.read, mode: 'wb') else 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| - 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 on_error.call(attachment, path_in_download_dir, response.code) end