. */ declare(strict_types=1); namespace FireflyIII\Repositories\Attachment; use Crypt; use Exception; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Factory\AttachmentFactory; use FireflyIII\Helpers\Attachments\AttachmentHelperInterface; use FireflyIII\Models\Attachment; use FireflyIII\Models\Note; use FireflyIII\User; use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Contracts\Encryption\DecryptException; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Storage; use League\Flysystem\UnableToDeleteFile; use LogicException; /** * Class AttachmentRepository. * */ class AttachmentRepository implements AttachmentRepositoryInterface { /** @var User */ private $user; /** * @param Attachment $attachment * * @return bool * @throws Exception */ public function destroy(Attachment $attachment): bool { /** @var AttachmentHelperInterface $helper */ $helper = app(AttachmentHelperInterface::class); $path = $helper->getAttachmentLocation($attachment); try { Storage::disk('upload')->delete($path); } catch (UnableToDeleteFile $e) { // @ignoreException } $attachment->delete(); return true; } /** * @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 { return $this->user->attachments()->get(); } /** * @param Attachment $attachment * * @return string */ public function getContent(Attachment $attachment): string { // create a disk. $disk = Storage::disk('upload'); $file = $attachment->fileName(); $unencryptedContent = ''; if ($disk->exists($file)) { $encryptedContent = (string)$disk->get($file); try { $unencryptedContent = Crypt::decrypt($encryptedContent); // verified } catch (DecryptException $e) { Log::debug(sprintf('Could not decrypt attachment #%d but this is fine: %s', $attachment->id, $e->getMessage())); $unencryptedContent = $encryptedContent; } } return $unencryptedContent; } /** * Get attachment note text or empty string. * * @param Attachment $attachment * * @return string|null */ public function getNoteText(Attachment $attachment): ?string { $note = $attachment->notes()->first(); if (null !== $note) { return (string)$note->text; } return null; } /** * @param User|Authenticatable|null $user */ public function setUser(User|Authenticatable|null $user): void { if (null !== $user) { $this->user = $user; } } /** * @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; } /** * @param Attachment $attachment * @param array $data * * @return Attachment */ public function update(Attachment $attachment, array $data): Attachment { if (array_key_exists('title', $data)) { $attachment->title = $data['title']; } if (array_key_exists('filename', $data) && '' !== (string)$data['filename'] && $data['filename'] !== $attachment->filename) { $attachment->filename = $data['filename']; } // 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']); } $attachment->save(); $attachment->refresh(); if (array_key_exists('notes', $data)) { $this->updateNote($attachment, (string)$data['notes']); } return $attachment; } /** * @param Attachment $attachment * @param string $note * * @return bool */ public function updateNote(Attachment $attachment, string $note): bool { if ('' === $note) { $dbNote = $attachment->notes()->first(); if (null !== $dbNote) { try { $dbNote->delete(); } catch (LogicException $e) { Log::error($e->getMessage()); } } return true; } $dbNote = $attachment->notes()->first(); if (null === $dbNote) { $dbNote = new Note(); $dbNote->noteable()->associate($attachment); } $dbNote->text = trim($note); $dbNote->save(); return true; } }