Add support for all types of media

This commit is contained in:
Kegan Myers 2017-04-16 14:29:50 -05:00
parent 85d4fcef87
commit b237504cf8

View file

@ -8,21 +8,22 @@ if (argv._.length !== 2) {
process.exit(1); 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 targetPath = path.resolve(process.cwd(), argv._[1], 'images/media_attachments');
const targetSuffix = 'files'; const targetSuffix = 'files';
const sizes = ['original', 'small']; const config = {
"accounts": {
function handleDir(...prefix) { avatars: ["original", "static"],
const contents = fs.readdirSync(path.join(basePath, ...prefix)); headers: ["original", "static"],
},
if (contents.length === 2 && _.difference(contents, sizes).length === 0) { media_attachments: {
return { prefix }; files: ["original", "small"]
} },
preview_cards: {
return _.flatten(contents.map((content) => handleDir(...prefix, content))); images: ["original"],
} },
};
const action = !argv['dry-run'] ? (sourceFile, targetDir, targetFile) => { const action = !argv['dry-run'] ? (sourceFile, targetDir, targetFile) => {
fs.mkdirpSync(targetDir); fs.mkdirpSync(targetDir);
@ -32,18 +33,48 @@ const action = !argv['dry-run'] ? (sourceFile, targetDir, targetFile) => {
console.log(`cp ${sourceFile} -> ${targetFile}`); console.log(`cp ${sourceFile} -> ${targetFile}`);
} }
const allFiles = handleDir().map((info) => (Object.assign( Object.keys(config).forEach((type) => {
{ number: parseInt(info.prefix.join(''), 10).toString() }, const typeBase = path.join(basePath, type);
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_');
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);
})
});
});
})