From 39ab03f6a012f8f075ce2230776d95dde6266adb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 9 Feb 2019 11:07:51 +0100 Subject: [PATCH] Perform map pattern matching --- lib/plug/parsers/multipart.ex | 39 ++++++++++++++++------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/lib/plug/parsers/multipart.ex b/lib/plug/parsers/multipart.ex index c69dc5ee..31bbb424 100644 --- a/lib/plug/parsers/multipart.ex +++ b/lib/plug/parsers/multipart.ex @@ -173,38 +173,35 @@ defmodule Plug.Parsers.MULTIPART do end defp handle_disposition(params, name, headers) do - case Map.fetch(params, "filename") do - {:ok, ""} -> + case params do + %{"filename" => ""} -> :skip - {:ok, filename} -> + %{"filename" => filename} -> path = Plug.Upload.random_file!("multipart") content_type = get_header(headers, "content-type") upload = %Plug.Upload{filename: filename, path: path, content_type: content_type} {:file, name, path, upload} - :error -> - case Map.fetch(params, "filename*") do - {:ok, ""} -> - :skip + %{"filename*" => ""} -> + :skip - {:ok, "utf-8''" <> filename} -> - filename = URI.decode(filename) + %{"filename*" => "utf-8''" <> filename} -> + filename = URI.decode(filename) - Plug.Conn.Utils.validate_utf8!( - filename, - Plug.Parsers.BadEncodingError, - "multipart filename" - ) + Plug.Conn.Utils.validate_utf8!( + filename, + Plug.Parsers.BadEncodingError, + "multipart filename" + ) - path = Plug.Upload.random_file!("multipart") - content_type = get_header(headers, "content-type") - upload = %Plug.Upload{filename: filename, path: path, content_type: content_type} - {:file, name, path, upload} + path = Plug.Upload.random_file!("multipart") + content_type = get_header(headers, "content-type") + upload = %Plug.Upload{filename: filename, path: path, content_type: content_type} + {:file, name, path, upload} - :error -> - {:binary, name} - end + %{} -> + {:binary, name} end end