Use export disk for zip file.

This commit is contained in:
James Cole 2016-02-23 07:27:29 +01:00
parent 6b00f5a97d
commit 08b4c9ea5c
2 changed files with 20 additions and 24 deletions

View File

@ -14,6 +14,7 @@ use FireflyIII\Export\Entry;
use FireflyIII\Models\ExportJob; use FireflyIII\Models\ExportJob;
use League\Csv\Writer; use League\Csv\Writer;
use SplFileObject; use SplFileObject;
use Storage;
/** /**
* Class CsvExporter * Class CsvExporter
@ -25,9 +26,6 @@ class CsvExporter extends BasicExporter implements ExporterInterface
/** @var string */ /** @var string */
private $fileName; private $fileName;
/** @var resource */
private $handler;
/** /**
* CsvExporter constructor. * CsvExporter constructor.
* *
@ -36,6 +34,7 @@ class CsvExporter extends BasicExporter implements ExporterInterface
public function __construct(ExportJob $job) public function __construct(ExportJob $job)
{ {
parent::__construct($job); parent::__construct($job);
} }
/** /**
@ -54,9 +53,11 @@ class CsvExporter extends BasicExporter implements ExporterInterface
// create temporary file: // create temporary file:
$this->tempFile(); $this->tempFile();
// necessary for CSV writer:
$fullPath = storage_path('export') . DIRECTORY_SEPARATOR . $this->fileName;
// create CSV writer: // create CSV writer:
$writer = Writer::createFromPath(new SplFileObject($this->fileName, 'a+'), 'w'); $writer = Writer::createFromPath(new SplFileObject($fullPath, 'a+'), 'w');
//the $writer object open mode will be 'w'!!
// all rows: // all rows:
$rows = []; $rows = [];
@ -76,8 +77,6 @@ class CsvExporter extends BasicExporter implements ExporterInterface
private function tempFile() private function tempFile()
{ {
$fileName = $this->job->key . '-records.csv'; $this->fileName = $this->job->key . '-records.csv';
$this->fileName = storage_path('export') . DIRECTORY_SEPARATOR . $fileName;
$this->handler = fopen($this->fileName, 'w');
} }
} }

View File

@ -12,12 +12,12 @@ namespace FireflyIII\Export;
use Auth; use Auth;
use Config; use Config;
use ErrorException;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\ExportJob; use FireflyIII\Models\ExportJob;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Log; use Log;
use Storage;
use ZipArchive; use ZipArchive;
/** /**
@ -130,33 +130,30 @@ class Processor
public function createZipFile() public function createZipFile()
{ {
$zip = new ZipArchive; $zip = new ZipArchive;
$filename = storage_path('export') . DIRECTORY_SEPARATOR . $this->job->key . '.zip'; $file = $this->job->key . '.zip';
Log::debug('Will create zip file at ' . $filename); $fullPath = storage_path('export') . '/' . $file;
Log::debug('Will create zip file at ' . $fullPath);
if ($zip->open($filename, ZipArchive::CREATE) !== true) { if ($zip->open($fullPath, ZipArchive::CREATE) !== true) {
throw new FireflyException('Cannot store zip file.'); throw new FireflyException('Cannot store zip file.');
} }
// for each file in the collection, add it to the zip file. // for each file in the collection, add it to the zip file.
$search = storage_path('export') . DIRECTORY_SEPARATOR . $this->job->key . '-'; $disk = Storage::disk('export');
/** @var string $file */ foreach ($this->getFiles() as $entry) {
foreach ($this->getFiles() as $file) { // is part of this job?
Log::debug('Will add "' . $file . '" to zip file.'); $zipFileName = str_replace($this->job->key . '-', '', $entry);
$zipName = str_replace($search, '', $file); $result = $zip->addFromString($zipFileName, $disk->get($entry));
$result = $zip->addFile($file, $zipName);
if (!$result) { if (!$result) {
Log::error('Could not add "' . $file . '" into zip file as "' . $zipName . '".'); Log::error('Could not add "' . $entry . '" into zip file as "' . $zipFileName . '".');
} }
} }
$zip->close(); $zip->close();
// delete the files: // delete the files:
foreach ($this->getFiles() as $file) { foreach ($this->getFiles() as $file) {
Log::debug('Will now delete file "' . $file . '".'); Log::debug('Will now delete file "' . $file . '".');
try { $disk->delete($file);
unlink($file);
} catch (ErrorException $e) {
Log::error('Cannot unlink file "' . $file . '" because: ' . $e->getMessage());
}
} }
Log::debug('Done!'); Log::debug('Done!');
} }