Working s3 compatible file uplaods!

This commit is contained in:
2026-03-30 14:28:44 -04:00
parent 6152dcdeea
commit 830ee36f84
16 changed files with 664 additions and 15 deletions

View File

@@ -19,5 +19,9 @@ defmodule Mixer.Posts do
rpc_action :update_tweet, :update
rpc_action :destroy_tweet, :destroy
end
resource Mixer.Posts.Media do
rpc_action :read_media, :read
end
end
end

View File

@@ -5,7 +5,6 @@ defmodule Mixer.Posts.Media do
data_layer: AshPostgres.DataLayer,
authorizers: [Ash.Policy.Authorizer],
extensions: [
#AshStateMachine,
AshTypescript.Resource
]
@@ -19,7 +18,20 @@ defmodule Mixer.Posts.Media do
end
actions do
defaults [:read, :destroy, create: :*, update: :*]
defaults [:read]
create :upload do
accept [:s3_key]
change relate_actor(:user)
end
update :link_to_tweet do
accept [:tweet_id]
end
destroy :destroy do
primary? true
end
end
attributes do
@@ -29,12 +41,41 @@ defmodule Mixer.Posts.Media do
allow_nil? false
public? true
end
end
relationships do
belongs_to :tweet, Mixer.Posts.Tweet do
attribute :user_id, :uuid do
allow_nil? false
public? true
end
end
relationships do
belongs_to :user, Mixer.Accounts.User do
attribute_writable? true
allow_nil? false
public? true
end
belongs_to :tweet, Mixer.Posts.Tweet do
allow_nil? true
public? true
end
end
policies do
policy action_type(:read) do
authorize_if always()
end
policy action(:upload) do
authorize_if actor_present()
end
policy action(:link_to_tweet) do
authorize_if relates_to_actor_via(:user)
end
policy action_type(:destroy) do
authorize_if relates_to_actor_via(:user)
end
end
end

View File

@@ -0,0 +1,24 @@
defmodule Mixer.Posts.MediaUploader do
use Waffle.Definition
@async false
@versions [:original]
@extensions ~w(.jpg .jpeg .png .gif .webp .mp4 .mov)
def validate({file, _scope}) do
ext = file.file_name |> Path.extname() |> String.downcase()
if ext in @extensions, do: :ok, else: {:error, "unsupported file type #{ext}"}
end
def storage_dir(_version, {_file, scope}), do: "uploads/media/#{scope.id}"
def filename(_version, {file, _scope}) do
Path.basename(file.file_name, Path.extname(file.file_name))
end
def s3_object_headers(_version, {file, _scope}) do
[content_type: MIME.from_path(file.file_name)]
end
def acl(_version, _), do: :public_read
end

View File

@@ -30,8 +30,25 @@ defmodule Mixer.Posts.Tweet do
create :create do
upsert? true
accept [:content]
argument :media_id, :uuid, allow_nil?: true
change relate_actor(:user)
change transition_state(:posted)
change fn changeset, context ->
case Ash.Changeset.get_argument(changeset, :media_id) do
nil ->
changeset
media_id ->
Ash.Changeset.after_action(changeset, fn _changeset, tweet ->
Mixer.Posts.Media
|> Ash.get!(media_id, authorize?: false)
|> Ash.Changeset.for_update(:link_to_tweet, %{tweet_id: tweet.id})
|> Ash.update!(actor: context.actor)
{:ok, tweet}
end)
end
end
end
end
@@ -63,7 +80,7 @@ defmodule Mixer.Posts.Tweet do
public? true
end
has_many :s3_key, Mixer.Posts.Media do
has_many :media, Mixer.Posts.Media do
public? true
end
end