REFACTOR: Use common path for RESTful DELETE action from upload image

component
This commit is contained in:
Robin Ward
2014-06-27 14:48:39 -04:00
parent 3cbb32cc20
commit 9000c358d1
9 changed files with 45 additions and 39 deletions

View File

@@ -13,10 +13,7 @@ export default Em.Component.extend(UploadMixin, {
this.set('imageUrl', data.result.url);
},
actions: {
trash: function() {
this.set('imageUrl', null);
this.sendAction('clear');
}
deleteDone: function() {
this.set('imageUrl', null);
}
});

View File

@@ -70,10 +70,6 @@ export default Discourse.ObjectController.extend({
actions: {
clearProfileBackground: function() {
this.get('model').clearProfileBackground();
},
save: function() {
var self = this;
this.setProperties({ saving: true, saved: false });

View File

@@ -12,17 +12,15 @@ export default Discourse.Controller.extend(Discourse.ModalFunctionality, {
local: false,
showMore: false,
init: function() {
this._super();
_initialize: function() {
this.setProperties({
local: this.get("allowLocal"),
showMore: false
});
},
}.on('init'),
allowLocal: function() {
return Discourse.SiteSettings.max_attachment_size_kb > 0;
}.property(),
maxSize: Discourse.computed.setting('max_attachment_size_kb'),
allowLocal: Em.computed.gt('maxSize', 0),
actions: {
useLocal: function() { this.setProperties({ local: true, showMore: false}); },

View File

@@ -6,6 +6,10 @@ export default Em.Mixin.create({
Em.warn("You should implement `uploadDone`");
},
deleteDone: function() {
Em.warn("You should implement `deleteDone`");
},
_initializeUploader: function() {
var $upload = this.$('input[type=file]'), // note: we can't cache this as fileupload replaces the input after upload
self = this;
@@ -49,6 +53,18 @@ export default Em.Mixin.create({
actions: {
selectFile: function() {
this.$('input[type=file]').click();
},
trash: function() {
var self = this;
Discourse.ajax(this.get('uploadUrl'), {
type: 'DELETE',
data: { image_type: this.get('type') }
}).then(function() {
self.deleteDone();
}).catch(function() {
bootbox.alert(I18n.t('generic_error'));
});
}
}
});

View File

@@ -343,19 +343,6 @@ Discourse.User = Discourse.Model.extend({
});
},
/*
Clear profile background
@method clearProfileBackground
@returns {Promise} the result of the clear profile background request
*/
clearProfileBackground: function() {
return Discourse.ajax("/users/" + this.get("username_lower") + "/preferences/profile_background/clear", {
type: 'PUT',
data: { }
});
},
/**
Determines whether the current user is allowed to upload a file.

View File

@@ -94,8 +94,7 @@
<div class="controls">
{{image-uploader uploadUrl=imageUploadUrl
imageUrl=profile_background
type="profile_background"
clear="clearProfileBackground"}}
type="profile_background"}}
</div>
</div>
{{/if}}

View File

@@ -7,7 +7,7 @@ class UsersController < ApplicationController
skip_before_filter :authorize_mini_profiler, only: [:avatar]
skip_before_filter :check_xhr, only: [:show, :password_reset, :update, :activate_account, :authorize_email, :user_preferences_redirect, :avatar, :my_redirect]
before_filter :ensure_logged_in, only: [:username, :update, :change_email, :user_preferences_redirect, :upload_user_image, :pick_avatar, :clear_profile_background, :destroy]
before_filter :ensure_logged_in, only: [:username, :update, :change_email, :user_preferences_redirect, :upload_user_image, :pick_avatar, :destroy_user_image, :destroy]
before_filter :respond_to_suspicious_request, only: [:create]
# we need to allow account creation with bad CSRF tokens, if people are caching, the CSRF token on the
@@ -380,11 +380,16 @@ class UsersController < ApplicationController
render nothing: true
end
def clear_profile_background
def destroy_user_image
user = fetch_user_from_params
guardian.ensure_can_edit!(user)
user.user_profile.clear_profile_background
image_type = params.require(:image_type)
if image_type == 'profile_background'
user.user_profile.clear_profile_background
else
raise Discourse::InvalidParameters.new(:image_type)
end
render nothing: true
end