Updated slides upload process

added chunked files

Change-Id: I8e69f7a8f7d4355898eb2f7578111a1d334fd353
Signed-off-by: smarcet <smarcet@gmail.com>
This commit is contained in:
smarcet 2020-11-30 23:30:01 -03:00
parent d888de09a6
commit 9ed56892ad
6 changed files with 66 additions and 26 deletions

View File

@ -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());

View File

@ -0,0 +1,20 @@
<?php namespace App\Http\Utils;
/**
* Copyright 2020 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
final class FileSizeUtil
{
public const Kb = 'KB';
public const MB = 'MB';
public const B = "Byte";
}

View File

@ -23,8 +23,9 @@ use Illuminate\Http\UploadedFile;
*/
final class FileUploadInfo
{
/**
* @var int
* @var int // in bytes
*/
private $size;
/**
@ -80,7 +81,6 @@ final class FileUploadInfo
$size = $file->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
*/

View File

@ -107,7 +107,7 @@ interface IPresentationService
$presentation_id,
array $slide_data,
array $allowed_extensions = [],
$max_file_size = 62914560
$max_file_size = 1048576 // bytes
);
/**

View File

@ -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();
}

View File

@ -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')
];