Skip to content

Commit

Permalink
Merge f4c5e64 into 045d786
Browse files Browse the repository at this point in the history
  • Loading branch information
MrOrz committed Jun 14, 2023
2 parents 045d786 + f4c5e64 commit b40d732
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ $ node build/scripts/blockUser.js --userId=<userId> --blockedReason=<Announcemen
- This command replaces all the variants of a media article's file on GCS with the variants of the new file.

```
$ node build/script/replaceMedia.js --articleId=<articleId> --url=<new-file-url>
$ node build/scripts/replaceMedia.js --articleId=<articleId> --url=<new-file-url>
```

### Generating a spreadsheet of new article-categories for human review
Expand Down
3 changes: 2 additions & 1 deletion ecosystem.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ module.exports = {
},
instances: process.env.WEB_CONCURRENCY ? +process.env.WEB_CONCURRENCY : 1,
exec_mode: 'cluster',
log_date_format: 'YYYY-MM-DD HH:mm Z',
out_file: '/dev/null',
error_file: '/dev/null',
},
],
};
40 changes: 24 additions & 16 deletions src/scripts/replaceMedia.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { uploadMedia } from 'graphql/mutations/CreateMediaArticle';
/**
* @param {object} args
*/
async function replaceMedia({ articleId, url } = {}) {
async function replaceMedia({ articleId, url, force = false } = {}) {
const {
body: { _source: article },
} = await client.get({ index: 'articles', type: 'doc', id: articleId });
Expand All @@ -22,22 +22,36 @@ async function replaceMedia({ articleId, url } = {}) {
const oldMediaEntry = await mediaManager.get(article.attachmentHash);

/* istanbul ignore if */
if (!oldMediaEntry)
if (!force && !oldMediaEntry)
throw new Error(
`Article ${articleId}'s attachment hash "${
article.attachmentHash
}" has no corresponding media entry`
);

// Delete old media first, so that new one can be written without worring overwriting existing files
//
if (oldMediaEntry) {
await Promise.all(
oldMediaEntry.variants.map(variant =>
oldMediaEntry
.getFile(variant)
.delete()
.then(() => {
console.info(`Old media entry variant=${variant} deleted`);
})
)
);
}

const newMediaEntry = await uploadMedia({
mediaUrl: url,
articleType: article.articleType,
});

console.info(
`Article ${articleId} attachment hash: ${oldMediaEntry.id} --> ${
newMediaEntry.id
}`
`Article ${articleId} attachment hash: ${oldMediaEntry?.id ??
article.attachmentHash} --> ${newMediaEntry.id}`
);
await client.update({
index: 'articles',
Expand All @@ -49,17 +63,6 @@ async function replaceMedia({ articleId, url } = {}) {
},
},
});

return Promise.all(
oldMediaEntry.variants.map(variant =>
oldMediaEntry
.getFile(variant)
.delete()
.then(() => {
console.info(`Old media entry variant=${variant} deleted`);
})
)
);
}

export default replaceMedia;
Expand All @@ -80,6 +83,11 @@ if (require.main === module) {
type: 'string',
demandOption: true,
},
force: {
alias: 'f',
description: 'Skip old media entry check',
type: 'boolean',
},
})
.help('help').argv;

Expand Down

0 comments on commit b40d732

Please sign in to comment.