'use strict'; const config = require('config'); const Promise = require('bluebird'); const saveProfileDetails = require('../save/profileDetails.js'); const fetchItem = require('./item.js'); const interpolate = require('../interpolate.js'); const save = require('../save/save.js'); const textToStream = require('../save/textToStream.js'); const saveMeta = require('../save/meta.js'); const mux = require('../save/mux.js'); const writeToIndex = require('../save/writeToIndex.js'); async function getStreams(item, post) { if (item.self) { return [textToStream(item.text)]; } const sources = item.mux ? [item.url].concat(item.mux) : [item.url]; const streams = await Promise.map(sources, source => fetchItem(source, 0, post)); if (streams.filter(stream => stream).length > 0) { return streams; } return null; } async function addMeta(filepath, item, post, user, ep) { if (item.type !== 'image/jpeg') { return false; } const meta = Object.entries(config.library.meta).reduce((acc, [key, value]) => { const interpolatedValue = interpolate(value, user, post, item); return interpolatedValue ? { ...acc, [key]: interpolatedValue } : acc; }, {}); if (Object.keys(meta).length > 0) { return saveMeta(filepath, meta, ep); } return false; } function getFilepath(item, post, user) { const type = item.type.split('/')[0]; return post.content.album ? interpolate(config.library.album[type], user, post, item) : interpolate(config.library[type], user, post, item); } async function fetchSaveContent(user, ep, args) { const profilePaths = await saveProfileDetails(user, args); const posts = await Promise.map(user.posts, async (post) => { await Promise.reduce(post.content.items, async (accItems, originalItem, index) => { const item = { ...originalItem, index }; const streams = await getStreams(item, post); // no streams, ignore item if (!streams || streams.length <= 0) { return accItems; } const filepath = getFilepath(item, post, user); const sourcePaths = await save(filepath, streams, item, post); if (item.mux) { await mux(filepath, sourcePaths, item); } await addMeta(filepath, item, post, user, ep); return sourcePaths; }, []); return post; }, { concurrency: config.fetch.concurrency, }); return writeToIndex(posts, profilePaths, user, args); } module.exports = fetchSaveContent;