Merge pull request #13654 from oparoz/tmpfile-with-extension

Keep the extension in temp files
This commit is contained in:
Vincent Petry 2015-03-26 23:08:08 +01:00
commit 746be98e03
3 changed files with 16 additions and 9 deletions

View File

@ -45,12 +45,16 @@ abstract class Office extends Provider {
shell_exec($exec); shell_exec($exec);
//create imagick object from pdf //create imagick object from pdf
$pdfPreview = null;
try{ try{
$pdf = new \imagick($absPath . '.pdf' . '[0]'); list( $dirname, , , $filename ) = array_values( pathinfo($absPath) );
$pdfPreview = $dirname . '/' . $filename . '.pdf';
$pdf = new \imagick($pdfPreview . '[0]');
$pdf->setImageFormat('jpg'); $pdf->setImageFormat('jpg');
} catch (\Exception $e) { } catch (\Exception $e) {
unlink($absPath); unlink($absPath);
unlink($absPath . '.pdf'); unlink($pdfPreview);
\OC_Log::write('core', $e->getmessage(), \OC_Log::ERROR); \OC_Log::write('core', $e->getmessage(), \OC_Log::ERROR);
return false; return false;
} }
@ -59,7 +63,7 @@ abstract class Office extends Provider {
$image->loadFromData($pdf); $image->loadFromData($pdf);
unlink($absPath); unlink($absPath);
unlink($absPath . '.pdf'); unlink($pdfPreview);
return $image->valid() ? $image : false; return $image->valid() ? $image : false;
} }

View File

@ -55,6 +55,9 @@ class TempManager implements ITempManager {
} }
protected function generatePath($postFix) { protected function generatePath($postFix) {
if ($postFix) {
$postFix = '.' . ltrim($postFix, '.');
}
return $this->tmpBaseDir . '/oc_tmp_' . md5(time() . rand()) . $postFix; return $this->tmpBaseDir . '/oc_tmp_' . md5(time() . rand()) . $postFix;
} }

View File

@ -51,7 +51,7 @@ class TempManager extends \Test\TestCase {
public function testGetFile() { public function testGetFile() {
$manager = $this->getManager(); $manager = $this->getManager();
$file = $manager->getTemporaryFile('.txt'); $file = $manager->getTemporaryFile('txt');
$this->assertStringEndsWith('.txt', $file); $this->assertStringEndsWith('.txt', $file);
$this->assertTrue(is_file($file)); $this->assertTrue(is_file($file));
$this->assertTrue(is_writable($file)); $this->assertTrue(is_writable($file));
@ -73,8 +73,8 @@ class TempManager extends \Test\TestCase {
public function testCleanFiles() { public function testCleanFiles() {
$manager = $this->getManager(); $manager = $this->getManager();
$file1 = $manager->getTemporaryFile('.txt'); $file1 = $manager->getTemporaryFile('txt');
$file2 = $manager->getTemporaryFile('.txt'); $file2 = $manager->getTemporaryFile('txt');
$this->assertTrue(file_exists($file1)); $this->assertTrue(file_exists($file1));
$this->assertTrue(file_exists($file2)); $this->assertTrue(file_exists($file2));
@ -105,8 +105,8 @@ class TempManager extends \Test\TestCase {
public function testCleanOld() { public function testCleanOld() {
$manager = $this->getManager(); $manager = $this->getManager();
$oldFile = $manager->getTemporaryFile('.txt'); $oldFile = $manager->getTemporaryFile('txt');
$newFile = $manager->getTemporaryFile('.txt'); $newFile = $manager->getTemporaryFile('txt');
$folder = $manager->getTemporaryFolder(); $folder = $manager->getTemporaryFolder();
$nonOcFile = $this->baseDir . '/foo.txt'; $nonOcFile = $this->baseDir . '/foo.txt';
file_put_contents($nonOcFile, 'bar'); file_put_contents($nonOcFile, 'bar');
@ -135,7 +135,7 @@ class TempManager extends \Test\TestCase {
$logger->expects($this->once()) $logger->expects($this->once())
->method('warning') ->method('warning')
->with($this->stringContains('Can not create a temporary file in directory')); ->with($this->stringContains('Can not create a temporary file in directory'));
$this->assertFalse($manager->getTemporaryFile('.txt')); $this->assertFalse($manager->getTemporaryFile('txt'));
} }
public function testLogCantCreateFolder() { public function testLogCantCreateFolder() {