Cleanup temporary files

This commit is contained in:
Romain Neutron
2013-08-07 12:55:44 +02:00
parent 679d81da2e
commit 1c94e12a8f
5 changed files with 11 additions and 5 deletions

View File

@@ -28,7 +28,7 @@
"neutron/silex-filesystem-provider": "~1.0", "neutron/silex-filesystem-provider": "~1.0",
"neutron/sphinxsearch-api" : "~2.0.6", "neutron/sphinxsearch-api" : "~2.0.6",
"neutron/recaptcha" : "~0.1.0", "neutron/recaptcha" : "~0.1.0",
"neutron/temporary-filesystem" : "~2.0", "neutron/temporary-filesystem" : "~2.1",
"php-xpdf/php-xpdf" : "~0.2.1", "php-xpdf/php-xpdf" : "~0.2.1",
"phpexiftool/phpexiftool" : "~0.2, >=0.2.2", "phpexiftool/phpexiftool" : "~0.2, >=0.2.2",
"silex/silex" : "~1.0.0", "silex/silex" : "~1.0.0",

View File

@@ -81,7 +81,7 @@ class RecordAdd extends Command
if ($input->getOption('in-place') !== '1') { if ($input->getOption('in-place') !== '1') {
$originalName = pathinfo($file, PATHINFO_BASENAME); $originalName = pathinfo($file, PATHINFO_BASENAME);
$tempfile = tempnam(sys_get_temp_dir(), 'addrecord') . '.' . pathinfo($file, PATHINFO_EXTENSION); $tempfile = $this->container['temporary-filesystem']->createTemporaryFile('add_record', null, pathinfo($file, PATHINFO_EXTENSION));
$this->container['monolog']->addInfo(sprintf('copy file from `%s` to temporary `%s`', $file, $tempfile)); $this->container['monolog']->addInfo(sprintf('copy file from `%s` to temporary `%s`', $file, $tempfile));
$this->container['filesystem']->copy($file, $tempfile, true); $this->container['filesystem']->copy($file, $tempfile, true);
$file = $tempfile; $file = $tempfile;
@@ -97,6 +97,7 @@ class RecordAdd extends Command
if ($input->getOption('force')) { if ($input->getOption('force')) {
switch ($input->getOption('force')) { switch ($input->getOption('force')) {
default: default:
$this->container['temporary-filesystem']->clean('add_record');
throw new \InvalidArgumentException(sprintf('`%s` is not a valid force option', $input->getOption('force'))); throw new \InvalidArgumentException(sprintf('`%s` is not a valid force option', $input->getOption('force')));
break; break;
case 'record': case 'record':
@@ -130,7 +131,7 @@ class RecordAdd extends Command
if ($tempfile) { if ($tempfile) {
$this->container['monolog']->addInfo(sprintf('Remove temporary file `%s`', $tempfile)); $this->container['monolog']->addInfo(sprintf('Remove temporary file `%s`', $tempfile));
$this->container['filesystem']->remove($tempfile); $this->container['temporary-filesystem']->clean('add_record');
} }
return; return;

View File

@@ -231,6 +231,7 @@ class Configuration implements ConfigurationInterface
} }
} }
unlink($tmpFile);
throw new RuntimeException(sprintf('Unable to write %s', $file)); throw new RuntimeException(sprintf('Unable to write %s', $file));
} }

View File

@@ -12,6 +12,7 @@
namespace Alchemy\Phrasea\Core\Provider; namespace Alchemy\Phrasea\Core\Provider;
use Neutron\TemporaryFilesystem\TemporaryFilesystem; use Neutron\TemporaryFilesystem\TemporaryFilesystem;
use Neutron\TemporaryFilesystem\Manager;
use Silex\Application; use Silex\Application;
use Silex\ServiceProviderInterface; use Silex\ServiceProviderInterface;
@@ -19,9 +20,12 @@ class TemporaryFilesystemServiceProvider implements ServiceProviderInterface
{ {
public function register(Application $app) public function register(Application $app)
{ {
$app['temporary-filesystem'] = $app->share(function (Application $app) { $app['temporary-filesystem.temporary-fs'] = $app->share(function (Application $app) {
return new TemporaryFilesystem($app['filesystem']); return new TemporaryFilesystem($app['filesystem']);
}); });
$app['temporary-filesystem'] = $app->share(function (Application $app) {
return new Manager($app['temporary-filesystem.temporary-fs'], $app['filesystem']);
});
} }
public function boot(Application $app) public function boot(Application $app)

View File

@@ -10,7 +10,7 @@ class TemporaryFilesystemServiceProvidertest extends ServiceProviderTestCase
public function provideServiceDescription() public function provideServiceDescription()
{ {
return array( return array(
array('Alchemy\Phrasea\Core\Provider\TemporaryFilesystemServiceProvider', 'temporary-filesystem', 'Neutron\TemporaryFilesystem\TemporaryFilesystem'), array('Alchemy\Phrasea\Core\Provider\TemporaryFilesystemServiceProvider', 'temporary-filesystem', 'Neutron\TemporaryFilesystem\TemporaryFilesystemInterface'),
); );
} }
} }