From e4881290bedc7a7682b961bfb20b93bbcbcf6327 Mon Sep 17 00:00:00 2001 From: Martin Brennan Date: Fri, 6 Dec 2019 10:58:47 +1000 Subject: [PATCH] FIX: Image file names with dots were showing incorrectly in composer markdown (#8465) When uploading an image file with dots in the filename we were splitting the string on dots and getting the last of the split items as the extension-less filename. However this did not work with filenames that have dots. We now just remove the extension using substr. --- .../javascripts/discourse/lib/uploads.js.es6 | 3 +- test/javascripts/lib/uploads-test.js.es6 | 30 ++++++++++++------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/app/assets/javascripts/discourse/lib/uploads.js.es6 b/app/assets/javascripts/discourse/lib/uploads.js.es6 index ed0271e8f3..11792ac9e4 100644 --- a/app/assets/javascripts/discourse/lib/uploads.js.es6 +++ b/app/assets/javascripts/discourse/lib/uploads.js.es6 @@ -7,8 +7,7 @@ function isGUID(value) { } function imageNameFromFileName(fileName) { - const split = fileName.split("."); - let name = split[split.length - 2]; + let name = fileName.substr(0, fileName.lastIndexOf(".")); if (isAppleDevice() && isGUID(name)) { name = I18n.t("upload_selector.default_image_alt_text"); diff --git a/test/javascripts/lib/uploads-test.js.es6 b/test/javascripts/lib/uploads-test.js.es6 index 5d08ed2ea1..0b6bb93765 100644 --- a/test/javascripts/lib/uploads-test.js.es6 +++ b/test/javascripts/lib/uploads-test.js.es6 @@ -205,6 +205,11 @@ QUnit.test("getUploadMarkdown", assert => { "![file name with space|100x200](/uploads/123/abcdef.ext)" ); + assert.equal( + testUploadMarkdown("image.file.name.with.dots.png"), + "![image.file.name.with.dots|100x200](/uploads/123/abcdef.ext)" + ); + const short_url = "uploads://asdaasd.ext"; assert.equal( @@ -213,15 +218,18 @@ QUnit.test("getUploadMarkdown", assert => { ); }); -QUnit.test("replaces GUID in image alt text on iOS", assert => { - assert.equal( - testUploadMarkdown("8F2B469B-6B2C-4213-BC68-57B4876365A0.jpeg"), - "![8F2B469B-6B2C-4213-BC68-57B4876365A0|100x200](/uploads/123/abcdef.ext)" - ); +QUnit.test( + "getUploadMarkdown - replaces GUID in image alt text on iOS", + assert => { + assert.equal( + testUploadMarkdown("8F2B469B-6B2C-4213-BC68-57B4876365A0.jpeg"), + "![8F2B469B-6B2C-4213-BC68-57B4876365A0|100x200](/uploads/123/abcdef.ext)" + ); - sandbox.stub(Utilities, "isAppleDevice").returns(true); - assert.equal( - testUploadMarkdown("8F2B469B-6B2C-4213-BC68-57B4876365A0.jpeg"), - "![image|100x200](/uploads/123/abcdef.ext)" - ); -}); + sandbox.stub(Utilities, "isAppleDevice").returns(true); + assert.equal( + testUploadMarkdown("8F2B469B-6B2C-4213-BC68-57B4876365A0.jpeg"), + "![image|100x200](/uploads/123/abcdef.ext)" + ); + } +);