From b237504cf81f5341dc02c8b3208721e85ad2cd2e Mon Sep 17 00:00:00 2001 From: Kegan Myers Date: Sun, 16 Apr 2017 14:29:50 -0500 Subject: [PATCH] Add support for all types of media --- move-media.js | 83 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 57 insertions(+), 26 deletions(-) diff --git a/move-media.js b/move-media.js index 37b4e42..4c1676f 100644 --- a/move-media.js +++ b/move-media.js @@ -8,21 +8,22 @@ if (argv._.length !== 2) { process.exit(1); } -const basePath = path.resolve(process.cwd(), argv._[0], 'public/system/media_attachments/files'); +const basePath = path.resolve(process.cwd(), argv._[0], 'public/system'); const targetPath = path.resolve(process.cwd(), argv._[1], 'images/media_attachments'); const targetSuffix = 'files'; -const sizes = ['original', 'small']; - -function handleDir(...prefix) { - const contents = fs.readdirSync(path.join(basePath, ...prefix)); - - if (contents.length === 2 && _.difference(contents, sizes).length === 0) { - return { prefix }; - } - - return _.flatten(contents.map((content) => handleDir(...prefix, content))); -} +const config = { + "accounts": { + avatars: ["original", "static"], + headers: ["original", "static"], + }, + media_attachments: { + files: ["original", "small"] + }, + preview_cards: { + images: ["original"], + }, +}; const action = !argv['dry-run'] ? (sourceFile, targetDir, targetFile) => { fs.mkdirpSync(targetDir); @@ -32,18 +33,48 @@ const action = !argv['dry-run'] ? (sourceFile, targetDir, targetFile) => { console.log(`cp ${sourceFile} -> ${targetFile}`); } -const allFiles = handleDir().map((info) => (Object.assign( - { number: parseInt(info.prefix.join(''), 10).toString() }, - info, - sizes - .map((name) => ({ [name]: fs.readdirSync(path.join(basePath, ...info.prefix, name))[0] })) - .reduce((a, b) => Object.assign({}, a, b)) -))).forEach((info) => { - sizes.forEach((size) => { - const sourceFile = path.join(basePath, ...info.prefix, size, info[size]); - const targetDir = path.join(targetPath, info.number, targetSuffix, size); - const targetFile = path.join(targetDir, 'img_'); +Object.keys(config).forEach((type) => { + const typeBase = path.join(basePath, type); - action(sourceFile, targetDir, targetFile); - }) -}); + if (!fs.existsSync(typeBase)) { + console.log(`skipping ${typeBase}`); + return; + } + + const subtypes = config[type]; + Object.keys(subtypes).forEach((subtype) => { + const subtypeBase = path.join(typeBase, subtype); + + if (!fs.existsSync(subtypeBase)) { + console.log(`skipping ${subtypeBase}`); + return; + } + + const sizes = subtypes[subtype]; + function handleDir(...prefix) { + const contents = fs.readdirSync(path.join(subtypeBase, ...prefix)); + + if (contents.length <= sizes.length && _.difference(contents, sizes).length === 0) { + return { prefix, sizes: contents }; + } + + return _.flatten(contents.map((content) => handleDir(...prefix, content))); + } + + handleDir().map((info) => (Object.assign( + { number: parseInt(info.prefix.join(''), 10).toString() }, + info, + info.sizes + .map((name) => ({ [name]: fs.readdirSync(path.join(subtypeBase, ...info.prefix, name))[0] })) + .reduce((a, b) => Object.assign({}, a, b)) + ))).forEach((info) => { + info.sizes.forEach((size) => { + const sourceFile = path.join(subtypeBase, ...info.prefix, size, info[size]); + const targetDir = path.join(targetPath, info.number, targetSuffix, size); + const targetFile = path.join(targetDir, 'img_'); + + action(sourceFile, targetDir, targetFile); + }) + }); + }); +})