From c000e852b50738a6db09d3155175c7918314a885 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Wed, 24 Sep 2014 17:13:12 -0400 Subject: [PATCH] closes #1935 --- public/language/en_GB/user.json | 1 + public/src/forum/account/edit.js | 40 +++++++++++++++++++++++++++----- src/controllers/accounts.js | 4 ++-- src/socket.io/user.js | 16 +++++++++++++ 4 files changed, 53 insertions(+), 8 deletions(-) diff --git a/public/language/en_GB/user.json b/public/language/en_GB/user.json index 2948c47292..a2995db1b3 100644 --- a/public/language/en_GB/user.json +++ b/public/language/en_GB/user.json @@ -32,6 +32,7 @@ "edit": "Edit", "uploaded_picture": "Uploaded Picture", "upload_new_picture": "Upload New Picture", + "upload_new_picture_from_url": "Upload New Picture From URL", "current_password": "Current Password", "change_password": "Change Password", "change_password_error": "Invalid Password!", diff --git a/public/src/forum/account/edit.js b/public/src/forum/account/edit.js index 4b2645a976..a99c6c448f 100644 --- a/public/src/forum/account/edit.js +++ b/public/src/forum/account/edit.js @@ -151,6 +151,16 @@ define('forum/account/edit', ['forum/account/header', 'uploader'], function(head } function handleImageUpload() { + function onUploadComplete(urlOnServer) { + urlOnServer = urlOnServer + '?' + new Date().getTime(); + + $('#user-current-picture').attr('src', urlOnServer); + $('#user-uploaded-picture').attr('src', urlOnServer); + $('#user-header-picture').attr('src', urlOnServer); + uploadedPicture = urlOnServer; + } + + $('#upload-picture-modal').on('hide', function() { $('#userPhotoInput').val(''); }); @@ -159,15 +169,33 @@ define('forum/account/edit', ['forum/account/header', 'uploader'], function(head $('#change-picture-modal').modal('hide'); uploader.open(config.relative_path + '/api/user/' + ajaxify.variables.get('userslug') + '/uploadpicture', {}, config.maximumProfileImageSize, function(imageUrlOnServer) { - imageUrlOnServer = imageUrlOnServer + '?' + new Date().getTime(); + onUploadComplete(imageUrlOnServer); + }); - $('#user-current-picture').attr('src', imageUrlOnServer); - $('#user-uploaded-picture').attr('src', imageUrlOnServer); - $('#user-header-picture').attr('src', imageUrlOnServer); + return false; + }); - uploadedPicture = imageUrlOnServer; - }); + $('#uploadFromUrlBtn').on('click', function() { + $('#change-picture-modal').modal('hide'); + var uploadModal = $('#upload-picture-from-url-modal'); + uploadModal.modal('show').removeClass('hide'); + uploadModal.find('.upload-btn').on('click', function() { + var url = uploadModal.find('#uploadFromUrl').val(); + if (!url) { + return; + } + socket.emit('user.uploadProfileImageFromUrl', url, function(err, imageUrlOnServer) { + if (err) { + return app.alertError(err.message); + } + onUploadComplete(imageUrlOnServer); + + uploadModal.modal('hide'); + }); + + return false; + }); return false; }); } diff --git a/src/controllers/accounts.js b/src/controllers/accounts.js index a81f7fcd07..ecaa5be6fa 100644 --- a/src/controllers/accounts.js +++ b/src/controllers/accounts.js @@ -453,8 +453,8 @@ accountsController.uploadPicture = function (req, res, next) { return res.json({error: err.message}); } - user.setUserField(updateUid, 'uploadedpicture', image.url); - user.setUserField(updateUid, 'picture', image.url); + user.setUserFields(updateUid, {uploadedpicture: image.url, picture: image.url}); + res.json({ path: image.url }); diff --git a/src/socket.io/user.js b/src/socket.io/user.js index 0b34551d08..8bfe07417d 100644 --- a/src/socket.io/user.js +++ b/src/socket.io/user.js @@ -186,6 +186,22 @@ SocketUser.changePicture = function(socket, data, callback) { }); }; +SocketUser.uploadProfileImageFromUrl = function(socket, url, callback) { + if (!socket.uid || !url) { + return; + } + + plugins.fireHook('filter:uploadImage', {url: url}, function(err, image) { + if (err) { + return callback(err); + } + + user.setUserFields(socket.uid, {uploadedpicture: image.url, picture: image.url}, function(err) { + callback(err, image.url); + }); + }); +} + SocketUser.follow = function(socket, data, callback) { if (!socket.uid || !data) { return;