// Дополнительный массив
var columnsHeight = Array.from({
length: columnsCount
}, function () {
return [];
});
React.Children.forEach(children, function (child, index) {
if (child && React.isValidElement(child)) {
const { sizeHeight } = child.props.document;
columnsHeight[index % columnsCount] = +columnsHeight[index % columnsCount] + sizeHeight
var minIndex = columnsHeight.indexOf(Math.min.apply(null, columnsHeight));
if (columns[minIndex]) {
columns[minIndex].push(child)
}
//columns[index % columnsCount].push(child);
}
});
function updateStats(request) {
return fs.appendFileSync("views.log", `${request.document}|`, 'UTF-8') // request.document = часть URL по которой идет выборка
}
//file.split('|').filter(function (e) { return e })
function setCountObject(request) {
return documents = [], Promise.all(request.map(async (document) => {
return (item = documents.find(e => e.url === document)), item && (item.count = item.count + 1) || documents.push({
url: document,
count: 1
})
})), documents
}
function updateDocuments(request) {
return Promise.all(request.map(async (document) => {
return Collection.updateOne({ url: document.url }, { $set: { "stats.view": document.count }})
}))
}
return Wallpapers
.aggregate([
{ $match: { public: { $gte: 2 }, $text: { $search: 'сам запрос' } } },
{ $project: { _id: 1, score: { $meta: "textScore" } } },
{ $sort: request.sort },
{ $skip: request.skip },
{ $limit: request.limit },
{ $group: { _id: null, uid: { $push: '$_id' }}}
]).then(e => {
return e.length ? Wallpapers.aggregate([
{ $match: { _id: { $in: e[0].uid }} },
{
$lookup: {
from: 'users',
localField: 'owner',
foreignField: '_id',
as: 'owner'
}
},
{ $project: {
_id: 1,
image: { $concat: ['$domain', 'o/', '$image', '?route=thumb&h=350'] },
large: { $concat: ['$domain', 'o/', '$image', '?route=mid&h=750'] },
href: { $concat: [request.CONST.href, 'wallpaper/', '$url', '.html'] },
hrefClass: config.hrefAddClass,
tags: { $slice: ['$tags', 12] },
size: {
width: { $trunc: { $multiply: [ { $divide: ['$size.width', '$size.height'] }, 350] } },
height: { $trunc: 350 }
},
style: {
$concat: [
'background-color: rgb(',
{ $toLower: { $arrayElemAt: [ '$pixels.rgb.r', 0 ] } }, ',',
{ $toLower: { $arrayElemAt: [ '$pixels.rgb.g', 0 ] } }, ',',
{ $toLower: { $arrayElemAt: [ '$pixels.rgb.b', 0 ] } }, ')'
]
},
owner: { $arrayElemAt: [ '$owner', 0 ] },
likes: 1,
indexAt: 1,
title: 1,
}}, {
$group: {
_id: "$_id",
image: { $first: '$image' },
large: { $first: '$large' },
href: { $first: '$href' },
hrefClass: { $first: '$hrefClass' },
tags: { $first: '$tags' },
size: { $first: '$size' },
style: { $first: '$style' },
owner: { $first: {
_id: '$owner._id',
name: '$owner.name',
avatar: { $concat: ['background-image:url(', '$owner.avatar', ')'] },
href: { $concat: [request.CONST.href, '$owner.username', '/'] }
} },
likes: { $first: '$likes' },
indexAt: { $first: '$indexAt' },
title: { $first: '$title' }
}
}
]).then() : []
})
return sharp( path.join( config.path.dir, '../', request.document.replace(/^https?:\/\//, '') ) )
.resize( request.params.width, request.params.height )
.toFile(request.params.file)
.then()
.catch(e => {
throw new Error(e)
})
function createFileGM(request) {
return new Promise((resolve, reject) => {
return gm( path.join( config.path.dir, '../', request.document.replace(/^https?:\/\//, '') ) )
.resize(request.params.width, request.params.height, '^')
.gravity('Center')
.crop(request.params.width, request.params.height)
.write(request.params.file, (e) => {
if(e) return reject(e)
return resolve()
})
})
}