From f9f9c8a0bde041aa6071b90a571863e0c6706913 Mon Sep 17 00:00:00 2001 From: Niels Simenon Date: Sun, 1 Jul 2018 01:49:59 +0200 Subject: [PATCH] Fixed extension getting cut off at filename limit. Cleaned up save module. --- src/save/save.js | 72 +++++++++++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 31 deletions(-) diff --git a/src/save/save.js b/src/save/save.js index 2d945aa..0cc525f 100644 --- a/src/save/save.js +++ b/src/save/save.js @@ -3,48 +3,58 @@ const config = require('config'); const fs = require('fs-extra'); const path = require('path'); -const ffmpeg = require('fluent-ffmpeg'); -function save(requestedFilepath, streams, item, post) { - const filepath = requestedFilepath.split('/').map(component => { - console.log(component); - - if(config.library.truncate && component.length > config.library.truncate.limit) { - return component.slice(0, config.library.truncate.limit - config.library.truncate.truncator.length) + config.library.truncate.truncator; +function limitPathElement(element, limit) { + return element.split('/').map((component) => { + if (config.library.truncate && component.length > limit) { + return component.slice(0, limit - config.library.truncate.truncator.length) + config.library.truncate.truncator; } return component; }).join(path.sep); +} - console.log(filepath); +function getPathElements(requestedFilepath) { + const originalPathElements = path.parse(requestedFilepath); - const pathComponents = path.parse(filepath); + return { + root: originalPathElements.root, + dir: limitPathElement(originalPathElements.dir, config.library.truncate.limit), + name: limitPathElement(originalPathElements.name, config.library.truncate.limit - originalPathElements.ext.length), + ext: originalPathElements.ext, + }; +} - // allow for single stream argument - streams = [].concat(streams); +function pipeStreamToFile(target, stream, item) { + const file = fs.createWriteStream(target); - return Promise.resolve().then(() => { - return fs.ensureDir(pathComponents.dir); - }).then(() => { - 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); - return new Promise((resolve, reject) => { - stream.pipe(file).on('error', error => { - reject(error); - }).on('finish', () => { - if(item && item.mux) { - console.log(`Temporarily saved '${target}', queued for muxing`); - } else { - console.log('\x1b[32m%s\x1b[0m', `Saved '${target}'`); - } + stream.on('error', reject); + stream.on('finish', () => { + if (item && item.mux) { + console.log(`Temporarily saved '${target}', queued for muxing`); + } else { + console.log('\x1b[32m%s\x1b[0m', `Saved '${target}'`); + } - resolve(target); - }); - }); - })); + resolve(target); + }); }); -}; +} + +async function save(requestedFilepath, streamOrStreams, item) { + const pathElements = getPathElements(requestedFilepath); + const streams = [].concat(streamOrStreams); // allow for single stream argument + + await fs.ensureDir(pathElements.dir); + + return Promise.all(streams.map((stream, index) => { + const target = path.join(pathElements.root, pathElements.dir, `${pathElements.name}${streams.length > 1 ? `-${index}` : ''}${pathElements.ext}`); + + return pipeStreamToFile(target, stream, item); + })); +} module.exports = save;