2015-07-19 09:37:28 +02:00
|
|
|
<?php
|
2016-05-20 12:41:23 +02:00
|
|
|
/**
|
|
|
|
* AttachmentRepository.php
|
2017-10-21 08:40:00 +02:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-05-20 12:41:23 +02:00
|
|
|
*
|
2017-10-21 08:40:00 +02:00
|
|
|
* This file is part of Firefly III.
|
2016-10-05 06:52:15 +02:00
|
|
|
*
|
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:44:05 +01:00
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-05-20 12:41:23 +02:00
|
|
|
*/
|
2017-04-09 07:44:22 +02:00
|
|
|
declare(strict_types=1);
|
2015-07-19 09:37:28 +02:00
|
|
|
|
|
|
|
namespace FireflyIII\Repositories\Attachment;
|
|
|
|
|
2016-10-23 09:44:14 +02:00
|
|
|
use Carbon\Carbon;
|
2017-01-30 16:46:30 +01:00
|
|
|
use Crypt;
|
2018-05-26 08:04:50 +02:00
|
|
|
use Exception;
|
2018-06-24 06:51:22 +02:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
|
|
|
use FireflyIII\Factory\AttachmentFactory;
|
2016-05-02 20:49:19 +02:00
|
|
|
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
|
2015-07-19 09:37:28 +02:00
|
|
|
use FireflyIII\Models\Attachment;
|
2018-03-19 15:28:35 +01:00
|
|
|
use FireflyIII\Models\Note;
|
2016-03-03 08:38:24 +01:00
|
|
|
use FireflyIII\User;
|
2018-07-26 06:10:17 +02:00
|
|
|
use Illuminate\Contracts\Filesystem\FileNotFoundException;
|
2016-02-23 06:39:01 +01:00
|
|
|
use Illuminate\Support\Collection;
|
2018-10-13 09:56:26 +03:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2019-02-13 17:38:41 +01:00
|
|
|
use Log;
|
2015-07-19 09:37:28 +02:00
|
|
|
|
|
|
|
/**
|
2017-11-15 12:25:49 +01:00
|
|
|
* Class AttachmentRepository.
|
2018-07-22 18:50:27 +02:00
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
2015-07-19 09:37:28 +02:00
|
|
|
*/
|
|
|
|
class AttachmentRepository implements AttachmentRepositoryInterface
|
|
|
|
{
|
2016-03-03 08:38:24 +01:00
|
|
|
/** @var User */
|
|
|
|
private $user;
|
2015-07-19 09:37:28 +02:00
|
|
|
|
2018-09-03 08:41:03 +02:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2018-12-15 07:59:02 +01:00
|
|
|
if ('testing' === config('app.env')) {
|
2018-09-03 08:41:03 +02:00
|
|
|
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-26 15:51:07 +02:00
|
|
|
/**
|
|
|
|
* @param Attachment $attachment
|
|
|
|
*
|
|
|
|
* @return bool
|
2018-04-28 06:23:13 +02:00
|
|
|
* @throws \Exception
|
2015-07-26 15:51:07 +02:00
|
|
|
*/
|
2016-02-06 18:59:48 +01:00
|
|
|
public function destroy(Attachment $attachment): bool
|
2015-07-26 15:51:07 +02:00
|
|
|
{
|
2016-05-02 20:49:19 +02:00
|
|
|
/** @var AttachmentHelperInterface $helper */
|
|
|
|
$helper = app(AttachmentHelperInterface::class);
|
2015-07-26 15:51:07 +02:00
|
|
|
|
2018-10-13 09:56:26 +03:00
|
|
|
$path = $helper->getAttachmentLocation($attachment);
|
2018-05-26 08:04:50 +02:00
|
|
|
try {
|
2018-10-13 09:56:26 +03:00
|
|
|
Storage::disk('upload')->delete($path);
|
2018-05-26 08:04:50 +02:00
|
|
|
} catch (Exception $e) {
|
2018-07-22 18:50:27 +02:00
|
|
|
Log::error(sprintf('Could not delete file for attachment %d: %s', $attachment->id, $e->getMessage()));
|
2018-05-26 08:04:50 +02:00
|
|
|
}
|
2015-07-26 15:51:07 +02:00
|
|
|
$attachment->delete();
|
2016-02-23 06:39:01 +01:00
|
|
|
|
2016-02-06 18:59:48 +01:00
|
|
|
return true;
|
2015-07-26 15:51:07 +02:00
|
|
|
}
|
|
|
|
|
2016-12-25 12:23:36 +01:00
|
|
|
/**
|
|
|
|
* @param Attachment $attachment
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function exists(Attachment $attachment): bool
|
|
|
|
{
|
|
|
|
/** @var Storage $disk */
|
|
|
|
$disk = Storage::disk('upload');
|
|
|
|
|
|
|
|
return $disk->exists($attachment->fileName());
|
|
|
|
}
|
|
|
|
|
2017-07-30 08:22:39 +02:00
|
|
|
/**
|
2018-07-23 21:49:15 +02:00
|
|
|
* @param int $attachmentId
|
2018-07-26 06:10:17 +02:00
|
|
|
*
|
2018-07-25 06:45:25 +02:00
|
|
|
* @return Attachment|null
|
2017-07-30 08:22:39 +02:00
|
|
|
*/
|
2018-07-25 06:45:25 +02:00
|
|
|
public function findWithoutUser(int $attachmentId): ?Attachment
|
2017-07-30 08:22:39 +02:00
|
|
|
{
|
2018-07-22 18:50:27 +02:00
|
|
|
|
2018-07-25 06:45:25 +02:00
|
|
|
return Attachment::find($attachmentId);
|
2017-07-30 08:22:39 +02:00
|
|
|
}
|
|
|
|
|
2016-02-23 06:39:01 +01:00
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function get(): Collection
|
|
|
|
{
|
2016-03-03 08:38:24 +01:00
|
|
|
return $this->user->attachments()->get();
|
2016-02-23 06:39:01 +01:00
|
|
|
}
|
|
|
|
|
2016-10-23 09:44:14 +02:00
|
|
|
/**
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getBetween(Carbon $start, Carbon $end): Collection
|
|
|
|
{
|
|
|
|
$query = $this->user
|
|
|
|
->attachments()
|
|
|
|
->leftJoin('transaction_journals', 'attachments.attachable_id', '=', 'transaction_journals.id')
|
|
|
|
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
|
|
|
|
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
|
|
|
|
->get(['attachments.*']);
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
2016-12-25 12:23:36 +01:00
|
|
|
/**
|
|
|
|
* @param Attachment $attachment
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getContent(Attachment $attachment): string
|
|
|
|
{
|
|
|
|
// create a disk.
|
2017-06-12 17:07:29 +02:00
|
|
|
$disk = Storage::disk('upload');
|
|
|
|
$file = $attachment->fileName();
|
|
|
|
$content = '';
|
2016-12-25 12:23:36 +01:00
|
|
|
|
|
|
|
if ($disk->exists($file)) {
|
2018-07-26 06:10:17 +02:00
|
|
|
try {
|
|
|
|
$content = Crypt::decrypt($disk->get($file));
|
|
|
|
} catch (FileNotFoundException $e) {
|
|
|
|
Log::debug(sprintf('File not found: %e', $e->getMessage()));
|
|
|
|
$content = false;
|
|
|
|
}
|
2017-06-12 17:07:29 +02:00
|
|
|
}
|
2018-04-28 06:23:13 +02:00
|
|
|
if (\is_bool($content)) {
|
2017-06-12 17:07:29 +02:00
|
|
|
Log::error(sprintf('Attachment #%d may be corrupted: the content could not be decrypted.', $attachment->id));
|
2016-12-25 12:23:36 +01:00
|
|
|
|
2017-06-12 17:07:29 +02:00
|
|
|
return '';
|
2016-12-25 12:23:36 +01:00
|
|
|
}
|
|
|
|
|
2017-06-12 17:07:29 +02:00
|
|
|
return $content;
|
2016-12-25 12:23:36 +01:00
|
|
|
}
|
|
|
|
|
2018-03-19 15:28:35 +01:00
|
|
|
/**
|
|
|
|
* Get attachment note text or empty string.
|
|
|
|
*
|
|
|
|
* @param Attachment $attachment
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-03-25 09:01:43 +02:00
|
|
|
public function getNoteText(Attachment $attachment): ?string
|
2018-03-19 15:28:35 +01:00
|
|
|
{
|
|
|
|
$note = $attachment->notes()->first();
|
2018-04-02 15:10:40 +02:00
|
|
|
if (null !== $note) {
|
|
|
|
return (string)$note->text;
|
2018-03-19 15:28:35 +01:00
|
|
|
}
|
|
|
|
|
2018-03-25 09:01:43 +02:00
|
|
|
return null;
|
2018-03-19 15:28:35 +01:00
|
|
|
}
|
|
|
|
|
2017-01-30 16:46:30 +01:00
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
*/
|
2018-06-24 06:51:22 +02:00
|
|
|
public function setUser(User $user): void
|
2017-01-30 16:46:30 +01:00
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
|
|
|
|
2018-06-24 06:51:22 +02:00
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return Attachment
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function store(array $data): Attachment
|
|
|
|
{
|
|
|
|
/** @var AttachmentFactory $factory */
|
|
|
|
$factory = app(AttachmentFactory::class);
|
|
|
|
$factory->setUser($this->user);
|
|
|
|
$result = $factory->create($data);
|
|
|
|
if (null === $result) {
|
|
|
|
throw new FireflyException('Could not store attachment.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2015-07-19 09:37:28 +02:00
|
|
|
/**
|
|
|
|
* @param Attachment $attachment
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return Attachment
|
|
|
|
*/
|
2016-02-06 18:59:48 +01:00
|
|
|
public function update(Attachment $attachment, array $data): Attachment
|
2015-07-19 09:37:28 +02:00
|
|
|
{
|
2018-03-19 15:28:35 +01:00
|
|
|
$attachment->title = $data['title'];
|
2018-06-24 06:51:22 +02:00
|
|
|
|
|
|
|
// update filename, if present and different:
|
|
|
|
if (isset($data['filename']) && '' !== $data['filename'] && $data['filename'] !== $attachment->filename) {
|
|
|
|
$attachment->filename = $data['filename'];
|
|
|
|
}
|
2015-07-19 09:37:28 +02:00
|
|
|
$attachment->save();
|
2018-06-24 06:51:22 +02:00
|
|
|
$this->updateNote($attachment, $data['notes'] ?? '');
|
2015-07-19 09:37:28 +02:00
|
|
|
|
|
|
|
return $attachment;
|
|
|
|
}
|
2018-03-19 15:28:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Attachment $attachment
|
|
|
|
* @param string $note
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function updateNote(Attachment $attachment, string $note): bool
|
|
|
|
{
|
2018-06-24 06:51:22 +02:00
|
|
|
if ('' === $note) {
|
2018-03-19 15:28:35 +01:00
|
|
|
$dbNote = $attachment->notes()->first();
|
|
|
|
if (null !== $dbNote) {
|
2019-02-13 17:38:41 +01:00
|
|
|
try {
|
|
|
|
$dbNote->delete();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
Log::debug(sprintf('Could not delete note: %s', $e->getMessage()));
|
|
|
|
}
|
2018-03-19 15:28:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$dbNote = $attachment->notes()->first();
|
|
|
|
if (null === $dbNote) {
|
|
|
|
$dbNote = new Note;
|
|
|
|
$dbNote->noteable()->associate($attachment);
|
|
|
|
}
|
|
|
|
$dbNote->text = trim($note);
|
|
|
|
$dbNote->save();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2015-07-19 09:38:44 +02:00
|
|
|
}
|