AliceClass/php/upload.php

95 lines
2.4 KiB
PHP
Raw Normal View History

2024-08-01 17:31:21 -03:00
<?php
2024-08-06 10:42:22 -03:00
// Cria as pastas do usuário
function create_user_dir($target_dir) {
$paths = array('', '/src', '/dest', '/thumbs');
foreach($paths as $path) {
file_exists($target_dir . $path) || mkdir($target_dir . $path, 0775, true);
}
}
2024-08-01 17:31:21 -03:00
2024-08-06 10:42:22 -03:00
// Checa a extensão do arquivo
function check_file_extension($target_file) {
$fileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
2024-08-01 17:31:21 -03:00
2024-08-06 10:42:22 -03:00
if ($fileType != '.mkv') {
echo "Apenas arquivos .mkv são aceitos. ";
exit(1);
}
2024-08-01 17:31:21 -03:00
2024-08-06 10:42:22 -03:00
return 1;
}
2024-08-05 17:37:08 -03:00
2024-08-06 10:42:22 -03:00
// Checa se o arquivo existe
function check_file_exists($brute_file) {
if (file_exists($brute_file)) {
echo "Sorry, file already exists.";
return 0;
}
2024-08-05 17:37:08 -03:00
2024-08-06 10:42:22 -03:00
return 1;
2024-08-01 17:31:21 -03:00
}
2024-08-06 10:42:22 -03:00
// Move uploaded file to correct dir
function upload($orig_file, $brute_file) {
if (!move_uploaded_file($orig_file, $brute_file)) {
2024-08-01 17:31:21 -03:00
echo "Houve um problema com o upload do arquivo.";
2024-08-06 10:42:22 -03:00
return 0;
2024-08-01 17:31:21 -03:00
}
2024-08-06 10:42:22 -03:00
return 1;
}
// Processa o vídeo com o script do flavio
function process_file($username, $title, $input_file, $output_file) {
$cmd = sprintf('%s "%s" "%s" "%s" "%s" "%s"',
$SCRIPT_PATH, $title, $username, $input_file, '', $output_file);
$output = shell_exec($cmd);
return 1;
2024-08-01 17:31:21 -03:00
}
2024-08-06 10:42:22 -03:00
function insert_into_db($username, $title, $desc, $level, $software, $date) {
include("library/json-db.class.php");
$db = new DB('/var/www/src/AliceClass/_data/aulas.json');
$data = array(
"title" => $title,
"user" => $username,
"nome" => $username,
"description" => $desc,
"date" => $date,
"software" => $software,
"level" => $level
);
$db->insert($data);
return 1;
}
$username = $_SERVER['PHP_AUTH_USER'];
$video_title = $_POST["videoTitle"];
$video_desc = $_POST["videoDescription"];
$video_level = $_POST["level"];
$video_software = $_POST["software"];
$video_date = date('d/m/Y');
$orig_file = $_FILES["fileToUpload"]["tmp_name"];
$target_dir = "/var/www/html/aliceclass_videos/" . $username;
$brute_file = $target_dir . "/src/" . $video_title . '.mkv';
$target_file = $target_dir . "/dest/" . $video_title . '.mp4';
2024-08-01 17:31:21 -03:00
2024-08-06 10:42:22 -03:00
create_user_dir($target_dir) || exit;
check_file_extension($_FILES["fileToUpload"]["tmp_name"]) || exit;
!check_file_exists($brute_file) || exit;
upload($brute_file) || exit;
process_file($username, $video_title, $brute_file, $target_file) || exit;
insert_into_db($username, $video_title, $video_desc, $video_level, $video_software, $video_date);
2024-08-01 17:31:21 -03:00
2024-08-06 10:42:22 -03:00
echo "The file ". htmlspecialchars(basename($orig_file)). " has been uploaded.";
2024-08-01 17:31:21 -03:00
?>