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