Skip to content

Commit

Permalink
Merge pull request #4 from bettyblocks/feat/detect-more-mime-types
Browse files Browse the repository at this point in the history
Use mimetype to detect mime-types, when installed
  • Loading branch information
archan937 authored Nov 9, 2022
2 parents 9192590 + bf45cca commit c52dd52
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/elixir.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ jobs:
path: deps
key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }}
restore-keys: ${{ runner.os }}-mix-
- name: Install dependencies
- name: Install OS dependencies
env:
DEBIAN_FRONTEND: noninteractive
run: sudo apt install libfile-mimeinfo-perl shared-mime-info
- name: Install Elixir dependencies
run: mix deps.get
- name: Run tests
run: mix test
Expand Down
19 changes: 18 additions & 1 deletion lib/api/s3/file.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule FileStorageApi.API.S3.File do

@impl true
def upload(container_name, connection_name, filename, blob_name) do
[{_, file_mime_type}] = filename |> FileInfo.get_info() |> Map.to_list()
file_mime_type = mime_type(filename)
object = blob_name || Path.basename(filename)

container_name
Expand All @@ -31,6 +31,23 @@ defmodule FileStorageApi.API.S3.File do
|> request(connection_name)
end

defp mime_type(filename) do
case System.shell("which mimetype") do
{_location, 0} ->
{result, 0} = System.shell("mimetype #{filename}")

result
|> String.trim()
|> String.split(" ")
|> Enum.reverse()
|> hd()

{_empty, 1} ->
[{_, file_mime_type}] = filename |> FileInfo.get_info() |> Map.to_list()
file_mime_type
end
end

@impl true
def public_url(container_name, "/" <> file_path, start_time, expire_time, connection_name),
do: public_url(container_name, file_path, start_time, expire_time, connection_name)
Expand Down
11 changes: 11 additions & 0 deletions test/api/s3/file_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ defmodule FileStorageApi.API.S3.FileTest do
assert {:ok, Path.basename(file_path)} == File.upload("block-store-container", :default, file_path, nil)
end

test "upload a file with mime type application/javascript (requires mimetype to be installed)" do
file_path = "./test/support/hello.js"

expect(AwsMock, :request, fn operation, _config ->
assert %{http_method: :put, path: "hello.js", headers: %{"content-type" => "application/javascript"}} = operation
{:ok, %{status_code: 200}}
end)

assert {:ok, Path.basename(file_path)} == File.upload("block-store-container", :default, file_path, nil)
end

test "failing upload should return error tuple" do
file_path = "./test/support/test_icon.png"

Expand Down
1 change: 1 addition & 0 deletions test/support/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alert("Hello, world")

0 comments on commit c52dd52

Please sign in to comment.