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:
Régis Hanol
2017-05-11 00:16:57 +02:00
parent a5c4ddd334
commit 9641d2413d
27 changed files with 391 additions and 483 deletions

View File

@@ -48,7 +48,7 @@ describe UploadsController do
end
it 'is successful with an image' do
Jobs.expects(:enqueue).with(:create_thumbnails, anything)
Jobs.expects(:enqueue).with(:create_avatar_thumbnails, anything)
message = MessageBus.track_publish do
xhr :post, :create, file: logo, type: "avatar"
@@ -78,7 +78,7 @@ describe UploadsController do
SiteSetting.authorized_extensions = "*"
controller.stubs(:is_api?).returns(true)
Jobs.expects(:enqueue).with(:create_thumbnails, anything)
Jobs.expects(:enqueue).with(:create_avatar_thumbnails, anything)
stub_request(:get, "http://example.com/image.png").to_return(body: File.read('spec/fixtures/images/logo.png'))
@@ -92,7 +92,7 @@ describe UploadsController do
it 'correctly sets retain_hours for admins' do
log_in :admin
Jobs.expects(:enqueue).with(:create_thumbnails, anything)
Jobs.expects(:enqueue).with(:create_avatar_thumbnails, anything).never
message = MessageBus.track_publish do
xhr :post, :create, file: logo, retain_hours: 100, type: "profile_background"
@@ -110,7 +110,7 @@ describe UploadsController do
end.first
expect(response.status).to eq 200
expect(message.data["errors"]).to eq(I18n.t("upload.file_missing"))
expect(message.data["errors"]).to contain_exactly(I18n.t("upload.file_missing"))
end
it 'properly returns errors' do
@@ -139,7 +139,7 @@ describe UploadsController do
end
it 'returns an error when it could not determine the dimensions of an image' do
Jobs.expects(:enqueue).with(:create_thumbnails, anything).never
Jobs.expects(:enqueue).with(:create_avatar_thumbnails, anything).never
message = MessageBus.track_publish do
xhr :post, :create, file: fake_jpg, type: "composer"
@@ -148,8 +148,7 @@ describe UploadsController do
expect(response.status).to eq 200
expect(message.channel).to eq("/uploads/composer")
expect(message.data["errors"]).to be
expect(message.data["errors"][0]).to eq(I18n.t("upload.images.size_not_found"))
expect(message.data["errors"]).to contain_exactly(I18n.t("upload.images.size_not_found"))
end
end

View File

@@ -171,7 +171,7 @@ HTML
it 'can handle uploads based of ThemeField' do
theme = Theme.new(name: 'theme', user_id: -1)
upload = Upload.create_for(-1, image, "logo.png", File.size(image))
upload = UploadCreator.new(image, "logo.png").create_for(-1)
theme.set_field(target: :common, name: :logo, upload_id: upload.id, type: :theme_upload_var)
theme.set_field(target: :common, name: :scss, value: 'body {background-image: url($logo)}')
theme.save!

View File

@@ -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) }