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);
}
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);
})
});
});
})