Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not create thumbs for invalid pictures #16

Merged
merged 3 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions bin/rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

#
# This file was generated by Bundler.
#
# The application 'rspec' is installed as part of a gem, and
# this file is here to facilitate running it.
#

ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)

bundle_binstub = File.expand_path("bundle", __dir__)

if File.file?(bundle_binstub)
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
load(bundle_binstub)
else
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
end
end

require "rubygems"
require "bundler/setup"

load Gem.bin_path("rspec-core", "rspec")
17 changes: 10 additions & 7 deletions lib/alchemy/dragonfly/s3/create_picture_thumb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@ class CreatePictureThumb
def self.call(variant, signature, uid)
# create the thumb before uploading
# to prevent db race conditions
thumb = variant.picture.thumbs.create!(
picture: variant.picture,
signature: signature,
uid: uid,
)
thumb = nil
if variant.picture.valid?
thumb = Alchemy::PictureThumb.create!(
picture: variant.picture,
signature: signature,
uid: uid,
)
end
begin
# fetch and process the image
image = variant.image
# upload the processed image
image.store(path: uid)
rescue RuntimeError, Excon::Error => e
Rails.logger.warn(e)
ErrorTracking.notification_handler.call(e)
# destroy the thumb if processing or upload fails
thumb.destroy
thumb&.destroy
end
end
end
Expand Down
11 changes: 10 additions & 1 deletion spec/alchemy/dragonfly/s3/create_picture_thumb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@
end

it "creates thumb on picture thumbs collection" do
expect { subject }.to change { variant.picture.thumbs.length }.by(1)
expect { subject }.to change { variant.picture.thumbs.reload.length }.by(1)
end

context "with an invalid picture" do
let(:picture) { FactoryBot.build(:alchemy_picture) }

it "does not create a thumb" do
expect(picture).to receive(:valid?) { false }
expect { subject }.not_to change { variant.picture.thumbs.reload.length }
end
end

context "on processing errors" do
Expand Down