I need to allow files to be downloaded, but have to track the number of times they were downloaded by each user.
I have a simple chunk of code here that allows users to download a file, and then the callback increments the counter in the datastore for that user when its done.
This works fine if a user is NOT using a download manager. But if they are (ie: DownThemAll) then the manager makes multiple HTTP requests and the callback gets called everytime. This spikes the download count and makes it unreliable. I've tried short circuiting multiple requests by watching a session variable - which in theory would have cut multiple requests down to just one. But it appeared DownThemAll didn't share the same session across requests, so it didn't work.
Is there a way to make this work?
return \Response::stream(function() use ($stream, $file, $callback) {
while (!feof($stream)) {
echo fread($stream, 1024);
}
fclose($stream);
Log::info('Resource downloaded', ['resource' => $file]);
if (is_callable($callback)) {
call_user_func($callback);
}
}, 200, $headers);