Skip to content

Commit

Permalink
handle thumbnail logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kuldp18 committed Mar 14, 2024
1 parent 44289a8 commit 72783ae
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
23 changes: 23 additions & 0 deletions controllers/upload_video.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,26 @@ function is_video_file_invalid(array $video): bool
return true;
}
}

// check if thumbnail is not empty and is a valid image
function is_thumbnail_file_invalid(array $thumbnail): bool
{
$thumbnail_name = $thumbnail['name'];
$thumbnail_size = $thumbnail['size'];
$thumbnail_error = $thumbnail['error'];

$thumbnail_ext = explode('.', $thumbnail_name);
$thumbnail_actual_ext = strtolower(end($thumbnail_ext));

$allowed = ['jpg', 'jpeg', 'png'];

if (in_array($thumbnail_actual_ext, $allowed)) {
if ($thumbnail_error === 0 && $thumbnail_size > 0) {
return false;
} else {
return true;
}
} else {
return true;
}
}
7 changes: 6 additions & 1 deletion includes/upload_video.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
$description = $_POST['description'];
$thumbnail = $_FILES['thumbnail'];



try {
require_once "./db_handler.inc.php";
require_once "../models/upload_video.inc.php";
Expand All @@ -24,7 +26,10 @@
else if (is_video_file_invalid($video)) {
$errors['invalid_video'] = 'Please upload a valid video in .mp4 format';
}

// check if thumbnail array is not empty and is a valid image, if empty, ignore
else if (!empty($thumbnail['name']) && is_thumbnail_file_invalid($thumbnail)) {
$errors['invalid_thumbnail'] = 'Please upload a valid image in .jpg, .jpeg or .png format';
}
require_once "./config_session.inc.php";

if ($errors) {
Expand Down

0 comments on commit 72783ae

Please sign in to comment.