Skip to content

Commit

Permalink
#828 and #924
Browse files Browse the repository at this point in the history
  • Loading branch information
mayel committed Aug 18, 2024
1 parent 28e77f0 commit da926f3
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 63 deletions.
92 changes: 29 additions & 63 deletions flavours/classic/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -155,71 +155,37 @@ config :paper_trail,

config :nx, default_backend: EXLA.Backend

# NOTE: need to declare types to avoid LV uploads failing with `invalid accept filter provided to allow_upload. Expected a file extension with a known MIME type.`
config :mime, :types, %{
"application/json" => ["json"],
"application/activity+json" => ["activity+json"],
"application/ld+json" => ["ld+json"],
"application/jrd+json" => ["jrd+json"],
# images
"image/png" => ["png"],
"image/apng" => ["apng"],
"image/jpeg" => ["jpg", "jpeg"],
"image/gif" => ["gif"],
"image/svg+xml" => ["svg"],
"image/webp" => ["webp"],
"image/tiff" => ["tiff"],
# text
"text/plain" => ["txt"],
"text/markdown" => ["md"],
# doc
"text/csv" => ["csv"],
"text/tab-separated-values" => ["tsv"],
"application/pdf" => ["pdf"],
"application/rtf" => ["rtf"],
"application/msword" => ["doc", "dot"],
"application/vnd.openxmlformats-officedocument.wordprocessingml.document" => ["docx"],
"application/vnd.ms-excel" => ["xls"],
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" => ["xlsx"],
"application/vnd.oasis.opendocument.presentation" => ["odp"],
"application/vnd.oasis.opendocument.spreadsheet" => ["ods"],
"application/vnd.oasis.opendocument.text" => ["odt"],
"application/epub+zip" => ["epub"],
# archives
"application/x-tar" => ["tar"],
"application/x-bzip" => ["bzip"],
"application/x-bzip2" => ["bzip2"],
"application/gzip" => ["gz", "gzip"],
"application/zip" => ["zip"],
"application/vnd.rar" => ["rar"],
"application/x-7z-compressed" => ["7z"],
# audio
"audio/aac" => ["aac"],
"audio/mpeg" => ["mpa", "mp2"],
"audio/mp3" => ["mp3"],
"audio/ogg" => ["oga"],
"audio/wav" => ["wav"],
"audio/m4a" => ["m4a"],
"audio/x-m4a" => ["m4a"],
"audio/mp4" => ["m4a", "mp4"],
# "audio/webm"=> ["webm"],
"audio/opus" => ["opus"],
"audio/flac" => ["flac"],
# video
"video/mp4" => ["mp4"],
"video/mpeg" => ["mpeg"],
"video/ogg" => ["ogg", "ogv"],
"video/webm" => ["webm"],
"video/x-matroska" => ["mkv"],
"application/x-matroska" => ["mkv"]
}
Code.eval_file(
"mime_types.ex",
cond do
File.exists?("extensions/bonfire_files/lib/mime_types.ex") -> "extensions/bonfire_files/lib/"
File.exists?("deps/bonfire_files/lib/mime_types.ex") -> "deps/bonfire_files/lib/"
true -> "config/"
end
)

# NOTE: need to declare any types we want to allow to upload to avoid LiveView uploads failing with `invalid accept filter provided to allow_upload. Expected a file extension with a known MIME type.`
config :mime,
:types,
Map.merge(
%{
"application/json" => ["json"],
"application/activity+json" => ["activity+json"],
"application/ld+json" => ["ld+json"],
"application/jrd+json" => ["jrd+json"],
"application/x-tar" => ["tar"],
"application/x-bzip" => ["bzip"],
"application/x-bzip2" => ["bzip2"],
"application/gzip" => ["gz", "gzip"],
"application/zip" => ["zip"],
"application/vnd.rar" => ["rar"],
"application/x-7z-compressed" => ["7z"]
},
Bonfire.Files.MimeTypes.supported_media()
)

# define which is preferred when more than one
config :mime, :extensions, %{
"mkv" => "video/x-matroska",
"m4a" => "audio/m4a",
"mp4" => "video/mp4"
}
config :mime, :extensions, Bonfire.Files.MimeTypes.unique_extension_for_mime()

config :os_mon,
disk_space_check_interval: 60,
Expand Down
39 changes: 39 additions & 0 deletions flavours/classic/config/mime_types.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
defmodule Bonfire.Files.MimeTypes do
# NOTE: this file is here just as a fallback and is intended to get overridden by bonfire_files extension

def supported_media,
do:
Map.merge(image_media(), video_media())
|> Map.merge(extra_media())

# TODO: how can we make these editable or at least extensible with ENV vars?

# NOTE: first extension will be considered canonical

def image_media,
do: %{
"image/png" => ["png"]
}

def video_media, do: %{}

def extra_media,
do: %{
"application/json" => ["json"],
"application/activity+json" => ["activity+json"],
"application/ld+json" => ["ld+json"],
"application/jrd+json" => ["jrd+json"]
}

# define which is preferred when more than one
def unique_extension_for_mime do
supported_media()
|> Enum.flat_map(fn {mime, extensions} ->
extensions
|> Enum.reverse()
|> Enum.map(fn ext -> {ext, mime} end)
end)
# |> Enum.uniq_by(fn {x, _} -> x end)
|> Map.new()
end
end

0 comments on commit da926f3

Please sign in to comment.