mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2024-11-27 11:20:39 -06:00
Use export disk for zip file.
This commit is contained in:
parent
6b00f5a97d
commit
08b4c9ea5c
@ -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';
|
||||
}
|
||||
}
|
||||
|
@ -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!');
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user