diff --git a/app/Http/Controllers/Apis/Protected/Summit/OAuth2PresentationApiController.php b/app/Http/Controllers/Apis/Protected/Summit/OAuth2PresentationApiController.php index 001af3de..3ecbf285 100644 --- a/app/Http/Controllers/Apis/Protected/Summit/OAuth2PresentationApiController.php +++ b/app/Http/Controllers/Apis/Protected/Summit/OAuth2PresentationApiController.php @@ -14,6 +14,7 @@ use App\Http\Utils\FileTypes; use App\Http\Utils\MultipartFormDataCleaner; use App\Models\Foundation\Main\IGroup; +use Illuminate\Support\Facades\Config; use libs\utils\HTMLCleaner; use models\exceptions\EntityNotFoundException; use models\exceptions\ValidationException; @@ -612,8 +613,9 @@ final class OAuth2PresentationApiController extends OAuth2ProtectedController $data = MultipartFormDataCleaner::cleanBool('featured', $data); $rules = [ - 'file' => 'required_without:link', - 'link' => 'required_without:file|url', + 'file' => 'required_without_all:link,filepath', + 'link' => 'required_without_all:file,filepath|url', + 'filepath' => 'required_without_all:link,file', 'name' => 'required|string:512', 'description' => 'nullable|string', 'display_on_site' => 'nullable|boolean', @@ -639,7 +641,8 @@ final class OAuth2PresentationApiController extends OAuth2ProtectedController $request, $presentation_id, HTMLCleaner::cleanData($data, $fields), - array_merge(FileTypes::ImagesExntesions, FileTypes::SlidesExtensions) + array_merge(FileTypes::ImagesExntesions, FileTypes::SlidesExtensions), + intval(Config::get("mediaupload.slides_max_file_size")) ); return $this->created(SerializerRegistry::getInstance()->getSerializer($slide)->serialize()); @@ -720,7 +723,8 @@ final class OAuth2PresentationApiController extends OAuth2ProtectedController $presentation_id, $slide_id, HTMLCleaner::cleanData($data, $fields), - array_merge(FileTypes::ImagesExntesions, FileTypes::SlidesExtensions) + array_merge(FileTypes::ImagesExntesions, FileTypes::SlidesExtensions), + intval(Config::get("mediaupload.slides_max_file_size")) ); return $this->updated(SerializerRegistry::getInstance()->getSerializer($slide)->serialize()); diff --git a/app/Http/Utils/FileSizeUtil.php b/app/Http/Utils/FileSizeUtil.php new file mode 100644 index 00000000..f433a4e7 --- /dev/null +++ b/app/Http/Utils/FileSizeUtil.php @@ -0,0 +1,20 @@ +getSize(); if($size == 0) throw new ValidationException("File size is zero."); - $size = $size/1024; // convert bytes to KB $fileName = $file->getClientOriginalName(); $fileExt = pathinfo($fileName, PATHINFO_EXTENSION); } @@ -97,7 +97,6 @@ final class FileUploadInfo if($size == 0) throw new ValidationException("File size is zero."); - $size = $size/1024; // convert bytes to KB $fileName = pathinfo($payload['filepath'],PATHINFO_BASENAME); $fileExt = pathinfo($fileName, PATHINFO_EXTENSION); $file = new UploadedFile($disk->path($payload['filepath']), $fileName); @@ -111,13 +110,19 @@ final class FileUploadInfo } /** - * @return int + * @param string $unit + * @return int|null */ - public function getSize(): ?int + public function getSize(string $unit = FileSizeUtil::Kb): ?int { + if($unit === FileSizeUtil::Kb){ + return $this->size / 1024; + } + return $this->size; } + /** * @return UploadedFile */ diff --git a/app/Services/Model/IPresentationService.php b/app/Services/Model/IPresentationService.php index fe9f6e10..a4ee99c7 100644 --- a/app/Services/Model/IPresentationService.php +++ b/app/Services/Model/IPresentationService.php @@ -107,7 +107,7 @@ interface IPresentationService $presentation_id, array $slide_data, array $allowed_extensions = [], - $max_file_size = 62914560 + $max_file_size = 1048576 // bytes ); /** diff --git a/app/Services/Model/Imp/PresentationService.php b/app/Services/Model/Imp/PresentationService.php index 5817f4c6..354e6b67 100644 --- a/app/Services/Model/Imp/PresentationService.php +++ b/app/Services/Model/Imp/PresentationService.php @@ -13,6 +13,7 @@ **/ use App\Events\PresentationMaterialDeleted; use App\Events\PresentationMaterialUpdated; +use App\Http\Utils\FileSizeUtil; use App\Http\Utils\FileUploadInfo; use App\Http\Utils\IFileUploader; use App\Jobs\Emails\PresentationSubmissions\PresentationCreatorNotificationEmail; @@ -690,7 +691,7 @@ final class PresentationService $presentation_id, array $slide_data, array $allowed_extensions = [], - $max_file_size = 10485760 + $max_file_size = 10485760 // bytes ) { $slide = $this->tx_service->transaction(function () use ( @@ -710,30 +711,33 @@ final class PresentationService throw new EntityNotFoundException('presentation not found!'); $hasLink = isset($slide_data['link']) && !empty($slide_data['link']); - $hasFile = $request->hasFile('file'); + $fileInfo = FileUploadInfo::build($request, $slide_data); + $hasFile = !is_null($fileInfo); if($hasFile && $hasLink){ throw new ValidationException("you must provide a file or a link, not both."); } $slide = PresentationSlideFactory::build($slide_data); - // check if there is any file sent if($hasFile){ - $file = $request->file('file'); - if (!in_array($file->extension(), $allowed_extensions)) { + + if (!in_array($fileInfo->getFileExt(), $allowed_extensions)) { throw new ValidationException( sprintf("file does not has a valid extension '(%s)'.", implode("','", $allowed_extensions))); } - if ($file->getSize() > $max_file_size) { + if ($fileInfo->getSize(FileSizeUtil::B) > $max_file_size) { throw new ValidationException(sprintf("file exceeds max_file_size (%s MB).", ($max_file_size / 1024) / 1024)); } - $slideFile = $this->file_uploader->build( - $file, + $slideFile = $this->file_uploader->build + ( + $fileInfo->getFile(), sprintf('summits/%s/presentations/%s/slides', $presentation->getSummitId(), $presentation_id), - false); + false + ); + $slide->setSlide($slideFile); } @@ -762,7 +766,7 @@ final class PresentationService $slide_id, array $slide_data, array $allowed_extensions = [], - $max_file_size = 10485760 + $max_file_size = 10485760 // bytes ){ $slide = $this->tx_service->transaction(function () use @@ -793,7 +797,8 @@ final class PresentationService $hasLink = isset($slide_data['link']) && !empty($slide_data['link']); - $hasFile = $request->hasFile('file'); + $fileInfo = FileUploadInfo::build($request, $slide_data); + $hasFile = !is_null($fileInfo); if($hasFile && $hasLink){ throw new ValidationException("you must provide a file or a link, not both."); @@ -814,17 +819,22 @@ final class PresentationService // check if there is any file sent if($hasFile){ - $file = $request->file('file'); - if (!in_array($file->extension(), $allowed_extensions)) { + if (!in_array($fileInfo->getFileExt(), $allowed_extensions)) { throw new ValidationException( sprintf("file does not has a valid extension '(%s)'.", implode("','", $allowed_extensions))); } - if ($file->getSize() > $max_file_size) { + if ($fileInfo->getSize(FileSizeUtil::B) > $max_file_size){ throw new ValidationException(sprintf("file exceeds max_file_size (%s MB).", ($max_file_size / 1024) / 1024)); } - $slideFile = $this->file_uploader->build($file, sprintf('summits/%s/presentations/%s/slides', $presentation->getSummitId(), $presentation_id), false); + $slideFile = $this->file_uploader->build + ( + $fileInfo->getFile(), + sprintf('summits/%s/presentations/%s/slides', $presentation->getSummitId(), $presentation_id), + false + ); + $slide->setSlide($slideFile); $slide->clearLink(); } diff --git a/config/mediaupload.php b/config/mediaupload.php index 457464be..aeeabbc4 100644 --- a/config/mediaupload.php +++ b/config/mediaupload.php @@ -13,6 +13,7 @@ **/ return [ - // in MB + // in Bytes + 'slides_max_file_size' => env('MEDIA_UPLOAD_SLIDES_MAX_FILE_SIZE', 524288000), 'mounting_folder' => env('MEDIA_UPLOAD_MOUNTING_FOLDER','PresentationMediaUploads') ];