diff options
author | neodarz <neodarz@neodarz.net> | 2017-02-06 16:52:00 +0100 |
---|---|---|
committer | neodarz <neodarz@neodarz.net> | 2017-02-06 16:52:00 +0100 |
commit | 120bf89930ce7e136ba0b0499bce7b0f8b95f296 (patch) | |
tree | d86d7320d75f33fac32b897277296f5a6c80e645 /web/controllers | |
parent | 663d70f2d9d917ac5f89faebc289a664b7dcc22a (diff) | |
download | the_transcriber_backend-120bf89930ce7e136ba0b0499bce7b0f8b95f296.tar.xz the_transcriber_backend-120bf89930ce7e136ba0b0499bce7b0f8b95f296.zip |
Add a not secure function for upload files with curl
Diffstat (limited to '')
-rw-r--r-- | web/controllers/audio_file_api_controller.ex | 67 | ||||
-rw-r--r-- | web/controllers/audio_file_controller.ex | 2 |
2 files changed, 67 insertions, 2 deletions
diff --git a/web/controllers/audio_file_api_controller.ex b/web/controllers/audio_file_api_controller.ex new file mode 100644 index 0000000..65b968c --- /dev/null +++ b/web/controllers/audio_file_api_controller.ex @@ -0,0 +1,67 @@ +defmodule TheTranscriberBackend.AudioFileAPIController do + use TheTranscriberBackend.Web, :controller + + alias TheTranscriberBackend.AudioFileAPI + alias TheTranscriberBackend.AudioFile + + def index(conn, _params) do + audio_file_api = Repo.all(AudioFile) + render(conn, "index.json", audio_file_api: audio_file_api) + end + + def create(conn, %{"audio_file" => %{"audio_duration" => audio_duration, "audio_path" => upload, "transcription_file_path" => transcription_file_path}}) do + + repo_last_id = (Repo.one(from x in AudioFileAPI, order_by: [desc: x.id], limit: 1)).id + + path = "/media/phoenix_test/#{repo_last_id}_#{upload.filename}" + File.cp(upload.path, path) + + changeset = AudioFile.changeset(%AudioFile{}, + %{audio_path: path, + transcription_file_path: transcription_file_path, + audio_duration: audio_duration}) + + IO.inspect path + + case Repo.insert(changeset) do + {:ok, audio_file_api} -> + conn + |> put_status(:created) + |> put_resp_header("location", audio_file_api_path(conn, :show, audio_file_api)) + |> render("show.json", audio_file_api: audio_file_api) + {:error, changeset} -> + conn + |> put_status(:unprocessable_entity) + |> render(TheTranscriberBackend.ChangesetView, "error.json", changeset: changeset) + end + end + + def show(conn, %{"id" => id}) do + audio_file_api = Repo.get!(AudioFileAPI, id) + render(conn, "show.json", audio_file_api: audio_file_api) + end + + def update(conn, %{"id" => id, "audio_file_api" => audio_file_api_params}) do + audio_file_api = Repo.get!(AudioFileAPI, id) + changeset = AudioFileAPI.changeset(audio_file_api, audio_file_api_params) + + case Repo.update(changeset) do + {:ok, audio_file_api} -> + render(conn, "show.json", audio_file_api: audio_file_api) + {:error, changeset} -> + conn + |> put_status(:unprocessable_entity) + |> render(TheTranscriberBackend.ChangesetView, "error.json", changeset: changeset) + end + end + + def delete(conn, %{"id" => id}) do + audio_file_api = Repo.get!(AudioFileAPI, id) + + # Here we use delete! (with a bang) because we expect + # it to always work (and if it does not, it will raise). + Repo.delete!(audio_file_api) + + send_resp(conn, :no_content, "") + end +end diff --git a/web/controllers/audio_file_controller.ex b/web/controllers/audio_file_controller.ex index db90da7..1c2acb9 100644 --- a/web/controllers/audio_file_controller.ex +++ b/web/controllers/audio_file_controller.ex @@ -23,8 +23,6 @@ defmodule TheTranscriberBackend.AudioFileController do transcription_file_path: transcription_file_path, audio_duration: audio_duration}) - IO.inspect changeset - case Repo.insert(changeset) do {:ok, _audio_file} -> conn |