From 5de48eb99362a68fac46072e05b7cc8d6423c57a Mon Sep 17 00:00:00 2001 From: daemondshu Date: Fri, 15 Mar 2019 19:51:26 +0800 Subject: [PATCH 1/2] skip assets of unpublished posts and delete them if exist --- lib/plugins/processor/post.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/plugins/processor/post.js b/lib/plugins/processor/post.js index 7336025bbb..de69a78afe 100644 --- a/lib/plugins/processor/post.js +++ b/lib/plugins/processor/post.js @@ -159,6 +159,14 @@ module.exports = ctx => { }).filter(item => !isExcludedFile(item, ctx.config)).map(item => { const id = join(assetDir, item).substring(baseDirLength).replace(/\\/g, '/'); const asset = PostAsset.findById(id); + + if (post.published == false) { // skip copying assets for unpulished posts + if (asset) { // delete if already exist + return asset.remove() + } + return; + } + if (asset) return undefined; return PostAsset.save({ @@ -187,7 +195,7 @@ module.exports = ctx => { // TODO: Better post searching const post = Post.toArray().find(post => file.source.startsWith(post.asset_dir)); - if (post != null) { + if (post != null && post.published) { return PostAsset.save({ _id: id, slug: file.source.substring(post.asset_dir.length), From 6c59cbab4e4d3473957956b2fb8af8dbeeb8b626 Mon Sep 17 00:00:00 2001 From: daemondshu Date: Sat, 16 Mar 2019 20:34:10 +0800 Subject: [PATCH 2/2] test(processor/post): skip assets of unpublished posts --- lib/plugins/processor/post.js | 10 ++------- test/scripts/processors/post.js | 36 +++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/lib/plugins/processor/post.js b/lib/plugins/processor/post.js index de69a78afe..f3e50546ef 100644 --- a/lib/plugins/processor/post.js +++ b/lib/plugins/processor/post.js @@ -160,14 +160,8 @@ module.exports = ctx => { const id = join(assetDir, item).substring(baseDirLength).replace(/\\/g, '/'); const asset = PostAsset.findById(id); - if (post.published == false) { // skip copying assets for unpulished posts - if (asset) { // delete if already exist - return asset.remove() - } - return; - } - - if (asset) return undefined; + if (asset) return post.published === false ? asset.remove() : undefined; // delete if already exist + else if (post.published === false) return undefined; // skip assets for unpulished posts and return PostAsset.save({ _id: id, diff --git a/test/scripts/processors/post.js b/test/scripts/processors/post.js index 07cacaa210..d37a96c70e 100644 --- a/test/scripts/processors/post.js +++ b/test/scripts/processors/post.js @@ -826,6 +826,42 @@ describe('post', () => { ]); }); + it('post - post_asset_folder enabled with unpublished posts', async () => { + hexo.config.post_asset_folder = true; + + const body = [ + 'title: "Hello world"', + 'published: false', + '---' + ].join('\n'); + + const file = newFile({ + path: 'foo.html', + published: true, + type: 'create', + renderable: true + }); + + const assetId = 'source/_posts/foo/bar.jpg'; + const assetPath = join(hexo.base_dir, assetId); + + await Promise.all([ + writeFile(file.source, body), + writeFile(assetPath, '') + ]); + await process(file); + const post = Post.findOne({ source: file.path }); + + post.published.should.be.false; + should.not.exist(PostAsset.findById(assetId)); + post.remove(); + + await Promise.all([ + unlink(file.source), + unlink(assetPath) + ]); + }); + it('post - post_asset_folder disabled', async () => { hexo.config.post_asset_folder = false;