'use strict'; const config = require('config'); const path = require('path'); const url = require('url'); const dateFns = require('date-fns'); const extensions = { 'image/jpeg': '.jpg', 'image/gif': '.gif', 'video/mp4': '.mp4', 'video/webm': '.webm' }; function interpolate(pattern, user, post, item) { const dateFormat = config.library.dateFormat || 'YYYYMMDD'; const vars = {}; if(config.library.base) { pattern = path.join(config.library.base, pattern); } 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 : '', $profileId: user.profile.id, $profileTitle: user.profile.title, $profileDescription: user.profile.description, $profileOver18: user.profile.over18 ? config.library.booleans.over18 : '' }); } if(post) { Object.assign(vars, { $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, $host: post.host.label }); 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) }); } } if(item) { Object.assign(vars, { $itemId: item.id, $itemTitle: (item.title || '').slice(0, config.library.titleLength), $itemDescription: item.description, $itemDate: dateFns.format(item.datetime, dateFormat), $itemIndex: item.index + config.library.indexOffset, $extracted: item.extracted ? config.library.booleans.extracted : '', $ext: item.extension || (item.type ? extensions[item.type] : path.extname(url.parse(item.url).pathname)) }); } return Object.entries(vars).reduce((acc, [key, value], index) => { // substitute slashes for filesystem compatability value = (value || '').toString().replace(/\//g, config.library.slashSubstitute); return acc.replace(key, value); }, pattern); }; module.exports = interpolate;