aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorneodarz <neodarz@neodarz.net>2017-01-25 11:50:27 +0100
committerneodarz <neodarz@neodarz.net>2017-01-25 11:50:27 +0100
commit8e65ac05eb7778785602f4504465e1dcbf035812 (patch)
tree82f5178be54b732e289ba2ffadf02b4de6099aba
parent033d07a519bac03fdfd28ca15e09cc287e80fd14 (diff)
downloadthe_transcriber_backend-8e65ac05eb7778785602f4504465e1dcbf035812.tar.xz
the_transcriber_backend-8e65ac05eb7778785602f4504465e1dcbf035812.zip
Add unfonctional code for upload a file
-rw-r--r--lib/the_transcriber_backend/endpoint.ex5
-rw-r--r--web/controllers/audio_file_controller.ex9
2 files changed, 11 insertions, 3 deletions
diff --git a/lib/the_transcriber_backend/endpoint.ex b/lib/the_transcriber_backend/endpoint.ex
index a7b1478..32987a5 100644
--- a/lib/the_transcriber_backend/endpoint.ex
+++ b/lib/the_transcriber_backend/endpoint.ex
@@ -25,9 +25,10 @@ defmodule TheTranscriberBackend.Endpoint do
plug Plug.Logger
plug Plug.Parsers,
- parsers: [:urlencoded, :multipart, :json],
+ parsers: [:urlencoded, :multipart, :json, :length],
pass: ["*/*"],
- json_decoder: Poison
+ json_decoder: Poison,
+ length: 80_000_000
plug Plug.MethodOverride
plug Plug.Head
diff --git a/web/controllers/audio_file_controller.ex b/web/controllers/audio_file_controller.ex
index 8cf5a00..3a73e87 100644
--- a/web/controllers/audio_file_controller.ex
+++ b/web/controllers/audio_file_controller.ex
@@ -14,12 +14,18 @@ defmodule TheTranscriberBackend.AudioFileController do
end
def create(conn, %{"audio_file" => audio_file_params}) do
+ if upload = audio_file_params["audio_path"] do
+ extension = Path.extname(upload.filename)
+ path = "/media/phoenix_test/#{upload.filename}"
+ File.cp(upload.path, path)
+ end
changeset = AudioFile.changeset(%AudioFile{}, audio_file_params)
+ Ecto.Changeset.put_change(changeset, :audio_path, path)
case Repo.insert(changeset) do
{:ok, _audio_file} ->
conn
- |> put_flash(:info, "Audio file created successfully.")
+ |> put_flash(:info, "Audio file uploaded successfully.")
|> redirect(to: audio_file_path(conn, :index))
{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
@@ -56,6 +62,7 @@ defmodule TheTranscriberBackend.AudioFileController do
# Here we use delete! (with a bang) because we expect
# it to always work (and if it does not, it will raise).
+ File.rm(audio_file.audio_path)
Repo.delete!(audio_file)
conn