Permalink
Browse files

Code clean, refactor download-cart action.

  • Loading branch information...
1 parent a04c85e commit 47331a1fbaff15a9cdaa9b38300c9ad3a4140750 @cdujeu cdujeu committed May 31, 2016
@@ -521,7 +521,6 @@ public static function sendMessage($logMessage, $errorMessage, $print = true)
* Extract all the user data and put it in XML
* @static
* @param AbstractAjxpUser|null $userObject
- * @internal param bool $details
* @return string
*/
public static function getUserXML($userObject = null)
@@ -23,10 +23,13 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Pydio\Core\Exception\AuthRequiredException;
+use Pydio\Core\Exception\PydioException;
use Pydio\Core\Exception\WorkspaceNotFoundException;
use Pydio\Core\Http\Middleware\ITopLevelMiddleware;
use Pydio\Core\Http\Response\SerializableResponseStream;
use Pydio\Core\Http\Server;
+use Pydio\Tasks\Task;
+use Pydio\Tasks\TaskService;
use Symfony\Component\Console\Output\OutputInterface;
defined('AJXP_EXEC') or die('Access not allowed');
@@ -49,6 +52,7 @@ public function handleRequest(ServerRequestInterface $requestInterface, Response
$output = $requestInterface->getAttribute("cli-output");
$options = $requestInterface->getAttribute("cli-options");
$statusFile = (!empty($options["s"]) ? $options["s"] : false);
+ $taskId = $requestInterface->getAttribute("pydio-task-id");
try {
@@ -62,12 +66,28 @@ public function handleRequest(ServerRequestInterface $requestInterface, Response
if($statusFile !== false){
file_put_contents($statusFile, "ERROR:Authentication Failed.");
}
+ if(!empty($taskId)){
+ TaskService::getInstance()->updateTaskStatus($taskId, Task::STATUS_FAILED, "Authentication Failed");
+ }
- }catch (\Exception $e){
+ } catch (PydioException $e){
$output->writeln("ERROR:".$e->getMessage());
if($statusFile !== false){
- file_put_contents($statusFile, "ERROR:Authentication Failed.");
+ file_put_contents($statusFile, "ERROR:".$e->getMessage());
+ }
+ if(!empty($taskId)){
+ TaskService::getInstance()->updateTaskStatus($taskId, Task::STATUS_FAILED, $e->getMessage());
+ }
+
+ } catch (\Exception $e){
+
+ $output->writeln("ERROR:".$e->getMessage());
+ if($statusFile !== false){
+ file_put_contents($statusFile, "ERROR:".$e->getMessage());
+ }
+ if(!empty($taskId)){
+ TaskService::getInstance()->updateTaskStatus($taskId, Task::STATUS_FAILED, $e->getMessage());
}
}
@@ -59,6 +59,13 @@ public function addChunk($chunk){
}
/**
+ * @return SerializableResponseChunk[]
+ */
+ public function getChunks(){
+ return $this->data;
+ }
+
+ /**
* @return string
*/
public function getContents()
@@ -694,8 +694,8 @@ public function switchAction(ServerRequestInterface &$request, ResponseInterface
if($request->getAttribute("pydio-task-id") === null){
$task = TaskService::actionAsTask($action, $httpVars);
$task->setFlags(Task::FLAG_STOPPABLE);
- TaskService::getInstance()->enqueueTask($task, $request, $response);
- return;
+ $response = TaskService::getInstance()->enqueueTask($task, $request, $response);
+ break;
}
if($taskId !== null){
@@ -828,8 +828,8 @@ public function switchAction(ServerRequestInterface &$request, ResponseInterface
if($taskId === null && (!$selection->isUnique() || !is_file($selection->getUniqueNode()->getUrl()))){
$task = TaskService::actionAsTask($action, $httpVars);
$task->setFlags(Task::FLAG_STOPPABLE);
- TaskService::getInstance()->enqueueTask($task, $request, $response);
- return;
+ $response = TaskService::getInstance()->enqueueTask($task, $request, $response);
+ break;
}
if(!empty($taskId)){
TaskService::getInstance()->updateTaskStatus($taskId, Task::STATUS_RUNNING, "Starting operation in background");
@@ -28,7 +28,6 @@
use Pydio\Access\Core\Model\AJXP_Node;
use Pydio\Access\Core\RecycleBinManager;
use Pydio\Access\Driver\StreamProvider\FS\fsAccessDriver;
-use Pydio\Core\Http\Message\BgActionTrigger;
use Pydio\Core\Http\Response\SerializableResponseStream;
use Pydio\Core\Services\AuthService;
use Pydio\Core\Services\ConfService;
@@ -142,7 +141,7 @@ public function uploadActions(ServerRequestInterface &$request, ResponseInterfac
if ($nextFile!='') {
//$x->addChunk(new BgActionTrigger("next_to_remote", array(), "Copying file ".TextEncoder::toUTF8($nextFile)." to remote server"));
$newTask = TaskService::actionAsTask("next_to_remote", []);
- TaskService::getInstance()->enqueueTask($newTask, $request, $response);
+ $response = TaskService::getInstance()->enqueueTask($newTask, $request, $response);
} else {
//$x->addChunk(new BgActionTrigger("reload_node", array(), "Upload done, reloading client."));
TaskService::getInstance()->updateTaskStatus($taskId, Task::STATUS_COMPLETE, "");
@@ -35,10 +35,20 @@ public function switchAction (\Psr\Http\Message\ServerRequestInterface &$request
$indexer = PluginsService::getInstance()->getUniqueActivePluginForType("index");
if($indexer == false) return;
- $httpVars["return_selection"] = true;
+ $httpVars = $requestInterface->getParsedBody();
unset($httpVars["get_action"]);
$requestInterface = $requestInterface->withAttribute("action", "search")->withParsedBody($httpVars);
- $res = Controller::run($requestInterface);
+ $response = Controller::run($requestInterface);
+ $body = $response->getBody();
+ if($body instanceof \Pydio\Core\Http\Response\SerializableResponseStream){
+ $chunks = $body->getChunks();
+ foreach($chunks as $chunk){
+ if($chunk instanceof \Pydio\Access\Core\Model\NodesList){
+ $res = $chunk->getChildren();
+ }
+ }
+ }
+
if (isSet($res) && is_array($res)) {
$newHttpVars = array(
@@ -47,7 +57,7 @@ public function switchAction (\Psr\Http\Message\ServerRequestInterface &$request
"archive_name" => $httpVars["archive_name"]
);
$requestInterface = $requestInterface->withAttribute("action", "download")->withParsedBody($newHttpVars);
- Controller::run($requestInterface);
+ $responseInterface = Controller::run($requestInterface);
}
@@ -60,22 +60,7 @@ public function receiveAction(\Psr\Http\Message\ServerRequestInterface &$request
$currentDirPath = Utils::safeDirname($userSelection->getUniqueNode()->getPath());
$currentDirPath = rtrim($currentDirPath, "/") . "/";
$currentDirUrl = $userSelection->currentBaseUrl().$currentDirPath;
-
- if (empty($httpVars["compression_id"])) {
- $compressionId = sha1(rand());
- $httpVars["compression_id"] = $compressionId;
- } else {
- $compressionId = $httpVars["compression_id"];
- }
- $progressCompressionFileName = $this->getPluginCacheDir(false, true) . DIRECTORY_SEPARATOR . "progressCompressionID-" . $compressionId . ".txt";
- if (empty($httpVars["extraction_id"])) {
- $extractId = sha1(rand());
- $httpVars["extraction_id"] = $extractId;
- } else {
- $extractId = $httpVars["extraction_id"];
- }
- $progressExtractFileName = $this->getPluginCacheDir(false, true) . DIRECTORY_SEPARATOR . "progressExtractID-" . $extractId . ".txt";
-
+
$serializableStream = new \Pydio\Core\Http\Response\SerializableResponseStream();
$responseInterface = $responseInterface->withBody($serializableStream);
@@ -102,14 +87,13 @@ public function receiveAction(\Psr\Http\Message\ServerRequestInterface &$request
if(empty($taskId)){
$task = TaskService::actionAsTask("compression", $httpVars, $repository->getId(), "", [], Task::FLAG_STOPPABLE | Task::FLAG_HAS_PROGRESS);
$task->setLabel($messages["compression.5"]);
- file_put_contents($progressCompressionFileName, $messages["compression.5"]);
- TaskService::getInstance()->enqueueTask($task, $requestInterface, $responseInterface);
- return;
+ $responseInterface = TaskService::getInstance()->enqueueTask($task, $requestInterface, $responseInterface);
+ break;
}
$task = TaskService::getInstance()->getTaskById($taskId);
- $postMessageStatus = function($message, $taskStatus, $progress = null) use($progressCompressionFileName, $task){
- $this->operationStatus($progressCompressionFileName, $task, $message, $taskStatus, $progress);
+ $postMessageStatus = function($message, $taskStatus, $progress = null) use($task){
+ $this->operationStatus($task, $message, $taskStatus, $progress);
};
$maxAuthorizedSize = 4294967296;
@@ -189,37 +173,6 @@ public function receiveAction(\Psr\Http\Message\ServerRequestInterface &$request
break;
- case "check_compression_status":
-
- $archivePath = Utils::decodeSecureMagic($httpVars["archive_path"]);
- $progressCompression = file_get_contents($progressCompressionFileName);
- $substrProgressCompression = substr($progressCompression, 0, 5);
- if ($progressCompression != "SUCCESS" && $substrProgressCompression != "Error") {
-
- $serializableStream->addChunk(new BgActionTrigger("check_compression_status", array(
- "repository_id" => $repository->getId(),
- "compression_id" => $compressionId,
- "archive_path" => TextEncoder::toUTF8($archivePath)
- ), $progressCompression, 5));
-
- } elseif ($progressCompression == "SUCCESS") {
- $newNode = new AJXP_Node($userSelection->currentBaseUrl() . $archivePath);
- $nodesDiffs = new NodesDiff();
- $nodesDiffs->add($newNode);
- Controller::applyHook("node.change", array(null, $newNode, false));
- $serializableStream->addChunk(new \Pydio\Core\Http\Message\UserMessage($messages["compression.8"]));
- $serializableStream->addChunk($nodesDiffs);
- if (file_exists($progressCompressionFileName)) {
- unlink($progressCompressionFileName);
- }
- } elseif ($substrProgressCompression == "Error") {
- if (file_exists($progressCompressionFileName)) {
- unlink($progressCompressionFileName);
- }
- throw new PydioException($progressCompression);
- }
- break;
-
case "extraction":
$fileArchive = Utils::sanitize(Utils::decodeSecureMagic($httpVars["file"]), AJXP_SANITIZE_DIRNAME);
@@ -261,13 +214,12 @@ public function receiveAction(\Psr\Http\Message\ServerRequestInterface &$request
if(empty($taskId)){
$task = TaskService::actionAsTask("extraction", $httpVars, $repository->getId(), "", [], Task::FLAG_STOPPABLE | Task::FLAG_HAS_PROGRESS);
$task->setLabel($messages["compression.12"]);
- file_put_contents($progressExtractFileName, $messages["compression.12"]);
- TaskService::getInstance()->enqueueTask($task, $requestInterface, $responseInterface);
- return;
+ $responseInterface = TaskService::getInstance()->enqueueTask($task, $requestInterface, $responseInterface);
+ break;
}
$task = TaskService::getInstance()->getTaskById($taskId);
- $postMessageStatus = function($message, $taskStatus, $progress = null) use($progressExtractFileName, $task){
- $this->operationStatus($progressExtractFileName, $task, $message, $taskStatus, $progress);
+ $postMessageStatus = function($message, $taskStatus, $progress = null) use($task){
+ $this->operationStatus($task, $message, $taskStatus, $progress);
};
mkdir($currentDirUrl . $onlyFileName, 0777, true);
@@ -306,66 +258,20 @@ public function receiveAction(\Psr\Http\Message\ServerRequestInterface &$request
Controller::run($indexRequest);
break;
- case "check_extraction_status":
-
- $currentDirUrl = $httpVars["currentDirUrl"];
- $onlyFileName = $httpVars["onlyFileName"];
- $progressExtract = file_get_contents($progressExtractFileName);
- $substrProgressExtract = substr($progressExtract, 0, 5);
- if ($progressExtract != "SUCCESS" && $progressExtract != "INDEX" && $substrProgressExtract != "Error") {
-
- $serializableStream->addChunk(new BgActionTrigger("check_extraction_status", array(
- "repository_id" => $repository->getId(),
- "extraction_id" => $extractId,
- "currentDirUrl" => $currentDirUrl,
- "onlyFileName" => $onlyFileName
- ), $progressExtract, 4));
-
- } elseif ($progressExtract == "SUCCESS") {
- $newNode = new AJXP_Node($currentDirUrl . $onlyFileName);
- $nodesDiffs = new NodesDiff();
- $nodesDiffs->add($newNode);
- Controller::applyHook("node.change", array(null, $newNode, false));
-
- $serializableStream->addChunk(new \Pydio\Core\Http\Message\UserMessage(sprintf($messages["compression.14"], $onlyFileName)));
- $serializableStream->addChunk(new BgActionTrigger("check_index_status", array(
- "repository_id" => $newNode->getRepositoryId()
- ), "starting indexation", 5));
- $serializableStream->addChunk($nodesDiffs);
-
- if (file_exists($progressExtractFileName)) {
- unlink($progressExtractFileName);
- }
-
- } elseif ($substrProgressExtract == "Error") {
- if (file_exists($progressExtractFileName)) {
- unlink($progressExtractFileName);
- }
- throw new PydioException($progressExtract);
- }
- break;
default:
break;
}
}
/**
- * @param string $progressCompressionFileName
* @param Task $task
* @param string $message
* @param integer $taskStatus
* @param null|integer $progress
*/
- private function operationStatus($progressCompressionFileName, $task, $message, $taskStatus, $progress = null)
+ private function operationStatus($task, $message, $taskStatus, $progress = null)
{
- $fileMessage = $message;
- if ($taskStatus == Task::STATUS_FAILED) {
- $fileMessage = "Error : " . $message;
- } else if ($taskStatus == Task::STATUS_COMPLETE) {
- $fileMessage = "SUCCESS";
- }
- file_put_contents($progressCompressionFileName, $fileMessage);
$task->setStatusMessage($message);
$task->setStatus($taskStatus);
if ($progress != null) {
@@ -107,11 +107,6 @@
]]></clientForm>
</processing>
</action>
- <action name="check_compression_status">
- <processing>
- <serverCallback methodName="receiveAction"/>
- </processing>
- </action>
<action name="extraction">
<gui text="compression.9" title="compression.10" src="archive_insert.png" iconClass="icon-briefcase" accessKey="" hasAccessKey="false">
<context selection="true" dir="true" recycle="hidden"
@@ -138,11 +133,6 @@
<serverCallback methodName="receiveAction" pluginId="action.compression" />
</processing>
</action>
- <action name="check_extraction_status">
- <processing>
- <serverCallback methodName="receiveAction"/>
- </processing>
- </action>
</actions>
</registry_contributions>
<class_definition filename="plugins/action.compression/class.PluginCompression.php" classname="PluginCompression"/>
@@ -130,7 +130,7 @@ public function crossRepositoryCopy(ServerRequestInterface &$requestInterface, R
$taskId = $requestInterface->getAttribute("pydio-task-id");
if(empty($taskId)){
$task = TaskService::actionAsTask("cross_copy", $httpVars);
- TaskService::getInstance()->enqueueTask($task, $requestInterface, $responseInterface);
+ $responseInterface = TaskService::getInstance()->enqueueTask($task, $requestInterface, $responseInterface);
return;
}
@@ -61,6 +61,13 @@ public function addBranch($nodeOrList){
}
}
+ /**
+ * @return array
+ */
+ public function getChildren(){
+ return $this->children;
+ }
+
public function setPaginationData($count, $currentPage, $totalPages, $dirsCount = -1, $remoteSortAttributes = null){
$this->paginationData = [
'count' => $count,
Oops, something went wrong.

0 comments on commit 47331a1

Please sign in to comment.