defmodule LivebookWeb.SessionLive.CellUploadComponent do use LivebookWeb, :live_component alias Livebook.FileSystem @impl true def mount(socket) do {:ok, assign(socket, name: "", error_message: nil)} end @impl true def render(assigns) do ~H"""

Insert image

Invalid image file. The image must be either GIF, JPEG, SVG or PNG and cannot exceed 5MB in size.
<%= @error_message %>
<%= entry.client_name %> <%= entry.progress %>%
<.link patch={@return_to} class="button-base button-outlined-gray"> Cancel
""" end @impl true def handle_event("validate", %{"name" => name}, socket) do {:noreply, assign(socket, name: name)} end def handle_event("save", %{"name" => name}, socket) do %{images_dir: images_dir} = socket.assigns.session consume_uploaded_entries(socket, :cell_image, fn %{path: path}, entry -> upload_file = FileSystem.File.local(path) ext = Path.extname(entry.client_name) filename = if name == "", do: entry.client_name, else: name <> ext destination_file = FileSystem.File.resolve(images_dir, filename) result = with :ok <- FileSystem.File.copy(upload_file, destination_file) do {:ok, filename} end {:ok, result} end) |> case do [{:ok, filename}] -> url = "images/#{URI.encode(filename, &URI.char_unreserved?/1)}" send(self(), {:cell_upload_complete, socket.assigns.cell_upload_metadata, url}) {:noreply, push_patch(socket, to: socket.assigns.return_to)} [{:error, message}] -> {:noreply, assign(socket, error_message: message)} end end end