nova_multipart_file_handler (nova v0.18.1)

View Source

nova_multipart_handler that streams a file part to disk under a random name in InitArgs``dir. The client filename is never used in the path. The part is written as <name>.part and renamed on handle_end/1, so a file at its final name is always complete. Sweep dir for stale *.part files left by a process crash.

Only a client extension listed in extensions is kept on the stored name, lowercased. It is an allowlist because dir may be served by a web server, where a stored .html or .svg is stored XSS. Anything else is dropped, not rejected: the extension is client controlled and says nothing about the bytes.

Result: #{path => Path}.

Summary

Types

init_args()

-type init_args() :: #{dir := file:filename_all(), extensions => [binary()]}.

Functions

handle_abort(Reason, HandlerState)

-spec handle_abort(Reason :: term(), HandlerState :: map()) -> ok.

handle_data(Data, HandlerState)

-spec handle_data(Data :: binary(), HandlerState :: map()) ->
                     {ok, NewHandlerState :: map()} | {error, Reason :: term()}.

handle_end(HandlerState)

-spec handle_end(HandlerState :: map()) ->
                    {ok, #{path := file:filename_all()}} | {error, Reason :: term()}.

init/2

-spec init(nova_multipart_handler:part_info(), InitArgs :: init_args()) ->
              {ok, HandlerState :: map()} | {error, Reason :: term()}.