feat: #7743 groups/cover,create,data
parent
382a9c4187
commit
5e8614e15b
@ -1,80 +1,60 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
var path = require('path');
|
||||
const path = require('path');
|
||||
|
||||
var db = require('../database');
|
||||
var image = require('../image');
|
||||
var file = require('../file');
|
||||
const db = require('../database');
|
||||
const image = require('../image');
|
||||
const file = require('../file');
|
||||
|
||||
module.exports = function (Groups) {
|
||||
Groups.updateCoverPosition = function (groupName, position, callback) {
|
||||
Groups.updateCoverPosition = async function (groupName, position) {
|
||||
if (!groupName) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
Groups.setGroupField(groupName, 'cover:position', position, callback);
|
||||
await Groups.setGroupField(groupName, 'cover:position', position);
|
||||
};
|
||||
|
||||
Groups.updateCover = function (uid, data, callback) {
|
||||
// Position only? That's fine
|
||||
if (!data.imageData && !data.file && data.position) {
|
||||
return Groups.updateCoverPosition(data.groupName, data.position, callback);
|
||||
}
|
||||
|
||||
var tempPath = data.file ? data.file : '';
|
||||
var url;
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
if (tempPath) {
|
||||
return next(null, tempPath);
|
||||
}
|
||||
image.writeImageDataToTempFile(data.imageData, next);
|
||||
},
|
||||
function (_tempPath, next) {
|
||||
tempPath = _tempPath;
|
||||
|
||||
const filename = 'groupCover-' + data.groupName + path.extname(tempPath);
|
||||
image.uploadImage(filename, 'files', {
|
||||
path: tempPath,
|
||||
uid: uid,
|
||||
name: 'groupCover',
|
||||
}, next);
|
||||
},
|
||||
function (uploadData, next) {
|
||||
url = uploadData.url;
|
||||
Groups.setGroupField(data.groupName, 'cover:url', url, next);
|
||||
},
|
||||
function (next) {
|
||||
image.resizeImage({
|
||||
path: tempPath,
|
||||
width: 358,
|
||||
}, next);
|
||||
},
|
||||
function (next) {
|
||||
image.uploadImage('groupCoverThumb-' + data.groupName + path.extname(tempPath), 'files', {
|
||||
path: tempPath,
|
||||
uid: uid,
|
||||
name: 'groupCover',
|
||||
}, next);
|
||||
},
|
||||
function (uploadData, next) {
|
||||
Groups.setGroupField(data.groupName, 'cover:thumb:url', uploadData.url, next);
|
||||
},
|
||||
function (next) {
|
||||
if (data.position) {
|
||||
Groups.updateCoverPosition(data.groupName, data.position, next);
|
||||
} else {
|
||||
next(null);
|
||||
}
|
||||
},
|
||||
], function (err) {
|
||||
Groups.updateCover = async function (uid, data) {
|
||||
let tempPath = data.file ? data.file : '';
|
||||
try {
|
||||
// Position only? That's fine
|
||||
if (!data.imageData && !data.file && data.position) {
|
||||
return await Groups.updateCoverPosition(data.groupName, data.position);
|
||||
}
|
||||
if (!tempPath) {
|
||||
tempPath = await image.writeImageDataToTempFile(data.imageData);
|
||||
}
|
||||
const filename = 'groupCover-' + data.groupName + path.extname(tempPath);
|
||||
const uploadData = await image.uploadImage(filename, 'files', {
|
||||
path: tempPath,
|
||||
uid: uid,
|
||||
name: 'groupCover',
|
||||
});
|
||||
const url = uploadData.url;
|
||||
await Groups.setGroupField(data.groupName, 'cover:url', url);
|
||||
|
||||
await image.resizeImage({
|
||||
path: tempPath,
|
||||
width: 358,
|
||||
});
|
||||
const thumbUploadData = await image.uploadImage('groupCoverThumb-' + data.groupName + path.extname(tempPath), 'files', {
|
||||
path: tempPath,
|
||||
uid: uid,
|
||||
name: 'groupCover',
|
||||
});
|
||||
await Groups.setGroupField(data.groupName, 'cover:thumb:url', thumbUploadData.url);
|
||||
|
||||
if (data.position) {
|
||||
await Groups.updateCoverPosition(data.groupName, data.position);
|
||||
}
|
||||
|
||||
return { url: url };
|
||||
} finally {
|
||||
file.delete(tempPath);
|
||||
callback(err, { url: url });
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
Groups.removeCover = function (data, callback) {
|
||||
db.deleteObjectFields('group:' + data.groupName, ['cover:url', 'cover:thumb:url', 'cover:position'], callback);
|
||||
Groups.removeCover = async function (data) {
|
||||
await db.deleteObjectFields('group:' + data.groupName, ['cover:url', 'cover:thumb:url', 'cover:position']);
|
||||
};
|
||||
};
|
||||
|
Loading…
Reference in New Issue