Skip to content

Commit 4cd7fb7

Browse files
authored
Merge pull request #68 from sherlockode/fix/remove_old_files
Fix remove old files CLI
2 parents e365ff6 + cf7d69c commit 4cd7fb7

File tree

7 files changed

+96
-57
lines changed

7 files changed

+96
-57
lines changed

Command/RemoveTemporaryFileCommand.php

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace Sherlockode\AdvancedFormBundle\Command;
44

5+
use Doctrine\ORM\EntityManagerInterface;
56
use Sherlockode\AdvancedFormBundle\Storage\FilesystemStorage;
67
use Symfony\Component\Console\Command\Command;
8+
use Symfony\Component\Console\Exception\RuntimeException;
79
use Symfony\Component\Console\Input\InputInterface;
810
use Symfony\Component\Console\Input\InputOption;
911
use Symfony\Component\Console\Output\OutputInterface;
@@ -13,23 +15,42 @@
1315
*/
1416
class RemoveTemporaryFileCommand extends Command
1517
{
18+
/**
19+
* @var EntityManagerInterface
20+
*/
21+
private $em;
22+
23+
/**
24+
* @var FilesystemStorage
25+
*/
26+
private $storage;
27+
28+
/**
29+
* @var string
30+
*/
31+
private $tmpUploadFileClass;
32+
1633
/**
1734
* RemoveTemporaryFileCommand constructor.
1835
*
19-
* @param FilesystemStorage $storage
36+
* @param EntityManagerInterface $em
37+
* @param FilesystemStorage $storage
38+
* @param string $tmpUploadFileClass
2039
*/
21-
public function __construct(FilesystemStorage $storage)
40+
public function __construct(EntityManagerInterface $em, FilesystemStorage $storage, $tmpUploadFileClass = null)
2241
{
2342
parent::__construct();
43+
$this->em = $em;
2444
$this->storage = $storage;
45+
$this->tmpUploadFileClass = $tmpUploadFileClass;
2546
}
2647

2748
protected function configure()
2849
{
2950
$this
3051
->setName('sherlockode:afb:cleanup-tmp')
3152
->setDescription('Remove all temporary files.')
32-
->addOption('older-than', null, InputOption::VALUE_REQUIRED)
53+
->addOption('older-than', null, InputOption::VALUE_OPTIONAL)
3354
;
3455
}
3556

@@ -48,23 +69,29 @@ protected function execute(InputInterface $input, OutputInterface $output)
4869
$limit->sub(new \DateInterval(sprintf('P%s', strtoupper($olderThan))));
4970
}
5071

51-
$files = $this->storage->all();
52-
$count = 0;
72+
if (!$this->tmpUploadFileClass) {
73+
throw new RuntimeException(sprintf('The "tmp_uploaded_file_class" has to be configured for this action.'));
74+
}
75+
76+
$qb = $this->em->createQueryBuilder()
77+
->select('f')
78+
->from($this->tmpUploadFileClass, 'f');
79+
80+
if ($limit) {
81+
$qb
82+
->andWhere('f.createdAt < :createdAt')
83+
->setParameter('createdAt', $limit);
84+
}
85+
86+
$files = $qb->getQuery()->getResult();
5387

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-
}
88+
foreach ($files as $file) {
89+
$this->storage->remove($file->getKey());
90+
$this->em->remove($file);
6591
}
6692

67-
$output->writeln(sprintf('<info>%s files has been removed</info>', $count));
93+
$this->em->flush();
94+
$output->writeln(sprintf('<info>%s files has been removed</info>', count($files)));
6895

6996
return 0;
7097
}

Manager/UploadManager.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ public function uploadTemporary(UploadedFile $uploadedFile)
134134
$obj->setKey($key);
135135
$obj->setToken(rand());
136136
$obj->setFilename($uploadedFile->getClientOriginalName());
137+
$obj->setCreatedAt(new \DateTime());
137138

138139
$this->em->persist($obj);
139140
$this->em->flush($obj);

