Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function _arruma_arquivo_legenda_hash($nomearquivo, $path, $debug = false) {
- $caminho = $path;
- $arquivo_legendas = "/LTV/Legendas.tv.txt";
- $arquivo_legendasurl = "/LTV/Legendas.tv.url";
- $arquivo_comentarios = "/LTV/comentario.txt";
- $ext = strtolower(substr($nomearquivo, -3));
- $temp_dir = strtolower(substr($nomearquivo, 0, -4));//remove o .zip ou .rar
- //cria pasta temporaria
- {
- //mkdir teste_leg
- $cmd = 'mkdir ' . $caminho . $temp_dir;
- // echo $cmd;
- // exit;
- $result = false;
- @exec($cmd,$result);
- if($debug) {
- debug($cmd);
- debug($result);
- }
- }
- //extrai .rar ou .zip para a pasta temporaria
- {
- switch($ext) {
- case 'zip':
- // $cmd = 'cd ' . $caminho . ';';
- // $cmd .= 'unzip ' . $nomearquivo . ' -d ./' . $temp_dir;
- $cmd = '/usr/bin/unzip ' . $caminho . $nomearquivo . ' -d ' . $caminho . $temp_dir;
- // echo $cmd;
- // exit;
- $result = false;
- @exec($cmd,$result);
- if($debug) {
- debug($cmd);
- debug($result);
- }
- break;
- case 'rar':
- //rar e -y teste_leg.rar ./teste_leg
- // $cmd = 'cd ' . $caminho . ';';
- // $cmd .= 'rar x -y ' . $nomearquivo . ' ./' . $temp_dir;
- $cmd = '/usr/bin/rar x -y ' . $caminho . $nomearquivo . ' ' . $caminho . $temp_dir;
- // echo $cmd;
- // exit;
- $result = false;
- @exec($cmd,$result);
- if($debug) {
- debug($cmd);
- debug($result);
- }
- break;
- }
- }
- //cria novo .rar somente com arquivos .txt ou .srt ou .sub ou .ass
- {
- $cmd = 'cd ' . $caminho.$temp_dir . ';';
- $cmd .= 'find . -name "*.txt" -o -name "*.srt" -o -name "*.sub" -o -name "*.ass" | /usr/bin/rar a ' . $temp_dir . '.rar @;';
- //inclui arquivo legendas.tv.url e comentario
- $cmd .= '/usr/bin/rar a -ep ' . $temp_dir . '.rar ' . $arquivo_legendasurl . ' -c -z' . $arquivo_comentarios;
- // echo $cmd;
- // exit;
- $result = false;
- @exec($cmd,$result);
- if($debug) {
- debug($cmd);
- debug($result);
- }
- }
- if($debug) {
- // exit;
- }
- //remove arquivo original
- {
- $cmd = '/bin/rm -rf ' . $caminho.$nomearquivo;
- $result = false;
- @exec($cmd,$result);
- if($debug) {
- debug($cmd);
- debug($result);
- }
- }
- //sobrescreve rar orginal com novo rar
- {
- // $cmd = 'cd ' . $caminho . ';';
- // $cmd .= 'rm -rf ' . $temp_dir . '.'.$ext.';';
- // $cmd .= 'mv ' . $temp_dir.DS.$temp_dir . '.rar .';
- $cmd = '/bin/mv ' . $caminho.$temp_dir . '/'.$temp_dir.'.rar ' . $caminho;
- $result = false;
- @exec($cmd,$result);
- if($debug) {
- debug($cmd);
- debug($result);
- }
- }
- //excluir pasta temporaria
- {
- // $cmd = 'rm -rf ' . $caminho . $temp_dir . ';';
- $cmd = '/bin/rm -rf ' . $caminho . $temp_dir . ';';
- $result = false;
- @exec($cmd,$result);
- if($debug) {
- debug($cmd);
- debug($result);
- }
- }
- if($debug) {
- exit;
- }
- return $temp_dir . '.rar';
- }
- function _uploadFileHash($arquivo, $caminho = null, $prefixo)
- {
- ini_set("memory_limit","200M");
- set_time_limit(0);
- switch ($arquivo['error'])
- {
- case UPLOAD_ERR_INI_SIZE:
- die('Erro: Arquivo muito grande');
- case UPLOAD_ERR_FORM_SIZE:
- die('Erro: Arquivo muito grande');
- case UPLOAD_ERR_PARTIAL:
- die('Erro ao enviar arquivo, tente novamente(UPLOAD_ERR_PARTIAL)');
- case UPLOAD_ERR_NO_FILE:
- die('Erro: Nenhum arquivo enviado');
- case UPLOAD_ERR_NO_TMP_DIR:
- die('Erro ao enviar arquivo, tente novamente(UPLOAD_ERR_NO_TMP_DIR)');
- case UPLOAD_ERR_CANT_WRITE:
- die('Erro ao enviar arquivo, tente novamente(UPLOAD_ERR_CANT_WRITE)');
- case UPLOAD_ERR_EXTENSION:
- die('Erro: Tipo de arquivo inválido.');
- }
- if($arquivo["name"] != "")
- {
- $timestamp = date("YmdHisu");
- $filenamePieces = explode(".",$arquivo["name"]);
- $extension = $filenamePieces[count($filenamePieces)-1];
- // $filename = $this->prefixo_arquivo.$timestamp.".".$extension;
- // When the file is opened and cleaned, it always become a rar
- $filename = $prefixo.$timestamp.".rar";
- $filenameMd5 = md5($filename);
- $fstTwo = substr($filenameMd5, 0, 2);
- $sndTwo = substr($filenameMd5, 2, 2);
- $newPath = "/LTV/files/$fstTwo/$sndTwo/";
- while (file_exists($newPath.$filename)) {
- $timestamp = date("YmdHisu");
- $filenamePieces = explode(".",$arquivo["name"]);
- $extension = $filenamePieces[count($filenamePieces)-1];
- $filename = $prefixo.$timestamp.".rar";
- $filenameMd5 = md5($filename);
- $fstTwo = substr($filenameMd5, 0, 2);
- $sndTwo = substr($filenameMd5, 2, 2);
- $newPath = "/LTV/files/$fstTwo/$sndTwo/";
- }
- if (file_exists($arquivo['tmp_name'])) {
- if (!is_dir($newPath)) {
- if (!mkdir($newPath, 0775, true)) {
- return '';
- }
- }
- $moveError = rename($arquivo['tmp_name'], $newPath.$filename);
- if ($moveError) {
- $obj = array();
- $obj["path"] = $newPath;
- $obj["filename"] = $filename;
- return $obj;
- } else {
- return '';
- }
- } else {
- return '';
- }
- }
- else
- {
- return "";
- }
- }
- function main($json) {
- $data = json_decode($json);
- var_dump($data);
- $tempFile = $data->file;
- $uid = $data->uid;
- $prefixo = $data->prefixo;
- // $tempFile = $data["file"];
- // $uid = $data["uid"];
- // $prefixo = $data["prefixo"];
- $errormessage = "";
- $erro = false;
- $returnObj = array();
- //Tamanho do arquivo
- if($tempFile->size >= 2048000) { //2048000 = 2MB
- // if($tempFile["size"] >= 2048000) { //2048000 = 2MB
- $errormessage .= 'O arquivo da legenda deve ter no máximo 2MB.<br />';
- $erro = true;
- } else {
- //extensao zip ou rar
- $ext = strtoupper(substr($tempFile->name, -3));
- // $ext = strtoupper(substr($tempFile["name"], -3));
- if ($ext == 'RAR' || $ext == 'ZIP') {
- $pathAndFilename = _uploadFileHash(json_decode(json_encode($tempFile), true), "", $prefixo);
- $newPath = false;
- $newFilename = false;
- if (is_array($pathAndFilename)) {
- $newPath = $pathAndFilename["path"];
- $newFilename = $pathAndFilename["filename"];
- }
- if (!$newFilename || $newFilename == '') {
- $errormessage .= 'Erro ao salvar arquivo, por favor tente novamente.<br />';
- $erro = true;
- } else {
- $newFilename = _arruma_arquivo_legenda_hash($newFilename, $newPath);
- $returnObj["dsc_url_legenda"] = $newFilename;
- $returnObj["dsc_file_path"] = base64_encode($newPath);
- $returnObj["uid"] = $uid;
- }
- } else {
- $errormessage .= 'O arquivo da legenda deve estar em formato RAR ou ZIP.<br />';
- $erro = true;
- }
- }
- if ($erro) {
- echo date("Y-m-d H:i:s")." Problemas - {$json}\n";
- file_put_contents("/LTV/async-worker.log", date("Y-m-d H:i:s")." Problemas - {$json}\n", FILE_APPEND);
- } else {
- // echo json_encode($returnObj)."\n";
- $notificationURL = "http://legendas.tv/background/update_path_and_filename/{$returnObj['uid']}/{$returnObj['dsc_url_legenda']}/{$returnObj['dsc_file_path']}";
- echo $notificationURL."\n";
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $notificationURL);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_exec($ch);
- curl_close($ch);
- }
- }
- $worker = new GearmanWorker();
- $worker->addServer('roberts.legendas.tv');
- $worker->addFunction("enviar_legenda", function(GearmanJob $job) {
- file_put_contents("/LTV/async-worker.log", date("Y-m-d H:i:s")." Processando novo upload - {$job->workload()}\n", FILE_APPEND);
- if (strlen($job->workload()) > 0) main($job->workload());
- else echo date("Y-m-d H:i:s")." - Nenhum json informado.";
- });
- $worker->work();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement