From 55778c85700d0c1c38b18ce0c0af731ee4842aea Mon Sep 17 00:00:00 2001 From: Thomas Lynch Date: Mon, 14 Dec 2020 09:22:30 +0000 Subject: [PATCH] change filename stripping, make browser hash files and send hash of file to strip name instead of sending names to prevent duplicate both getting stripped or filename filters on backend breaking them. also need the hashes for another feature later on too. --- gulp/res/js/forms.js | 15 +++++++++++++-- gulp/res/js/uploaditem.js | 5 ++++- models/forms/makepost.js | 2 +- views/mixins/catalogtile.pug | 4 ++-- views/mixins/uploaditem.pug | 7 ++++--- 5 files changed, 24 insertions(+), 9 deletions(-) diff --git a/gulp/res/js/forms.js b/gulp/res/js/forms.js index fbfadbaa..2eeb5772 100644 --- a/gulp/res/js/forms.js +++ b/gulp/res/js/forms.js @@ -294,14 +294,25 @@ class formHandler { this.updateFilesText(); } - addFile(file) { + async addFile(file) { if (this.fileRequired) { //prevent drag+drop issues by removing required this.fileInput.removeAttribute('required'); } this.files.push(file); + console.log('got file', file.name, ); + let fileHash; + if (window.crypto.subtle) { + const fileBuffer = await file.arrayBuffer(); + const fileDigest = await window.crypto.subtle.digest('SHA-256', fileBuffer); + fileHash = Array.from(new Uint8Array(fileDigest)) + .map(c => c.toString(16).padStart(2, '0')) + .join(''); + console.log('file hash', fileHash); + } const item = { spoilers: this.fileUploadList.dataset.spoilers === 'true', - name: file.name + name: file.name, + hash: fileHash, } switch (file.type.split('/')[0]) { case 'image': diff --git a/gulp/res/js/uploaditem.js b/gulp/res/js/uploaditem.js index a6b81e81..457d00bb 100644 --- a/gulp/res/js/uploaditem.js +++ b/gulp/res/js/uploaditem.js @@ -10,7 +10,10 @@ pug_html = pug_html + "\u003Cdiv\u003E\u003Cdiv class=\"upload-item\"\u003E\u003 if (item.spoilers) { pug_html = pug_html + "\u003Clabel\u003E\u003Cinput" + (" type=\"checkbox\" name=\"spoiler\""+pug_attr("value", item.name, true, false)) + "\u002F\u003ESpoiler\u003C\u002Flabel\u003E"; } -pug_html = pug_html + "\u003Clabel\u003E\u003Cinput" + (" type=\"checkbox\" name=\"strip_filename\""+pug_attr("value", item.name, true, false)) + "\u002F\u003EStrip Filename\u003C\u002Flabel\u003E\u003C\u002Fdiv\u003E\u003C\u002Fdiv\u003E"; +if (item.hash) { +pug_html = pug_html + "\u003Clabel\u003E\u003Cinput" + (" type=\"checkbox\" name=\"strip_filename\""+pug_attr("value", item.hash, true, false)) + "\u002F\u003EStrip Filename\u003C\u002Flabel\u003E"; +} +pug_html = pug_html + "\u003C\u002Fdiv\u003E\u003C\u002Fdiv\u003E"; }; pug_mixins["uploaditem"](uploaditem); }.call(this, "uploaditem" in locals_for_with ? diff --git a/models/forms/makepost.js b/models/forms/makepost.js index 268dbdbc..362e9d35 100644 --- a/models/forms/makepost.js +++ b/models/forms/makepost.js @@ -234,7 +234,7 @@ ${res.locals.numFiles > 0 ? req.files.file.map(f => f.name+'|'+(f.phash || '')). spoiler: (res.locals.permLevel >= 4 || userPostSpoiler) && req.body.spoiler && req.body.spoiler.includes(file.name), hash: file.sha256, filename: file.filename, //could probably remove since we have hash and extension - originalFilename: req.body.strip_filename && req.body.strip_filename.includes(file.name) ? file.filename : file.name, + originalFilename: req.body.strip_filename && req.body.strip_filename.includes(file.sha256) ? file.filename : file.name, mimetype: file.mimetype, size: file.size, extension, diff --git a/views/mixins/catalogtile.pug b/views/mixins/catalogtile.pug index 89d9baa2..36fb1472 100644 --- a/views/mixins/catalogtile.pug +++ b/views/mixins/catalogtile.pug @@ -15,7 +15,7 @@ mixin catalogtile(post, index) data-date=post.date data-replies=post.replyposts data-bump=post.bumped) - - const postURL = `/${post.board}/${modview ? 'manage/' : ''}thread/${post.postId}.html#${post.postId}` + - const postURL = `/${post.board}/${modview ? 'manage/' : ''}thread/${post.postId}.html` .post-info if !index div @@ -36,7 +36,7 @@ mixin catalogtile(post, index) span(title='Page') P: #{Math.ceil(index/10)} if post.files.length > 0 .post-file-src - a(href=postURL) + a(href=`${postURL}#${post.postId}`) - const file = post.files[0] if post.spoiler || file.spoiler div.spoilerimg.catalog-thumb diff --git a/views/mixins/uploaditem.pug b/views/mixins/uploaditem.pug index 2c298198..0b89d7f2 100644 --- a/views/mixins/uploaditem.pug +++ b/views/mixins/uploaditem.pug @@ -9,6 +9,7 @@ mixin uploaditem(item) label input(type='checkbox', name='spoiler', value=item.name) | Spoiler - label - input(type='checkbox', name='strip_filename', value=item.name) - | Strip Filename + if item.hash + label + input(type='checkbox', name='strip_filename', value=item.hash) + | Strip Filename