| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use DateTimeImmutable; |
| 6 |
use Exception; |
| 7 |
use FluentBoards\App\Models\Meta; |
| 8 |
use FluentBoards\App\Services\Constant; |
| 9 |
use FluentBoards\App\Services\Libs\FileSystem; |
| 10 |
use FluentBoardsPro\App\Models\Attachment; |
| 11 |
use function Sodium\add; |
| 12 |
|
| 13 |
class FileHandler |
| 14 |
{ |
| 15 |
// private function validateFile($file) |
| 16 |
// { |
| 17 |
// if (!$file) { |
| 18 |
// throw new Exception('File is empty.'); |
| 19 |
// } |
| 20 |
// if (!$this->isFileTypeSupported($file)) { |
| 21 |
// throw new Exception('File type not supported'); |
| 22 |
// } |
| 23 |
// if ($file['size'] > $this->getFileUploadLimit()) { |
| 24 |
// throw new Exception('File size is too large'); |
| 25 |
// } |
| 26 |
// } |
| 27 |
|
| 28 |
private function getFileUploadLimit() { |
| 29 |
// Logic for calculating file upload limit as in your original code |
| 30 |
return min( |
| 31 |
wp_convert_hr_to_bytes(ini_get('upload_max_filesize')), |
| 32 |
wp_convert_hr_to_bytes(ini_get('post_max_size')), |
| 33 |
wp_max_upload_size() |
| 34 |
); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Summary of deleteFileByUrl |
| 39 |
* @param mixed $file_url |
| 40 |
* @return bool |
| 41 |
*/ |
| 42 |
public function deleteFileByUrl($file_url) |
| 43 |
{ |
| 44 |
$exists = Attachment::where('full_url', $file_url)->exists(); |
| 45 |
if($exists) { |
| 46 |
return; |
| 47 |
} |
| 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 |
// Check if the file exists |
| 53 |
if (file_exists($file_path)) { |
| 54 |
// Delete the file |
| 55 |
$deleted = unlink($file_path); |
| 56 |
|
| 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); |
| 62 |
} |
| 63 |
|
| 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; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
/** |
| 73 |
* Summary of isFileTypeSupported checking file type it will allow only file which is readable by browser |
| 74 |
* @param mixed $file |
| 75 |
* TODO: Refactorable: This can be in a Helper class. and we may pass it to frontend via wp_localize_script appvars |
| 76 |
* so that we can check similarly for better experience. |
| 77 |
* @return bool |
| 78 |
*/ |
| 79 |
public function isFileTypeSupported($file) |
| 80 |
{ |
| 81 |
// Define supported file types that are generally allowed by user |
| 82 |
$allowedMimeTypes = get_allowed_mime_types(); |
| 83 |
|
| 84 |
// Check if the file type is supported |
| 85 |
return in_array(strtolower($file['type']), $allowedMimeTypes); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* @throws Exception |
| 90 |
*/ |
| 91 |
public function handleMediaFileUpload($data) |
| 92 |
{ |
| 93 |
// Check if the uploaded file is an image |
| 94 |
$wp_filetype = wp_check_filetype_and_ext( $_FILES['file']['tmp_name'], $_FILES['file']['name'] ); |
| 95 |
|
| 96 |
if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) { |
| 97 |
throw new Exception('The uploaded file is not a valid image. Please try again.'); |
| 98 |
} |
| 99 |
require_once(ABSPATH . 'wp-admin/includes/image.php'); |
| 100 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 101 |
require_once( ABSPATH . 'wp-admin/includes/media.php' ); |
| 102 |
$attachment_id = media_handle_upload( 'file', 0, [] ); |
| 103 |
|
| 104 |
$attachment = wp_prepare_attachment_for_js( $attachment_id); |
| 105 |
if(!$attachment) { |
| 106 |
throw new Exception('The uploaded file is not a valid image. Please try again.'); |
| 107 |
} else { |
| 108 |
return $attachment; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* This function will delete meta where default board default image is used. |
| 114 |
* @param $id |
| 115 |
* @return void |
| 116 |
*/ |
| 117 |
public function mediaFileDeleted($id) |
| 118 |
{ |
| 119 |
$boardImage = Meta::where('object_id', $id)->where('object_type', Constant::BOARD_DEFAULT_IMAGE)->first(); |
| 120 |
if($boardImage) { |
| 121 |
$boardImage->delete(); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|