| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Services; |
| 4 |
|
| 5 |
|
| 6 |
use FluentBoards\App\Services\Libs\FileSystem; |
| 7 |
use FluentBoards\Framework\Support\Arr; |
| 8 |
|
| 9 |
class UploadService |
| 10 |
{ |
| 11 |
|
| 12 |
/** |
| 13 |
* @throws \Exception |
| 14 |
*/ |
| 15 |
public static function handleFileUpload($file, $boardId, $taskId) |
| 16 |
{ |
| 17 |
$uploadInfo = FileSystem::setSubDir('board_'.$boardId)->put($file); |
| 18 |
|
| 19 |
if (!empty($uploadInfo) && is_array($uploadInfo)) { |
| 20 |
return $uploadInfo; |
| 21 |
} |
| 22 |
|
| 23 |
return new \WP_Error('file_upload_error', __('File upload failed', 'fluent-boards')); |
| 24 |
} |
| 25 |
|
| 26 |
public function validateFile($file) |
| 27 |
{ |
| 28 |
if (!$file) { |
| 29 |
throw new \Exception('File is empty.'); |
| 30 |
} |
| 31 |
if (!$this->isFileTypeSupported($file)) { |
| 32 |
throw new \Exception('File type not supported'); |
| 33 |
} |
| 34 |
if ($file['size'] > $this->getFileUploadLimit()) { |
| 35 |
throw new \Exception('File size is too large'); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
public function getFileUploadLimit() { |
| 40 |
// Logic for calculating file upload limit as in your original code |
| 41 |
return min( |
| 42 |
wp_convert_hr_to_bytes(ini_get('upload_max_filesize')), |
| 43 |
wp_convert_hr_to_bytes(ini_get('post_max_size')), |
| 44 |
wp_max_upload_size() |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
public function isFileTypeSupported($file) |
| 49 |
{ |
| 50 |
// Define supported file types that are generally allowed by user |
| 51 |
$allowedMimeTypes = get_allowed_mime_types(); |
| 52 |
// Check if the file type is supported |
| 53 |
return in_array(strtolower($file['type']), $allowedMimeTypes); |
| 54 |
} |
| 55 |
|
| 56 |
} |