8000 feature #11212 [HttpFoundation] Added a switch to delete file after t… · symfony/symfony@01346f7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 01346f7

Browse files
committed
feature #11212 [HttpFoundation] Added a switch to delete file after the response is send (WybrenKoelmans)
This PR was merged into the 2.6-dev branch. Discussion ---------- [HttpFoundation] Added a switch to delete file after the response is send | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | N/A | License | MIT | Doc PR | symfony/symfony-docs#3975 I have not done any Unit Tests for this code as I suspect there may already be a way to solve my problem of deleting a file after the request was sent. Is it possible to use `sendContent` and delete the file after that? My attempts were unsuccessful. If this code is desirable, please assist me in how I would write an unit test for this. Thanks. TODO: - [x] Add unit tests - [x] Update documentation - [x] Mention that using `X-Sendfile` will overwrite deleteFileAfterSend Commits ------- 1fff158 [HttpFoundation] Added a switch to delete file after the response is send
2 parents d941f80 + 1fff158 commit 01346f7

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/Symfony/Component/HttpFoundation/BinaryFileResponse.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class BinaryFileResponse extends Response
3030
protected $file;
3131
protected $offset;
3232
protected $maxlen;
33+
protected $deleteFileAfterSend = false;
3334

3435
/**
3536
* Constructor.
@@ -258,6 +259,10 @@ public function sendContent()
258259

259260
fclose($out);
260261
fclose($file);
262+
263+
if ($this->deleteFileAfterSend) {
264+
unlink($this->file->getPathname());
265+
}
261266
}
262267

263268
/**
@@ -289,4 +294,19 @@ public static function trustXSendfileTypeHeader()
289294
{
290295
self::$trustXSendfileTypeHeader = true;
291296
}
297+
298+
/**
299+
* If this is set to true, the file will be unlinked after the request is send
300+
* Note: If the X-Sendfile header is used, the deleteFileAfterSend setting will not be used.
301+
* @param bool $shouldDelete
302+
*
303+
* @return BinaryFileResponse
304+
*/
305+
public function deleteFileAfterSend($shouldDelete)
306+
{
307+
$this->deleteFileAfterSend = $shouldDelete;
308+
309+
return $this;
310+
}
311+
292312
}

src/Symfony/Component/HttpFoundation/Tests/BinaryFileResponseTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,24 @@ public function testXAccelMapping($realpath, $mapping, $virtual)
193193
$this->assertEquals($virtual, $response->headers->get('X-Accel-Redirect'));
194194
}
195195

196+
public function testDeleteFileAfterSend()
197+
{
198+
$request = Request::create('/');
199+
200+
$path = __DIR__.'/File/Fixtures/to_delete';
201+
touch($path);
202+
$realPath = realpath($path);
203+
$this->assertFileExists($realPath);
204+
205+
$response = new BinaryFileResponse($realPath);
206+
$response->deleteFileAfterSend(true);
207+
208+
$response->prepare($request);
209+
$response->sendContent();
210+
211+
$this->assertFileNotExists($path);
212+
}
213+
196214
public function getSampleXAccelMappings()
197215
{
198216
return array(
@@ -205,4 +223,12 @@ protected function provideResponse()
205223
{
206224
return new BinaryFileResponse(__DIR__ . '/../README.md');
207225
}
226+
227+
public static function tearDownAfterClass()
228+
{
229+
$path = __DIR__.'/../Fixtures/to_delete';
230+
if (file_exists($path)) {
231+
@unlink($path);
232+
}
233+
}
208234
}

0 commit comments

Comments
 (0)
0