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

View File

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