diff --git a/app/assets/javascripts/discourse/components/utilities.js b/app/assets/javascripts/discourse/components/utilities.js index 80ebbb736..06387851e 100644 --- a/app/assets/javascripts/discourse/components/utilities.js +++ b/app/assets/javascripts/discourse/components/utilities.js @@ -172,6 +172,8 @@ Discourse.Utilities = { return false; } else if (files.length > 0) { var upload = files[0]; + // if the image was pasted, sets its name to a default one + if (upload instanceof Blob && !(upload instanceof File) && upload.type === "image/png") { upload.name = "blob.png"; } // check that the uploaded file is authorized if (!Discourse.Utilities.isAuthorizedUpload(upload)) { var extensions = Discourse.SiteSettings.authorized_extensions.replace(/\|/g, ", "); diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rb index 7b6cb5edd..0df554064 100644 --- a/lib/cooked_post_processor.rb +++ b/lib/cooked_post_processor.rb @@ -154,7 +154,7 @@ class CookedPostProcessor def get_filename(upload, src) return File.basename(src) unless upload - return upload.original_filename unless upload.original_filename == "blob" + return upload.original_filename unless upload.original_filename =~ /^blob(\.png)?$/i return I18n.t('upload.pasted_image_filename') end diff --git a/spec/components/cooked_post_processor_spec.rb b/spec/components/cooked_post_processor_spec.rb index 018f7af99..2fe886071 100644 --- a/spec/components/cooked_post_processor_spec.rb +++ b/spec/components/cooked_post_processor_spec.rb @@ -230,7 +230,7 @@ describe CookedPostProcessor do end it "returns a generic name for pasted images" do - upload = Fabricate.build(:upload, { original_filename: "blob" }) + upload = Fabricate.build(:upload, { original_filename: "blob.png" }) cpp.get_filename(upload, "http://domain.com/image.png").should == I18n.t('upload.pasted_image_filename') end diff --git a/test/javascripts/components/utilities_test.js b/test/javascripts/components/utilities_test.js index 35078481e..7b0e7426b 100644 --- a/test/javascripts/components/utilities_test.js +++ b/test/javascripts/components/utilities_test.js @@ -41,12 +41,28 @@ test("prevents files that are too big from being uploaded", function() { ok(bootbox.alert.calledWith(Em.String.i18n('post.errors.upload_too_large', { max_size_kb: 5 }))); }); +var dummyBlob = function() { + window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder; + if (window.BlobBuilder) { + var bb = new window.BlobBuilder(); + bb.append([1]); + return bb.getBlob("image/png"); + } else { + return new Blob([1], { "type" : "image\/png" }); + } +}; + test("allows valid uploads to go through", function() { - var image = { name: "image.png", size: 10 * 1024 }; Discourse.SiteSettings.max_upload_size_kb = 15; this.stub(bootbox, "alert"); + // image + var image = { name: "image.png", size: 10 * 1024 }; ok(validUpload([image])); + // pasted image + var pastedImage = dummyBlob(); + ok(validUpload([pastedImage])); + ok(!bootbox.alert.calledOnce); }); diff --git a/test/javascripts/jshint_all.js.erb b/test/javascripts/jshint_all.js.erb index afdeb4bc8..d3361934e 100644 --- a/test/javascripts/jshint_all.js.erb +++ b/test/javascripts/jshint_all.js.erb @@ -125,7 +125,9 @@ var jsHintOpts = { "controllerFor", "containsInstance", "deepEqual", - "resolvingPromiseWith"], + "resolvingPromiseWith", + "Blob", + "File"], "node" : false, "browser" : true, "boss" : true, @@ -183,4 +185,4 @@ var jsHintOpts = { /external_development\//, /external_production\//, /defer\//, - /locales\//]) %> \ No newline at end of file + /locales\//]) %>