-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Instead of configuring a `PictureThumb.generator_class` that needs to implement the creation of the `Alchemy::PictureThumb` records and make sure do that in a multi-concurrency safe way, we keep the implementation in core and abstract the storage class instead.
- Loading branch information
Showing
5 changed files
with
78 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
module Alchemy | ||
class PictureThumb < BaseRecord | ||
# Stores the render result of a Alchemy::PictureVariant | ||
# in the configured Dragonfly datastore | ||
# (Default: Dragonfly::FileDataStore) | ||
# | ||
class FileStore | ||
class << self | ||
# @param [Alchemy::PictureVariant] variant the to be rendered image | ||
# @param [String] uid The Unique Image Identifier the image is stored at | ||
# | ||
def call(variant, uid) | ||
# process the image | ||
image = variant.image | ||
# store the processed image | ||
image.to_file(server_path(uid)).close | ||
end | ||
|
||
private | ||
|
||
# Alchemys dragonfly datastore config seperates the storage path from the public server | ||
# path for security reasons. The Dragonfly FileDataStorage does not support that, | ||
# so we need to build the path on our own. | ||
def server_path(uid) | ||
dragonfly_app = ::Dragonfly.app(:alchemy_pictures) | ||
"#{dragonfly_app.datastore.server_root}/#{uid}" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe Alchemy::PictureThumb::FileStore do | ||
let(:image) { File.new(File.expand_path("../../../fixtures/image.png", __dir__)) } | ||
let(:picture) { FactoryBot.create(:alchemy_picture, image_file: image) } | ||
let!(:variant) { Alchemy::PictureVariant.new(picture, { size: "1x1" }) } | ||
let(:uid_path) { "pictures/#{picture.id}/1234" } | ||
|
||
let(:root_path) do | ||
datastore = Dragonfly.app(:alchemy_pictures).datastore | ||
datastore.server_root | ||
end | ||
|
||
subject(:store) do | ||
Alchemy::PictureThumb::FileStore.call(variant, "/#{uid_path}/image.png") | ||
end | ||
|
||
before do | ||
FileUtils.rm_rf("#{root_path}/#{uid_path}") | ||
end | ||
|
||
it "stores thumb on the disk" do | ||
expect { store }.to change { Dir.glob("#{root_path}/#{uid_path}").length }.by(1) | ||
end | ||
end |