Files
firefly-iii/app/Repositories/Attachment/AttachmentRepository.php

230 lines
6.2 KiB
PHP
Raw Normal View History

2015-07-19 09:37:28 +02:00
<?php
/**
* AttachmentRepository.php
2020-02-16 14:00:57 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 08:40:00 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2015-07-19 09:37:28 +02:00
namespace FireflyIII\Repositories\Attachment;
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;
use FireflyIII\Models\Note;
2016-03-03 08:38:24 +01:00
use FireflyIII\User;
use Illuminate\Contracts\Encryption\DecryptException;
2018-07-26 06:10:17 +02:00
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Collection;
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.
2019-08-17 10:47:29 +02:00
*
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
2015-07-26 15:51:07 +02:00
/**
* @param Attachment $attachment
*
* @return bool
2021-03-12 06:20:01 +01: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
$path = $helper->getAttachmentLocation($attachment);
2018-05-26 08:04:50 +02:00
try {
Storage::disk('upload')->delete($path);
2021-04-07 07:28:43 +02:00
} catch (Exception $e) { // @phpstan-ignore-line
// @ignoreException
2018-05-26 08:04:50 +02:00
}
2015-07-26 15:51:07 +02:00
$attachment->delete();
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());
}
/**
* @return Collection
*/
public function get(): Collection
{
2016-03-03 08:38:24 +01:00
return $this->user->attachments()->get();
}
2016-12-25 12:23:36 +01:00
/**
* @param Attachment $attachment
*
* @return string
*/
public function getContent(Attachment $attachment): string
{
// create a disk.
$disk = Storage::disk('upload');
$file = $attachment->fileName();
$unencryptedContent = '';
2016-12-25 12:23:36 +01:00
if ($disk->exists($file)) {
$encryptedContent = '';
2018-07-26 06:10:17 +02:00
try {
$encryptedContent = $disk->get($file);
2018-07-26 06:10:17 +02:00
} catch (FileNotFoundException $e) {
Log::error($e->getMessage());
2018-07-26 06:10:17 +02:00
}
2016-12-25 12:23:36 +01:00
try {
$unencryptedContent = Crypt::decrypt($encryptedContent); // verified
} catch (DecryptException $e) {
2021-03-12 06:20:01 +01:00
$unencryptedContent = $encryptedContent;
}
2016-12-25 12:23:36 +01:00
}
return $unencryptedContent;
2016-12-25 12:23:36 +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
{
$note = $attachment->notes()->first();
2018-04-02 15:10:40 +02:00
if (null !== $note) {
return (string)$note->text;
}
2018-03-25 09:01:43 +02:00
return null;
}
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
{
2021-03-13 14:33:48 +01:00
if (array_key_exists('title', $data)) {
$attachment->title = $data['title'];
}
2018-06-24 06:51:22 +02:00
2021-03-13 14:33:48 +01:00
if (array_key_exists('filename', $data)) {
if ('' !== (string)$data['filename'] && $data['filename'] !== $attachment->filename) {
$attachment->filename = $data['filename'];
}
2018-06-24 06:51:22 +02:00
}
2021-03-20 07:21:13 +01:00
// update model (move attachment)
// should be validated already:
if (array_key_exists('attachable_type', $data) && array_key_exists('attachable_id', $data)) {
$attachment->attachable_id = (int)$data['attachable_id'];
$attachment->attachable_type = sprintf('FireflyIII\\Models\\%s', $data['attachable_type']);
}
2015-07-19 09:37:28 +02:00
$attachment->save();
2021-03-20 07:21:13 +01:00
$attachment->refresh();
2021-03-13 14:33:48 +01:00
if (array_key_exists('notes', $data)) {
$this->updateNote($attachment, (string)$data['notes']);
}
2015-07-19 09:37:28 +02:00
return $attachment;
}
/**
* @param Attachment $attachment
* @param string $note
*
* @return bool
2019-02-16 08:05:48 +01:00
* @throws Exception
*/
public function updateNote(Attachment $attachment, string $note): bool
{
2018-06-24 06:51:22 +02:00
if ('' === $note) {
$dbNote = $attachment->notes()->first();
if (null !== $dbNote) {
2019-02-13 17:38:41 +01:00
try {
$dbNote->delete();
2021-04-07 07:28:43 +02:00
} catch (Exception $e) { // @phpstan-ignore-line
// @ignoreException
2019-02-13 17:38:41 +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
}