59 lines
1.5 KiB
Elixir
59 lines
1.5 KiB
Elixir
defmodule Confient.Deposit do
|
|
alias Confient.Student.Work
|
|
alias Confient.Repo
|
|
import Ecto.Query
|
|
|
|
def list_works(), do: Repo.all(from w in Work, preload: [{:student, :class}, :assignment])
|
|
|
|
def verify_date(due) do
|
|
date = DateTime.now!(Application.fetch_env!(:confient, :timezone)) |> DateTime.to_date()
|
|
if Timex.before?(date, due), do: :ok, else: {:error, :date_too_late}
|
|
end
|
|
|
|
def verify_file(file) do
|
|
supported = [
|
|
"application/pdf",
|
|
"application/rtf",
|
|
"application/vnd.oasis.opendocument.text",
|
|
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
"application/msword"
|
|
]
|
|
|
|
if Enum.member?(supported, file.content_type) do
|
|
{:ok, Path.extname(file.filename)}
|
|
else
|
|
{:error, :invalid_file_type}
|
|
end
|
|
end
|
|
|
|
def get_upload_dir(class_name, assignment_slug) do
|
|
dir = Application.fetch_env!(:confient, :upload_dir)
|
|
"#{dir}/#{class_name}/#{assignment_slug}"
|
|
end
|
|
|
|
def ensure_upload_dir(dir) do
|
|
File.mkdir_p!(dir)
|
|
dir
|
|
end
|
|
|
|
def gen_filename(
|
|
class_name,
|
|
assignment_slug,
|
|
%Confient.School.Student{
|
|
firstname: fname,
|
|
lastname: lname
|
|
},
|
|
ext
|
|
) do
|
|
Enum.join(
|
|
[
|
|
class_name,
|
|
assignment_slug,
|
|
"#{String.downcase(String.at(fname, 0) <> String.replace(lname, " ", "-"))}",
|
|
String.replace(DateTime.to_iso8601(DateTime.now!("Europe/Paris")), ":", "-")
|
|
],
|
|
"_"
|
|
) <> ext
|
|
end
|
|
end
|