Skip to content

Commit

Permalink
FIX: Use correct version when generating file path for optimized image (
Browse files Browse the repository at this point in the history
  • Loading branch information
vinothkannans committed Jan 11, 2019
1 parent 0fa9269 commit f94c028
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/file_store/base_store.rb
Expand Up @@ -116,7 +116,8 @@ def get_path_for_upload(upload)

def get_path_for_optimized_image(optimized_image)
upload = optimized_image.upload
extension = "_#{OptimizedImage::VERSION}_#{optimized_image.width}x#{optimized_image.height}#{optimized_image.extension}"
version = optimized_image.version || 1
extension = "_#{version}_#{optimized_image.width}x#{optimized_image.height}#{optimized_image.extension}"
get_path_for("optimized".freeze, upload.id, upload.sha1, extension)
end

Expand Down
15 changes: 15 additions & 0 deletions spec/components/file_store/base_store_spec.rb
Expand Up @@ -19,4 +19,19 @@
end
end
end

describe '#get_path_for_optimized_image' do
let(:upload) { Fabricate(:upload) }
let(:optimized_path) { "optimized/1X/#{upload.sha1}_1_100x200.png" }

it 'should return the right path' do
optimized = Fabricate(:optimized_image, upload: upload, version: 1)
expect(FileStore::BaseStore.new.get_path_for_optimized_image(optimized)).to eq(optimized_path)
end

it 'should return the right path for `nil` version' do
optimized = Fabricate(:optimized_image, upload: upload, version: nil)
expect(FileStore::BaseStore.new.get_path_for_optimized_image(optimized)).to eq(optimized_path)
end
end
end
18 changes: 18 additions & 0 deletions spec/components/file_store/s3_store_spec.rb
Expand Up @@ -164,6 +164,24 @@
store.remove_upload(upload)
end

it "removes the optimized image from s3 with the right paths" do
optimized = Fabricate(:optimized_image, version: 1)
upload = optimized.upload
path = "optimized/1X/#{upload.sha1}_#{optimized.version}_#{optimized.width}x#{optimized.height}.png"

store.expects(:get_depth_for).with(upload.id).returns(0)
s3_helper.expects(:s3_bucket).returns(s3_bucket).at_least_once
optimized.update_attributes!(url: "//s3-upload-bucket.s3.dualstack.us-west-1.amazonaws.com/#{path}")
s3_object = stub

s3_bucket.expects(:object).with("tombstone/#{path}").returns(s3_object)
s3_object.expects(:copy_from).with(copy_source: "s3-upload-bucket/#{path}")
s3_bucket.expects(:object).with(path).returns(s3_object)
s3_object.expects(:delete)

store.remove_optimized_image(optimized)
end

describe "when s3_upload_bucket includes folders path" do
before do
SiteSetting.s3_upload_bucket = "s3-upload-bucket/discourse-uploads"
Expand Down
1 change: 1 addition & 0 deletions spec/fabricators/optimized_image_fabricator.rb
Expand Up @@ -5,4 +5,5 @@
width 100
height 200
url "138569_100x200.png"
version OptimizedImage::VERSION
end

1 comment on commit f94c028

@SamSaffron
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent, this is a big one!

Please sign in to comment.