8000 fix(storage): require a thumbnail for every huddl cover by Beehtrix · Pull Request #769 · huddlz-hq/huddlz · GitHub
[go: up one dir, main page]

Skip to content
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
2 changes: 1 addition & 1 deletion lib/huddlz/communities/huddl_cover_image.ex
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ defmodule Huddlz.Communities.HuddlCoverImage do
end

attribute :thumbnail_path, :string do
allow_nil? true
allow_nil? false
public? true
description "Path to the resized thumbnail (1280x720) in storage"
end
Expand Down
22 changes: 6 additions & 16 deletions lib/huddlz/storage/huddl_cover_images.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ defmodule Huddlz.Storage.HuddlCoverImages do
Returns metadata suitable for creating a `HuddlCoverImage` record. If either copy
fails, any destination file already written is removed.
"""
def duplicate(image, destination) do
def duplicate(%{thumbnail_path: source_thumbnail} = image, destination)
when is_binary(source_thumbnail) and source_thumbnail != "" do
# The stored extension names the format written, which the filename may not.
storage_path = duplicate_path(destination, image.storage_path)
thumbnail_path = duplicate_thumbnail_path(image, storage_path)
thumbnail_path = generate_thumbnail_path(storage_path)

with {:ok, _path} <- Storage.copy(image.storage_path, storage_path, image.content_type),
:ok <- duplicate_thumbnail(image.thumbnail_path, thumbnail_path) do
{:ok, _path} <- Storage.copy(source_thumbnail, thumbnail_path, "image/jpeg") do
{:ok,
%{
filename: image.filename,
Expand All @@ -82,6 +83,8 @@ defmodule Huddlz.Storage.HuddlCoverImages do
end
end

def duplicate(_image, _destination), do: {:error, :missing_thumbnail}

@doc """
Delete a huddl image by its storage path.
"""
Expand Down Expand Up @@ -134,19 +137,6 @@ defmodule Huddlz.Storage.HuddlCoverImages do

defp duplicate_path(huddl_id, filename), do: generate_path(huddl_id, filename)

defp duplicate_thumbnail_path(thumbnail_path, storage_path) do
if thumbnail_path, do: generate_thumbnail_path(storage_path)
end

defp duplicate_thumbnail(nil, nil), do: :ok

defp duplicate_thumbnail(source_path, destination_path) do
case Storage.copy(source_path, destination_path, "image/jpeg") do
{:ok, _path} -> :ok
{:error, reason} -> {:error, reason}
end
end

@doc """
Returns the list of allowed file extensions.
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule Huddlz.Repo.Migrations.RequireHuddlCoverThumbnail do
@moduledoc """
Updates resources based on their most recent snapshots.

This file was autogenerated with `mix ash_postgres.generate_migrations`
"""

use Ecto.Migration

def up do
alter table(:huddl_cover_images) do
modify :thumbnail_path, :text, null: false
end
end

