refactor user/picture.js

v1.18.x
barisusakli 8 years ago
parent 23ed1af282
commit b33d34f7cf

@ -98,10 +98,11 @@ module.exports = function (User) {
return callback(new Error('[[error:no-plugin]]')); return callback(new Error('[[error:no-plugin]]'));
} }
request.head(url, function (err, res) { async.waterfall([
if (err) { function (next) {
return callback(err); request.head(url, next);
} },
function (res, body, next) {
var uploadSize = parseInt(meta.config.maximumProfileImageSize, 10) || 256; var uploadSize = parseInt(meta.config.maximumProfileImageSize, 10) || 256;
var size = res.headers['content-length']; var size = res.headers['content-length'];
var type = res.headers['content-type']; var type = res.headers['content-type'];
@ -115,24 +116,23 @@ module.exports = function (User) {
return callback(new Error('[[error:file-too-big, ' + uploadSize + ']]')); return callback(new Error('[[error:file-too-big, ' + uploadSize + ']]'));
} }
var picture = { plugins.fireHook('filter:uploadImage', {
uid: uid,
image: {
url: url, url: url,
name: '' name: ''
};
plugins.fireHook('filter:uploadImage', {
image: picture,
uid: uid
}, function (err, image) {
if (err) {
return callback(err);
} }
}, next);
},
function (image, next) {
User.setUserFields(uid, { User.setUserFields(uid, {
uploadedpicture: image.url, uploadedpicture: image.url,
picture: image.url picture: image.url
}, function (err) {
next(err, image);
}); });
callback(null, image); }
}); ], callback);
});
}; };
User.updateCoverPosition = function (uid, position, callback) { User.updateCoverPosition = function (uid, position, callback) {
@ -140,8 +140,12 @@ module.exports = function (User) {
}; };
User.updateCoverPicture = function (data, callback) { User.updateCoverPicture = function (data, callback) {
var keepAllVersions = parseInt(meta.config['profile:keepAllUserImages'], 10) === 1;
var url, md5sum; var url;
var image = {
name: 'profileCover',
uid: data.uid
};
if (!data.imageData && data.position) { if (!data.imageData && data.position) {
return User.updateCoverPosition(data.uid, data.position, callback); return User.updateCoverPosition(data.uid, data.position, callback);
@ -160,94 +164,42 @@ module.exports = function (User) {
} }
if (data.file) { if (data.file) {
return next(); return setImmediate(next, null, data.file.path);
} }
md5sum = crypto.createHash('md5'); saveImageDataToTempFile(data.imageData, next);
md5sum.update(data.imageData);
md5sum = md5sum.digest('hex');
data.file = {
path: path.join(os.tmpdir(), md5sum)
};
var buffer = new Buffer(data.imageData.slice(data.imageData.indexOf('base64') + 7), 'base64');
fs.writeFile(data.file.path, buffer, {
encoding: 'base64'
}, next);
}, },
function (next) { function (path, next) {
var image = { image.path = path;
name: 'profileCover',
path: data.file.path,
uid: data.uid
};
if (plugins.hasListeners('filter:uploadImage')) {
return plugins.fireHook('filter:uploadImage', {
image: image,
uid: data.uid
}, next);
}
var extension = file.typeToExtension(data.imageData.slice(5, data.imageData.indexOf('base64') - 1)); uploadProfileOrCover('profilecover', image, data.imageData, next);
var filename = data.uid + '-profilecover' + (keepAllVersions ? '-' + Date.now() : '') + (extension || '');
async.waterfall([
function (next) {
file.isFileTypeAllowed(data.file.path, next);
},
function (next) {
file.saveFileToLocal(filename, 'profile', image.path, next);
},
function (upload, next) {
next(null, {
url: nconf.get('relative_path') + upload.url,
name: image.name
});
}
], next);
}, },
function (uploadData, next) { function (uploadData, next) {
url = uploadData.url; url = uploadData.url;
User.setUserField(data.uid, 'cover:url', uploadData.url, next); User.setUserField(data.uid, 'cover:url', uploadData.url, next);
}, },
function (next) { function (next) {
fs.unlink(data.file.path, function (err) { if (data.position) {
if (err) { User.updateCoverPosition(data.uid, data.position, next);
winston.error(err); } else {
setImmediate(next);
} }
next();
});
} }
], function (err) { ], function (err) {
if (err) { deleteFile(image.path);
return fs.unlink(data.file.path, function (unlinkErr) {
if (unlinkErr) {
winston.error(unlinkErr);
}
callback(err); // send back the original error
});
}
if (data.position) {
User.updateCoverPosition(data.uid, data.position, function (err) {
callback(err, { callback(err, {
url: url url: url
}); });
}); });
} else {
callback(err, {
url: url
});
}
});
}; };
User.uploadCroppedPicture = function (data, callback) { User.uploadCroppedPicture = function (data, callback) {
var keepAllVersions = parseInt(meta.config['profile:keepAllUserImages'], 10) === 1;
var url, md5sum; var url;
var image = {
name: 'profileAvatar',
uid: data.uid
};
if (!data.imageData) { if (!data.imageData) {
return callback(new Error('[[error:invalid-data]]')); return callback(new Error('[[error:invalid-data]]'));
@ -255,45 +207,69 @@ module.exports = function (User) {
async.waterfall([ async.waterfall([
function (next) { function (next) {
var size = data.file ? data.file.size : data.imageData.length; var size = data.imageData.length;
var uploadSize = parseInt(meta.config.maximumProfileImageSize, 10) || 256; var uploadSize = parseInt(meta.config.maximumProfileImageSize, 10) || 256;
if (size > uploadSize * 1024) { if (size > uploadSize * 1024) {
return next(new Error('[[error:file-too-big, ' + meta.config.maximumProfileImageSize + ']]')); return next(new Error('[[error:file-too-big, ' + meta.config.maximumProfileImageSize + ']]'));
} }
md5sum = crypto.createHash('md5'); saveImageDataToTempFile(data.imageData, next);
md5sum.update(data.imageData); },
md5sum = md5sum.digest('hex'); function (path, next) {
image.path = path;
data.file = { uploadProfileOrCover('profileavatar', image, data.imageData, next);
path: path.join(os.tmpdir(), md5sum) },
function (uploadData, next) {
url = uploadData.url;
User.setUserFields(data.uid, {
uploadedpicture: url,
picture: url
}, next);
}
], function (err) {
deleteFile(image.path);
callback(err, {
url: url
});
});
}; };
var buffer = new Buffer(data.imageData.slice(data.imageData.indexOf('base64') + 7), 'base64'); function saveImageDataToTempFile(imageData, callback) {
var filename = crypto.createHash('md5').update(imageData).digest('hex');
var filepath = path.join(os.tmpdir(), filename);
var buffer = new Buffer(imageData.slice(imageData.indexOf('base64') + 7), 'base64');
fs.writeFile(data.file.path, buffer, { fs.writeFile(filepath, buffer, {
encoding: 'base64' encoding: 'base64'
}, next); }, function (err) {
}, callback(err, filepath);
function (next) { });
var image = { }
name: 'profileAvatar',
path: data.file.path,
uid: data.uid
};
function uploadProfileOrCover(type, image, imageData, callback) {
if (plugins.hasListeners('filter:uploadImage')) { if (plugins.hasListeners('filter:uploadImage')) {
return plugins.fireHook('filter:uploadImage', { return plugins.fireHook('filter:uploadImage', {
image: image, image: image,
uid: data.uid uid: image.uid
}, next); }, callback);
}
var filename = generateProfileImageFilename(image.uid, type, imageData);
saveFileToLocal(filename, image, callback);
} }
var extension = file.typeToExtension(data.imageData.slice(5, data.imageData.indexOf('base64') - 1)); function generateProfileImageFilename(uid, type, imageData) {
var filename = data.uid + '-profileavatar' + (keepAllVersions ? '-' + Date.now() : '') + (extension || ''); var extension = file.typeToExtension(imageData.slice(5, imageData.indexOf('base64') - 1));
var keepAllVersions = parseInt(meta.config['profile:keepAllUserImages'], 10) === 1;
var filename = uid + '-' + type + (keepAllVersions ? '-' + Date.now() : '') + (extension || '');
return filename;
}
function saveFileToLocal(filename, image, callback) {
async.waterfall([ async.waterfall([
function (next) { function (next) {
file.isFileTypeAllowed(data.file.path, next); file.isFileTypeAllowed(image.path, next);
}, },
function (next) { function (next) {
file.saveFileToLocal(filename, 'profile', image.path, next); file.saveFileToLocal(filename, 'profile', image.path, next);
@ -304,34 +280,19 @@ module.exports = function (User) {
name: image.name name: image.name
}); });
} }
], next); ], callback);
}, }
function (uploadData, next) {
url = uploadData.url; function deleteFile(path) {
User.setUserFields(data.uid, { if (path) {
uploadedpicture: url, fs.unlink(path, function (err) {
picture: url
}, next);
},
function (next) {
fs.unlink(data.file.path, function (err) {
if (err) { if (err) {
winston.error(err); winston.error(err);
} }
next();
}); });
} }
], function (err) {
if (err) {
return callback(err); // send back the original error
} }
callback(err, {
url: url
});
});
};
User.removeCoverPicture = function (data, callback) { User.removeCoverPicture = function (data, callback) {
db.deleteObjectFields('user:' + data.uid, ['cover:url', 'cover:position'], callback); db.deleteObjectFields('user:' + data.uid, ['cover:url', 'cover:position'], callback);
}; };

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save