FEATURE: upload backups

This commit is contained in:
Régis Hanol
2014-02-22 01:41:01 +01:00
parent 23066edbe1
commit 68a935c36b
16 changed files with 1093 additions and 25 deletions
@@ -0,0 +1,119 @@
/*global Resumable:true */
/**
Example usage:
{{resumable-upload
target="/admin/backups/upload"
success="successAction"
error="errorAction"
uploadText="UPLOAD"
}}
@class ResumableUploadComponent
@extends Ember.Component
@namespace Discourse
@module Discourse
**/
Discourse.ResumableUploadComponent = Ember.Component.extend({
tagName: "button",
classNames: ["btn", "ru"],
classNameBindings: ["isUploading"],
resumable: null,
isUploading: false,
progress: 0,
shouldRerender: Discourse.View.renderIfChanged("isUploading", "progress"),
text: function() {
if (this.get("isUploading")) {
return this.get("progress") + " %";
} else {
return this.get("uploadText");
}
}.property("isUploading", "progress"),
render: function(buffer) {
var icon = this.get("isUploading") ? "times" : "upload";
buffer.push("<i class='fa fa-" + icon + "'></i>");
buffer.push("<span class='ru-label'>" + this.get("text") + "</span>");
buffer.push("<span class='ru-progress' style='width:" + this.get("progress") + "%'></span>");
},
click: function() {
if (this.get("isUploading")) {
this.resumable.cancel();
var self = this;
Em.run.later(function() { self._reset(); });
return false;
} else {
return true;
}
},
_reset: function() {
this.setProperties({ isUploading: false, progress: 0 });
},
_initialize: function() {
this.resumable = new Resumable({
target: this.get("target"),
maxFiles: 1, // only 1 file at a time
headers: { "X-CSRF-Token": $("meta[name='csrf-token']").attr("content") }
});
var self = this;
this.resumable.on("fileAdded", function() {
// automatically upload the selected file
self.resumable.upload();
// mark as uploading
Em.run.later(function() {
self.set("isUploading", true);
});
});
this.resumable.on("fileProgress", function(file) {
// update progress
Em.run.later(function() {
self.set("progress", parseInt(file.progress() * 100, 10));
});
});
this.resumable.on("fileSuccess", function(file) {
Em.run.later(function() {
// mark as not uploading anymore
self._reset();
// fire an event to allow the parent route to reload its model
self.sendAction("success", file.fileName);
});
});
this.resumable.on("fileError", function(file, message) {
Em.run.later(function() {
// mark as not uploading anymore
self._reset();
// fire an event to allow the parent route to display the error message
self.sendAction("error", file.fileName, message);
});
});
}.on("init"),
_assignBrowse: function() {
var self = this;
Em.run.schedule("afterRender", function() {
self.resumable.assignBrowse(self.$());
});
}.on("didInsertElement"),
_teardown: function() {
if (this.resumable) {
this.resumable.cancel();
this.resumable = null;
}
}.on("willDestroyElement")
});
@@ -1 +1,5 @@
Discourse.AdminBackupsController = Ember.ObjectController.extend({});
Discourse.AdminBackupsController = Ember.ObjectController.extend({
noOperationIsRunning: Em.computed.not("isOperationRunning"),
rollbackEnabled: Em.computed.and("canRollback", "restoreEnabled", "noOperationIsRunning"),
rollbackDisabled: Em.computed.not("rollbackEnabled"),
});
@@ -2,17 +2,11 @@ Discourse.AdminBackupsIndexController = Ember.ArrayController.extend({
needs: ["adminBackups"],
status: Em.computed.alias("controllers.adminBackups"),
rollbackDisabled: Em.computed.not("rollbackEnabled"),
uploadText: function() { return I18n.t("admin.backups.upload.text"); }.property(),
rollbackEnabled: function() {
return this.get("status.canRollback") && this.get("restoreEnabled");
}.property("status.canRollback", "restoreEnabled"),
readOnlyModeDisabled: Em.computed.alias("status.isOperationRunning"),
restoreDisabled: Em.computed.not("restoreEnabled"),
restoreEnabled: function() {
return Discourse.SiteSettings.allow_restore && !this.get("status.isOperationRunning");
}.property("status.isOperationRunning"),
restoreDisabled: Em.computed.alias("status.restoreDisabled"),
restoreTitle: function() {
if (!Discourse.SiteSettings.allow_restore) {
@@ -24,6 +18,8 @@ Discourse.AdminBackupsIndexController = Ember.ArrayController.extend({
}
}.property("status.isOperationRunning"),
destroyDisabled: Em.computed.alias("status.isOperationRunning"),
destroyTitle: function() {
if (this.get("status.isOperationRunning")) {
return I18n.t("admin.backups.operation_already_running");
@@ -64,7 +60,7 @@ Discourse.AdminBackupsIndexController = Ember.ArrayController.extend({
} else {
this._toggleReadOnlyMode(false);
}
},
}
},
@@ -0,0 +1,17 @@
/**
Data model for representing the status of backup/restore
@class BackupStatus
@extends Discourse.Model
@namespace Discourse
@module Discourse
**/
Discourse.BackupStatus = Discourse.Model.extend({
restoreDisabled: Em.computed.not("restoreEnabled"),
restoreEnabled: function() {
return Discourse.SiteSettings.allow_restore && !this.get("isOperationRunning");
}.property("isOperationRunning"),
});
@@ -29,7 +29,7 @@ Discourse.AdminBackupsRoute = Discourse.Route.extend({
return PreloadStore.getAndRemove("operations_status", function() {
return Discourse.ajax("/admin/backups/status.json");
}).then(function (status) {
return Em.Object.create({
return Discourse.BackupStatus.create({
isOperationRunning: status.is_operation_running,
canRollback: status.can_rollback,
});
@@ -57,7 +57,7 @@ Discourse.AdminBackupsRoute = Discourse.Route.extend({
Discourse.User.currentProp("hideReadOnlyAlert", true);
Discourse.Backup.start().then(function() {
self.controllerFor("adminBackupsLogs").clear();
self.controllerFor("adminBackups").set("isOperationRunning", true);
self.modelFor("adminBackups").set("isOperationRunning", true);
self.transitionTo("admin.backups.logs");
});
}
@@ -104,7 +104,7 @@ Discourse.AdminBackupsRoute = Discourse.Route.extend({
Discourse.User.currentProp("hideReadOnlyAlert", true);
backup.restore().then(function() {
self.controllerFor("adminBackupsLogs").clear();
self.controllerFor("adminBackups").set("isOperationRunning", true);
self.modelFor("adminBackups").set("isOperationRunning", true);
self.transitionTo("admin.backups.logs");
});
}
@@ -148,6 +148,19 @@ Discourse.AdminBackupsRoute = Discourse.Route.extend({
}
);
},
uploadSuccess: function(filename) {
var self = this;
bootbox.alert(I18n.t("admin.backups.upload.success", { filename: filename }), function() {
Discourse.Backup.find().then(function (backups) {
self.controllerFor("adminBackupsIndex").set("model", backups);
});
});
},
uploadError: function(filename, message) {
bootbox.alert(I18n.t("admin.backups.upload.error", { filename: filename, message: message }));
},
}
});
@@ -6,6 +6,9 @@
</ul>
</div>
<div class="pull-right">
{{#if canRollback}}
<button {{action rollback}} class="btn btn-rollback" title="{{i18n admin.backups.operations.rollback.title}}" {{bind-attr disabled="rollbackDisabled"}}><i class="fa fa-ambulance fa-flip-horizontal"></i>{{i18n admin.backups.operations.rollback.text}}</button>
{{/if}}
{{#if isOperationRunning}}
<button {{action cancelOperation}} class="btn btn-danger" title="{{i18n admin.backups.operations.cancel.title}}"><i class="fa fa-times"></i>{{i18n admin.backups.operations.cancel.text}}</button>
{{else}}
@@ -3,10 +3,8 @@
<th width="40%">{{i18n admin.backups.columns.filename}}</th>
<th width="30%">{{i18n admin.backups.columns.size}}</th>
<th>
<button {{action toggleReadOnlyMode}} class="btn" {{bind-attr title=readOnlyModeTitle}}><i class="fa fa-eye"></i>{{readOnlyModeText}}</button>
{{#if status.canRollback}}
<button {{action rollback}} class="btn btn-rollback" title="{{i18n admin.backups.operations.rollback.title}}" {{bind-attr disabled=rollbackDisabled}}><i class="fa fa-ambulance fa-flip-horizontal"></i>{{i18n admin.backups.operations.rollback.text}}</button>
{{/if}}
{{resumable-upload target="/admin/backups/upload" success="uploadSuccess" error="uploadError" uploadText=uploadText}}
<button {{action toggleReadOnlyMode}} class="btn" {{bind-attr disabled="readOnlyModeDisabled" title="readOnlyModeTitle"}}><i class="fa fa-eye"></i>{{readOnlyModeText}}</button>
</th>
</tr>
{{#each backup in model}}
@@ -15,7 +13,7 @@
<td>{{humanSize backup.size}}</td>
<td>
<a {{bind-attr href="backup.link"}} class="btn download" title="{{i18n admin.backups.operations.download.title}}"><i class="fa fa-download"></i>{{i18n admin.backups.operations.download.text}}</a>
<button {{action destroyBackup backup}} class="btn btn-danger" {{bind-attr disabled="status.isOperationRunning" title="destroyTitle"}}><i class="fa fa-trash-o"></i>{{i18n admin.backups.operations.destroy.text}}</button>
<button {{action destroyBackup backup}} class="btn btn-danger" {{bind-attr disabled="destroyDisabled" title="destroyTitle"}}><i class="fa fa-trash-o"></i>{{i18n admin.backups.operations.destroy.text}}</button>
<button {{action startRestore backup}} class="btn" {{bind-attr disabled="restoreDisabled" title="restoreTitle"}}><i class="fa fa-undo"></i>{{i18n admin.backups.operations.restore.text}}</button>
</td>
</tr>