From be7bc1b10da2b95a5e0311bcf23a385a7acfccef Mon Sep 17 00:00:00 2001 From: DebaucheryLibrarian Date: Wed, 11 Sep 2024 05:16:55 +0200 Subject: [PATCH] Added filepath component length limit with truncator. Fixed double extension dot when copying original extension. Moved API keys to local config. --- config/default.js | 16 +++++++++------- src/fetch/content.js | 2 -- src/interpolate.js | 2 +- src/save/save.js | 11 ++++++++++- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/config/default.js b/config/default.js index d85acd9..cf92c35 100644 --- a/config/default.js +++ b/config/default.js @@ -26,8 +26,10 @@ module.exports = { comment: '$itemDescription' }, dateFormat: 'YYYYMMDD', - filenameLimit: 250, - truncator: '...', + truncate: { + limit: 250, + truncator: '...' + }, indexOffset: 1, slashSubstitute: '#', }, @@ -45,18 +47,18 @@ module.exports = { }, reddit: { api: { - userAgent: 'wat', - clientId: 'VPquALMpTGl3ag', - access_token: 'xxxW9FDeaOIeHwsOvTkE4YLo_Fk', + userAgent: 'reddit-post-dump', + clientId: '1234567abcdefg', + access_token: 'abcD123eFg45Hi6J7klmnop8qr9', token_type: 'bearer', expires_in: 3600, - refresh_token: '8427871-D-Hb-m8WAazo2hBP-0iCPcUSPLM', + refresh_token: '1234567-A-Bc-defg8912hij-klm345opqr', scope: 'history identity mysubreddits read subscribe' } }, methods: { imgur: { - clientId: '455ceaa737dbac0' + clientId: '1234567abcdefgh' } } }; diff --git a/src/fetch/content.js b/src/fetch/content.js index c9f62a4..543db96 100644 --- a/src/fetch/content.js +++ b/src/fetch/content.js @@ -46,8 +46,6 @@ module.exports = function(posts) { const filepath = post.content.album ? interpolate(config.library.album[type], post.user, post, item) : interpolate(config.library[type], post.user, post, item); return Promise.resolve().then(() => { - return fs.ensureDir(path.dirname(filepath)); - }).then(() => { return save(filepath, item.streams || item.stream, item, post); }).then(sourcePaths => { if(item.mux) { diff --git a/src/interpolate.js b/src/interpolate.js index 2a0d6aa..dc7fe4d 100644 --- a/src/interpolate.js +++ b/src/interpolate.js @@ -64,7 +64,7 @@ function interpolate(pattern, user, post, item) { $itemIndex: item.index + config.library.indexOffset, $extracted: item.extracted ? config.library.booleans.extracted : '', $preview: item.preview ? config.library.booleans.preview : '', - $ext: `.${mime.extension(item.type) || path.extname(url.parse(item.url).pathname)}` + $ext: item.type ? `.${mime.extension(item.type)}` : path.extname(url.parse(item.url).pathname) }); } diff --git a/src/save/save.js b/src/save/save.js index 2b26285..42fe12c 100644 --- a/src/save/save.js +++ b/src/save/save.js @@ -1,10 +1,19 @@ 'use strict'; +const config = require('config'); const fs = require('fs-extra'); const path = require('path'); const ffmpeg = require('fluent-ffmpeg'); -function save(filepath, streams, item, post) { +function save(requestedFilepath, streams, item, post) { + const filepath = requestedFilepath.split('/').map(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; + } + + return component; + }).join(path.sep); + const pathComponents = path.parse(filepath); // allow for single stream argument