Branch
Hash :
91e07948
Author :
Thomas de Grivel
Date :
2024-11-26T01:34:02
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
defmodule FXController do
require File
require HTTP
require List
require Str
def file_index = fn {
(path, target) { file_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
file_index(rest, target, dir, acc)
else
url = Str.slice(path, 1, -1)
item = %{type: :file,
url: url,
name: file,
path: path}
file_index(rest, target, dir, [item | acc])
end
}
(path, target, dir, acc) {
if (type(path) == Str) do
file_index(List.sort(File.list(path)), target, dir, acc)
end
}
}
def directory_index = fn {
(path, target) { directory_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
directory_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}
directory_index(rest, target, dir, [item | acc])
end
end
}
(path, target, dir, acc) {
if (type(path) == Str) do
directory_index(List.sort(File.list(path)), target, dir, acc)
end
}
}
def show_file = fn (path, url) {
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 = directory_index(path, path)
menu = FXView.render_menu_index(menu_index)
index = file_index(path, path)
file = FXView.render_index(index)
else
menu_path = File.dirname(path)
menu_index = directory_index(menu_path, path)
menu = FXView.render_menu_index(menu_index)
file = FXView.render_show_file_preview(path, :full)
end
properties = Fx.property_index(path)
tags = Fx.tag_index(path)
properties = FXView.render_properties(properties)
page = FXView.render_show_file(path, menu, file, properties)
body = LayoutView.render(path, page, url)
%HTTP.Response{body: body}
end
}
def fx_route = fn (request) {
if (request.method == GET ||
request.method == HEAD) &&
(request.url == "/fx" ||
Str.starts_with?(request.url, "/fx/")) do
path = "." + request.url
show_file(path, request.url)
end
}
def tag_create = fn (path, tag) {
Fx.tag_add(path, tag)
body = """
<html>
<head>
<title>Tag create</title>
</head>
<body>
<p><h1>Tag create</h1></p>
<p>Tag : #{HTML.escape(inspect(tag))}</p>
<p>Path : #{HTML.escape(inspect(path))}</p>
</body>
</html>
"""
%HTTP.Response{body: body}
}
def tag_delete = fn (path, tag) {
Fx.tag_remove(path, tag)
body = """
<html>
<head>
<title>Tag delete</title>
</head>
<body>
<p><h1>Tag delete</h1></p>
<p>Tag : #{HTML.escape(inspect(tag))}</p>
<p>Path : #{HTML.escape(inspect(path))}</p>
</body>
</html>
"""
%HTTP.Response{body: body}
}
def tags_route = fn (req) {
["", "tags", url] = Str.split(req.url, "/")
path = "./fx" + req.url
if (req.method == POST) do
response = tag_create(path, tag)
else
if (req.method == DELETE) do
response = tag_delete(path, tag)
else
error_405_page(req)
end
end
end
}
def property_create = fn (path, property, value) {
Fx.property_add(path, property, value)
HTTPd.redirect_to("/" + path)
}
def property_delete = fn (path, property, value) {
Fx.property_remove(path, property, value)
HTTPd.redirect_to("/" + path)
}
def properties_route = fn (req) {
url = if (req.url == "/properties/fx") do
req.url + "/"
else
req.url
end
if Str.starts_with?(url, "/properties/fx/") do
path = Str.slice(url, 12, -1)
body = req.body
property = body["property_key"]
value = body["property_value"]
if (property == void || value == void) do
HTTPd.error_400_page(req)
else
if (req.method == POST) do
property_create(path, property, value)
else
if (req.method == DELETE) do
property_delete(path, property, value)
else
HTTPd.error_405_page(req)
end
end
end
else
HTTPd.error_404_page(req)
end
}
end