Skip to content

Commit e3d5f45

Browse files
authored
Merge pull request #66 from sherlockode/feature/remove_tmp_files_cli
Add a CLI to remove temporary files
2 parents ba8d81e + c61836b commit e3d5f45

File tree

6 files changed

+136
-0
lines changed

6 files changed

+136
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Sherlockode\AdvancedFormBundle\Command;
4+
5+
use Sherlockode\AdvancedFormBundle\Storage\FilesystemStorage;
6+
use Symfony\Component\Console\Command\Command;
7+
use Symfony\Component\Console\Input\InputInterface;
8+
use Symfony\Component\Console\Input\InputOption;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
11+
/**
12+
* Class RemoveTemporaryFileCommand
13+
*/
14+
class RemoveTemporaryFileCommand extends Command
15+
{
16+
/**
17+
* RemoveTemporaryFileCommand constructor.
18+
*
19+
* @param FilesystemStorage $storage
20+
*/
21+
public function __construct(FilesystemStorage $storage)
22+
{
23+
parent::__construct();
24+
$this->storage = $storage;
25+
}
26+
27+
protected function configure()
28+
{
29+
$this
30+
->setName('sherlockode:afb:cleanup-tmp')
31+
->setDescription('Remove all temporary files.')
32+
->addOption('older-than', null, InputOption::VALUE_REQUIRED)
33+
;
34+
}
35+
36+
/**
37+
* @param InputInterface $input
38+
* @param OutputInterface $output
39+
*
40+
* @return int
41+
*/
42+
protected function execute(InputInterface $input, OutputInterface $output)
43+
{
44+
$olderThan = $input->getOption('older-than');
45+
$limit = null;
46+
if ($olderThan) {
47+
$limit = clone new \DateTime();
48+
$limit->sub(new \DateInterval(sprintf('P%s', strtoupper($olderThan))));
49+
}
50+
51+
$files = $this->storage->all();
52+
$count = 0;
53+
54+
foreach ($this->storage->all() as $filePath) {
55+
if ($limit) {
56+
$file = $this->storage->getFileObject($filePath);
57+
if ($file->getCTime() < $limit->getTimestamp()) {
58+
$this->storage->remove($filePath);
59+
$count++;
60+
}
61+
} else {
62+
$this->storage->remove($filePath);
63+
$count++;
64+
}
65+
}
66+
67+
$output->writeln(sprintf('<info>%s files has been removed</info>', $count));
68+
69+
return 0;
70+
}
71+
}

DependencyInjection/SherlockodeAdvancedFormExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ private function loadServices(ContainerBuilder $container)
7474
{
7575
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
7676
$toBeLoaded = [
77+
'command.yml',
7778
'controller.yml',
7879
'form.yml',
7980
'manager.yml',

Resources/config/command.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
services:
2+
sherlockode_afb.command.remove_tmp_files:
3+
class: Sherlockode\AdvancedFormBundle\Command\RemoveTemporaryFileCommand
4+
arguments:
5+
- '@sherlockode_afb.storage.tmp_storage'
6+
tags: ['console.command']

Resources/doc/temporary_upload.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,19 @@ $builder
6666
])
6767
;
6868
```
69+
70+
Remove files from temporary storage
71+
-------------
72+
73+
Sometimes user uploads a file and never submit the entity form. To remove all non-used temporary files, you can run
74+
this command:
75+
76+
```bash
77+
$ php bin/console afb:cleanup:tmpfiles
78+
```
79+
80+
Use the `older-than` option to only remove older files:
81+
82+
```bash
83+
$ php bin/console afb:cleanup:tmpfiles --older-than=3d
84+
```

Storage/FilesystemStorage.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Sherlockode\AdvancedFormBundle\Storage;
44

5+
use Symfony\Component\HttpFoundation\File\File;
6+
57
class FilesystemStorage implements StorageInterface
68
{
79
private $dir;
@@ -11,6 +13,21 @@ public function __construct($dir)
1113
$this->dir = $dir;
1214
}
1315

16+
public function all()
17+
{
18+
$files = [];
19+
20+
if ($this->dir) {
21+
foreach (new \DirectoryIterator($this->dir) as $fileInfo) {
22+
if (!$fileInfo->isDot()) {
23+
$files[] = $fileInfo->getFilename();
24+
}
25+
}
26+
}
27+
28+
return $files;
29+
}
30+
1431
public function write($key, $data)
1532
{
1633
return file_put_contents($this->dir . '/' . $key, $data);
@@ -25,4 +42,15 @@ public function remove($key)
2542
{
2643
return unlink($this->dir .'/' . $key);
2744
}
45+
46+
public function getFileObject($key)
47+
{
48+
$path = sprintf('%s/%s', $this->dir, $key);
49+
50+
if (!file_exists($path)) {
51+
return null;
52+
}
53+
54+
return new File($path);
55+
}
2856
}

Storage/StorageInterface.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@
22

33
namespace Sherlockode\AdvancedFormBundle\Storage;
44

5+
use Symfony\Component\HttpFoundation\File\File;
6+
57
interface StorageInterface
68
{
9+
/**
10+
* @return array
11+
*/
12+
public function all();
13+
714
/**
815
* @param string $key
916
*
@@ -25,4 +32,11 @@ public function write($key, $data);
2532
* @return bool
2633
*/
2734
public function remove($key);
35+
36+
/**
37+
* @param string $key
38+
*
39+
* @return File
40+
*/
41+
public function getFileObject($key);
2842
}

0 commit comments

Comments
 (0)