| 
									
										
										
										
											2015-07-19 09:37:28 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * AttachmentRepository.php | 
					
						
							| 
									
										
										
										
											2020-02-16 14:00:57 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2016-10-05 06:52:15 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02: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 08:40:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +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 | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * GNU Affero General Public License for more details. | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +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/>. | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2025-02-23 12:28:27 +01:00
										 |  |  | use FireflyIII\Support\Repositories\UserGroup\UserGroupInterface; | 
					
						
							|  |  |  | use FireflyIII\Support\Repositories\UserGroup\UserGroupTrait; | 
					
						
							| 
									
										
										
										
											2019-08-25 07:25:01 +02:00
										 |  |  | use Illuminate\Contracts\Encryption\DecryptException; | 
					
						
							| 
									
										
										
										
											2016-02-23 06:39:01 +01:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2025-02-23 12:47:04 +01:00
										 |  |  | use Illuminate\Support\Facades\Crypt; | 
					
						
							| 
									
										
										
										
											2018-10-13 09:56:26 +03:00
										 |  |  | use Illuminate\Support\Facades\Storage; | 
					
						
							| 
									
										
										
										
											2022-12-30 09:28:03 +01:00
										 |  |  | use League\Flysystem\UnableToDeleteFile; | 
					
						
							| 
									
										
										
										
											2025-05-27 16:57:36 +02:00
										 |  |  | use Exception; | 
					
						
							|  |  |  | use LogicException; | 
					
						
							| 
									
										
										
										
											2015-07-19 09:37:28 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |  * Class AttachmentRepository. | 
					
						
							| 
									
										
										
										
											2015-07-19 09:37:28 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2025-02-23 12:28:27 +01:00
										 |  |  | class AttachmentRepository implements AttachmentRepositoryInterface, UserGroupInterface | 
					
						
							| 
									
										
										
										
											2015-07-19 09:37:28 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-02-23 12:28:27 +01:00
										 |  |  |     use UserGroupTrait; | 
					
						
							| 
									
										
										
										
											2015-07-19 09:37:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-26 15:51:07 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2025-05-27 16:57:36 +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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-14 17:45:16 +01:00
										 |  |  |         $path   = $helper->getAttachmentLocation($attachment); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-26 08:04:50 +02:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2018-10-13 09:56:26 +03:00
										 |  |  |             Storage::disk('upload')->delete($path); | 
					
						
							| 
									
										
										
										
											2025-05-04 13:47:00 +02:00
										 |  |  |         } catch (UnableToDeleteFile) { | 
					
						
							| 
									
										
										
										
											2021-04-07 07:28:43 +02:00
										 |  |  |             // @ignoreException
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     public function getContent(Attachment $attachment): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // create a disk.
 | 
					
						
							| 
									
										
										
										
											2019-08-25 07:25:01 +02:00
										 |  |  |         $disk               = Storage::disk('upload'); | 
					
						
							|  |  |  |         $file               = $attachment->fileName(); | 
					
						
							|  |  |  |         $unencryptedContent = ''; | 
					
						
							| 
									
										
										
										
											2016-12-25 12:23:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if ($disk->exists($file)) { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             $encryptedContent = (string) $disk->get($file); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-25 07:25:01 +02:00
										 |  |  |             try { | 
					
						
							| 
									
										
										
										
											2025-02-23 12:47:04 +01:00
										 |  |  |                 $unencryptedContent = Crypt::decrypt($encryptedContent); // verified
 | 
					
						
							| 
									
										
										
										
											2019-08-25 07:25:01 +02:00
										 |  |  |             } catch (DecryptException $e) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |                 app('log')->debug(sprintf('Could not decrypt attachment #%d but this is fine: %s', $attachment->id, $e->getMessage())); | 
					
						
							| 
									
										
										
										
											2021-03-12 06:20:01 +01:00
										 |  |  |                 $unencryptedContent = $encryptedContent; | 
					
						
							| 
									
										
										
										
											2019-08-25 07:25:01 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2016-12-25 12:23:36 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-25 07:25:01 +02:00
										 |  |  |         return $unencryptedContent; | 
					
						
							| 
									
										
										
										
											2016-12-25 12:23:36 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     public function exists(Attachment $attachment): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var Storage $disk */ | 
					
						
							|  |  |  |         $disk = Storage::disk('upload'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $disk->exists($attachment->fileName()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function get(): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->user->attachments()->get(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-19 15:28:35 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get attachment note text or empty 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) { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             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
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-06-24 06:51:22 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function store(array $data): Attachment | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var AttachmentFactory $factory */ | 
					
						
							|  |  |  |         $factory = app(AttachmentFactory::class); | 
					
						
							|  |  |  |         $factory->setUser($this->user); | 
					
						
							| 
									
										
										
										
											2025-03-14 17:45:16 +01:00
										 |  |  |         $result  = $factory->create($data); | 
					
						
							| 
									
										
										
										
											2018-06-24 06:51:22 +02:00
										 |  |  |         if (null === $result) { | 
					
						
							|  |  |  |             throw new FireflyException('Could not store attachment.'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         if (array_key_exists('filename', $data) && '' !== (string) $data['filename'] && $data['filename'] !== $attachment->filename) { | 
					
						
							| 
									
										
										
										
											2021-05-24 08:06:56 +02:00
										 |  |  |             $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)) { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             $attachment->attachable_id   = (int) $data['attachable_id']; | 
					
						
							| 
									
										
										
										
											2024-06-15 16:16:40 +02:00
										 |  |  |             $attachment->attachable_type = sprintf('FireflyIII\Models\%s', $data['attachable_type']); | 
					
						
							| 
									
										
										
										
											2021-03-20 07:21:13 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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)) { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             $this->updateNote($attachment, (string) $data['notes']); | 
					
						
							| 
									
										
										
										
											2021-03-13 14:33:48 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-19 09:37:28 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $attachment; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-19 15:28:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     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(); | 
					
						
							| 
									
										
										
										
											2025-05-27 16:57:36 +02:00
										 |  |  |                 } catch (LogicException $e) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |                     app('log')->error($e->getMessage()); | 
					
						
							| 
									
										
										
										
											2019-02-13 17:38:41 +01:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2018-03-19 15:28:35 +01:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-03-14 17:45:16 +01:00
										 |  |  |         $dbNote       = $attachment->notes()->first(); | 
					
						
							| 
									
										
										
										
											2018-03-19 15:28:35 +01:00
										 |  |  |         if (null === $dbNote) { | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:28 +01:00
										 |  |  |             $dbNote = new Note(); | 
					
						
							| 
									
										
										
										
											2018-03-19 15:28:35 +01:00
										 |  |  |             $dbNote->noteable()->associate($attachment); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $dbNote->text = trim($note); | 
					
						
							|  |  |  |         $dbNote->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-19 09:38:44 +02:00
										 |  |  | } |