From 0313b291da9e5ead05f29608c30795b92eed05a2 Mon Sep 17 00:00:00 2001 From: Niels Simenon Date: Thu, 26 Apr 2018 03:35:42 +0200 Subject: [PATCH] Adapted primary save function to handle single streams and non-item saves (e.g. profiles). --- src/fetch/content.js | 4 ++-- src/save/mux.js | 2 +- src/save/save.js | 11 +++++++---- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/fetch/content.js b/src/fetch/content.js index 36f2c6f..c6d4724 100644 --- a/src/fetch/content.js +++ b/src/fetch/content.js @@ -42,10 +42,10 @@ module.exports = function(posts, user) { return Promise.resolve().then(() => { return fs.ensureDir(path.dirname(filepath)); }).then(() => { - return save(filepath, item); + return save(filepath, item.streams, item); }).then(sourcePaths => { if(item.mux) { - return mux(sourcePaths, filepath, item); + return mux(filepath, sourcePaths, item); } }).then(() => { const meta = Object.entries(config.library.meta).reduce((acc, [key, value]) => { diff --git a/src/save/mux.js b/src/save/mux.js index 016d539..642eac5 100644 --- a/src/save/mux.js +++ b/src/save/mux.js @@ -3,7 +3,7 @@ const ffmpeg = require('fluent-ffmpeg'); const fs = require('fs-extra'); -function mux(sources, target, item) { +function mux(target, sources, item) { return new Promise((resolve, reject) => { return sources.reduce((acc, source) => { return acc.input(source); diff --git a/src/save/save.js b/src/save/save.js index 42461f8..f332582 100644 --- a/src/save/save.js +++ b/src/save/save.js @@ -4,21 +4,24 @@ const fs = require('fs-extra'); const path = require('path'); const ffmpeg = require('fluent-ffmpeg'); -function save(filepath, item) { +function save(filepath, streams, item) { const pathComponents = path.parse(filepath); + // allow for single stream argument + streams = [].concat(streams); + return Promise.resolve().then(() => { return fs.ensureDir(path.dirname(pathComponents.dir)); }).then(() => { - return Promise.all(item.streams.map((stream, index) => { - const target = item.streams.length > 1 ? path.join(pathComponents.dir, `${pathComponents.name}-${index}${pathComponents.ext}`) : filepath; + return Promise.all(streams.map((stream, index) => { + const target = streams.length > 1 ? path.join(pathComponents.dir, `${pathComponents.name}-${index}${pathComponents.ext}`) : filepath; const file = fs.createWriteStream(target); return new Promise((resolve, reject) => { stream.pipe(file).on('error', error => { reject(error); }).on('finish', () => { - if(item.mux) { + if(item && item.mux) { console.log(`Temporarily saved '${target}', queued for muxing`); } else { console.log('\x1b[32m%s\x1b[0m', `Saved '${target}'`);