2015-07-19 02:37:28 -05:00
|
|
|
<?php
|
2016-05-20 05:41:23 -05:00
|
|
|
/**
|
|
|
|
* AttachmentRepository.php
|
2020-02-16 07:00:57 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2016-05-20 05:41:23 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* 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 01:40:00 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2017-10-21 01:40:00 -05:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 01:40:00 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05: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/>.
|
2016-05-20 05:41:23 -05:00
|
|
|
*/
|
2017-04-09 00:44:22 -05:00
|
|
|
declare(strict_types=1);
|
2015-07-19 02:37:28 -05:00
|
|
|
|
|
|
|
namespace FireflyIII\Repositories\Attachment;
|
|
|
|
|
2017-01-30 09:46:30 -06:00
|
|
|
use Crypt;
|
2018-05-26 01:04:50 -05:00
|
|
|
use Exception;
|
2018-06-23 23:51:22 -05:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
|
|
|
use FireflyIII\Factory\AttachmentFactory;
|
2016-05-02 13:49:19 -05:00
|
|
|
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
|
2015-07-19 02:37:28 -05:00
|
|
|
use FireflyIII\Models\Attachment;
|
2018-03-19 09:28:35 -05:00
|
|
|
use FireflyIII\Models\Note;
|
2016-03-03 01:38:24 -06:00
|
|
|
use FireflyIII\User;
|
2019-08-25 00:25:01 -05:00
|
|
|
use Illuminate\Contracts\Encryption\DecryptException;
|
2018-07-25 23:10:17 -05:00
|
|
|
use Illuminate\Contracts\Filesystem\FileNotFoundException;
|
2016-02-22 23:39:01 -06:00
|
|
|
use Illuminate\Support\Collection;
|
2018-10-13 01:56:26 -05:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2019-02-13 10:38:41 -06:00
|
|
|
use Log;
|
2015-07-19 02:37:28 -05:00
|
|
|
|
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Class AttachmentRepository.
|
2019-08-17 03:47:29 -05:00
|
|
|
*
|
2015-07-19 02:37:28 -05:00
|
|
|
*/
|
|
|
|
class AttachmentRepository implements AttachmentRepositoryInterface
|
|
|
|
{
|
2016-03-03 01:38:24 -06:00
|
|
|
/** @var User */
|
|
|
|
private $user;
|
2015-07-19 02:37:28 -05:00
|
|
|
|
2018-09-03 01:41:03 -05:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2018-12-15 00:59:02 -06:00
|
|
|
if ('testing' === config('app.env')) {
|
2019-06-07 11:20:15 -05:00
|
|
|
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
|
2018-09-03 01:41:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-26 08:51:07 -05:00
|
|
|
/**
|
|
|
|
* @param Attachment $attachment
|
|
|
|
*
|
|
|
|
* @return bool
|
2018-04-27 23:23:13 -05:00
|
|
|
* @throws \Exception
|
2015-07-26 08:51:07 -05:00
|
|
|
*/
|
2016-02-06 11:59:48 -06:00
|
|
|
public function destroy(Attachment $attachment): bool
|
2015-07-26 08:51:07 -05:00
|
|
|
{
|
2016-05-02 13:49:19 -05:00
|
|
|
/** @var AttachmentHelperInterface $helper */
|
|
|
|
$helper = app(AttachmentHelperInterface::class);
|
2015-07-26 08:51:07 -05:00
|
|
|
|
2018-10-13 01:56:26 -05:00
|
|
|
$path = $helper->getAttachmentLocation($attachment);
|
2018-05-26 01:04:50 -05:00
|
|
|
try {
|
2018-10-13 01:56:26 -05:00
|
|
|
Storage::disk('upload')->delete($path);
|
2018-05-26 01:04:50 -05:00
|
|
|
} catch (Exception $e) {
|
2018-07-22 11:50:27 -05:00
|
|
|
Log::error(sprintf('Could not delete file for attachment %d: %s', $attachment->id, $e->getMessage()));
|
2018-05-26 01:04:50 -05:00
|
|
|
}
|
2015-07-26 08:51:07 -05:00
|
|
|
$attachment->delete();
|
2016-02-22 23:39:01 -06:00
|
|
|
|
2016-02-06 11:59:48 -06:00
|
|
|
return true;
|
2015-07-26 08:51:07 -05:00
|
|
|
}
|
|
|
|
|
2016-12-25 05:23:36 -06:00
|
|
|
/**
|
|
|
|
* @param Attachment $attachment
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function exists(Attachment $attachment): bool
|
|
|
|
{
|
|
|
|
/** @var Storage $disk */
|
|
|
|
$disk = Storage::disk('upload');
|
|
|
|
|
|
|
|
return $disk->exists($attachment->fileName());
|
|
|
|
}
|
|
|
|
|
2016-02-22 23:39:01 -06:00
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function get(): Collection
|
|
|
|
{
|
2016-03-03 01:38:24 -06:00
|
|
|
return $this->user->attachments()->get();
|
2016-02-22 23:39:01 -06:00
|
|
|
}
|
|
|
|
|
2016-12-25 05:23:36 -06:00
|
|
|
/**
|
|
|
|
* @param Attachment $attachment
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getContent(Attachment $attachment): string
|
|
|
|
{
|
|
|
|
// create a disk.
|
2019-08-25 00:25:01 -05:00
|
|
|
$disk = Storage::disk('upload');
|
|
|
|
$file = $attachment->fileName();
|
|
|
|
$unencryptedContent = '';
|
2016-12-25 05:23:36 -06:00
|
|
|
|
|
|
|
if ($disk->exists($file)) {
|
2019-08-25 00:25:01 -05:00
|
|
|
$encryptedContent = '';
|
2018-07-25 23:10:17 -05:00
|
|
|
try {
|
2019-08-25 00:25:01 -05:00
|
|
|
$encryptedContent = $disk->get($file);
|
2018-07-25 23:10:17 -05:00
|
|
|
} catch (FileNotFoundException $e) {
|
2019-08-25 00:25:01 -05:00
|
|
|
Log::error($e->getMessage());
|
2018-07-25 23:10:17 -05:00
|
|
|
}
|
2016-12-25 05:23:36 -06:00
|
|
|
|
2019-08-25 00:25:01 -05:00
|
|
|
try {
|
|
|
|
$unencryptedContent = Crypt::decrypt($encryptedContent); // verified
|
|
|
|
} catch (DecryptException $e) {
|
2020-06-27 10:33:18 -05:00
|
|
|
$unencryptedContent = $encryptedContent;
|
2019-08-25 00:25:01 -05:00
|
|
|
}
|
2016-12-25 05:23:36 -06:00
|
|
|
}
|
|
|
|
|
2019-08-25 00:25:01 -05:00
|
|
|
return $unencryptedContent;
|
2016-12-25 05:23:36 -06:00
|
|
|
}
|
|
|
|
|
2018-03-19 09:28:35 -05:00
|
|
|
/**
|
|
|
|
* Get attachment note text or empty string.
|
|
|
|
*
|
|
|
|
* @param Attachment $attachment
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-03-25 02:01:43 -05:00
|
|
|
public function getNoteText(Attachment $attachment): ?string
|
2018-03-19 09:28:35 -05:00
|
|
|
{
|
|
|
|
$note = $attachment->notes()->first();
|
2018-04-02 08:10:40 -05:00
|
|
|
if (null !== $note) {
|
|
|
|
return (string)$note->text;
|
2018-03-19 09:28:35 -05:00
|
|
|
}
|
|
|
|
|
2018-03-25 02:01:43 -05:00
|
|
|
return null;
|
2018-03-19 09:28:35 -05:00
|
|
|
}
|
|
|
|
|
2017-01-30 09:46:30 -06:00
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
*/
|
2018-06-23 23:51:22 -05:00
|
|
|
public function setUser(User $user): void
|
2017-01-30 09:46:30 -06:00
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
|
|
|
|
2018-06-23 23:51:22 -05: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 02:37:28 -05:00
|
|
|
/**
|
|
|
|
* @param Attachment $attachment
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return Attachment
|
|
|
|
*/
|
2016-02-06 11:59:48 -06:00
|
|
|
public function update(Attachment $attachment, array $data): Attachment
|
2015-07-19 02:37:28 -05:00
|
|
|
{
|
2018-03-19 09:28:35 -05:00
|
|
|
$attachment->title = $data['title'];
|
2018-06-23 23:51:22 -05:00
|
|
|
|
|
|
|
// update filename, if present and different:
|
|
|
|
if (isset($data['filename']) && '' !== $data['filename'] && $data['filename'] !== $attachment->filename) {
|
|
|
|
$attachment->filename = $data['filename'];
|
|
|
|
}
|
2015-07-19 02:37:28 -05:00
|
|
|
$attachment->save();
|
2018-06-23 23:51:22 -05:00
|
|
|
$this->updateNote($attachment, $data['notes'] ?? '');
|
2015-07-19 02:37:28 -05:00
|
|
|
|
|
|
|
return $attachment;
|
|
|
|
}
|
2018-03-19 09:28:35 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Attachment $attachment
|
|
|
|
* @param string $note
|
|
|
|
*
|
|
|
|
* @return bool
|
2019-02-16 01:05:48 -06:00
|
|
|
* @throws Exception
|
2018-03-19 09:28:35 -05:00
|
|
|
*/
|
|
|
|
public function updateNote(Attachment $attachment, string $note): bool
|
|
|
|
{
|
2018-06-23 23:51:22 -05:00
|
|
|
if ('' === $note) {
|
2018-03-19 09:28:35 -05:00
|
|
|
$dbNote = $attachment->notes()->first();
|
|
|
|
if (null !== $dbNote) {
|
2019-02-13 10:38:41 -06:00
|
|
|
try {
|
|
|
|
$dbNote->delete();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
Log::debug(sprintf('Could not delete note: %s', $e->getMessage()));
|
|
|
|
}
|
2018-03-19 09:28:35 -05: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 02:38:44 -05:00
|
|
|
}
|