From 4b23b687d61158fe4b2dee7c75300780b05f187f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bjo=CC=88rn=20Dahlgren?= Date: Wed, 4 Oct 2017 01:04:09 +0200 Subject: [PATCH] Add delete button to log files list --- lib/logs.js | 18 ++++++++++++++++++ public/js/app/models/log.js | 3 ++- public/js/app/views/logs/list_item.js | 20 ++++++++++++++++++++ public/js/tpl/logs/list.html | 1 + public/js/tpl/logs/list_item.html | 6 ++++++ routes/logs.js | 11 +++++++++++ 6 files changed, 58 insertions(+), 1 deletion(-) diff --git a/lib/logs.js b/lib/logs.js index 681de6c..85a97a1 100644 --- a/lib/logs.js +++ b/lib/logs.js @@ -28,6 +28,24 @@ Logs.generateLogFileName = function () { return 'arma3server_' + dateStr + '.log' } +Logs.prototype.delete = function (filename, callback) { + this.getLogFile(filename, function (err, logFile) { + if (err) { + if (callback) { + return callback(err) + } + } else { + if (logFile && logFile.path) { + fs.unlink(logFile.path, callback) + } else { + if (callback) { + return callback(new Error('File not found')) + } + } + } + }) +} + Logs.prototype.generateLogFilePath = function () { return path.join(this.logsPath(), Logs.generateLogFileName()) } diff --git a/public/js/app/models/log.js b/public/js/app/models/log.js index 41dc7a0..2f538a7 100644 --- a/public/js/app/models/log.js +++ b/public/js/app/models/log.js @@ -11,7 +11,8 @@ define(function (require) { name: '', formattedSize: '0 B', size: 0, - } + }, + idAttribute: 'name', }); }); diff --git a/public/js/app/views/logs/list_item.js b/public/js/app/views/logs/list_item.js index cccf971..d15872d 100644 --- a/public/js/app/views/logs/list_item.js +++ b/public/js/app/views/logs/list_item.js @@ -6,6 +6,7 @@ define(function (require) { _ = require('underscore'), Backbone = require('backbone'), Marionette = require('marionette'), + swal = require('sweet-alert'), tpl = require('text!tpl/logs/list_item.html'), template = _.template(tpl); @@ -13,5 +14,24 @@ define(function (require) { return Marionette.ItemView.extend({ tagName: "tr", template: template, + + events: { + "click .destroy": "destroy", + }, + + destroy: function (event) { + var self = this; + sweetAlert({ + title: "Are you sure?", + text: "The log will be deleted from the server!", + type: "warning", + showCancelButton: true, + confirmButtonClass: "btn-danger", + confirmButtonText: "Yes, delete it!", + }, + function(){ + self.model.destroy(); + }); + }, }); }); diff --git a/public/js/tpl/logs/list.html b/public/js/tpl/logs/list.html index 149982f..2f34c62 100644 --- a/public/js/tpl/logs/list.html +++ b/public/js/tpl/logs/list.html @@ -4,6 +4,7 @@ Filename Size + diff --git a/public/js/tpl/logs/list_item.html b/public/js/tpl/logs/list_item.html index 466cd2b..ea46def 100644 --- a/public/js/tpl/logs/list_item.html +++ b/public/js/tpl/logs/list_item.html @@ -15,3 +15,9 @@ Download + + + + Delete + + diff --git a/routes/logs.js b/routes/logs.js index 06289d0..d86a5ff 100644 --- a/routes/logs.js +++ b/routes/logs.js @@ -13,6 +13,17 @@ module.exports = function (logsManager) { }) }) + router.delete('/:log', function (req, res) { + var filename = req.params.log + logsManager.delete(filename, function (err) { + if (err) { + res.status(500).send(err) + } else { + res.status(204).send() + } + }) + }) + router.get('/:log/:mode', function (req, res) { var requestedFilename = req.params.log var mode = req.params.mode === 'view' ? 'view' : 'download'