Skip to content

Commit

Permalink
avoid force utf8 encoding and also handle mime type for file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
zoedsoupe committed Jan 4, 2025
1 parent e9293bc commit 4b81eb6
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ jobs:
lint:
runs-on: ubuntu-latest

env:
MIX_ENV: test

strategy:
matrix:
elixir: [1.18.1]
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ result
# Nix files
result

# LSP files
.elixir_ls/
.elixir-tools/
.lexical/
7 changes: 7 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Config

if config_env() == :dev do
config :supabase_potion, SupabasePotion.Client,
base_url: System.fetch_env!("SUPABASE_URL"),
api_key: System.fetch_env!("SUPABASE_KEY")
end
3 changes: 3 additions & 0 deletions config/runtime.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Config

config :supabase_potion, env: config_env()
16 changes: 15 additions & 1 deletion lib/supabase/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,23 @@ defmodule Supabase.Application do

@impl true
def start(_start_type, _args) do
children = [{Finch, @finch_opts}]
children =
[{Finch, @finch_opts}]
|> maybe_append_child(fn e -> e == :dev end, SupabasePotion.Client)

opts = [strategy: :one_for_one, name: Supabase.Supervisor]

Supervisor.start_link(children, opts)
end

@spec maybe_append_child(list(Supervisor.child_spec()), (env -> bool), Supervisor.child_spec()) ::
list(Supervisor.child_spec())
when env: :dev | :prod | :test
defp maybe_append_child(children, pred, child) do
env = get_env()

if pred.(env), do: children ++ [child], else: children
end

defp get_env, do: Application.get_env(:supabase_potion, :env, :dev)
end
7 changes: 5 additions & 2 deletions lib/supabase/fetcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,18 @@ defmodule Supabase.Fetcher do
"""
@impl true
def upload(method, url, file, headers \\ []) do
body_stream = File.stream!(file, 4096, encoding: :utf8)
mime_type = MIME.from_path(file)
body_stream = File.stream!(file, 2048, [:raw])
%File.Stat{size: content_length} = File.stat!(file)
content_headers = [{"content-length", to_string(content_length)}]
content_headers = [{"content-length", to_string(content_length)}, {"content-type", mime_type}]
headers = merge_headers(headers, content_headers)
conn = new_connection(method, url, {:stream, body_stream}, headers)

conn
|> Finch.request(Supabase.Finch)
|> format_response()
rescue
e in File.Error -> {:error, e.reason}
end

def get_full_url(base_url, path) do
Expand Down
7 changes: 6 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ defmodule Supabase.MixProject do
deps: deps(),
docs: docs(),
package: package(),
description: description()
description: description(),
elixirc_paths: elixirc_paths(Mix.env())
]
end

defp elixirc_paths(e) when e in [:dev, :test], do: ["lib", "priv", "test/support"]
defp elixirc_paths(_), do: ["lib"]

def application do
[
mod: {Supabase.Application, []},
Expand All @@ -26,6 +30,7 @@ defmodule Supabase.MixProject do

defp deps do
[
{:mime, "~> 2.0"},
{:finch, "~> 0.16"},
{:jason, "~> 1.4"},
{:ecto, "~> 3.10"},
Expand Down
5 changes: 5 additions & 0 deletions priv/local/client.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# local client to be used in tests and also
# dev env
defmodule SupabasePotion.Client do
use Supabase.Client, otp_app: :supabase_potion
end
2 changes: 1 addition & 1 deletion supabase/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ enabled = false
# max_header_length = 4096

[studio]
enabled = false
enabled = true
# Port to use for Supabase Studio.
port = 54323
# External URL of the API server that frontend connects to.
Expand Down

0 comments on commit 4b81eb6

Please sign in to comment.