| @@ -3,10 +3,8 @@ | ||
| 3 | 3 | namespace FluentBoards\App\Hooks\Handlers; |
| 4 | 4 | |
| 5 | 5 | use DateTimeImmutable; |
| 6 | 6 | use Exception; |
| 7 | -use FluentBoards\App\Models\Meta; | |
| 8 | -use FluentBoards\App\Services\Constant; | |
| 9 | 7 | use FluentBoards\App\Services\Libs\FileSystem; |
| 10 | 8 | use FluentBoards\App\Models\Attachment; |
| 11 | 9 | use function Sodium\add; |
| 12 | 10 | |
| @@ -34,39 +32,48 @@ | ||
| 34 | 32 | ); |
| 35 | 33 | } |
| 36 | 34 | |
| 37 | 35 | /** |
| 38 | - * Summary of deleteFileByUrl | |
| 39 | - * @param mixed $file_url | |
| 36 | + * Delete a local attachment only when its stored path belongs to Fluent Boards. | |
| 37 | + * | |
| 38 | + * @param mixed $attachment | |
| 39 | + * @param int|null $boardId | |
| 40 | 40 | * @return bool |
| 41 | 41 | */ |
| 42 | - public function deleteFileByUrl($file_url) | |
| 42 | + public function deleteAttachmentFile($attachment, $boardId = null) | |
| 43 | 43 | { |
| 44 | - $exists = Attachment::where('full_url', $file_url)->exists(); | |
| 45 | - if($exists) { | |
| 46 | - return; | |
| 44 | + if ( | |
| 45 | + !$attachment || | |
| 46 | + $attachment->attachment_type === 'url' || | |
| 47 | + (!empty($attachment->driver) && $attachment->driver !== 'local') || | |
| 48 | + empty($attachment->file_path) | |
| 49 | + ) { | |
| 50 | + return false; | |
| 47 | 51 | } |
| 48 | - // Convert the URL to the local file path | |
| 49 | - $upload_dir = wp_upload_dir(); | |
| 50 | - $file_path = str_replace($upload_dir['baseurl'], $upload_dir['basedir'], $file_url); | |
| 51 | 52 | |
| 52 | - // Check if the file exists | |
| 53 | - if (file_exists($file_path)) { | |
| 54 | - // Delete the file | |
| 55 | - $deleted = wp_delete_file($file_path); | |
| 53 | + $storedFilename = rawurldecode((string) $attachment->file_path); | |
| 54 | + $isBareFilename = $storedFilename !== '' | |
| 55 | + && strpos($storedFilename, '/') === false | |
| 56 | + && strpos($storedFilename, '\\') === false; | |
| 57 | + $filePath = FileSystem::resolveLocalAttachmentPath($attachment->file_path, $boardId); | |
| 56 | 58 | |
| 57 | - // Optionally, you can also remove the file from the media library | |
| 58 | - // Note: This won't delete the file physically, but it will remove it from the media library | |
| 59 | - $attachment_id = attachment_url_to_postid($file_url); | |
| 60 | - if ($attachment_id) { | |
| 61 | - wp_delete_attachment($attachment_id, true); | |
| 59 | + if (!$filePath) { | |
| 60 | + return false; | |
| 61 | + } | |
| 62 | + | |
| 63 | + // Legacy filenames need the board-qualified URL to distinguish same-named files across boards. | |
| 64 | + if ($isBareFilename) { | |
| 65 | + if ( | |
| 66 | + empty($attachment->full_url) || | |
| 67 | + Attachment::where('full_url', $attachment->full_url)->exists() | |
| 68 | + ) { | |
| 69 | + return false; | |
| 62 | 70 | } |
| 71 | + } elseif (Attachment::where('file_path', $attachment->file_path)->exists()) { | |
| 72 | + return false; | |
| 73 | + } | |
| 63 | 74 | |
| 64 | - // Return true if the file was successfully deleted | |
| 65 | - return $deleted; | |
| 66 | - } | |
| 67 | - // Return false if the file does not exist | |
| 68 | - return false; | |
| 75 | + return (bool) wp_delete_file($filePath); | |
| 69 | 76 | } |
| 70 | 77 | |
| 71 | 78 | |
| 72 | 79 | /** |
| @@ -126,17 +133,5 @@ | ||
| 126 | 133 | return $attachment; |
| 127 | 134 | } |
| 128 | 135 | } |
| 129 | 136 | |
| 130 | - /** | |
| 131 | - * This function will delete meta where default board default image is used. | |
| 132 | - * @param $id | |
| 133 | - * @return void | |
| 134 | - */ | |
| 135 | - public function mediaFileDeleted($id) | |
| 136 | - { | |
| 137 | - $boardImage = Meta::where('object_id', $id)->where('object_type', Constant::BOARD_DEFAULT_IMAGE)->first(); | |
| 138 | - if($boardImage) { | |
| 139 | - $boardImage->delete(); | |
| 140 | - } | |
| 141 | - } | |
| 142 | 137 | } |