Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 60 additions & 8 deletions modules/v2/controllers/NodeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

use app\helpers\ApiHelper;
use app\models\Node;
use app\models\OutBackup;
use app\models\OutStp;
use Yii;
use yii\rest\Controller;
Expand Down Expand Up @@ -97,7 +98,7 @@ public function behaviors()
'rules' => [
[
'allow' => true,
'actions' => ['search', 'list', 'get', 'get-nodes', 'lookup', 'get-stp', 'interim'],
'actions' => ['search', 'list', 'get', 'get-nodes', 'lookup', 'get-stp', 'interim', 'download-backup'],
'roles' => ['APIReader', 'admin'],
],
],
Expand All @@ -106,13 +107,14 @@ public function behaviors()
$behaviors['verbs'] = [
'class' => VerbFilter::class,
'actions' => [
'list' => ['get'],
'get' => ['get'],
'get-nodes' => ['get'],
'get-stp' => ['get'],
'interim' => ['post'],
'search' => ['post'],
'lookup' => ['post'],
'list' => ['get'],
'get' => ['get'],
'get-nodes' => ['get'],
'get-stp' => ['get'],
'interim' => ['post'],
'search' => ['post'],
'lookup' => ['post'],
'download-backup' => ['get'],
],
];

Expand Down Expand Up @@ -359,5 +361,55 @@ public function actionInterim(): array
return $nodes;

}

/**
* @param $id
* @param string $put
* @param string|null $hash
* @param bool $crlf
* @return Response
* @throws \yii\web\RangeNotSatisfiableHttpException
* @throws \yii\base\ExitException
*/
public function actionDownload($id, $put, $hash = null, $crlf = false)
{

$config = '';
$suffix = null;

/** Get configuration backup based on put */
if(!empty($hash)) {

$meta = Node::getCommitMetaData($hash);
$config = Node::getBackupGitVersion($id, $hash);

if( array_key_exists(3, $meta) ) {
$suffix = preg_replace(['/:/', '/[^\d|\-]/'], ['-', '_'], $meta[3]);
$suffix = ".".substr($suffix, 0, -7);
}

}
elseif ($put == 'file') {
$file_path = \Y::param('dataPath') . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . "{$id}.txt";
$config = file_get_contents($file_path);
}
elseif ($put == 'db') {
$config = OutBackup::find()->select('config')->where(['node_id' => $id])->scalar();
}
else {
\Y::flashAndRedirect('warning', Yii::t('node', 'Unknown backup destination passed'), 'node/view', ['id' => $id]);
Yii::$app->end();
}

if( isset($crlf) && $crlf == true ) {
$config = preg_replace('~\R~u', "\r\n", $config);
}

return Yii::$app->response->sendContentAsFile($config, "$id.conf{$suffix}.txt", [
'mimeType' => 'text/plain',
'inline' => false,
]);

}

}