feat(upload): avatar and calendar cover via local disk storage.
POST /v1/user/me/avatar and /v1/calendars/:id/cover; GET /v1/media; UPLOAD_DIR on eventhub-data volume; MIME/size limits. Refs EventHub/EventHubBack#71 Refs EventHub/EventHubSpec#8
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @doc POST /v1/user/me/avatar — multipart image upload (Back#71).
|
||||
%%% @end
|
||||
%%%-------------------------------------------------------------------
|
||||
-module(handler_user_avatar).
|
||||
-behaviour(cowboy_handler).
|
||||
-export([init/2, trails/0]).
|
||||
-include("records.hrl").
|
||||
|
||||
-spec init(cowboy_req:req(), any()) -> {ok, cowboy_req:req(), any()}.
|
||||
init(Req, _Opts) ->
|
||||
case cowboy_req:method(Req) of
|
||||
<<"POST">> -> post_avatar(Req);
|
||||
_ -> handler_utils:send_error(Req, 405, <<"Method not allowed">>)
|
||||
end.
|
||||
|
||||
-spec trails() -> [map()].
|
||||
trails() ->
|
||||
[
|
||||
#{
|
||||
path => <<"/v1/user/me/avatar">>,
|
||||
method => <<"POST">>,
|
||||
description => <<"Upload current user avatar (multipart field file)">>,
|
||||
tags => [<<"Users">>],
|
||||
requestBody => #{
|
||||
required => true,
|
||||
content => #{
|
||||
<<"multipart/form-data">> => #{
|
||||
schema => #{
|
||||
type => object,
|
||||
required => [<<"file">>],
|
||||
properties => #{file => #{type => string, format => binary}}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
responses => #{
|
||||
200 => #{description => <<"Updated user profile">>},
|
||||
401 => #{description => <<"Unauthorized">>},
|
||||
413 => #{description => <<"File too large">>},
|
||||
415 => #{description => <<"Unsupported media type">>},
|
||||
400 => #{description => <<"Missing file / bad multipart">>}
|
||||
}
|
||||
}
|
||||
].
|
||||
|
||||
post_avatar(Req) ->
|
||||
case handler_utils:auth_user(Req) of
|
||||
{ok, UserId, Req1} ->
|
||||
case handler_upload_utils:read_image_part(Req1) of
|
||||
{ok, Bin, Req2} ->
|
||||
case logic_upload:save_avatar(UserId, Bin) of
|
||||
{ok, Url} ->
|
||||
Old = case core_user:get_by_id(UserId) of
|
||||
{ok, #user{avatar_url = A}} -> A;
|
||||
_ -> undefined
|
||||
end,
|
||||
case core_user:update(UserId, [{avatar_url, Url}]) of
|
||||
{ok, User} ->
|
||||
_ = logic_upload:maybe_delete_url(Old),
|
||||
handler_utils:send_json(Req2, 200, handler_utils:user_to_json(User));
|
||||
{error, not_found} ->
|
||||
handler_utils:send_error(Req2, 404, <<"User not found">>);
|
||||
{error, _} ->
|
||||
handler_utils:send_error(Req2, 400, <<"Update failed">>)
|
||||
end;
|
||||
{error, too_large} ->
|
||||
handler_utils:send_error(Req2, 413, <<"File too large">>);
|
||||
{error, unsupported_media} ->
|
||||
handler_utils:send_error(Req2, 415, <<"Unsupported media type">>);
|
||||
{error, _} ->
|
||||
handler_utils:send_error(Req2, 500, <<"Upload failed">>)
|
||||
end;
|
||||
{error, missing_file, Req2} ->
|
||||
handler_utils:send_error(Req2, 400, <<"Missing file field">>);
|
||||
{error, too_large, Req2} ->
|
||||
handler_utils:send_error(Req2, 413, <<"File too large">>);
|
||||
{error, unsupported_media, Req2} ->
|
||||
handler_utils:send_error(Req2, 415, <<"Unsupported media type">>);
|
||||
{error, bad_multipart, Req2} ->
|
||||
handler_utils:send_error(Req2, 400, <<"Invalid multipart body">>)
|
||||
end;
|
||||
{error, Code, Message, Req1} ->
|
||||
handler_utils:send_error(Req1, Code, Message)
|
||||
end.
|
||||
Reference in New Issue
Block a user