From 45c38d2dc8f27b2c3dff4a4e6bcd24b7e84e9135 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Sat, 3 Jan 2026 20:27:16 -0800 Subject: avoid duplicate downloads on sequential requests of same URL --- downloader.lua | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'downloader.lua') diff --git a/downloader.lua b/downloader.lua index 768d726..a8519dd 100644 --- a/downloader.lua +++ b/downloader.lua @@ -29,16 +29,24 @@ function mt:exec(url, file, cb) args = cmd, playback_only = false, }, function(success, res) - self.running = false - self:exec_next() - if success and res.status == 0 then - os.rename(tmp, file) - if cb then - cb(url, file) - end + success = success and res.status == 0 + if success then + success = os.rename(tmp, file) else os.remove(tmp) end + + -- execute next download after renaming the file but before + -- executing the callback function. this avoids duplicate + -- downloads when the same file is requested twice in a row, + -- without unnecessarily waiting for the callback to complete + -- before starting the next download. + self.running = false + self:exec_next() + + if success and cb then + cb(url, file) + end end) end -- cgit v1.2.3-70-g09d2