Hash :
d0ebd60c
Author :
Thomas de Grivel
Date :
2024-09-17T12:21:04
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 103 104 105 106 107 108 109 110 111 112
defmodule FXController do
require File
require List
require Str
def fx_index = fn {
(path, target) { fx_index(path, target, path, []) }
([], target, dir, acc) { List.reverse(acc) }
([file | rest], target, dir, acc) {
path = dir + file
if (Str.starts_with?(file, ".") ||
File.is_directory?(path)) do
fx_index(rest, target, dir, acc)
else
url = Str.slice(path, 1, -1)
if File.is_directory?(path) do
#items = fx_index(List.sort(File.list(path)), target,
# path + "/", [])
items = []
item = %{type: :dir,
url: url,
name: file,
items: items}
fx_index(rest, target, dir, [item | acc])
else
item = %{type: :file,
url: url,
name: file,
items: []}
fx_index(rest, target, dir, [item | acc])
end
end
}
(path, target, dir, acc) {
if (type(path) == Str) do
fx_index(List.sort(File.list(path)), target, dir, acc)
end
}
}
def get_menu_index = fn {
(path, target) { get_menu_index(path, target, path, []) }
([], target, dir, acc) { List.reverse(acc) }
([file | rest], target, dir, acc) {
path = dir + file
if (Str.starts_with?(file, ".") ||
! File.is_directory?(path)) do
get_menu_index(rest, target, dir, acc)
else
url = Str.slice(path, 1, -1)
if File.is_directory?(path) do
items = []
item = %{type: :dir,
url: url,
name: file,
items: items}
get_menu_index(rest, target, dir, [item | acc])
else
item = %{type: :file,
url: url,
name: file,
items: []}
get_menu_index(rest, target, dir, [item | acc])
end
end
}
(path, target, dir, acc) {
if (type(path) == Str) do
get_menu_index(List.sort(File.list(path)), target, dir, acc)
end
}
}
def get_properties = fn (path) {
Facts.collect_with_tags(Facts.env_facts(), path, ?, ?,
fn (fact) { {fact.predicate, fact.object} })
}
def show_file = fn (path) {
if (File.exists?(path)) do
if (File.is_directory?(path)) do
slash = if Str.ends_with?(path, "/") do "" else "/" end
path = path + slash
menu_index = get_menu_index(path, path)
menu = FXView.render_index(menu_index)
index = fx_index(path, path)
file = FXView.render_index(index)
else
menu_path = File.dirname(path)
menu_index = get_menu_index(menu_path, path)
menu = FXView.render_index(menu_index)
file = FXView.render_show_file_preview(path)
end
properties = get_properties(path)
page = FXView.render_show_file(path, menu, file, properties)
body = LayoutView.render(path, page)
%HTTP.Response{body: body}
end
}
def route = fn (req) {
if ((req.method == GET ||
req.method == HEAD) &&
(req.url == "/fx" ||
Str.starts_with?(req.url, "/fx/"))) do
path = "." + req.url
show_file(path)
end
}
end