Hash :
2075e2e4
Author :
Thomas de Grivel
Date :
2024-10-14T16:10:35
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
defmodule FXView do
require HTML
require List
require Str
require URL
def path_links = fn {
(path) {
[_dot | path_list] = Str.split(path, "/")
path_links(path_list, "/", [])
}
([], _, acc) { List.join(List.reverse(acc), "/") }
([first | rest], url, acc) {
a = """
<a href="#{URL.escape(url + first)}">#{HTML.escape(first)}</a>
"""
path_links(rest, "#{url}#{first}/", [a | acc])
}
}
def nav2_template =
EKC3.load("app/templates/fx/nav2.html.ekc3")
def menu_index_template =
EKC3.load("app/templates/fx/menu_index.html.ekc3")
def index_template =
EKC3.load("app/templates/fx/index.html.ekc3")
def properties_template =
EKC3.load("app/templates/fx/properties.html.ekc3")
def show_file_template =
EKC3.load("app/templates/fx/show_file.html.ekc3")
def render_index = fn (index) {
EKC3.render(index_template)
}
def render_menu_index = fn (index) {
EKC3.render(menu_index_template)
}
def render_properties = fn (properties) {
EKC3.render(properties_template)
}
def render_show_file_preview = fn (path, size) {
file_size = File.size(path)
# mime = HTTP.mime_type(File.ext(path))
mime = system(["file", "-bi", path])
# puts("mime: #{inspect(mime)}")
mime_first = (Sym) first(Str.split((Str) mime, "/"))
# puts("mime_first: #{inspect(mime_first)}")
file_path = "/file" + Str.slice(path, 4, -1)
preview = if (mime_first == :image) do
if (size == :small && file_size > 16024024) do
""
else
"""<img class="#{size}" src="#{URL.escape(file_path)}" alt="#{HTML.escape(path)}" />"""
end
else
if (mime_first == :video) do
"""<video class="#{size}" controls><source src="#{URL.escape(file_path)}" /></video>"""
else
if (mime_first == :audio) do
"""<audio class="#{size}" controls src="#{URL.escape(file_path)}" />"""
else
if (mime_first == :text ||
Str.starts_with?(mime, "application/javascript") ||
mime == "application/x-shellscript\n") do
file = if (size == :small && file_size > 77) do
Str.slice(File.read_max(path, 77), 0, 77) + "..."
else
File.read_all(path)
end
"""<pre class="#{size}"><code class="language-#{File.ext(path)}">#{HTML.escape(file)}</code> </pre>"""
else
file = if (size == :small) do
sh_path = Sh.escape(path)
system(["sh", "-c",
"head -n 1 #{sh_path} | cut -b 1-47 | hexdump -C | head -n 3"])
else
system(["hexdump", "-C", path])
end
"""<pre class="#{size}">#{HTML.escape(file)}</pre>"""
end
end
end
end
"""<span class="size">#{HTML.escape(human_size(file_size))}</span>
<span class="file">#{HTML.escape(system(["file", "-b", path]))}</span>
<div class="#{size}">#{preview}<hr /></div>"""
}
def render_show_file = fn (path, menu, file, properties) {
nav2 = EKC3.render(nav2_template)
EKC3.render(show_file_template)
}
end