Skip to content

Commit

Permalink
Merge pull request #16 from cBackup/developer
Browse files Browse the repository at this point in the history
Download arbitrary config file revision from git commit history modal
  • Loading branch information
launcherx authored Feb 27, 2018
2 parents 6144bd3 + 27a83d7 commit 01e15e6
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 31 deletions.
39 changes: 35 additions & 4 deletions controllers/NodeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function behaviors()
'class' => AjaxFilter::className(),
'only' => [
'inquire',
'ajax-download',
'ajax-load-config',
'ajax-load-file-diff',
'ajax-set-auth-template',
Expand All @@ -90,7 +91,6 @@ public function behaviors()
'ajax-backup-node',
'ajax-set-node-credentials',
'ajax-protect-node',

]
]
];
Expand Down Expand Up @@ -409,17 +409,31 @@ public function actionAjaxLoadFileDiff()
/**
* @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, $crlf = false)
public function actionDownload($id, $put, $hash = null, $crlf = false)
{

$config = '';
$suffix = null;

/** Get configuration backup based on put */
if ($put == 'file') {
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);
}
Expand All @@ -428,20 +442,37 @@ public function actionDownload($id, $put, $crlf = false)
}
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.txt", [
return Yii::$app->response->sendContentAsFile($config, "$id.conf{$suffix}.txt", [
'mimeType' => 'text/plain',
'inline' => false,
]);

}


/**
* @param int $id
* @param string $put
* @param string|null $hash
* @return string
*/
public function actionAjaxDownload($id, $put, $hash = null)
{
return $this->renderPartial('_download_modal', [
'id' => $id,
'put' => $put,
'hash' => $hash
]);
}


/**
* @throws HttpException
* @return string
Expand Down
50 changes: 50 additions & 0 deletions views/node/_download_modal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* This file is part of cBackup, network equipment configuration backup tool
* Copyright (C) 2018, Oļegs Čapligins, Imants Černovs, Dmitrijs Galočkins
*
* cBackup is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use yii\helpers\Html;

/**
* @var $id int
* @var $put string
* @var $hash string|null
*/
?>
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header bg-light-blue-active">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span></button>
<h4 class="modal-title"><?= Yii::t('app', 'Choose end of line format') ?></h4>
</div>
<div class="modal-body">
<div class="row">
<div class="col-xs-12">
<?php
echo Html::a('&nbsp;&nbsp;LF&nbsp;&nbsp;', ['download', 'id' => $id, 'put' => $put, 'hash' => $hash], [
'class' => 'btn btn-primary pull-left',
'onclick' => '(function() { $("#download_modal").modal("hide"); })();'
]);
echo Html::a('CRLF', ['download', 'id' => $id, 'put' => $put, 'hash' => $hash, 'crlf' => true], [
'class' => 'btn btn-primary pull-right',
'onclick' => '(function() { $("#download_modal").modal("hide"); })();'
]);
?>
</div>
</div>
</div>
</div>
</div>
63 changes: 38 additions & 25 deletions views/node/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -842,11 +842,19 @@
]);
}

echo Html::a('<i class="fa fa-download"></i> ' . Yii::t('app', 'Download'), null, [
'class' => 'btn btn-xs btn-default ' . $disabled,
'data-toggle' => 'modal',
'data-target' => '#download_modal',
])
echo Html::a('<i class="fa fa-download"></i> ' . Yii::t('app', 'Download'), Url::to(['ajax-download',
'id' => $data->id,
'put' => $task_info->put,
'hash' => null
]), [
'class' => 'btn btn-xs btn-default ' . $disabled,
'title' => Yii::t('app', 'Download'),
'data-dismiss' => 'modal',
'data-toggle' => 'modal',
'data-target' => '#download_modal',
'data-backdrop' => 'static',
'data-keyboard' => 'false'
]);
?>
</td>
</tr>
Expand Down Expand Up @@ -924,24 +932,15 @@
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header bg-light-blue-active">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span></button>
<h4 class="modal-title"><?= Yii::t('app', 'Choose end of line format') ?></h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span></button>
<h4 class="modal-title"><?= Yii::t('app', 'Wait...') ?></h4>
</div>
<div class="modal-body">
<div class="row">
<div class="col-xs-12">
<?php
echo Html::a('&nbsp;&nbsp;LF&nbsp;&nbsp;', ['download', 'id' => $data->id, 'put' => $task_info->put], [
'class' => 'btn btn-primary pull-left',
'onclick' => '(function() { $("#download_modal").modal("hide"); })();'
]);
echo Html::a('CRLF', ['download', 'id' => $data->id, 'put' => $task_info->put, 'crlf' => true], [
'class' => 'btn btn-primary pull-right',
'onclick' => '(function() { $("#download_modal").modal("hide"); })();'
]);
?>
</div>
</div>
<span style="margin-left: 24%;"><?= Html::img('@web/img/modal_loading.gif', ['alt' => Yii::t('app', 'Loading...')]) ?></span>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal"><?= Yii::t('app', 'Close') ?></button>
</div>
</div>
</div>
Expand Down Expand Up @@ -983,18 +982,32 @@
<td class="hide-overflow"><?= $log[1] ?></td>
<td><?= $log[2] ?></td>
<td><?= preg_replace("/(\s+\+\d*)/i", '', $log[3]) ?></td>
<td>
<td style="white-space: nowrap">
<?php
echo Html::a('<i class="fa fa-eye"></i>', null, [
'id' => 'reload_history',
echo Html::a('<i class="fa fa-save"></i>', Url::to(['ajax-download',
'id' => $data->id,
'put' => $task_info->put,
'hash' => $log[0],
]), [
'style' => 'cursor:pointer;',
'title' => Yii::t('app', 'Download'),
'data-dismiss' => 'modal',
'data-toggle' => 'modal',
'data-target' => '#download_modal',
'data-backdrop' => 'static',
'data-keyboard' => 'false'
]);
echo '&nbsp;';
echo Html::a('<i class="fa fa-eye"></i>', 'javascript:;', [
'class' => 'reload_history',
'data-url' => Url::to(['ajax-load-file-diff']),
'style' => 'cursor:pointer;',
'title' => Yii::t('app', 'View'),
'data-params' => json_encode([
'node_id' => $data->id,
'hash' => $log[0],
])
])
]);
?>
</td>
</tr>
Expand Down
4 changes: 2 additions & 2 deletions web/js/system/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ $(document).on('click', '#show_config', function () {
});

/** Load git log for current node */
$(document).on('click', '#show_history, #reload_history', function () {
if (diff_content.is(':hidden') || this.id === 'reload_history') {
$(document).on('click', '#show_history, .reload_history', function () {
if (diff_content.is(':hidden') || this.className === 'reload_history') {
$.ajax({
url : $(this).data('url'),
type : 'post',
Expand Down

0 comments on commit 01e15e6

Please sign in to comment.