From 15552319aa9d31f762b4ef20665ea77652f70335 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 28 Feb 2012 13:47:57 -0500 Subject: [PATCH] CC-3230: Show appropriate error message if disk is full when attempting to upload files via the web UI - updated formating of file, removed tabs in favour of four spaces for some lines. --- airtime_mvc/application/models/StoredFile.php | 369 +++++++++--------- 1 file changed, 184 insertions(+), 185 deletions(-) diff --git a/airtime_mvc/application/models/StoredFile.php b/airtime_mvc/application/models/StoredFile.php index b88a58430..c795b2ccf 100644 --- a/airtime_mvc/application/models/StoredFile.php +++ b/airtime_mvc/application/models/StoredFile.php @@ -82,7 +82,7 @@ class Application_Model_StoredFile { * @param array $p_md * example: $p_md['MDATA_KEY_URL'] = 'http://www.fake.com' */ - public function setMetadata($p_md=null) + public function setMetadata($p_md=null) { if (is_null($p_md)) { $this->setDbColMetadata(); @@ -105,7 +105,7 @@ class Application_Model_StoredFile { * @param array $p_md * example: $p_md['url'] = 'http://www.fake.com' */ - public function setDbColMetadata($p_md=null) + public function setDbColMetadata($p_md=null) { if (is_null($p_md)) { foreach ($this->_dbMD as $dbColumn => $propelColumn) { @@ -627,7 +627,7 @@ class Application_Model_StoredFile { $fromTable = $unionTable; } - $results = Application_Model_StoredFile::searchFiles($displayColumns, $fromTable, $datatables); + $results = Application_Model_StoredFile::searchFiles($displayColumns, $fromTable, $datatables); foreach ($results['aaData'] as &$row) { @@ -666,77 +666,77 @@ class Application_Model_StoredFile { return $results; } - public static function searchFiles($displayColumns, $fromTable, $data) - { - $con = Propel::getConnection(CcFilesPeer::DATABASE_NAME); + public static function searchFiles($displayColumns, $fromTable, $data) + { + $con = Propel::getConnection(CcFilesPeer::DATABASE_NAME); $where = array(); - - if ($data["sSearch"] !== "") { - $searchTerms = explode(" ", $data["sSearch"]); - } - - $selectorCount = "SELECT COUNT(*) "; - $selectorRows = "SELECT ".join(",", $displayColumns)." "; - + + if ($data["sSearch"] !== "") { + $searchTerms = explode(" ", $data["sSearch"]); + } + + $selectorCount = "SELECT COUNT(*) "; + $selectorRows = "SELECT ".join(",", $displayColumns)." "; + $sql = $selectorCount." FROM ".$fromTable; $sqlTotalRows = $sql; - - if (isset($searchTerms)) { - $searchCols = array(); - for ($i = 0; $i < $data["iColumns"]; $i++) { - if ($data["bSearchable_".$i] == "true") { - $searchCols[] = $data["mDataProp_{$i}"]; - } - } - - $outerCond = array(); - - foreach ($searchTerms as $term) { - $innerCond = array(); - - foreach ($searchCols as $col) { + + if (isset($searchTerms)) { + $searchCols = array(); + for ($i = 0; $i < $data["iColumns"]; $i++) { + if ($data["bSearchable_".$i] == "true") { + $searchCols[] = $data["mDataProp_{$i}"]; + } + } + + $outerCond = array(); + + foreach ($searchTerms as $term) { + $innerCond = array(); + + foreach ($searchCols as $col) { $escapedTerm = pg_escape_string($term); - $innerCond[] = "{$col}::text ILIKE '%{$escapedTerm}%'"; - } - $outerCond[] = "(".join(" OR ", $innerCond).")"; - } - $where[] = "(".join(" AND ", $outerCond).")"; - } - // End Where clause - - // Order By clause - $orderby = array(); - for ($i = 0; $i < $data["iSortingCols"]; $i++){ - $num = $data["iSortCol_".$i]; - $orderby[] = $data["mDataProp_{$num}"]." ".$data["sSortDir_".$i]; - } - $orderby[] = "id"; - $orderby = join("," , $orderby); - // End Order By clause - - if (count($where) > 0) { - $where = join(" AND ", $where); - $sql = $selectorCount." FROM ".$fromTable." WHERE ".$where; - $sqlTotalDisplayRows = $sql; - - $sql = $selectorRows." FROM ".$fromTable." WHERE ".$where." ORDER BY ".$orderby." OFFSET ".$data["iDisplayStart"]." LIMIT ".$data["iDisplayLength"]; - } - else { - $sql = $selectorRows." FROM ".$fromTable." ORDER BY ".$orderby." OFFSET ".$data["iDisplayStart"]." LIMIT ".$data["iDisplayLength"]; - } - + $innerCond[] = "{$col}::text ILIKE '%{$escapedTerm}%'"; + } + $outerCond[] = "(".join(" OR ", $innerCond).")"; + } + $where[] = "(".join(" AND ", $outerCond).")"; + } + // End Where clause + + // Order By clause + $orderby = array(); + for ($i = 0; $i < $data["iSortingCols"]; $i++){ + $num = $data["iSortCol_".$i]; + $orderby[] = $data["mDataProp_{$num}"]." ".$data["sSortDir_".$i]; + } + $orderby[] = "id"; + $orderby = join("," , $orderby); + // End Order By clause + + if (count($where) > 0) { + $where = join(" AND ", $where); + $sql = $selectorCount." FROM ".$fromTable." WHERE ".$where; + $sqlTotalDisplayRows = $sql; + + $sql = $selectorRows." FROM ".$fromTable." WHERE ".$where." ORDER BY ".$orderby." OFFSET ".$data["iDisplayStart"]." LIMIT ".$data["iDisplayLength"]; + } + else { + $sql = $selectorRows." FROM ".$fromTable." ORDER BY ".$orderby." OFFSET ".$data["iDisplayStart"]." LIMIT ".$data["iDisplayLength"]; + } + try { - $r = $con->query($sqlTotalRows); - $totalRows = $r->fetchColumn(0); - - if (isset($sqlTotalDisplayRows)) { - $r = $con->query($sqlTotalDisplayRows); - $totalDisplayRows = $r->fetchColumn(0); - } - else { - $totalDisplayRows = $totalRows; - } - + $r = $con->query($sqlTotalRows); + $totalRows = $r->fetchColumn(0); + + if (isset($sqlTotalDisplayRows)) { + $r = $con->query($sqlTotalDisplayRows); + $totalDisplayRows = $r->fetchColumn(0); + } + else { + $totalDisplayRows = $totalRows; + } + $r = $con->query($sql); $r->setFetchMode(PDO::FETCH_ASSOC); $results = $r->fetchAll(); @@ -744,109 +744,109 @@ class Application_Model_StoredFile { catch (Exception $e) { Logging::log($e->getMessage()); } - - //display sql executed in airtime log for testing - Logging::log($sql); - - return array("sEcho" => intval($data["sEcho"]), "iTotalDisplayRecords" => $totalDisplayRows, "iTotalRecords" => $totalRows, "aaData" => $results); - } + + //display sql executed in airtime log for testing + Logging::log($sql); + + return array("sEcho" => intval($data["sEcho"]), "iTotalDisplayRecords" => $totalDisplayRows, "iTotalRecords" => $totalRows, "aaData" => $results); + } public static function uploadFile($p_targetDir) { // HTTP headers for no cache etc - header('Content-type: text/plain; charset=UTF-8'); - header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); - header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); - header("Cache-Control: no-store, no-cache, must-revalidate"); - header("Cache-Control: post-check=0, pre-check=0", false); - header("Pragma: no-cache"); - - // Settings - $cleanupTargetDir = false; // Remove old files - $maxFileAge = 60 * 60; // Temp file age in seconds - - // 5 minutes execution time - @set_time_limit(5 * 60); - // usleep(5000); - - // Get parameters - $chunk = isset($_REQUEST["chunk"]) ? $_REQUEST["chunk"] : 0; - $chunks = isset($_REQUEST["chunks"]) ? $_REQUEST["chunks"] : 0; - $fileName = isset($_REQUEST["name"]) ? $_REQUEST["name"] : ''; + header('Content-type: text/plain; charset=UTF-8'); + header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); + header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); + header("Cache-Control: no-store, no-cache, must-revalidate"); + header("Cache-Control: post-check=0, pre-check=0", false); + header("Pragma: no-cache"); + + // Settings + $cleanupTargetDir = false; // Remove old files + $maxFileAge = 60 * 60; // Temp file age in seconds + + // 5 minutes execution time + @set_time_limit(5 * 60); + // usleep(5000); + + // Get parameters + $chunk = isset($_REQUEST["chunk"]) ? $_REQUEST["chunk"] : 0; + $chunks = isset($_REQUEST["chunks"]) ? $_REQUEST["chunks"] : 0; + $fileName = isset($_REQUEST["name"]) ? $_REQUEST["name"] : ''; Logging::log(__FILE__.":uploadFile(): filename=$fileName to $p_targetDir"); - // Clean the fileName for security reasons + // Clean the fileName for security reasons //this needs fixing for songs not in ascii. - //$fileName = preg_replace('/[^\w\._]+/', '', $fileName); - - // Create target dir - if (!file_exists($p_targetDir)) - @mkdir($p_targetDir); - - // Remove old temp files - if (is_dir($p_targetDir) && ($dir = opendir($p_targetDir))) { - while (($file = readdir($dir)) !== false) { - $filePath = $p_targetDir . DIRECTORY_SEPARATOR . $file; - - // Remove temp files if they are older than the max age - if (preg_match('/\.tmp$/', $file) && (filemtime($filePath) < time() - $maxFileAge)) - @unlink($filePath); - } - - closedir($dir); - } else - die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}'); - - // Look for the content type header - if (isset($_SERVER["HTTP_CONTENT_TYPE"])) - $contentType = $_SERVER["HTTP_CONTENT_TYPE"]; - - if (isset($_SERVER["CONTENT_TYPE"])) - $contentType = $_SERVER["CONTENT_TYPE"]; + //$fileName = preg_replace('/[^\w\._]+/', '', $fileName); + + // Create target dir + if (!file_exists($p_targetDir)) + @mkdir($p_targetDir); + + // Remove old temp files + if (is_dir($p_targetDir) && ($dir = opendir($p_targetDir))) { + while (($file = readdir($dir)) !== false) { + $filePath = $p_targetDir . DIRECTORY_SEPARATOR . $file; + + // Remove temp files if they are older than the max age + if (preg_match('/\.tmp$/', $file) && (filemtime($filePath) < time() - $maxFileAge)) + @unlink($filePath); + } + + closedir($dir); + } else + die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}'); + + // Look for the content type header + if (isset($_SERVER["HTTP_CONTENT_TYPE"])) + $contentType = $_SERVER["HTTP_CONTENT_TYPE"]; + + if (isset($_SERVER["CONTENT_TYPE"])) + $contentType = $_SERVER["CONTENT_TYPE"]; // create temp file name (CC-3086) // we are not using mktemp command anymore. // plupload support unique_name feature. $tempFilePath= $p_targetDir . DIRECTORY_SEPARATOR . $fileName; - if (strpos($contentType, "multipart") !== false) { - if (isset($_FILES['file']['tmp_name']) && is_uploaded_file($_FILES['file']['tmp_name'])) { - // Open temp file - $out = fopen($tempFilePath, $chunk == 0 ? "wb" : "ab"); - if ($out) { - // Read binary input stream and append it to temp file - $in = fopen($_FILES['file']['tmp_name'], "rb"); - - if ($in) { - while ($buff = fread($in, 4096)) - fwrite($out, $buff); - } else - die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}'); - - fclose($out); - unlink($_FILES['file']['tmp_name']); - } else - die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}'); - } else - die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}'); - } else { - // Open temp file - $out = fopen($tempFilePath, $chunk == 0 ? "wb" : "ab"); - if ($out) { - // Read binary input stream and append it to temp file - $in = fopen("php://input", "rb"); - - if ($in) { - while ($buff = fread($in, 4096)) - fwrite($out, $buff); - } else - die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}'); - - fclose($out); - } else - die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}'); - } - - return $tempFilePath; + if (strpos($contentType, "multipart") !== false) { + if (isset($_FILES['file']['tmp_name']) && is_uploaded_file($_FILES['file']['tmp_name'])) { + // Open temp file + $out = fopen($tempFilePath, $chunk == 0 ? "wb" : "ab"); + if ($out) { + // Read binary input stream and append it to temp file + $in = fopen($_FILES['file']['tmp_name'], "rb"); + + if ($in) { + while ($buff = fread($in, 4096)) + fwrite($out, $buff); + } else + die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}'); + + fclose($out); + unlink($_FILES['file']['tmp_name']); + } else + die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}'); + } else + die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}'); + } else { + // Open temp file + $out = fopen($tempFilePath, $chunk == 0 ? "wb" : "ab"); + if ($out) { + // Read binary input stream and append it to temp file + $in = fopen("php://input", "rb"); + + if ($in) { + while ($buff = fread($in, 4096)) + fwrite($out, $buff); + } else + die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}'); + + fclose($out); + } else + die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}'); + } + + return $tempFilePath; } /** @@ -873,8 +873,8 @@ class Application_Model_StoredFile { $md5 = md5_file($audio_file); $duplicate = Application_Model_StoredFile::RecallByMd5($md5); if ($duplicate) { - if (PEAR::isError($duplicate)) { - $result = array("code" => 105, "message" => $duplicate->getMessage()); + if (PEAR::isError($duplicate)) { + $result = array("code" => 105, "message" => $duplicate->getMessage()); } if (file_exists($duplicate->getFilePath())) { $duplicateName = $duplicate->getMetadataValue('MDATA_KEY_TITLE'); @@ -882,36 +882,35 @@ class Application_Model_StoredFile { } } - if (!isset($result)){//The file has no duplicate, so procceed to copy. - $storDir = Application_Model_MusicDir::getStorDir(); - $stor = $storDir->getDirectory(); + if (!isset($result)){//The file has no duplicate, so procceed to copy. + $storDir = Application_Model_MusicDir::getStorDir(); + $stor = $storDir->getDirectory(); - //check to see if there is enough space in $stor to continue. - $result = Application_Model_StoredFile::checkForEnoughDiskSpaceToCopy($stor, $audio_file); - if (!isset($result)){//if result not set then there's enough disk space to copy the file over - $stor .= "/organize"; - $audio_stor = $stor . DIRECTORY_SEPARATOR . $fileName; + //check to see if there is enough space in $stor to continue. + $result = Application_Model_StoredFile::checkForEnoughDiskSpaceToCopy($stor, $audio_file); + if (!isset($result)){//if result not set then there's enough disk space to copy the file over + $stor .= "/organize"; + $audio_stor = $stor . DIRECTORY_SEPARATOR . $fileName; - Logging::log("copyFileToStor: moving file $audio_file to $audio_stor"); - //Martin K.: changed to rename: Much less load + quicker since this is an atomic operation - $r = @rename($audio_file, $audio_stor); - - if ($r === false) { - #something went wrong likely there wasn't enough space in the audio_stor to move the file too. - #warn the user that the file wasn't uploaded and they should check if there is enough disk space. - unlink($audio_file);//remove the file from the organize after failed rename - $result = array("code" => 108, "message" => "The file was not uploaded, this error will occur if the computer hard drive does not have enough disk space."); - - } - } - } - return $result; + Logging::log("copyFileToStor: moving file $audio_file to $audio_stor"); + //Martin K.: changed to rename: Much less load + quicker since this is an atomic operation + $r = @rename($audio_file, $audio_stor); + + if ($r === false) { + #something went wrong likely there wasn't enough space in the audio_stor to move the file too. + #warn the user that the file wasn't uploaded and they should check if there is enough disk space. + unlink($audio_file);//remove the file from the organize after failed rename + $result = array("code" => 108, "message" => "The file was not uploaded, this error will occur if the computer hard drive does not have enough disk space."); + } + } + } + return $result; } public static function getFileCount() { - global $CC_CONFIG, $CC_DBC; + global $CC_CONFIG, $CC_DBC; $sql = "SELECT count(*) as cnt FROM ".$CC_CONFIG["filesTable"]; return $CC_DBC->GetOne($sql); }