mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-08-07 23:03:00 +03:00
ZIP Exports: Improved temp file tracking & clean-up
This commit is contained in:
@@ -9,7 +9,7 @@ use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
class DownloadResponseFactory
|
||||
{
|
||||
public function __construct(
|
||||
protected Request $request
|
||||
protected Request $request,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -35,6 +35,32 @@ class DownloadResponseFactory
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a response that downloads the given file via a stream.
|
||||
* Has the option to delete the provided file once the stream is closed.
|
||||
*/
|
||||
public function streamedFileDirectly(string $filePath, string $fileName, int $fileSize, bool $deleteAfter = false): StreamedResponse
|
||||
{
|
||||
$stream = fopen($filePath, 'r');
|
||||
|
||||
if ($deleteAfter) {
|
||||
// Delete the given file if it still exists after the app terminates
|
||||
$callback = function () use ($filePath) {
|
||||
if (file_exists($filePath)) {
|
||||
unlink($filePath);
|
||||
}
|
||||
};
|
||||
|
||||
// We watch both app terminate and php shutdown to cover both normal app termination
|
||||
// as well as other potential scenarios (connection termination).
|
||||
app()->terminating($callback);
|
||||
register_shutdown_function($callback);
|
||||
}
|
||||
|
||||
return $this->streamedDirectly($stream, $fileName, $fileSize);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a file download response that provides the file with a content-type
|
||||
* correct for the file, in a way so the browser can show the content in browser,
|
||||
|
Reference in New Issue
Block a user