@@ -32,40 +32,42 @@ set file [file join $::appfs::cachedir $filekey] file mkdir [file dirname $file] - if {![file exists $file]} { - set tmpfile "${file}.new" - - set fd [open $tmpfile "w"] - fconfigure $fd -translation binary - - catch { - set token [::http::geturl $url -channel $fd -binary true] - } - - if {[info exists token]} { - set ncode [::http::ncode $token] - ::http::reset $token - } else { - set ncode "900" - } - - close $fd - - if {$keyIsHash} { - set hash [string tolower [sha1::sha1 -hex -file $tmpfile]] - } else { - set hash $key - } - - if {$ncode == "200" && $hash == $key} { - file rename -force -- $tmpfile $file - } else { - file delete -force -- $tmpfile - } + if {[file exists $file]} { + return $file + } + + set tmpfile "${file}.[expr {rand()}]" + + set fd [open $tmpfile "w"] + fconfigure $fd -translation binary + + catch { + set token [::http::geturl $url -channel $fd -binary true] + } + + if {[info exists token]} { + set ncode [::http::ncode $token] + ::http::reset $token + } else { + set ncode "900" + } + + close $fd + + if {$keyIsHash} { + set hash [string tolower [sha1::sha1 -hex -file $tmpfile]] + } else { + set hash $key + } + + if {$ncode == "200" && $hash == $key} { + file rename -force -- $tmpfile $file + } else { + file delete -force -- $tmpfile } return $file }