Barış Soner Uşaklı 5 years ago
parent bffb830d87
commit e53a18f219

@ -206,7 +206,7 @@ define('forum/account/edit', ['forum/account/header', 'translator', 'components'
updateHeader(urlOnServer); updateHeader(urlOnServer);
if (ajaxify.data.picture.length) { if (ajaxify.data.picture && ajaxify.data.picture.length) {
$('#user-current-picture, img.avatar').attr('src', urlOnServer); $('#user-current-picture, img.avatar').attr('src', urlOnServer);
ajaxify.data.uploadedpicture = urlOnServer; ajaxify.data.uploadedpicture = urlOnServer;
} else { } else {
@ -228,6 +228,7 @@ define('forum/account/edit', ['forum/account/header', 'translator', 'components'
pictureCropper.show({ pictureCropper.show({
socketMethod: 'user.uploadCroppedPicture', socketMethod: 'user.uploadCroppedPicture',
route: config.relative_path + '/api/user/' + ajaxify.data.userslug + '/uploadpicture',
aspectRatio: 1 / 1, aspectRatio: 1 / 1,
paramName: 'uid', paramName: 'uid',
paramValue: ajaxify.data.theirid, paramValue: ajaxify.data.theirid,

@ -7,6 +7,7 @@ const helpers = require('../helpers');
const groups = require('../../groups'); const groups = require('../../groups');
const accountHelpers = require('./helpers'); const accountHelpers = require('./helpers');
const privileges = require('../../privileges'); const privileges = require('../../privileges');
const file = require('../../file');
const editController = module.exports; const editController = module.exports;
@ -124,3 +125,29 @@ async function getUserData(req) {
userData.hasPassword = await user.hasPassword(userData.uid); userData.hasPassword = await user.hasPassword(userData.uid);
return userData; return userData;
} }
editController.uploadPicture = async function (req, res, next) {
const userPhoto = req.files.files[0];
try {
const updateUid = await user.getUidByUserslug(req.params.userslug);
const isAllowed = await privileges.users.canEdit(req.uid, updateUid);
if (!isAllowed) {
return helpers.notAllowed(req, res);
}
await user.checkMinReputation(req.uid, updateUid, 'min:rep:profile-picture');
const image = await user.uploadCroppedPictureFile({
uid: updateUid,
file: userPhoto,
});
res.json([{
name: userPhoto.name,
url: image.url,
}]);
} catch (err) {
next(err);
} finally {
await file.delete(userPhoto.path);
}
};

@ -34,4 +34,6 @@ module.exports = function (app, middleware, controllers) {
var middlewares = [middleware.maintenanceMode, multipartMiddleware, middleware.validateFiles, middleware.applyCSRF]; var middlewares = [middleware.maintenanceMode, multipartMiddleware, middleware.validateFiles, middleware.applyCSRF];
router.post('/post/upload', middlewares, uploadsController.uploadPost); router.post('/post/upload', middlewares, uploadsController.uploadPost);
router.post('/topic/thumb/upload', middlewares, uploadsController.uploadThumb); router.post('/topic/thumb/upload', middlewares, uploadsController.uploadThumb);
router.post('/user/:userslug/uploadpicture', middlewares.concat([middleware.exposeUid, middleware.authenticate, middleware.canViewUsers, middleware.checkAccountPermissions]), controllers.accounts.edit.uploadPicture);
}; };

@ -61,10 +61,52 @@ module.exports = function (User) {
url: uploadData.url, url: uploadData.url,
}; };
} finally { } finally {
file.delete(picture.path); await file.delete(picture.path);
} }
}; };
// uploads a image file as profile picture
User.uploadCroppedPictureFile = async function (data) {
const userPhoto = data.file;
if (!meta.config.allowProfileImageUploads) {
throw new Error('[[error:profile-image-uploads-disabled]]');
}
if (userPhoto.size > meta.config.maximumProfileImageSize * 1024) {
throw new Error('[[error:file-too-big, ' + meta.config.maximumProfileImageSize + ']]');
}
if (!userPhoto.type || !User.getAllowedImageTypes().includes(userPhoto.type)) {
throw new Error('[[error:invalid-image]]');
}
const extension = file.typeToExtension(userPhoto.type);
if (!extension) {
throw new Error('[[error:invalid-image-extension]]');
}
const newPath = await convertToPNG(userPhoto.path);
await image.resizeImage({
path: newPath,
width: meta.config.profileImageDimension,
height: meta.config.profileImageDimension,
});
const filename = generateProfileImageFilename(data.uid, extension);
const uploadedImage = await image.uploadImage(filename, 'profile', {
uid: data.uid,
path: newPath,
});
await User.setUserFields(data.uid, {
uploadedpicture: uploadedImage.url,
picture: uploadedImage.url,
});
return uploadedImage;
};
// uploads image data in base64 as profile picture
User.uploadCroppedPicture = async function (data) { User.uploadCroppedPicture = async function (data) {
const picture = { const picture = {
name: 'profileAvatar', name: 'profileAvatar',
@ -101,7 +143,7 @@ module.exports = function (User) {
}); });
return uploadedImage; return uploadedImage;
} finally { } finally {
file.delete(picture.path); await file.delete(picture.path);
} }
}; };
@ -126,7 +168,7 @@ module.exports = function (User) {
return path; return path;
} }
const newPath = await image.normalise(path); const newPath = await image.normalise(path);
file.delete(path); await file.delete(path);
return newPath; return newPath;
} }

Loading…
Cancel
Save