2016-02-04 10:16:16 -06:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* AttachmentCollector.php
|
2017-10-21 01:40:00 -05:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-02-04 10:16:16 -06:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* This file is part of Firefly III.
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-02-04 10:16:16 -06:00
|
|
|
*/
|
2017-04-09 00:44:22 -05:00
|
|
|
declare(strict_types=1);
|
2016-05-20 05:27:31 -05:00
|
|
|
|
2016-02-04 10:16:16 -06:00
|
|
|
namespace FireflyIII\Export\Collector;
|
|
|
|
|
2016-10-23 02:44:14 -05:00
|
|
|
use Carbon\Carbon;
|
2016-02-04 10:16:16 -06:00
|
|
|
use Crypt;
|
|
|
|
use FireflyIII\Models\Attachment;
|
2016-02-22 23:39:01 -06:00
|
|
|
use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface;
|
2016-02-07 02:11:46 -06:00
|
|
|
use Illuminate\Contracts\Encryption\DecryptException;
|
2016-02-22 23:54:51 -06:00
|
|
|
use Illuminate\Support\Collection;
|
2016-02-04 10:16:16 -06:00
|
|
|
use Log;
|
2016-02-22 23:54:51 -06:00
|
|
|
use Storage;
|
2016-02-04 10:16:16 -06:00
|
|
|
|
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Class AttachmentCollector.
|
2016-02-04 10:16:16 -06:00
|
|
|
*/
|
|
|
|
class AttachmentCollector extends BasicCollector implements CollectorInterface
|
|
|
|
{
|
2017-11-15 05:25:49 -06:00
|
|
|
/** @var Carbon */
|
2016-10-23 02:44:14 -05:00
|
|
|
private $end;
|
2016-02-22 23:54:51 -06:00
|
|
|
/** @var \Illuminate\Contracts\Filesystem\Filesystem */
|
|
|
|
private $exportDisk;
|
2017-11-15 05:25:49 -06:00
|
|
|
/** @var AttachmentRepositoryInterface */
|
2016-02-22 23:39:01 -06:00
|
|
|
private $repository;
|
2017-11-15 05:25:49 -06:00
|
|
|
/** @var Carbon */
|
2016-10-23 02:44:14 -05:00
|
|
|
private $start;
|
2016-02-22 23:54:51 -06:00
|
|
|
/** @var \Illuminate\Contracts\Filesystem\Filesystem */
|
|
|
|
private $uploadDisk;
|
2016-02-22 23:39:01 -06:00
|
|
|
|
2016-02-04 10:16:16 -06:00
|
|
|
/**
|
|
|
|
* AttachmentCollector constructor.
|
|
|
|
*/
|
2017-02-05 08:58:55 -06:00
|
|
|
public function __construct()
|
2016-02-04 10:16:16 -06:00
|
|
|
{
|
2017-11-15 05:25:49 -06:00
|
|
|
// @var AttachmentRepositoryInterface repository
|
2016-05-01 08:05:29 -05:00
|
|
|
$this->repository = app(AttachmentRepositoryInterface::class);
|
2016-02-22 23:54:51 -06:00
|
|
|
// make storage:
|
|
|
|
$this->uploadDisk = Storage::disk('upload');
|
|
|
|
$this->exportDisk = Storage::disk('export');
|
2016-02-22 23:39:01 -06:00
|
|
|
|
2017-02-05 08:58:55 -06:00
|
|
|
parent::__construct();
|
2016-02-04 10:16:16 -06:00
|
|
|
}
|
|
|
|
|
2016-02-05 08:41:40 -06:00
|
|
|
/**
|
2016-04-06 09:37:28 -05:00
|
|
|
* @return bool
|
2016-02-05 08:41:40 -06:00
|
|
|
*/
|
2016-04-06 09:37:28 -05:00
|
|
|
public function run(): bool
|
2016-02-04 10:16:16 -06:00
|
|
|
{
|
|
|
|
// grab all the users attachments:
|
2016-02-22 23:54:51 -06:00
|
|
|
$attachments = $this->getAttachments();
|
2016-02-04 10:16:16 -06:00
|
|
|
|
|
|
|
/** @var Attachment $attachment */
|
|
|
|
foreach ($attachments as $attachment) {
|
2016-02-22 23:54:51 -06:00
|
|
|
$this->exportAttachment($attachment);
|
2016-02-04 10:16:16 -06:00
|
|
|
}
|
2016-02-07 02:11:46 -06:00
|
|
|
|
2016-04-06 09:37:28 -05:00
|
|
|
return true;
|
2016-02-07 02:11:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-10-23 02:44:14 -05:00
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
2016-02-07 02:11:46 -06:00
|
|
|
*/
|
2016-10-23 02:44:14 -05:00
|
|
|
public function setDates(Carbon $start, Carbon $end)
|
2016-02-07 02:11:46 -06:00
|
|
|
{
|
2016-10-23 02:44:14 -05:00
|
|
|
$this->start = $start;
|
|
|
|
$this->end = $end;
|
2016-02-04 10:16:16 -06:00
|
|
|
}
|
2016-02-22 23:54:51 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Attachment $attachment
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function exportAttachment(Attachment $attachment): bool
|
|
|
|
{
|
|
|
|
$file = $attachment->fileName();
|
|
|
|
if ($this->uploadDisk->exists($file)) {
|
|
|
|
try {
|
|
|
|
$decrypted = Crypt::decrypt($this->uploadDisk->get($file));
|
|
|
|
$exportFile = $this->exportFileName($attachment);
|
|
|
|
$this->exportDisk->put($exportFile, $decrypted);
|
2016-10-23 02:44:14 -05:00
|
|
|
$this->getEntries()->push($exportFile);
|
2016-02-22 23:54:51 -06:00
|
|
|
} catch (DecryptException $e) {
|
|
|
|
Log::error('Catchable error: could not decrypt attachment #' . $attachment->id . ' because: ' . $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the new file name for the export file.
|
|
|
|
*
|
|
|
|
* @param $attachment
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function exportFileName($attachment): string
|
|
|
|
{
|
|
|
|
return sprintf('%s-Attachment nr. %s - %s', $this->job->key, strval($attachment->id), $attachment->filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
private function getAttachments(): Collection
|
|
|
|
{
|
2017-09-14 14:17:19 -05:00
|
|
|
$this->repository->setUser($this->user);
|
2016-10-23 02:44:14 -05:00
|
|
|
$attachments = $this->repository->getBetween($this->start, $this->end);
|
2016-02-22 23:54:51 -06:00
|
|
|
|
|
|
|
return $attachments;
|
|
|
|
}
|
2016-02-10 09:01:18 -06:00
|
|
|
}
|