Files
firefly-iii/app/Export/Collector/AttachmentCollector.php

150 lines
4.1 KiB
PHP
Raw Normal View History

2016-02-04 17:16:16 +01:00
<?php
/**
* AttachmentCollector.php
2018-05-11 10:08:34 +02:00
* Copyright (c) 2018 thegrumpydictator@gmail.com
2016-02-04 17:16:16 +01:00
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
2017-10-21 08:40:00 +02: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
2017-12-17 14:41:58 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2016-02-04 17:16:16 +01:00
*/
2018-05-11 10:08:34 +02:00
declare(strict_types=1);
2016-02-04 17:16:16 +01:00
namespace FireflyIII\Export\Collector;
2016-10-23 09:44:14 +02:00
use Carbon\Carbon;
2016-02-04 17:16:16 +01:00
use Crypt;
use FireflyIII\Models\Attachment;
use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface;
2016-02-07 09:11:46 +01:00
use Illuminate\Contracts\Encryption\DecryptException;
2016-02-23 06:54:51 +01:00
use Illuminate\Support\Collection;
2016-02-04 17:16:16 +01:00
use Log;
2016-02-23 06:54:51 +01:00
use Storage;
2016-02-04 17:16:16 +01:00
/**
2017-11-15 12:25:49 +01:00
* Class AttachmentCollector.
2016-02-04 17:16:16 +01:00
*/
class AttachmentCollector extends BasicCollector implements CollectorInterface
{
/** @var Carbon The end date of the range. */
2016-10-23 09:44:14 +02:00
private $end;
/** @var \Illuminate\Contracts\Filesystem\Filesystem File system */
2016-02-23 06:54:51 +01:00
private $exportDisk;
/** @var AttachmentRepositoryInterface Attachment repository */
private $repository;
/** @var Carbon Start date of range */
2016-10-23 09:44:14 +02:00
private $start;
/** @var \Illuminate\Contracts\Filesystem\Filesystem Disk with uploads on it */
2016-02-23 06:54:51 +01:00
private $uploadDisk;
2016-02-04 17:16:16 +01:00
/**
* AttachmentCollector constructor.
*/
2017-02-05 15:58:55 +01:00
public function __construct()
2016-02-04 17:16:16 +01:00
{
2018-01-17 09:32:18 +01:00
/** @var AttachmentRepositoryInterface repository */
2016-05-01 15:05:29 +02:00
$this->repository = app(AttachmentRepositoryInterface::class);
2016-02-23 06:54:51 +01:00
// make storage:
$this->uploadDisk = Storage::disk('upload');
$this->exportDisk = Storage::disk('export');
2017-02-05 15:58:55 +01:00
parent::__construct();
2016-02-04 17:16:16 +01:00
}
2016-02-05 15:41:40 +01:00
/**
* Run the routine.
*
2016-04-06 16:37:28 +02:00
* @return bool
2016-02-05 15:41:40 +01:00
*/
2016-04-06 16:37:28 +02:00
public function run(): bool
2016-02-04 17:16:16 +01:00
{
// grab all the users attachments:
2016-02-23 06:54:51 +01:00
$attachments = $this->getAttachments();
2016-02-04 17:16:16 +01:00
/** @var Attachment $attachment */
foreach ($attachments as $attachment) {
2016-02-23 06:54:51 +01:00
$this->exportAttachment($attachment);
2016-02-04 17:16:16 +01:00
}
2016-02-07 09:11:46 +01:00
2016-04-06 16:37:28 +02:00
return true;
2016-02-07 09:11:46 +01:00
}
/**
* Set the start and end date.
*
2016-10-23 09:44:14 +02:00
* @param Carbon $start
* @param Carbon $end
2016-02-07 09:11:46 +01:00
*/
2016-10-23 09:44:14 +02:00
public function setDates(Carbon $start, Carbon $end)
2016-02-07 09:11:46 +01:00
{
2016-10-23 09:44:14 +02:00
$this->start = $start;
$this->end = $end;
2016-02-04 17:16:16 +01:00
}
2016-02-23 06:54:51 +01:00
/** @noinspection MultipleReturnStatementsInspection */
2016-02-23 06:54:51 +01:00
/**
* Export attachments.
*
2016-02-23 06:54:51 +01:00
* @param Attachment $attachment
*
* @return bool
*/
private function exportAttachment(Attachment $attachment): bool
{
$file = $attachment->fileName();
$decrypted = false;
2016-02-23 06:54:51 +01:00
if ($this->uploadDisk->exists($file)) {
try {
2018-04-28 06:23:13 +02:00
$decrypted = Crypt::decrypt($this->uploadDisk->get($file));
2018-08-06 19:14:30 +02:00
} catch (DecryptException $e) {
2016-02-23 06:54:51 +01:00
Log::error('Catchable error: could not decrypt attachment #' . $attachment->id . ' because: ' . $e->getMessage());
2018-04-28 06:23:13 +02:00
return false;
2016-02-23 06:54:51 +01:00
}
}
if (false === $decrypted) {
return false;
}
2018-04-28 06:23:13 +02:00
$exportFile = $this->exportFileName($attachment);
$this->exportDisk->put($exportFile, $decrypted);
$this->getEntries()->push($exportFile);
2016-02-23 06:54:51 +01:00
return true;
}
/**
* Returns the new file name for the export file.
*
* @param $attachment
*
* @return string
*/
private function exportFileName($attachment): string
{
2018-04-02 14:42:07 +02:00
return sprintf('%s-Attachment nr. %s - %s', $this->job->key, (string)$attachment->id, $attachment->filename);
2016-02-23 06:54:51 +01:00
}
/**
* Get the attachments.
*
2016-02-23 06:54:51 +01:00
* @return Collection
*/
private function getAttachments(): Collection
{
$this->repository->setUser($this->user);
2016-02-23 06:54:51 +01:00
2018-04-02 14:42:07 +02:00
return $this->repository->getBetween($this->start, $this->end);
2016-02-23 06:54:51 +01:00
}
2016-02-10 16:01:18 +01:00
}