Skip to content

Commit

Permalink
Arc upload fix (#404)
Browse files Browse the repository at this point in the history
## Why?
- Since we need to fetch the tenant name before storing, fetching and deleting images according to the tenant, hence, we have set the tenant name in tenant field of scope.

## This change addresses the need by:
- Setting the tenant field to Repo.get_prefix for product brand, general configuration, taxon and 
   product before passing it to the following functions of arc to store the images tenantwise:
 1. store
 2. url
 3. delete

[delivers #162948170]
  • Loading branch information
jyotigautam authored and pkrawat1 committed Jan 3, 2019
1 parent 102440c commit 1743269
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ erl_crash.dump

/vendor/
/uploads/images/

**/.DS_Store
3 changes: 2 additions & 1 deletion apps/admin_app/lib/admin_app/etsy_importer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ defmodule Avia.Etsy.Importer do
@store_consumer_secret "ETSY_CONSUMER_SECRET"

alias Snitch.Data.Model.StoreProps
alias Snitch.Data.Model.Image, as: ImageModel
alias Snitch.Tools.Helper.Taxonomy, as: TaxonomyHelper
alias Snitch.Repo
alias Snitch.Data.Schema.{Product, Taxon, Image}
Expand Down Expand Up @@ -157,7 +158,7 @@ defmodule Avia.Etsy.Importer do
path: file_path
}

{:ok, filename} = ImageUploader.store({upload, product})
{:ok, filename} = ImageModel.store(upload, product)
filename
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,7 @@ defmodule AdminAppWeb.ProductController do
end

def delete(conn, %{"id" => id} = params) do
with _ <- ESProductStore.update_product_to_es(id, :delete),
{:ok, _product} <- ProductModel.delete(id) do
with {:ok, _product} <- ProductModel.delete(id) do
conn
|> put_flash(:info, "Product deleted successfully")
|> redirect_with_updated_conn(params)
Expand Down
9 changes: 9 additions & 0 deletions apps/snitch_core/lib/core/data/model/images.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ defmodule Snitch.Data.Model.Image do
def delete_image_multi(multi, image, struct) do
multi
|> Multi.run(:remove_from_upload, fn _ ->
struct = %{struct | tenant: Repo.get_prefix()}

case ImageUploader.delete({image.name, struct}) do
:ok ->
{:ok, "success"}
Expand All @@ -78,9 +80,15 @@ defmodule Snitch.Data.Model.Image do
end)
end

def store(image, struct) do
struct = %{struct | tenant: Repo.get_prefix()}
ImageUploader.store({image, struct})
end

def upload_image_multi(multi, %{filename: name, path: path, type: type} = image) do
Multi.run(multi, :image_upload, fn %{struct: struct} ->
image = %Plug.Upload{filename: name, path: path, content_type: type}
struct = %{struct | tenant: Repo.get_prefix()}

case ImageUploader.store({image, struct}) do
{:ok, _} ->
Expand Down Expand Up @@ -112,6 +120,7 @@ defmodule Snitch.Data.Model.Image do
struct.
"""
def image_url(name, struct, version \\ :thumb) do
struct = %{struct | tenant: Repo.get_prefix()}
ImageUploader.url({name, struct}, version)
end

Expand Down
6 changes: 5 additions & 1 deletion apps/snitch_core/lib/core/data/model/product.ex
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ defmodule Snitch.Data.Model.Product do
@spec get(integer) :: {:ok, Product.t()} | {:error, Ecto.Changeset.t()} | nil
def delete(id) do
with %Product{} = product <- get(id),
_ <- ESProductStore.update_product_to_es(product, :delete),
changeset <- Product.delete_changeset(product) do
product = product |> Repo.preload(:images)
Enum.map(product.images, &delete_image(product.id, &1.id))
Repo.update(changeset)
end
end
Expand Down Expand Up @@ -290,7 +293,8 @@ defmodule Snitch.Data.Model.Product do
Enum.map(uploads, fn
%{"image" => %{filename: name, path: path, url: url, type: type} = upload} ->
upload = %Plug.Upload{filename: name, path: path, content_type: type}
ImageUploader.store({upload, product})
product = %{product | tenant: Repo.get_prefix()}
ImageModel.store(upload, product)

_ ->
{:ok, "success"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ defmodule Snitch.Data.Schema.GeneralConfiguration do
field(:backend_url, :string)
field(:currency, :string)
field(:hosted_payment_url, :string)
field(:tenant, :string, virtual: true)

has_one(:store_image, StoreLogo, on_replace: :delete)
has_one(:image, through: [:store_image, :image])
Expand Down
1 change: 1 addition & 0 deletions apps/snitch_core/lib/core/data/schema/product_brand.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Snitch.Data.Schema.ProductBrand do

schema "snitch_product_brands" do
field(:name, :string, null: false)
field(:tenant, :string, virtual: true)
timestamps()

has_many(:products, Product, foreign_key: :brand_id)
Expand Down
1 change: 1 addition & 0 deletions apps/snitch_core/lib/core/data/schema/taxanomy/taxon.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ defmodule Snitch.Data.Schema.Taxon do
field(:rgt, :integer)
field(:variation_theme_ids, {:array, :binary}, virtual: true)
field(:slug, :string)
field(:tenant, :string, virtual: true)

has_one(:taxon_image, TaxonImage, on_replace: :delete)
has_one(:image, through: [:taxon_image, :image])
Expand Down
3 changes: 1 addition & 2 deletions apps/snitch_core/lib/core/tools/helpers/image_uploader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ defmodule Snitch.Tools.Helper.ImageUploader do
end

def storage_dir(version, {_file, scope}) do
tenant = Repo.get_prefix()
scope_dir = get_scope_name(scope)
"uploads/#{tenant}/images/#{scope_dir}/#{scope.id}/images/#{version}"
"uploads/#{scope.tenant}/images/#{scope_dir}/#{scope.id}/images/#{version}"
end

defp get_scope_name(scope) do
Expand Down
2 changes: 1 addition & 1 deletion apps/snitch_core/priv/repo/demo/products.ex
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ defmodule Snitch.Demo.Product do
end

defp upload_image(%Plug.Upload{} = image, product) do
case ImageUploader.store({image, product}) do
case ImageModel.store(image, product) do
{:ok, _} ->
{:ok, product}

Expand Down

0 comments on commit 1743269

Please sign in to comment.