2024-09-11 03:16:53 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const config = require('config');
|
2024-09-11 03:16:53 +00:00
|
|
|
const path = require('path');
|
|
|
|
const url = require('url');
|
2024-09-11 03:16:53 +00:00
|
|
|
const dateFns = require('date-fns');
|
2024-09-11 03:16:55 +00:00
|
|
|
const mime = require('mime-types');
|
2024-09-11 03:16:53 +00:00
|
|
|
|
2024-09-11 03:16:53 +00:00
|
|
|
function interpolate(pattern, user, post, item) {
|
|
|
|
const dateFormat = config.library.dateFormat || 'YYYYMMDD';
|
2024-09-11 03:16:53 +00:00
|
|
|
|
2024-09-11 03:16:53 +00:00
|
|
|
const vars = {
|
|
|
|
$base: config.library.base
|
|
|
|
};
|
2024-09-11 03:16:53 +00:00
|
|
|
|
|
|
|
if(user) {
|
|
|
|
Object.assign(vars, {
|
|
|
|
$user: user.name,
|
|
|
|
$username: user.name,
|
|
|
|
$userId: user.id,
|
|
|
|
$userCreated: dateFns.format(user.created, dateFormat),
|
|
|
|
$userVerified: user.verified ? config.library.booleans.verified : '',
|
|
|
|
$userVerifiedEmail: user.verifiedEmail ? config.library.booleans.verifiedEmail : '',
|
|
|
|
$userGold: user.gold ? config.library.booleans.gold : '',
|
|
|
|
});
|
2024-09-11 03:16:53 +00:00
|
|
|
|
|
|
|
if(user.profile) {
|
|
|
|
Object.assign(vars, {
|
|
|
|
$profileId: user.profile.id,
|
|
|
|
$profileTitle: user.profile.title,
|
|
|
|
$profileDescription: user.profile.description,
|
|
|
|
$profileOver18: user.profile.over18 ? config.library.booleans.over18 : ''
|
|
|
|
});
|
|
|
|
}
|
2024-09-11 03:16:53 +00:00
|
|
|
}
|
2024-09-11 03:16:53 +00:00
|
|
|
|
2024-09-11 03:16:53 +00:00
|
|
|
if(post) {
|
2024-09-11 03:16:53 +00:00
|
|
|
Object.assign(vars, {
|
2024-09-11 03:16:53 +00:00
|
|
|
$postId: post.id,
|
|
|
|
$postTitle: (post.title || '').slice(0, config.library.titleLength),
|
|
|
|
$postUser: post.user || user.user,
|
|
|
|
$postDate: dateFns.format(post.datetime, dateFormat),
|
|
|
|
$postIndex: post.index + config.library.indexOffset,
|
2024-09-11 03:16:53 +00:00
|
|
|
$subreddit: post.subreddit,
|
2024-09-11 03:16:53 +00:00
|
|
|
$host: post.host.label
|
2024-09-11 03:16:53 +00:00
|
|
|
});
|
2024-09-11 03:16:53 +00:00
|
|
|
|
|
|
|
if(post.content.album) {
|
|
|
|
Object.assign(vars, {
|
|
|
|
$albumId: post.content.album.id,
|
|
|
|
$albumTitle: (post.content.album.title || '').slice(0, config.library.titleLength),
|
|
|
|
$albumDescription: post.content.album.description,
|
|
|
|
$albumDate: dateFns.format(post.content.album.datetime, dateFormat)
|
|
|
|
});
|
|
|
|
}
|
2024-09-11 03:16:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(item) {
|
|
|
|
Object.assign(vars, {
|
|
|
|
$itemId: item.id,
|
2024-09-11 03:16:53 +00:00
|
|
|
$itemTitle: (item.title || '').slice(0, config.library.titleLength),
|
2024-09-11 03:16:53 +00:00
|
|
|
$itemDescription: item.description,
|
|
|
|
$itemDate: dateFns.format(item.datetime, dateFormat),
|
2024-09-11 03:16:53 +00:00
|
|
|
$itemIndex: item.index + config.library.indexOffset,
|
|
|
|
$extracted: item.extracted ? config.library.booleans.extracted : '',
|
2024-09-11 03:16:54 +00:00
|
|
|
$preview: item.preview ? config.library.booleans.preview : '',
|
2024-09-11 03:16:55 +00:00
|
|
|
$ext: `.${mime.extension(item.type) || path.extname(url.parse(item.url).pathname)}`
|
2024-09-11 03:16:53 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object.entries(vars).reduce((acc, [key, value], index) => {
|
2024-09-11 03:16:53 +00:00
|
|
|
// substitute slashes for filesystem compatability
|
2024-09-11 03:16:53 +00:00
|
|
|
if(key !== '$base') {
|
|
|
|
value = (value || '').toString().replace(/\//g, config.library.slashSubstitute);
|
|
|
|
}
|
2024-09-11 03:16:53 +00:00
|
|
|
|
|
|
|
return acc.replace(key, value);
|
2024-09-11 03:16:53 +00:00
|
|
|
}, pattern);
|
2024-09-11 03:16:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = interpolate;
|