mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 16:01:20 +08:00
REFACTOR: upload workflow creation into UploadCreator
- Automatically convert large-ish PNG/BMP to JPEG - Updated fast_image to latest version
This commit is contained in:
@ -46,91 +46,6 @@ describe Upload do
|
||||
|
||||
end
|
||||
|
||||
context "#create_for" do
|
||||
|
||||
before do
|
||||
Upload.stubs(:fix_image_orientation)
|
||||
ImageOptim.any_instance.stubs(:optimize_image!)
|
||||
end
|
||||
|
||||
it "does not create another upload if it already exists" do
|
||||
Upload.expects(:find_by).with(sha1: image_sha1).returns(upload)
|
||||
Upload.expects(:save).never
|
||||
expect(Upload.create_for(user_id, image, image_filename, image_filesize)).to eq(upload)
|
||||
end
|
||||
|
||||
it "ensures images isn't huge before processing it" do
|
||||
Upload.expects(:fix_image_orientation).never
|
||||
upload = Upload.create_for(user_id, huge_image, huge_image_filename, huge_image_filesize)
|
||||
expect(upload.errors.size).to be > 0
|
||||
end
|
||||
|
||||
it "fix image orientation" do
|
||||
Upload.expects(:fix_image_orientation).with(image.path)
|
||||
Upload.create_for(user_id, image, image_filename, image_filesize)
|
||||
end
|
||||
|
||||
it "computes width & height for images" do
|
||||
ImageSizer.expects(:resize)
|
||||
Upload.create_for(user_id, image, image_filename, image_filesize)
|
||||
end
|
||||
|
||||
it "does not compute width & height for non-image" do
|
||||
FastImage.any_instance.expects(:size).never
|
||||
upload = Upload.create_for(user_id, attachment, attachment_filename, attachment_filesize)
|
||||
expect(upload.errors.size).to be > 0
|
||||
end
|
||||
|
||||
it "generates an error when the image is too large" do
|
||||
SiteSetting.stubs(:max_image_size_kb).returns(1)
|
||||
upload = Upload.create_for(user_id, image, image_filename, image_filesize)
|
||||
expect(upload.errors.size).to be > 0
|
||||
end
|
||||
|
||||
it "generates an error when the attachment is too large" do
|
||||
SiteSetting.stubs(:max_attachment_size_kb).returns(1)
|
||||
upload = Upload.create_for(user_id, attachment, attachment_filename, attachment_filesize)
|
||||
expect(upload.errors.size).to be > 0
|
||||
end
|
||||
|
||||
it "saves proper information" do
|
||||
store = {}
|
||||
Discourse.expects(:store).returns(store)
|
||||
store.expects(:store_upload).returns(url)
|
||||
|
||||
upload = Upload.create_for(user_id, image, image_filename, image_filesize)
|
||||
|
||||
expect(upload.user_id).to eq(user_id)
|
||||
expect(upload.original_filename).to eq(image_filename)
|
||||
expect(upload.filesize).to eq(image_filesize)
|
||||
expect(upload.width).to eq(244)
|
||||
expect(upload.height).to eq(66)
|
||||
expect(upload.url).to eq(url)
|
||||
end
|
||||
|
||||
context "when svg is authorized" do
|
||||
|
||||
before { SiteSetting.stubs(:authorized_extensions).returns("svg") }
|
||||
|
||||
it "consider SVG as an image" do
|
||||
store = {}
|
||||
Discourse.expects(:store).returns(store)
|
||||
store.expects(:store_upload).returns(url)
|
||||
|
||||
upload = Upload.create_for(user_id, image_svg, image_svg_filename, image_svg_filesize)
|
||||
|
||||
expect(upload.user_id).to eq(user_id)
|
||||
expect(upload.original_filename).to eq(image_svg_filename)
|
||||
expect(upload.filesize).to eq(image_svg_filesize)
|
||||
expect(upload.width).to eq(100)
|
||||
expect(upload.height).to eq(50)
|
||||
expect(upload.url).to eq(url)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context ".get_from_url" do
|
||||
let(:url) { "/uploads/default/original/3X/1/0/10f73034616a796dfd70177dc54b6def44c4ba6f.png" }
|
||||
let(:upload) { Fabricate(:upload, url: url) }
|
||||
|
Reference in New Issue
Block a user