Model/TemporaryUploadedFile.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Sherlockode\AdvancedFormBundle\Model;
44

55
/**
6-
* Class UploadedFile
6+
* Class TemporaryUploadedFile
77
*/
88
class TemporaryUploadedFile implements TemporaryUploadedFileInterface
99
{
@@ -22,6 +22,11 @@ class TemporaryUploadedFile implements TemporaryUploadedFileInterface
2222
*/
2323
protected $filename;
2424

25+
/**
26+
* @var \DateTime
27+
*/
28+
protected $createdAt;
29+
2530
/**
2631
* @return string
2732
*/
@@ -79,4 +84,24 @@ public function setFilename($filename)
7984

8085
return $this;
8186
}
87+
88+
/**
89+
* @return \DateTime
90+
*/
91+
public function getCreatedAt()
92+
{
93+
return $this->createdAt;
94+
}
95+
96+
/**
97+
* @param \DateTime $createdAt
98+
*
99+
* @return $this
100+
*/
101+
public function setCreatedAt(\DateTime $createdAt)
102+
{
103+
$this->createdAt = $createdAt;
104+
105+
return $this;
106+
}
82107
}

Model/TemporaryUploadedFileInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,16 @@ public function getFilename();
3333
* @param string $filename
3434
*/
3535
public function setFilename($filename);
36+
37+
/**
38+
* @return \DateTime
39+
*/
40+
public function getCreatedAt();
41+
42+
/**
43+
* @param \DateTime $createdAt
44+
*
45+
* @return $this
46+
*/
47+
public function setCreatedAt(\DateTime $createdAt);
3648
}

Resources/config/command.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@ services:
22
sherlockode_afb.command.remove_tmp_files:
33
class: Sherlockode\AdvancedFormBundle\Command\RemoveTemporaryFileCommand
44
arguments:
5+
- '@doctrine.orm.entity_manager'
56
- '@sherlockode_afb.storage.tmp_storage'
7+
- '%sherlockode_afb.tmp_uploaded_file_class%'
68
tags: ['console.command']

Storage/FilesystemStorage.php

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

33
namespace Sherlockode\AdvancedFormBundle\Storage;
44

5-
use Symfony\Component\HttpFoundation\File\File;
6-
75
class FilesystemStorage implements StorageInterface
86
{
97
private $dir;
@@ -13,21 +11,6 @@ public function __construct($dir)
1311
$this->dir = $dir;
1412
}
1513

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-
3114
public function write($key, $data)
3215
{
3316
return file_put_contents($this->dir . '/' . $key, $data);
@@ -40,17 +23,20 @@ public function read($key)
4023

4124
public function remove($key)
4225
{
43-
return unlink($this->dir .'/' . $key);
26+
if ($this->exists($key)) {
27+
return unlink($this->getPath($key));
28+
}
29+
30+
return false;
4431
}
4532

46-
public function getFileObject($key)
33+
private function exists($key)
4734
{
48-
$path = sprintf('%s/%s', $this->dir, $key);
49-
50-
if (!file_exists($path)) {
51-
return null;
52-
}
35+
return file_exists($this->getPath($key));
36+
}
5337

54-
return new File($path);
38+
private function getPath($key)
39+
{
40+
return $this->dir .'/' . $key;
5541
}
5642
}

Storage/StorageInterface.php

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

33
namespace Sherlockode\AdvancedFormBundle\Storage;
44

5-
use Symfony\Component\HttpFoundation\File\File;
6-
75
interface StorageInterface
86
{
9-
/**
10-
* @return array
11-
*/
12-
public function all();
13-
147
/**
158
* @param string $key
169
*
@@ -32,11 +25,4 @@ public function write($key, $data);
3225
* @return bool
3326
*/
3427
public function remove($key);
35-
36-
/**
37-
* @param string $key
38-
*
39-
* @return File
40-
*/
41-
public function getFileObject($key);
4228
}

0 commit comments

Comments
 (0)