mirror of
https://github.com/discourse/discourse.git
synced 2026-08-26 13:17:20 -05:00
FEATURE: upload backups
This commit is contained in:
@@ -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>
|
||||
|
||||
@@ -1 +1,3 @@
|
||||
//= require_tree ./admin
|
||||
//= require_tree ./admin
|
||||
|
||||
//= require resumable.js
|
||||
|
||||
@@ -987,3 +987,21 @@ $rollback-darker: darken($rollback, 20%) !default;
|
||||
max-height: 500px;
|
||||
overflow: auto;
|
||||
}
|
||||
|
||||
|
||||
button.ru {
|
||||
position: relative;
|
||||
min-width: 110px;
|
||||
}
|
||||
|
||||
.ru-progress {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
height: 100%;
|
||||
background: rgba(0, 175, 0, 0.3);
|
||||
}
|
||||
|
||||
.is-uploading:hover .ru-progress {
|
||||
background: rgba(200, 0, 0, 0.3);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ require_dependency "backup_restore"
|
||||
|
||||
class Admin::BackupsController < Admin::AdminController
|
||||
|
||||
skip_before_filter :check_xhr, only: [:index, :show, :logs]
|
||||
skip_before_filter :check_xhr, only: [:index, :show, :logs, :check_chunk, :upload_chunk]
|
||||
|
||||
def index
|
||||
respond_to do |format|
|
||||
@@ -83,4 +83,51 @@ class Admin::BackupsController < Admin::AdminController
|
||||
render nothing: true
|
||||
end
|
||||
|
||||
def check_chunk
|
||||
identifier = params.fetch(:resumableIdentifier)
|
||||
filename = params.fetch(:resumableFilename)
|
||||
chunk_number = params.fetch(:resumableChunkNumber)
|
||||
current_chunk_size = params.fetch(:resumableCurrentChunkSize).to_i
|
||||
|
||||
# path to chunk file
|
||||
chunk = Backup.chunk_path(identifier, filename, chunk_number)
|
||||
# check whether the chunk has already been uploaded
|
||||
has_chunk_been_uploaded = File.exists?(chunk) && File.size(chunk) == current_chunk_size
|
||||
# 200 = exists, 404 = not uploaded yet
|
||||
status = has_chunk_been_uploaded ? 200 : 404
|
||||
|
||||
render nothing: true, status: status
|
||||
end
|
||||
|
||||
def upload_chunk
|
||||
filename = params.fetch(:resumableFilename)
|
||||
return render nothing:true, status: 415 unless filename.to_s.end_with?(".tar.gz")
|
||||
|
||||
file = params.fetch(:file)
|
||||
identifier = params.fetch(:resumableIdentifier)
|
||||
chunk_number = params.fetch(:resumableChunkNumber).to_i
|
||||
chunk_size = params.fetch(:resumableChunkSize).to_i
|
||||
total_size = params.fetch(:resumableTotalSize).to_i
|
||||
current_chunk_size = params.fetch(:resumableCurrentChunkSize).to_i
|
||||
|
||||
# path to chunk file
|
||||
chunk = Backup.chunk_path(identifier, filename, chunk_number)
|
||||
dir = File.dirname(chunk)
|
||||
|
||||
# ensure directory exists
|
||||
FileUtils.mkdir_p(dir) unless Dir.exists?(dir)
|
||||
# save chunk to the directory
|
||||
File.open(chunk, "wb") { |f| f.write(file.tempfile.read) }
|
||||
|
||||
uploaded_file_size = chunk_number * chunk_size
|
||||
|
||||
# when all chunks are uploaded
|
||||
if uploaded_file_size + current_chunk_size >= total_size
|
||||
# merge all the chunks in a background thread
|
||||
Jobs.enqueue(:backup_chunks_merger, filename: filename, identifier: identifier, chunks: chunk_number)
|
||||
end
|
||||
|
||||
render nothing: true
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
module Jobs
|
||||
|
||||
class BackupChunksMerger < Jobs::Base
|
||||
sidekiq_options retry: false
|
||||
|
||||
def execute(args)
|
||||
filename = args[:filename]
|
||||
identifier = args[:identifier]
|
||||
chunks = args[:chunks].to_i
|
||||
|
||||
raise Discourse::InvalidParameters.new(:filename) if filename.blank?
|
||||
raise Discourse::InvalidParameters.new(:identifier) if identifier.blank?
|
||||
raise Discourse::InvalidParameters.new(:chunks) if chunks <= 0
|
||||
|
||||
backup = "#{Backup.base_directory}/#{filename}"
|
||||
|
||||
# delete destination
|
||||
File.delete(backup) rescue nil
|
||||
|
||||
# merge all the chunks
|
||||
File.open(backup, "a") do |backup|
|
||||
(1..chunks).each do |chunk_number|
|
||||
# path to chunk
|
||||
path = Backup.chunk_path(identifier, filename, chunk_number)
|
||||
# add chunk to backup
|
||||
backup << File.open(path).read
|
||||
# delete chunk
|
||||
File.delete(path) rescue nil
|
||||
end
|
||||
end
|
||||
|
||||
# remove tmp directory
|
||||
FileUtils.rm_rf(directory) rescue nil
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -34,4 +34,8 @@ class Backup
|
||||
@base_directory ||= File.join(Rails.root, "public", "backups", RailsMultisite::ConnectionManagement.current_db)
|
||||
end
|
||||
|
||||
def self.chunk_path(identifier, filename, chunk_number)
|
||||
File.join(Backup.base_directory, "tmp", identifier, "#{filename}.part#{chunk_number}")
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user