Se adiciona DOWNLOAD para FILES MANAGER. Se modifican get, post, put, delete para subdirectorios FILES MANAGER

This commit is contained in:
Freddy Daniel Rojas Valda
2014-02-12 17:09:51 -04:00
parent cf5b998d5a
commit a84d99e183
2 changed files with 153 additions and 82 deletions

View File

@@ -45,16 +45,17 @@ class FilesManager
{ {
try { try {
$sMainDirectory = current(explode("/", $path)); $sMainDirectory = current(explode("/", $path));
if ($path) if(strstr($path,'/')){
$sSubDirectory = substr($path, strpos($path, "/")); $sSubDirectory = substr($path, strpos($path, "/")+1). PATH_SEP ;
} else {
$sSubDirectory = '';
}
switch ($sMainDirectory) { switch ($sMainDirectory) {
case 'templates': case 'templates':
//$sDirectory = PATH_DATA_MAILTEMPLATES . $sProcessUID . PATH_SEP . $sSubDirectory; $sDirectory = PATH_DATA_MAILTEMPLATES . $sProcessUID . PATH_SEP . $sSubDirectory;
$sDirectory = PATH_DATA_MAILTEMPLATES . $sProcessUID . PATH_SEP;
break; break;
case 'folder': case 'folder':
//$sDirectory = PATH_DATA_PUBLIC . $sProcessUID . PATH_SEP . $sSubDirectory; $sDirectory = PATH_DATA_PUBLIC . $sProcessUID . PATH_SEP . $sSubDirectory;
$sDirectory = PATH_DATA_PUBLIC . $sProcessUID . PATH_SEP;
break; break;
default: default:
die(); die();
@@ -64,7 +65,6 @@ class FilesManager
$aTheFiles = array(); $aTheFiles = array();
$aDirectories = array(); $aDirectories = array();
$aFiles = array(); $aFiles = array();
$sCurrentDirectory = $sSubDirectory;
$oDirectory = dir($sDirectory); $oDirectory = dir($sDirectory);
while ($sObject = $oDirectory->read()) { while ($sObject = $oDirectory->read()) {
if (($sObject !== '.') && ($sObject !== '..')) { if (($sObject !== '.') && ($sObject !== '..')) {
@@ -72,8 +72,7 @@ class FilesManager
if (is_dir($sPath)) { if (is_dir($sPath)) {
$aTheFiles[] = array('name' => $sObject, $aTheFiles[] = array('name' => $sObject,
'type' => "folder", 'type' => "folder",
//'path' => $sDirectory); 'path' => $sDirectory);
'path' => ($sCurrentDirectory != '' ? $sCurrentDirectory . PATH_SEP : '') . $sObject);
} else { } else {
$aAux = pathinfo($sPath); $aAux = pathinfo($sPath);
$aAux['extension'] = (isset($aAux['extension'])?$aAux['extension']:''); $aAux['extension'] = (isset($aAux['extension'])?$aAux['extension']:'');
@@ -114,22 +113,30 @@ class FilesManager
public function addProcessFilesManager($sProcessUID, $userUID, $aData) public function addProcessFilesManager($sProcessUID, $userUID, $aData)
{ {
try { try {
if ($aData['path'] == 'templates/' || $aData['path'] == 'folder/') { $aData['path'] = rtrim($aData['path'], '/') . '/';
switch ($aData['path']) { $sMainDirectory = current(explode("/", $aData['path']));
case 'templates/': if ($sMainDirectory != 'folder' && $sMainDirectory != 'templates') {
$sDirectory = PATH_DATA_MAILTEMPLATES . $sProcessUID . PATH_SEP . $aData['file_name']; throw (new \Exception( 'invalid value specified for `prf_path`. Expecting `templates/` or `folder/`'));
}
if(strstr($aData['path'],'/')){
$sSubDirectory = substr($aData['path'], strpos($aData['path'], "/")+1) ;
} else {
$sSubDirectory = '';
}
switch ($sMainDirectory) {
case 'templates':
$sDirectory = PATH_DATA_MAILTEMPLATES . $sProcessUID . PATH_SEP . $sSubDirectory . $aData['file_name'];
$sCheckDirectory = PATH_DATA_MAILTEMPLATES . $sProcessUID . PATH_SEP . $sSubDirectory;
$sEditable = false; $sEditable = false;
break; break;
case 'folder/': case 'folder':
$sDirectory = PATH_DATA_PUBLIC . $sProcessUID . PATH_SEP . $aData['file_name']; $sDirectory = PATH_DATA_PUBLIC . $sProcessUID . PATH_SEP . $sSubDirectory . $aData['file_name'];
$sCheckDirectory = PATH_DATA_PUBLIC . $sProcessUID . PATH_SEP . $sSubDirectory;
break; break;
default: default:
$sDirectory = PATH_DATA_MAILTEMPLATES . $sProcessUID . PATH_SEP . $aData['file_name']; $sDirectory = PATH_DATA_MAILTEMPLATES . $sProcessUID . PATH_SEP . $sSubDirectory . $aData['file_name'];
break; break;
} }
} else {
throw (new \Exception( 'invalid value specified for `prf_path`. Expecting `templates/` or `folder/`'));
}
$extention = end(explode(".", $aData['file_name'])); $extention = end(explode(".", $aData['file_name']));
if ($extention == 'docx' || $extention == 'doc' || $extention == 'html' || $extention == 'php' || $extention == 'jsp' || if ($extention == 'docx' || $extention == 'doc' || $extention == 'html' || $extention == 'php' || $extention == 'jsp' ||
$extention == 'xlsx' || $extention == 'xls' || $extention == 'js' || $extention == 'css' || $extention == 'txt') { $extention == 'xlsx' || $extention == 'xls' || $extention == 'js' || $extention == 'css' || $extention == 'txt') {
@@ -137,6 +144,7 @@ class FilesManager
} else { } else {
$sEditable = false; $sEditable = false;
} }
\G::verifyPath($sCheckDirectory, true);
if (file_exists(PATH_SEP.$sDirectory)) { if (file_exists(PATH_SEP.$sDirectory)) {
throw (new \Exception( 'The file: '. $sDirectory . ' exists.')); throw (new \Exception( 'The file: '. $sDirectory . ' exists.'));
} }
@@ -225,22 +233,31 @@ class FilesManager
public function updateProcessFilesManager($sProcessUID, $userUID, $aData, $path) public function updateProcessFilesManager($sProcessUID, $userUID, $aData, $path)
{ {
try { try {
$arrayTaskUid = $this->getFileManagerUid($path); $path = rtrim($path, '/') . '/';
if ($aData['path'] == 'templates/' || $aData['path'] == 'folder/') { $sMainDirectory = current(explode("/", $path));
switch ($aData['path']) { if ($sMainDirectory != 'folder' && $sMainDirectory != 'templates') {
case 'templates/': throw (new \Exception( 'invalid value specified for `prf_path`. Expecting `templates/` or `folder/`'));
$sDirectory = PATH_DATA_MAILTEMPLATES . $sProcessUID . PATH_SEP . $aData['file_name']; }
if(strstr($path,'/')){
$sSubDirectory = substr($path, strpos($path, "/")+1) ;
} else {
$sSubDirectory = '';
}
switch ($sMainDirectory) {
case 'templates':
$sDirectory = PATH_DATA_MAILTEMPLATES . $sProcessUID . PATH_SEP . $sSubDirectory . $aData['file_name'];
$sEditable = false; $sEditable = false;
break; break;
case 'folder/': case 'folder':
$sDirectory = PATH_DATA_PUBLIC . $sProcessUID . PATH_SEP . $aData['file_name']; $sDirectory = PATH_DATA_PUBLIC . $sProcessUID . PATH_SEP . $sSubDirectory . $aData['file_name'];
break; break;
default: default:
$sDirectory = PATH_DATA_MAILTEMPLATES . $sProcessUID . PATH_SEP . $aData['file_name']; $sDirectory = PATH_DATA_MAILTEMPLATES . $sProcessUID . PATH_SEP . $aData['file_name'];
break; break;
} }
} else { $arrayTaskUid = $this->getFileManagerUid($sDirectory);
throw (new \Exception( 'invalid value specified for `prf_path`. Expecting `templates/` or `folder/`')); if (!$arrayTaskUid){
throw (new \Exception( 'invalid value specified for `path`.'));
} }
$extention = end(explode(".", $aData['file_name'])); $extention = end(explode(".", $aData['file_name']));
if ($extention == 'docx' || $extention == 'doc' || $extention == 'html' || $extention == 'php' || $extention == 'jsp' || if ($extention == 'docx' || $extention == 'doc' || $extention == 'html' || $extention == 'php' || $extention == 'jsp' ||
@@ -291,15 +308,48 @@ class FilesManager
public function deleteProcessFilesManager($sProcessUID, $path) public function deleteProcessFilesManager($sProcessUID, $path)
{ {
try { try {
$sPath = explode("/", $path); $sMainDirectory = current(explode("/", $path));
if ($sMainDirectory != 'folder' && $sMainDirectory != 'templates') {
throw (new \Exception( 'invalid value specified for `prf_path`. Expecting `templates/` or `folder/`'));
}
if ($sMainDirectory == 'folder') {
$sMainDirectory = 'public';
} else {
$sMainDirectory = 'mailTemplates';
}
$sfile = end(explode("/",$path)); $sfile = end(explode("/",$path));
$main = implode(array_slice($sPath, -3, 1)); $sSubDirectorytemp = substr($path, strpos($path, "/")+1);
$carpeta = ''; if(strstr($sSubDirectorytemp,'/')){
$sSubDirectory = str_replace('/'.$sfile,"",$sSubDirectorytemp);
$sSubDirectoryCheck = str_replace($sfile,"",$sSubDirectorytemp);
} else {
$sSubDirectory = '';
$sSubDirectoryCheck = '';
}
switch ($sMainDirectory) {
case 'mailTemplates':
$sDirectory = PATH_DATA_MAILTEMPLATES . $sProcessUID . PATH_SEP . $sSubDirectoryCheck . $sfile;
$sEditable = false;
break;
case 'public':
$sDirectory = PATH_DATA_PUBLIC . $sProcessUID . PATH_SEP . $sSubDirectoryCheck . $sfile;
break;
default:
$sDirectory = PATH_DATA_MAILTEMPLATES . $sProcessUID . PATH_SEP . $sfile;
break;
}
$arrayTaskUid = $this->getFileManagerUid($sDirectory);
if (!$arrayTaskUid){
throw (new \Exception( 'invalid value specified for `path`.'));
}
$oProcessMap = new \processMap(new \DBConnection()); $oProcessMap = new \processMap(new \DBConnection());
$oProcessMap->deleteFile($sProcessUID, $oProcessMap->deleteFile($sProcessUID,
$main, $sMainDirectory,
$carpeta, $sSubDirectory,
$sfile); $sfile);
$c = new \Criteria("workflow");
$c->add(\ProcessFilesPeer::PRF_PATH, $sDirectory, \Criteria::EQUAL);
$rs = \ProcessFilesPeer::doDelete($c);
} catch (Exception $e) { } catch (Exception $e) {
throw $e; throw $e;
} }
@@ -316,34 +366,54 @@ class FilesManager
public function downloadProcessFilesManager($sProcessUID, $path) public function downloadProcessFilesManager($sProcessUID, $path)
{ {
try { try {
$path = PATH_SEP.$path; $sMainDirectory = current(explode("/", $path));
//$url = http: if ($sMainDirectory != 'folder' && $sMainDirectory != 'templates') {
throw (new \Exception( 'invalid value specified for `prf_path`. Expecting `templates/` or `folder/`'));
if (file_exists($path)) {
$sfile = end(explode("/",$path));
\G::streamFile($path, true);
# open file to write
$fp = fopen ($path, 'w+');
# start curl
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
# set return transfer to false
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, false );
curl_setopt( $ch, CURLOPT_BINARYTRANSFER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
# increase timeout to download big file
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 10 );
# write data to local file
curl_setopt( $ch, CURLOPT_FILE, $fp );
# execute curl
curl_exec( $ch );
# close curl
curl_close( $ch );
# close local file
fclose( $fp );
if (filesize($path) > 0) return true;
} }
if ($sMainDirectory == 'folder') {
$sMainDirectory = 'public';
} else {
$sMainDirectory = 'mailTemplates';
}
$sfile = end(explode("/",$path));
$sSubDirectorytemp = substr($path, strpos($path, "/")+1);
if(strstr($sSubDirectorytemp,'/')){
$sSubDirectory = str_replace('/'.$sfile,"",$sSubDirectorytemp);
$sSubDirectoryCheck = str_replace($sfile,"",$sSubDirectorytemp);
} else {
$sSubDirectory = '';
$sSubDirectoryCheck = '';
}
switch ($sMainDirectory) {
case 'mailTemplates':
$sDirectory = PATH_DATA_MAILTEMPLATES . $sProcessUID . PATH_SEP . $sSubDirectoryCheck . $sfile;
$sEditable = false;
break;
case 'public':
$sDirectory = PATH_DATA_PUBLIC . $sProcessUID . PATH_SEP . $sSubDirectoryCheck . $sfile;
break;
default:
$sDirectory = PATH_DATA_MAILTEMPLATES . $sProcessUID . PATH_SEP . $sfile;
break;
}
$arrayTaskUid = $this->getFileManagerUid($sDirectory);
if (!$arrayTaskUid){
throw (new \Exception( 'invalid value specified for `path`.'));
}
/*
This is usefull when you are downloading big files, as it
will prevent time out of the script :
*/
set_time_limit(0);
ini_set('display_errors',true);//Just in case we get some errors, let us know....
$fp = fopen ($sDirectory, 'w+');//This is the file where we save the information
$ch = curl_init($sDirectory);//Here is the file we are downloading
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);
} catch (Exception $e) { } catch (Exception $e) {
throw $e; throw $e;
} }

View File

@@ -80,12 +80,12 @@ class FilesManager extends Api
/** /**
* @param string $prjUid {@min 32} {@max 32} * @param string $prjUid {@min 32} {@max 32}
* @param ProcessFilesManagerStructurePut $request_data
* @param string $path * @param string $path
* @param ProcessFilesManagerStructure1 $request_data
* *
* @url PUT /:prjUid/process-file-manager1 * @url PUT /:prjUid/process-file-manager
*/ */
public function doPutProcessFilesManager($prjUid, ProcessFilesManagerStructure1 $request_data, $path) public function doPutProcessFilesManager($prjUid, ProcessFilesManagerStructurePut $request_data, $path)
{ {
try { try {
$userUid = $this->getUserId(); $userUid = $this->getUserId();
@@ -116,7 +116,6 @@ class FilesManager extends Api
//response //response
throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()); throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage());
} }
return $response;
} }
/** /**
@@ -155,7 +154,8 @@ class ProcessFilesManagerStructure
public $content; public $content;
} }
class ProcessFilesManagerStructure1
class ProcessFilesManagerStructurePut
{ {
/** /**
* @var string {@from body} * @var string {@from body}
@@ -167,3 +167,4 @@ class ProcessFilesManagerStructure1
*/ */
public $content; public $content;
} }