Claude set up a '/labels' endpoint
This commit is contained in:
38
lib/labelmaker_web/controllers/labels_controller.ex
Normal file
38
lib/labelmaker_web/controllers/labels_controller.ex
Normal file
@@ -0,0 +1,38 @@
|
||||
defmodule LabelmakerWeb.LabelsController do
|
||||
use LabelmakerWeb, :controller
|
||||
|
||||
@label_dir Path.join(:code.priv_dir(:labelmaker), "static/labels")
|
||||
|
||||
def index(conn, _params) do
|
||||
labels = list_labels()
|
||||
render(conn, :index, labels: labels)
|
||||
end
|
||||
|
||||
defp list_labels do
|
||||
case File.ls(@label_dir) do
|
||||
{:ok, files} ->
|
||||
files
|
||||
|> Enum.filter(&String.ends_with?(&1, ".png"))
|
||||
|> Enum.map(fn filename ->
|
||||
filepath = Path.join(@label_dir, filename)
|
||||
stat = File.stat!(filepath)
|
||||
|
||||
%{
|
||||
filename: filename,
|
||||
filepath: filepath,
|
||||
size: stat.size,
|
||||
modified: stat.mtime,
|
||||
url: "/labels/#{filename}"
|
||||
}
|
||||
end)
|
||||
|> Enum.sort_by(& &1.modified, :desc)
|
||||
|
||||
{:error, :enoent} ->
|
||||
# Directory doesn't exist yet
|
||||
[]
|
||||
|
||||
{:error, _reason} ->
|
||||
[]
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user