Skip to content

Commit

Permalink
#53 Add report template import feature
Browse files Browse the repository at this point in the history
  • Loading branch information
nadouani committed Dec 9, 2016
1 parent 00c2c6a commit aea1124
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 2 deletions.
2 changes: 1 addition & 1 deletion thehive-cortex/app/controllers/ReportTemplate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class ReportTemplateCtrl @Inject() (
}
val importedReportTemplates: Seq[Future[(String, JsBoolean)]] = zipFile.getFileHeaders.toSeq.filter(_ != null).collect {
case fileHeader: FileHeader if !fileHeader.isDirectory
val Array(analyzerId, flavor) = (fileHeader.getFileName + "/").split("/", 2)
val Array(analyzerId, flavor, _*) = (fileHeader.getFileName + "/").split("/", 3)
val inputStream = zipFile.getInputStream(fileHeader)
val content = Source.fromInputStream(inputStream).mkString
inputStream.close()
Expand Down
34 changes: 33 additions & 1 deletion ui/app/scripts/controllers/admin/AdminReportTemplatesCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

angular.module('theHiveControllers')
.controller('AdminReportTemplatesCtrl', AdminReportTemplatesCtrl)
.controller('AdminReportTemplateDialogCtrl', AdminReportTemplateDialogCtrl);
.controller('AdminReportTemplateDialogCtrl', AdminReportTemplateDialogCtrl)
.controller('AdminReportTemplateImportCtrl', AdminReportTemplateImportCtrl);


function AdminReportTemplatesCtrl($q, $modal, AnalyzerSrv, ReportTemplateSrv) {
Expand Down Expand Up @@ -53,6 +54,20 @@
});
};

this.import = function (analyzer, dataType) {
var modalInstance = $modal.open({
animation: true,
templateUrl: 'views/partials/admin/report-template-import.html',
controller: 'AdminReportTemplateImportCtrl',
controllerAs: 'vm',
size: 'lg'
});

modalInstance.result.then(function() {
self.load();
});
};

this.load();
};

Expand Down Expand Up @@ -85,4 +100,21 @@


}

function AdminReportTemplateImportCtrl($modalInstance, ReportTemplateSrv, AlertSrv) {
this.formData = {};

this.ok = function () {
ReportTemplateSrv.import(this.formData)
.then(function() {
$modalInstance.close();
}, function(response) {
AlertSrv.error('AdminReportTemplateImportCtrl', response.data, response.status);
});
};

this.cancel = function () {
$modalInstance.dismiss('cancel');
};
}
})();
32 changes: 32 additions & 0 deletions ui/app/scripts/services/ReportTemplateSrv.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,38 @@
} else {
return $http.post(baseUrl, tpl, {});
}
},

import: function(post) {
var postData = {
templates: post.attachment
};

return $http({
method: 'POST',
url: baseUrl + '/_import',
headers: {
'Content-Type': undefined
},
transformRequest: function (data) {
var formData = new FormData(),
copy = angular.copy(data, {}),
_json = {};

angular.forEach(data, function (value, key) {
if (Object.getPrototypeOf(value) instanceof Blob || Object.getPrototypeOf(value) instanceof File) {
formData.append(key, value);
delete copy[key];
}
});

//formData.append("attributes", angular.toJson(_json));

return formData;
},
data: postData

});
}
}

Expand Down
20 changes: 20 additions & 0 deletions ui/app/views/partials/admin/report-template-import.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<form class="form-horizontal" name="form" ng-submit="vm.ok()">
<div class="modal-header bg-primary">
<h3 class="modal-title">Import report templates</h3>
</div>
<div class="modal-body">
<div class="form-group">
<label class="col-sm-2 control-label">Templates archive</label>
<div class="col-sm-10">
<input type="hidden" name="attachment" ng-model="vm.formData.attachment.status" required>
<div file-chooser="" filemodel="vm.formData.attachment"></div>
</div>
</div>

<pre>{{vm.formData | json}}</pre>
</div>
<div class="modal-footer">
<button class="btn btn-warning pull-left" type="button" ng-click="vm.cancel()">Cancel</button>
<button class="btn btn-primary pull-right" type="submit" ng-disabled="form.$invalid">Yes, Import template archive</button>
</div>
</form>
12 changes: 12 additions & 0 deletions ui/app/views/partials/admin/report-templates.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
<h2>Report template management</h2>
<div class="vpad10"></div>
<div class="row mb-s">
<div class="col-md-12 clearfix">
<div class="pull-left">
<!-- Single button -->
<div class="btn-group">
<button type="button" class="btn btn-primary" ng-click="vm.import()">
<span class="fa fa-upload"></span> Import templates
</button>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<table class="table table-striped">
Expand Down

0 comments on commit aea1124

Please sign in to comment.