7

I am working on a Symfony application where users can crop images with. After someone has downloaded the cropped image I want the application to remove it from the server.

I currently have this controller action:

/**
 * @Route(
 *      "/download/{crop_id}",
 *      name="download_cropped",
 *      options={"expose"=true}
 * )
 *
 * @ParamConverter(
 *      "crop",
 *      class="WebwijsCropperBundle:Crop",
 *      options={"id" = "crop_id"}
 * )
 *
 */
public function downloadAction(Crop $crop)
{
    $dir = $this->container->getParameter('image.cropped.dir');
    return new BinaryFileResponse($dir . '/' . $crop->getCroppedFile());
}

I created this EventListener where I want to remove the image after the response is sent. This Listener will be triggered after every response.

So how do I get the information that I need to know if the response comes from the correct controller action. And how can I get the crop_id parameter so I know which file to remove?

class FileRemovalListener
{
    private $logger;

    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    public function onKernelTerminate(PostResponseEvent $event)
    {
        $response = $event->getResponse();
        $request = $event->getRequest();
        // what do i have to do here to know 
        // from which controller action the response comes?

    }
}

This is the service definition in services.yml

WebwijsCropperBundle\EventListener\FileRemovalListener:
        tags:
            - { name: kernel.event_listener, event: kernel.terminate, method: onKernelTerminate }
2
  • create a new deleteAction. then define redirect route to that deleteAction after downloading
    – user8514915
    Aug 26, 2017 at 11:25
  • I would go with something like: If the user clicks the download button, you send ajax call to some kind of deleteAction and you forward the id or you give a lifetime to all cropped images images and you have a cron job that would delete all images that have expired every 15 minutes or something like that. Aug 26, 2017 at 13:14

3 Answers 3

22

If you only want to remove the file, try this:

public function downloadAction(Crop $crop)
{
    $dir = $this->container->getParameter('image.cropped.dir');
    $response = new BinaryFileResponse($dir . '/' . $crop->getCroppedFile());

    $response->deleteFileAfterSend(true);

    return $response;
}
3
  • Makes sense to me. I also need to delete the entity but I can do that before the response is sent. I will try if this works for me and then mark your answer as correct!
    – jrswgtr
    Aug 26, 2017 at 17:18
  • It took a while, I was on vacation. Works like a charm!
    – jrswgtr
    Sep 4, 2017 at 14:32
  • Wow... Works like a dream. Thank you. Aug 7, 2019 at 12:32
1

Use

deleteFileAfterSend(bool $shouldDelete)

If this is set to true, the file will be unlinked after the request is sent. Note: If the X-Sendfile header is used, the deleteFileAfterSend setting will not be used.

0

Just a precision about the function "deleteFileAfterSend(bool $bool)". As symfony documentation said "(...)note that this will not work when the X-Sendfile header is set.". See : https://symfony.com/doc/current/components/http_foundation.html#serving-files. Then you can do something like this for delete a file after response sending :

$zipName = 'downloads/' . uniqid().'.zip';
$response = new StreamedResponse();
$response->headers->set(...);

return $response->setCallBack(function () use($zipName) {
    readfile($zipName);
    $filesystem = new Filesystem();
    try {
       $filesystem->remove($zipName);
    } catch (IOExceptionInterface $exception) {
        echo "Cannot delete the zip file : ". $zipName;
    }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.