New export functionality.

This commit is contained in:
James Cole
2016-02-04 17:16:16 +01:00
parent d7a66f6782
commit 86c22c9fdd
24 changed files with 1311 additions and 13 deletions

View File

@@ -0,0 +1,54 @@
<?php
/**
* AttachmentCollector.php
* Copyright (C) 2016 Sander Dorigo
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace FireflyIII\Export\Collector;
use Auth;
use Crypt;
use FireflyIII\Models\Attachment;
use FireflyIII\Models\ExportJob;
use Log;
/**
* Class AttachmentCollector
*
* @package FireflyIII\Export\Collector
*/
class AttachmentCollector extends BasicCollector implements CollectorInterface
{
/**
* AttachmentCollector constructor.
*
* @param ExportJob $job
*/
public function __construct(ExportJob $job)
{
parent::__construct($job);
}
public function run()
{
// grab all the users attachments:
$attachments = Auth::user()->attachments()->get();
Log::debug('Found ' . $attachments->count() . ' attachments.');
/** @var Attachment $attachment */
foreach ($attachments as $attachment) {
$originalFile = storage_path('upload') . DIRECTORY_SEPARATOR . 'at-' . $attachment->id . '.data';
if (file_exists($originalFile)) {
Log::debug('Stored 1 attachment');
$decrypted = Crypt::decrypt(file_get_contents($originalFile));
$newFile = storage_path('export') . DIRECTORY_SEPARATOR . $this->job->key . '-Attachment nr. ' . $attachment->id . ' - ' . $attachment->filename;
file_put_contents($newFile, $decrypted);
$this->getFiles()->push($newFile);
}
}
}
}

View File

@@ -0,0 +1,55 @@
<?php
/**
* BasicCollector.php
* Copyright (C) 2016 Sander Dorigo
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace FireflyIII\Export\Collector;
use FireflyIII\Models\ExportJob;
use Illuminate\Support\Collection;
/**
* Class BasicCollector
*
* @package FireflyIII\Export\Collector
*/
class BasicCollector
{
/** @var Collection */
private $files;
/** @var ExportJob */
protected $job;
/**
* BasicCollector constructor.
*/
public function __construct(ExportJob $job)
{
$this->files = new Collection;
$this->job = $job;
}
/**
* @return Collection
*/
public function getFiles()
{
return $this->files;
}
/**
* @param Collection $files
*/
public function setFiles($files)
{
$this->files = $files;
}
}

View File

@@ -0,0 +1,21 @@
<?php
/**
* CollectorInterface.php
* Copyright (C) 2016 Sander Dorigo
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace FireflyIII\Export\Collector;
/**
* Interface CollectorInterface
*
* @package FireflyIII\Export\Collector
*/
interface CollectorInterface
{
public function run();
}

View File

@@ -0,0 +1,60 @@
<?php
/**
* UploadCollector.php
* Copyright (C) 2016 Sander Dorigo
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace FireflyIII\Export\Collector;
use Auth;
use Crypt;
use FireflyIII\Models\ExportJob;
/**
* Class UploadCollector
*
* @package FireflyIII\Export\Collector
*/
class UploadCollector extends BasicCollector implements CollectorInterface
{
/**
* AttachmentCollector constructor.
*
* @param ExportJob $job
*/
public function __construct(ExportJob $job)
{
parent::__construct($job);
}
public function run()
{
// grab upload directory.
$path = storage_path('upload');
$files = scandir($path);
// only allow old uploads for this user:
$expected = 'csv-upload-' . Auth::user()->id . '-';
$len = strlen($expected);
foreach ($files as $entry) {
if (substr($entry, 0, $len) === $expected) {
// this is an original upload.
$parts = explode('-', str_replace(['.csv.encrypted', $expected], '', $entry));
$originalUpload = intval($parts[1]);
$date = date('Y-m-d \a\t H-i-s', $originalUpload);
$newFileName = 'Old CSV import dated ' . $date . '.csv';
$content = Crypt::decrypt(file_get_contents($path . DIRECTORY_SEPARATOR . $entry));
$fullPath = storage_path('export') . DIRECTORY_SEPARATOR . $this->job->key . '-' . $newFileName;
// write to file:
file_put_contents($fullPath, $content);
// add entry to set:
$this->getFiles()->push($fullPath);
}
}
}
}