def down do
alter table(:huddl_cover_images) do
modify :thumbnail_path, :text, null: true
end
end
end
262 changes: 262 additions & 0 deletions priv/resource_snapshots/repo/huddl_cover_images/20261205015911.json
8
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
{
"attributes": [
{
"allow_nil?": false,
"default": "fragment(\"gen_random_uuid()\")",
"generated?": false,
"precision": null,
"primary_key?": true,
"references": null,
"scale": null,
"size": null,
"source": "id",
"type": "uuid"
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"precision": null,
"primary_key?": false,
"references": null,
"scale": null,
"size": null,
"source": "uploaded_by_id",
"type": "uuid"
},
{
"allow_nil?": false,
"default": "nil",
"generated?": false,
"precision": null,
"primary_key?": false,
"references": null,
"scale": null,
"size": null,
"source": "filename",
"type": "text"
},
{
"allow_nil?": false,
"default": "nil",
"generated?": false,
"precision": null,
"primary_key?": false,
"references": null,
"scale": null,
"size": null,
"source": "content_type",
"type": "text"
},
{
"allow_nil?": false,
"default": "nil",
"generated?": false,
"precision": null,
"primary_key?": false,
"references": null,
"scale": null,
"size": null,
"source": "size_bytes",
"type": "bigint"
},
{
"allow_nil?": false,
"default": "nil",
"generated?": false,
"precision": null,
"primary_key?": false,
"references": null,
"scale": null,
"size": null,
"source": "storage_path",
"type": "text"
},
{
"allow_nil?": false,
"default": "nil",
"generated?": false,
"precision": null,
"primary_key?": false,
"references": null,
"scale": null,
"size": null,
"source": "thumbnail_path",
"type": "text"
},
{
"allow_nil?": false,
"default": "fragment(\"(now() AT TIME ZONE 'utc')\")",
"generated?": false,
"precision": null,
"primary_key?": false,
"references": null,
"scale": null,
"size": null,
"source": "inserted_at",
"type": "utc_datetime_usec"
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"precision": null,
"primary_key?": false,
"references": null,
"scale": null,
"size": null,
"source": "deleted_at",
"type": "utc_datetime_usec"
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"precision": null,
"primary_key?": false,
"references": {
"deferrable": false,
"destination_attribute": "id",
"destination_attribute_default": null,
"destination_attribute_generated": null,
"index?": false,
"index_where": null,
"match_tenant?": false,
"match_type": null,
"match_with": null,
"multitenancy": {
"attribute": null,
"global": null,
"strategy": null
},
"name": "huddl_cover_images_pending_group_id_fkey",
"on_delete": "nilify",
"on_update": null,
"primary_key?": true,
"schema": "public",
"table": "groups"
},
"scale": null,
"size": null,
"source": "pending_group_id",
"type": "uuid"
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"precision": null,
"primary_key?": false,
"references": {
"deferrable": false,
"destination_attribute": "id",
"destination_attribute_default": null,
"destination_attribute_generated": null,
"index?": false,
"index_where": null,
"match_tenant?": false,
"match_type": null,
"match_with": null,
"multitenancy": {
"attribute": null,
"global": null,
"strategy": null
},
"name": "huddl_cover_images_huddl_id_fkey",
"on_delete": "delete",
"on_update": null,
"primary_key?": true,
"schema": "public",
"table": "huddlz"
},
"scale": null,
"size": null,
"source": "huddl_id",
"type": "uuid"
}
],
"base_filter": null,
"check_constraints": [],
"create_table_options": null,
"custom_indexes": [
{
"all_tenants?": false,
"concurrently": false,
"error_fields": [
"thumbnail_path"
],
"fields": [
{
"type": "atom",
"value": "thumbnail_path"
}
],
"include": null,
"include_base_filter?": true,
"message": null,
"name": null,
"nulls_distinct": true,
"prefix": null,
"table": null,
"unique": false,
"using": null,
"where": null
},
{
"all_tenants?": false,
"concurrently": false,
"error_fields": [
"huddl_id",
"inserted_at"
],
"fields": [
{
"type": "atom",
"value": "huddl_id"
},
{
"type": "atom",
"value": "inserted_at"
}
],
"include": null,
"include_base_filter?": true,
"message": null,
"name": null,
"nulls_distinct": true,
"prefix": null,
"table": null,
"unique": false,
"using": null,
"where": null
}
],
"custom_statements": [],
"has_create_action": true,
"hash": "85D3D07B0E83A71864BCEC2F3B7EBB99714AFC77CA48A1D318F67356BC3B9CF5",
"identities": [
{
"all_tenants?": false,
"base_filter": null,
"index_name": "huddl_cover_images_unique_storage_path_index",
"keys": [
{
"type": "atom",
"value": "storage_path"
}
],
"name": "unique_storage_path",
"nils_distinct?": true,
"where": null
}
],
"multitenancy": {
"attribute": null,
"global": null,
"strategy": null
},
"repo": "Elixir.Huddlz.Repo",
"schema": null,
"table": "huddl_cover_images",
"temporal": null
}
6 changes: 5 additions & 1 deletion test/huddlz/admin_access_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule Huddlz.AdminAccessTest do
alias Huddlz.Communities.GroupLocation
alias Huddlz.Communities.Huddl
alias Huddlz.Communities.HuddlCoverImage
alias Huddlz.Storage.HuddlCoverImages

setup do
admin = generate(user(role: :admin))
Expand All @@ -35,11 +36,14 @@ defmodule Huddlz.AdminAccessTest do
end

test "private media is hidden from nonmembers including administrators", ctx do
path = "/uploads/private-#{Ash.UUID.generate()}.jpg"

attrs = %{
filename: "private.jpg",
content_type: "image/jpeg",
size_bytes: 1000,
storage_path: "/uploads/private-#{Ash.UUID.generate()}.jpg"
storage_path: path,
thumbnail_path: HuddlCoverImages.generate_thumbnail_path(path)
}

group_image =
Expand Down
6 changes: 5 additions & 1 deletion test/huddlz/audit_nested_actions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule Huddlz.AuditNestedActionsTest do
alias Huddlz.Communities.Huddl
alias Huddlz.Communities.HuddlAttendee
alias Huddlz.Communities.HuddlCoverImage
alias Huddlz.Storage.HuddlCoverImages

setup do
owner = generate(user())
Expand Down Expand Up @@ -265,11 +266,14 @@ defmodule Huddlz.AuditNestedActionsTest do
end

defp image_attrs do
path = "/uploads/pending/audit-#{Ash.UUID.generate()}.jpg"

%{
filename: "audit.jpg",
content_type: "image/jpeg",
size_bytes: 1000,
storage_path: "/uploads/pending/audit-#{Ash.UUID.generate()}.jpg"
storage_path: path,
thumbnail_path: HuddlCoverImages.generate_thumbnail_path(path)
}
end

Expand Down
Loading
0