2015-11-04 01:22:39 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
2015-10-26 12:10:55 +01:00
|
|
|
%%% File : mod_http_upload.erl
|
|
|
|
%%% Author : Holger Weiss <holger@zedat.fu-berlin.de>
|
|
|
|
%%% Purpose : HTTP File Upload (XEP-0363)
|
|
|
|
%%% Created : 20 Aug 2015 by Holger Weiss <holger@zedat.fu-berlin.de>
|
2015-11-04 01:22:39 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2017-01-02 21:41:53 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2015-2017 ProcessOne
|
2015-11-04 01:22:39 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
|
|
|
%%%
|
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
2015-10-26 12:10:55 +01:00
|
|
|
|
|
|
|
-module(mod_http_upload).
|
|
|
|
-author('holger@zedat.fu-berlin.de').
|
|
|
|
|
2015-10-29 10:09:55 +01:00
|
|
|
-protocol({xep, 363, '0.1'}).
|
2015-10-28 23:52:33 +01:00
|
|
|
|
2015-10-26 12:10:55 +01:00
|
|
|
-define(GEN_SERVER, gen_server).
|
|
|
|
-define(SERVICE_REQUEST_TIMEOUT, 5000). % 5 seconds.
|
|
|
|
-define(SLOT_TIMEOUT, 18000000). % 5 hours.
|
|
|
|
-define(PROCNAME, ?MODULE).
|
|
|
|
-define(FORMAT(Error), file:format_error(Error)).
|
|
|
|
-define(URL_ENC(URL), binary_to_list(ejabberd_http:url_encode(URL))).
|
|
|
|
-define(ADDR_TO_STR(IP), ejabberd_config:may_hide_data(jlib:ip_to_list(IP))).
|
2016-09-24 22:34:28 +02:00
|
|
|
-define(STR_TO_INT(Str, B), binary_to_integer(iolist_to_binary(Str), B)).
|
2015-10-26 12:10:55 +01:00
|
|
|
-define(DEFAULT_CONTENT_TYPE, <<"application/octet-stream">>).
|
|
|
|
-define(CONTENT_TYPES,
|
|
|
|
[{<<".avi">>, <<"video/avi">>},
|
|
|
|
{<<".bmp">>, <<"image/bmp">>},
|
|
|
|
{<<".bz2">>, <<"application/x-bzip2">>},
|
|
|
|
{<<".gif">>, <<"image/gif">>},
|
|
|
|
{<<".gz">>, <<"application/x-gzip">>},
|
|
|
|
{<<".jpeg">>, <<"image/jpeg">>},
|
|
|
|
{<<".jpg">>, <<"image/jpeg">>},
|
|
|
|
{<<".mp3">>, <<"audio/mpeg">>},
|
|
|
|
{<<".mp4">>, <<"video/mp4">>},
|
|
|
|
{<<".mpeg">>, <<"video/mpeg">>},
|
|
|
|
{<<".mpg">>, <<"video/mpeg">>},
|
|
|
|
{<<".ogg">>, <<"application/ogg">>},
|
|
|
|
{<<".pdf">>, <<"application/pdf">>},
|
|
|
|
{<<".png">>, <<"image/png">>},
|
|
|
|
{<<".rtf">>, <<"application/rtf">>},
|
|
|
|
{<<".svg">>, <<"image/svg+xml">>},
|
|
|
|
{<<".tiff">>, <<"image/tiff">>},
|
|
|
|
{<<".txt">>, <<"text/plain">>},
|
|
|
|
{<<".wav">>, <<"audio/wav">>},
|
|
|
|
{<<".webp">>, <<"image/webp">>},
|
|
|
|
{<<".xz">>, <<"application/x-xz">>},
|
|
|
|
{<<".zip">>, <<"application/zip">>}]).
|
|
|
|
|
|
|
|
-behaviour(?GEN_SERVER).
|
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
|
|
|
%% gen_mod/supervisor callbacks.
|
|
|
|
-export([start_link/3,
|
|
|
|
start/2,
|
|
|
|
stop/1,
|
2016-07-06 13:58:48 +02:00
|
|
|
depends/2,
|
2015-10-26 12:10:55 +01:00
|
|
|
mod_opt_type/1]).
|
|
|
|
|
|
|
|
%% gen_server callbacks.
|
|
|
|
-export([init/1,
|
|
|
|
handle_call/3,
|
|
|
|
handle_cast/2,
|
|
|
|
handle_info/2,
|
|
|
|
terminate/2,
|
|
|
|
code_change/3]).
|
|
|
|
|
|
|
|
%% ejabberd_http callback.
|
|
|
|
-export([process/2]).
|
|
|
|
|
|
|
|
%% ejabberd_hooks callback.
|
|
|
|
-export([remove_user/2]).
|
|
|
|
|
|
|
|
%% Utility functions.
|
|
|
|
-export([get_proc_name/2,
|
2016-02-20 20:13:30 +01:00
|
|
|
expand_home/1,
|
|
|
|
expand_host/2]).
|
2015-10-26 12:10:55 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
|
|
|
-include("ejabberd_http.hrl").
|
2016-07-30 16:48:52 +02:00
|
|
|
-include("xmpp.hrl").
|
2015-10-26 12:10:55 +01:00
|
|
|
-include("logger.hrl").
|
|
|
|
|
|
|
|
-record(state,
|
|
|
|
{server_host :: binary(),
|
|
|
|
host :: binary(),
|
|
|
|
name :: binary(),
|
|
|
|
access :: atom(),
|
|
|
|
max_size :: pos_integer() | infinity,
|
|
|
|
secret_length :: pos_integer(),
|
|
|
|
jid_in_url :: sha1 | node,
|
|
|
|
file_mode :: integer() | undefined,
|
|
|
|
dir_mode :: integer() | undefined,
|
|
|
|
docroot :: binary(),
|
|
|
|
put_url :: binary(),
|
|
|
|
get_url :: binary(),
|
|
|
|
service_url :: binary() | undefined,
|
|
|
|
thumbnail :: boolean(),
|
2016-01-16 01:30:22 +01:00
|
|
|
slots = #{} :: map()}).
|
2015-10-26 12:10:55 +01:00
|
|
|
|
|
|
|
-record(media_info,
|
2015-11-02 23:46:31 +01:00
|
|
|
{type :: binary(),
|
2015-10-26 12:10:55 +01:00
|
|
|
height :: integer(),
|
2015-10-26 13:05:28 +01:00
|
|
|
width :: integer()}).
|
2015-10-26 12:10:55 +01:00
|
|
|
|
|
|
|
-type state() :: #state{}.
|
2015-11-09 19:12:08 +01:00
|
|
|
-type slot() :: [binary(), ...].
|
2015-10-26 12:10:55 +01:00
|
|
|
-type media_info() :: #media_info{}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% gen_mod/supervisor callbacks.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
|
|
|
|
-spec start_link(binary(), atom(), gen_mod:opts())
|
|
|
|
-> {ok, pid()} | ignore | {error, _}.
|
|
|
|
|
|
|
|
start_link(ServerHost, Proc, Opts) ->
|
|
|
|
?GEN_SERVER:start_link({local, Proc}, ?MODULE, {ServerHost, Opts}, []).
|
|
|
|
|
|
|
|
-spec start(binary(), gen_mod:opts()) -> {ok, _} | {ok, _, _} | {error, _}.
|
|
|
|
|
|
|
|
start(ServerHost, Opts) ->
|
|
|
|
case gen_mod:get_opt(rm_on_unregister, Opts,
|
|
|
|
fun(B) when is_boolean(B) -> B end,
|
|
|
|
true) of
|
2015-11-09 19:23:52 +01:00
|
|
|
true ->
|
|
|
|
ejabberd_hooks:add(remove_user, ServerHost, ?MODULE,
|
|
|
|
remove_user, 50);
|
|
|
|
false ->
|
|
|
|
ok
|
2015-10-26 12:10:55 +01:00
|
|
|
end,
|
|
|
|
Proc = get_proc_name(ServerHost, ?PROCNAME),
|
|
|
|
Spec = {Proc,
|
|
|
|
{?MODULE, start_link, [ServerHost, Proc, Opts]},
|
|
|
|
permanent,
|
|
|
|
3000,
|
|
|
|
worker,
|
|
|
|
[?MODULE]},
|
|
|
|
supervisor:start_child(ejabberd_sup, Spec).
|
|
|
|
|
|
|
|
-spec stop(binary()) -> ok.
|
|
|
|
|
|
|
|
stop(ServerHost) ->
|
|
|
|
case gen_mod:get_module_opt(ServerHost, ?MODULE, rm_on_unregister,
|
|
|
|
fun(B) when is_boolean(B) -> B end,
|
|
|
|
true) of
|
2015-11-09 19:23:52 +01:00
|
|
|
true ->
|
|
|
|
ejabberd_hooks:delete(remove_user, ServerHost, ?MODULE,
|
|
|
|
remove_user, 50);
|
|
|
|
false ->
|
|
|
|
ok
|
2015-10-26 12:10:55 +01:00
|
|
|
end,
|
|
|
|
Proc = get_proc_name(ServerHost, ?PROCNAME),
|
2015-11-02 23:46:58 +01:00
|
|
|
supervisor:terminate_child(ejabberd_sup, Proc),
|
|
|
|
supervisor:delete_child(ejabberd_sup, Proc).
|
2015-10-26 12:10:55 +01:00
|
|
|
|
|
|
|
-spec mod_opt_type(atom()) -> fun((term()) -> term()) | [atom()].
|
|
|
|
|
|
|
|
mod_opt_type(host) ->
|
|
|
|
fun iolist_to_binary/1;
|
|
|
|
mod_opt_type(name) ->
|
|
|
|
fun iolist_to_binary/1;
|
|
|
|
mod_opt_type(access) ->
|
2016-06-21 13:18:24 +02:00
|
|
|
fun acl:access_rules_validator/1;
|
2015-10-26 12:10:55 +01:00
|
|
|
mod_opt_type(max_size) ->
|
|
|
|
fun(I) when is_integer(I), I > 0 -> I;
|
|
|
|
(infinity) -> infinity
|
|
|
|
end;
|
|
|
|
mod_opt_type(secret_length) ->
|
|
|
|
fun(I) when is_integer(I), I >= 8 -> I end;
|
|
|
|
mod_opt_type(jid_in_url) ->
|
|
|
|
fun(sha1) -> sha1;
|
|
|
|
(node) -> node
|
|
|
|
end;
|
|
|
|
mod_opt_type(file_mode) ->
|
|
|
|
fun(Mode) -> ?STR_TO_INT(Mode, 8) end;
|
|
|
|
mod_opt_type(dir_mode) ->
|
|
|
|
fun(Mode) -> ?STR_TO_INT(Mode, 8) end;
|
|
|
|
mod_opt_type(docroot) ->
|
|
|
|
fun iolist_to_binary/1;
|
|
|
|
mod_opt_type(put_url) ->
|
|
|
|
fun(<<"http://", _/binary>> = URL) -> URL;
|
|
|
|
(<<"https://", _/binary>> = URL) -> URL
|
|
|
|
end;
|
|
|
|
mod_opt_type(get_url) ->
|
|
|
|
fun(<<"http://", _/binary>> = URL) -> URL;
|
|
|
|
(<<"https://", _/binary>> = URL) -> URL
|
|
|
|
end;
|
|
|
|
mod_opt_type(service_url) ->
|
|
|
|
fun(<<"http://", _/binary>> = URL) -> URL;
|
|
|
|
(<<"https://", _/binary>> = URL) -> URL
|
|
|
|
end;
|
|
|
|
mod_opt_type(custom_headers) ->
|
|
|
|
fun(Headers) ->
|
|
|
|
lists:map(fun({K, V}) ->
|
|
|
|
{iolist_to_binary(K), iolist_to_binary(V)}
|
|
|
|
end, Headers)
|
|
|
|
end;
|
|
|
|
mod_opt_type(rm_on_unregister) ->
|
|
|
|
fun(B) when is_boolean(B) -> B end;
|
|
|
|
mod_opt_type(thumbnail) ->
|
|
|
|
fun(B) when is_boolean(B) -> B end;
|
|
|
|
mod_opt_type(_) ->
|
|
|
|
[host, name, access, max_size, secret_length, jid_in_url, file_mode,
|
|
|
|
dir_mode, docroot, put_url, get_url, service_url, custom_headers,
|
|
|
|
rm_on_unregister, thumbnail].
|
|
|
|
|
2016-07-06 13:58:48 +02:00
|
|
|
-spec depends(binary(), gen_mod:opts()) -> [{module(), hard | soft}].
|
|
|
|
|
|
|
|
depends(_Host, _Opts) ->
|
|
|
|
[].
|
|
|
|
|
2015-10-26 12:10:55 +01:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% gen_server callbacks.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
|
|
|
|
-spec init({binary(), gen_mod:opts()}) -> {ok, state()}.
|
|
|
|
|
|
|
|
init({ServerHost, Opts}) ->
|
|
|
|
process_flag(trap_exit, true),
|
|
|
|
Host = gen_mod:get_opt_host(ServerHost, Opts, <<"upload.@HOST@">>),
|
|
|
|
Name = gen_mod:get_opt(name, Opts,
|
|
|
|
fun iolist_to_binary/1,
|
|
|
|
<<"HTTP File Upload">>),
|
|
|
|
Access = gen_mod:get_opt(access, Opts,
|
2016-06-21 13:18:24 +02:00
|
|
|
fun acl:access_rules_validator/1,
|
2015-10-26 12:10:55 +01:00
|
|
|
local),
|
|
|
|
MaxSize = gen_mod:get_opt(max_size, Opts,
|
|
|
|
fun(I) when is_integer(I), I > 0 -> I;
|
|
|
|
(infinity) -> infinity
|
|
|
|
end,
|
|
|
|
104857600),
|
|
|
|
SecretLength = gen_mod:get_opt(secret_length, Opts,
|
|
|
|
fun(I) when is_integer(I), I >= 8 -> I end,
|
|
|
|
40),
|
|
|
|
JIDinURL = gen_mod:get_opt(jid_in_url, Opts,
|
|
|
|
fun(sha1) -> sha1;
|
|
|
|
(node) -> node
|
|
|
|
end,
|
|
|
|
sha1),
|
|
|
|
DocRoot = gen_mod:get_opt(docroot, Opts,
|
|
|
|
fun iolist_to_binary/1,
|
|
|
|
<<"@HOME@/upload">>),
|
|
|
|
FileMode = gen_mod:get_opt(file_mode, Opts,
|
|
|
|
fun(Mode) -> ?STR_TO_INT(Mode, 8) end),
|
|
|
|
DirMode = gen_mod:get_opt(dir_mode, Opts,
|
|
|
|
fun(Mode) -> ?STR_TO_INT(Mode, 8) end),
|
|
|
|
PutURL = gen_mod:get_opt(put_url, Opts,
|
|
|
|
fun(<<"http://", _/binary>> = URL) -> URL;
|
|
|
|
(<<"https://", _/binary>> = URL) -> URL
|
|
|
|
end,
|
|
|
|
<<"http://@HOST@:5444">>),
|
|
|
|
GetURL = gen_mod:get_opt(get_url, Opts,
|
|
|
|
fun(<<"http://", _/binary>> = URL) -> URL;
|
|
|
|
(<<"https://", _/binary>> = URL) -> URL
|
|
|
|
end,
|
|
|
|
PutURL),
|
|
|
|
ServiceURL = gen_mod:get_opt(service_url, Opts,
|
|
|
|
fun(<<"http://", _/binary>> = URL) -> URL;
|
|
|
|
(<<"https://", _/binary>> = URL) -> URL
|
|
|
|
end),
|
|
|
|
Thumbnail = gen_mod:get_opt(thumbnail, Opts,
|
|
|
|
fun(B) when is_boolean(B) -> B end,
|
|
|
|
true),
|
2016-02-20 18:21:38 +01:00
|
|
|
DocRoot1 = expand_home(str:strip(DocRoot, right, $/)),
|
2016-02-20 20:13:30 +01:00
|
|
|
DocRoot2 = expand_host(DocRoot1, ServerHost),
|
2015-10-26 12:10:55 +01:00
|
|
|
case ServiceURL of
|
2015-11-09 19:23:52 +01:00
|
|
|
undefined ->
|
|
|
|
ok;
|
|
|
|
<<"http://", _/binary>> ->
|
|
|
|
application:start(inets);
|
|
|
|
<<"https://", _/binary>> ->
|
|
|
|
application:start(inets),
|
|
|
|
application:start(crypto),
|
|
|
|
application:start(asn1),
|
|
|
|
application:start(public_key),
|
|
|
|
application:start(ssl)
|
2015-10-26 12:10:55 +01:00
|
|
|
end,
|
|
|
|
case DirMode of
|
2015-11-09 19:23:52 +01:00
|
|
|
undefined ->
|
|
|
|
ok;
|
|
|
|
Mode ->
|
2016-02-20 20:13:30 +01:00
|
|
|
file:change_mode(DocRoot2, Mode)
|
2015-10-26 12:10:55 +01:00
|
|
|
end,
|
2015-11-02 01:12:10 +01:00
|
|
|
case Thumbnail of
|
2015-11-09 19:23:52 +01:00
|
|
|
true ->
|
|
|
|
case string:str(os:cmd("identify"), "Magick") of
|
|
|
|
0 ->
|
|
|
|
?ERROR_MSG("Cannot find 'identify' command, please install "
|
|
|
|
"ImageMagick or disable thumbnail creation", []);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
ok
|
2015-11-02 01:12:10 +01:00
|
|
|
end,
|
2016-03-13 09:38:40 +01:00
|
|
|
ejabberd_router:register_route(Host, ServerHost),
|
2015-10-26 12:10:55 +01:00
|
|
|
{ok, #state{server_host = ServerHost, host = Host, name = Name,
|
|
|
|
access = Access, max_size = MaxSize,
|
|
|
|
secret_length = SecretLength, jid_in_url = JIDinURL,
|
|
|
|
file_mode = FileMode, dir_mode = DirMode,
|
|
|
|
thumbnail = Thumbnail,
|
2016-02-20 20:13:30 +01:00
|
|
|
docroot = DocRoot2,
|
2015-10-26 12:10:55 +01:00
|
|
|
put_url = expand_host(str:strip(PutURL, right, $/), ServerHost),
|
|
|
|
get_url = expand_host(str:strip(GetURL, right, $/), ServerHost),
|
|
|
|
service_url = ServiceURL}}.
|
|
|
|
|
|
|
|
-spec handle_call(_, {pid(), _}, state())
|
|
|
|
-> {reply, {ok, pos_integer(), binary(),
|
|
|
|
pos_integer() | undefined,
|
|
|
|
pos_integer() | undefined}, state()} |
|
2016-05-08 15:36:51 +02:00
|
|
|
{reply, {error, atom()}, state()} | {noreply, state()}.
|
2015-10-26 12:10:55 +01:00
|
|
|
|
2016-05-08 15:36:51 +02:00
|
|
|
handle_call({use_slot, Slot, Size}, _From, #state{file_mode = FileMode,
|
|
|
|
dir_mode = DirMode,
|
|
|
|
get_url = GetPrefix,
|
|
|
|
thumbnail = Thumbnail,
|
|
|
|
docroot = DocRoot} = State) ->
|
2015-10-26 12:10:55 +01:00
|
|
|
case get_slot(Slot, State) of
|
2015-11-09 19:23:52 +01:00
|
|
|
{ok, {Size, Timer}} ->
|
|
|
|
timer:cancel(Timer),
|
|
|
|
NewState = del_slot(Slot, State),
|
|
|
|
Path = str:join([DocRoot | Slot], <<$/>>),
|
2016-05-08 15:36:51 +02:00
|
|
|
{reply, {ok, Path, FileMode, DirMode, GetPrefix, Thumbnail},
|
2015-11-09 19:23:52 +01:00
|
|
|
NewState};
|
2016-05-08 15:36:51 +02:00
|
|
|
{ok, {_WrongSize, _Timer}} ->
|
|
|
|
{reply, {error, size_mismatch}, State};
|
2015-11-09 19:23:52 +01:00
|
|
|
error ->
|
2016-05-08 15:36:51 +02:00
|
|
|
{reply, {error, invalid_slot}, State}
|
2015-10-26 12:10:55 +01:00
|
|
|
end;
|
|
|
|
handle_call(get_docroot, _From, #state{docroot = DocRoot} = State) ->
|
|
|
|
{reply, {ok, DocRoot}, State};
|
|
|
|
handle_call(Request, From, State) ->
|
|
|
|
?ERROR_MSG("Got unexpected request from ~p: ~p", [From, Request]),
|
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
-spec handle_cast(_, state()) -> {noreply, state()}.
|
|
|
|
|
|
|
|
handle_cast(Request, State) ->
|
|
|
|
?ERROR_MSG("Got unexpected request: ~p", [Request]),
|
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
-spec handle_info(timeout | _, state()) -> {noreply, state()}.
|
|
|
|
|
2016-11-21 12:06:11 +01:00
|
|
|
handle_info({route, From, To, #iq{} = Packet}, State) ->
|
|
|
|
IQ = xmpp:decode_els(Packet),
|
2016-07-30 16:48:52 +02:00
|
|
|
{Reply, NewState} = case process_iq(From, IQ, State) of
|
2015-11-09 19:23:52 +01:00
|
|
|
R when is_record(R, iq) ->
|
|
|
|
{R, State};
|
|
|
|
{R, S} ->
|
|
|
|
{R, S};
|
|
|
|
not_request ->
|
|
|
|
{none, State}
|
2015-10-26 12:10:55 +01:00
|
|
|
end,
|
|
|
|
if Reply /= none ->
|
2016-07-30 16:48:52 +02:00
|
|
|
ejabberd_router:route(To, From, Reply);
|
2015-10-26 12:10:55 +01:00
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
{noreply, NewState};
|
|
|
|
handle_info({slot_timed_out, Slot}, State) ->
|
|
|
|
NewState = del_slot(Slot, State),
|
|
|
|
{noreply, NewState};
|
|
|
|
handle_info(Info, State) ->
|
|
|
|
?ERROR_MSG("Got unexpected info: ~p", [Info]),
|
|
|
|
{noreply, State}.
|
|
|
|
|
2016-04-19 09:15:09 +02:00
|
|
|
-spec terminate(normal | shutdown | {shutdown, _} | _, state()) -> ok.
|
2015-10-26 12:10:55 +01:00
|
|
|
|
|
|
|
terminate(Reason, #state{server_host = ServerHost, host = Host}) ->
|
|
|
|
?DEBUG("Stopping HTTP upload process for ~s: ~p", [ServerHost, Reason]),
|
|
|
|
ejabberd_router:unregister_route(Host),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
-spec code_change({down, _} | _, state(), _) -> {ok, state()}.
|
|
|
|
|
|
|
|
code_change(_OldVsn, #state{server_host = ServerHost} = State, _Extra) ->
|
|
|
|
?DEBUG("Updating HTTP upload process for ~s", [ServerHost]),
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% ejabberd_http callback.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
|
|
|
|
-spec process([binary()], #request{})
|
|
|
|
-> {pos_integer(), [{binary(), binary()}], binary()}.
|
|
|
|
|
2016-01-06 00:12:36 +01:00
|
|
|
process(LocalPath, #request{method = Method, host = Host, ip = IP})
|
|
|
|
when length(LocalPath) < 3,
|
|
|
|
Method == 'PUT' orelse
|
|
|
|
Method == 'GET' orelse
|
|
|
|
Method == 'HEAD' ->
|
|
|
|
?DEBUG("Rejecting ~s request from ~s for ~s: Too few path components",
|
|
|
|
[Method, ?ADDR_TO_STR(IP), Host]),
|
|
|
|
http_response(Host, 404);
|
|
|
|
process(_LocalPath, #request{method = 'PUT', host = Host, ip = IP,
|
|
|
|
data = Data} = Request) ->
|
|
|
|
{Proc, Slot} = parse_http_request(Request),
|
2016-05-08 15:36:51 +02:00
|
|
|
case catch gen_server:call(Proc, {use_slot, Slot, byte_size(Data)}) of
|
|
|
|
{ok, Path, FileMode, DirMode, GetPrefix, Thumbnail} ->
|
2015-11-09 19:23:52 +01:00
|
|
|
?DEBUG("Storing file from ~s for ~s: ~s",
|
|
|
|
[?ADDR_TO_STR(IP), Host, Path]),
|
|
|
|
case store_file(Path, Data, FileMode, DirMode,
|
2015-11-11 22:51:40 +01:00
|
|
|
GetPrefix, Slot, Thumbnail) of
|
2015-11-09 19:23:52 +01:00
|
|
|
ok ->
|
|
|
|
http_response(Host, 201);
|
|
|
|
{ok, Headers, OutData} ->
|
|
|
|
http_response(Host, 201, Headers, OutData);
|
|
|
|
{error, Error} ->
|
|
|
|
?ERROR_MSG("Cannot store file ~s from ~s for ~s: ~p",
|
2015-11-17 00:12:20 +01:00
|
|
|
[Path, ?ADDR_TO_STR(IP), Host, ?FORMAT(Error)]),
|
2015-11-09 19:23:52 +01:00
|
|
|
http_response(Host, 500)
|
|
|
|
end;
|
2016-05-08 15:36:51 +02:00
|
|
|
{error, size_mismatch} ->
|
|
|
|
?INFO_MSG("Rejecting file from ~s for ~s: Unexpected size (~B)",
|
|
|
|
[?ADDR_TO_STR(IP), Host, byte_size(Data)]),
|
2015-11-09 19:23:52 +01:00
|
|
|
http_response(Host, 413);
|
2016-05-08 15:36:51 +02:00
|
|
|
{error, invalid_slot} ->
|
|
|
|
?INFO_MSG("Rejecting file from ~s for ~s: Invalid slot",
|
|
|
|
[?ADDR_TO_STR(IP), Host]),
|
2015-11-09 19:23:52 +01:00
|
|
|
http_response(Host, 403);
|
|
|
|
Error ->
|
|
|
|
?ERROR_MSG("Cannot handle PUT request from ~s for ~s: ~p",
|
|
|
|
[?ADDR_TO_STR(IP), Host, Error]),
|
|
|
|
http_response(Host, 500)
|
2015-10-26 12:10:55 +01:00
|
|
|
end;
|
2016-01-06 00:12:36 +01:00
|
|
|
process(_LocalPath, #request{method = Method, host = Host, ip = IP} = Request)
|
2015-10-26 12:10:55 +01:00
|
|
|
when Method == 'GET';
|
|
|
|
Method == 'HEAD' ->
|
2016-01-06 00:12:36 +01:00
|
|
|
{Proc, [_UserDir, _RandDir, FileName] = Slot} = parse_http_request(Request),
|
2015-10-26 12:10:55 +01:00
|
|
|
case catch gen_server:call(Proc, get_docroot) of
|
2015-11-09 19:23:52 +01:00
|
|
|
{ok, DocRoot} ->
|
2015-11-11 22:51:40 +01:00
|
|
|
Path = str:join([DocRoot | Slot], <<$/>>),
|
2015-11-09 19:23:52 +01:00
|
|
|
case file:read_file(Path) of
|
|
|
|
{ok, Data} ->
|
|
|
|
?INFO_MSG("Serving ~s to ~s", [Path, ?ADDR_TO_STR(IP)]),
|
|
|
|
ContentType = guess_content_type(FileName),
|
|
|
|
Headers1 = case ContentType of
|
|
|
|
<<"image/", _SubType/binary>> -> [];
|
|
|
|
<<"text/", _SubType/binary>> -> [];
|
|
|
|
_ ->
|
|
|
|
[{<<"Content-Disposition">>,
|
|
|
|
<<"attachment; filename=",
|
|
|
|
$", FileName/binary, $">>}]
|
|
|
|
end,
|
|
|
|
Headers2 = [{<<"Content-Type">>, ContentType} | Headers1],
|
|
|
|
http_response(Host, 200, Headers2, Data);
|
|
|
|
{error, eacces} ->
|
|
|
|
?INFO_MSG("Cannot serve ~s to ~s: Permission denied",
|
|
|
|
[Path, ?ADDR_TO_STR(IP)]),
|
|
|
|
http_response(Host, 403);
|
|
|
|
{error, enoent} ->
|
|
|
|
?INFO_MSG("Cannot serve ~s to ~s: No such file",
|
|
|
|
[Path, ?ADDR_TO_STR(IP)]),
|
|
|
|
http_response(Host, 404);
|
|
|
|
{error, eisdir} ->
|
|
|
|
?INFO_MSG("Cannot serve ~s to ~s: Is a directory",
|
|
|
|
[Path, ?ADDR_TO_STR(IP)]),
|
|
|
|
http_response(Host, 404);
|
|
|
|
{error, Error} ->
|
|
|
|
?INFO_MSG("Cannot serve ~s to ~s: ~s",
|
|
|
|
[Path, ?ADDR_TO_STR(IP), ?FORMAT(Error)]),
|
|
|
|
http_response(Host, 500)
|
|
|
|
end;
|
|
|
|
Error ->
|
|
|
|
?ERROR_MSG("Cannot handle ~s request from ~s for ~s: ~p",
|
|
|
|
[Method, ?ADDR_TO_STR(IP), Host, Error]),
|
|
|
|
http_response(Host, 500)
|
2015-10-26 12:10:55 +01:00
|
|
|
end;
|
|
|
|
process(_LocalPath, #request{method = 'OPTIONS', host = Host, ip = IP}) ->
|
|
|
|
?DEBUG("Responding to OPTIONS request from ~s for ~s",
|
|
|
|
[?ADDR_TO_STR(IP), Host]),
|
|
|
|
http_response(Host, 200);
|
|
|
|
process(_LocalPath, #request{method = Method, host = Host, ip = IP}) ->
|
|
|
|
?DEBUG("Rejecting ~s request from ~s for ~s",
|
|
|
|
[Method, ?ADDR_TO_STR(IP), Host]),
|
|
|
|
http_response(Host, 405, [{<<"Allow">>, <<"OPTIONS, HEAD, GET, PUT">>}]).
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Exported utility functions.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
|
|
|
|
-spec get_proc_name(binary(), atom()) -> atom().
|
|
|
|
|
|
|
|
get_proc_name(ServerHost, ModuleName) ->
|
|
|
|
PutURL = gen_mod:get_module_opt(ServerHost, ?MODULE, put_url,
|
|
|
|
fun(<<"http://", _/binary>> = URL) -> URL;
|
|
|
|
(<<"https://", _/binary>> = URL) -> URL;
|
|
|
|
(_) -> <<"http://@HOST@">>
|
|
|
|
end,
|
|
|
|
<<"http://@HOST@">>),
|
2016-01-06 00:12:36 +01:00
|
|
|
{ok, {_Scheme, _UserInfo, Host, _Port, Path, _Query}} =
|
|
|
|
http_uri:parse(binary_to_list(expand_host(PutURL, ServerHost))),
|
|
|
|
ProcPrefix = list_to_binary(string:strip(Host ++ Path, right, $/)),
|
|
|
|
gen_mod:get_module_proc(ProcPrefix, ModuleName).
|
2015-10-26 12:10:55 +01:00
|
|
|
|
|
|
|
-spec expand_home(binary()) -> binary().
|
|
|
|
|
|
|
|
expand_home(Subject) ->
|
|
|
|
{ok, [[Home]]} = init:get_argument(home),
|
|
|
|
Parts = binary:split(Subject, <<"@HOME@">>, [global]),
|
|
|
|
str:join(Parts, list_to_binary(Home)).
|
|
|
|
|
2016-02-20 20:13:30 +01:00
|
|
|
-spec expand_host(binary(), binary()) -> binary().
|
|
|
|
|
|
|
|
expand_host(Subject, Host) ->
|
|
|
|
Parts = binary:split(Subject, <<"@HOST@">>, [global]),
|
|
|
|
str:join(Parts, Host).
|
|
|
|
|
2015-10-26 12:10:55 +01:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Internal functions.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
|
|
|
|
%% XMPP request handling.
|
|
|
|
|
2016-07-30 16:48:52 +02:00
|
|
|
-spec process_iq(jid(), iq(), state()) -> {iq(), state()} | iq() | not_request.
|
2015-10-26 12:10:55 +01:00
|
|
|
|
|
|
|
process_iq(_From,
|
2016-07-30 16:48:52 +02:00
|
|
|
#iq{type = get, lang = Lang, sub_els = [#disco_info{}]} = IQ,
|
2015-10-26 12:10:55 +01:00
|
|
|
#state{server_host = ServerHost, name = Name}) ->
|
|
|
|
AddInfo = ejabberd_hooks:run_fold(disco_info, ServerHost, [],
|
|
|
|
[ServerHost, ?MODULE, <<"">>, <<"">>]),
|
2016-07-30 16:48:52 +02:00
|
|
|
xmpp:make_iq_result(IQ, iq_disco_info(ServerHost, Lang, Name, AddInfo));
|
|
|
|
process_iq(From, #iq{type = get, lang = Lang,
|
|
|
|
sub_els = [#upload_request{filename = File,
|
|
|
|
size = Size,
|
|
|
|
'content-type' = CType,
|
|
|
|
xmlns = XMLNS}]} = IQ,
|
|
|
|
#state{server_host = ServerHost, access = Access} = State) ->
|
2015-10-26 12:10:55 +01:00
|
|
|
case acl:match_rule(ServerHost, Access, From) of
|
2015-11-09 19:23:52 +01:00
|
|
|
allow ->
|
2016-07-30 16:48:52 +02:00
|
|
|
ContentType = yield_content_type(CType),
|
|
|
|
case create_slot(State, From, File, Size, ContentType, Lang) of
|
|
|
|
{ok, Slot} ->
|
|
|
|
{ok, Timer} = timer:send_after(?SLOT_TIMEOUT,
|
|
|
|
{slot_timed_out,
|
|
|
|
Slot}),
|
|
|
|
NewState = add_slot(Slot, Size, Timer, State),
|
2016-08-09 09:56:32 +02:00
|
|
|
NewSlot = mk_slot(Slot, State, XMLNS),
|
|
|
|
{xmpp:make_iq_result(IQ, NewSlot), NewState};
|
2016-07-30 16:48:52 +02:00
|
|
|
{ok, PutURL, GetURL} ->
|
|
|
|
Slot = mk_slot(PutURL, GetURL, XMLNS),
|
|
|
|
xmpp:make_iq_result(IQ, Slot);
|
2015-11-09 19:23:52 +01:00
|
|
|
{error, Error} ->
|
2016-07-30 16:48:52 +02:00
|
|
|
xmpp:make_error(IQ, Error)
|
2015-11-09 19:23:52 +01:00
|
|
|
end;
|
|
|
|
deny ->
|
|
|
|
?DEBUG("Denying HTTP upload slot request from ~s",
|
2015-11-24 16:44:13 +01:00
|
|
|
[jid:to_string(From)]),
|
2016-03-31 10:00:29 +02:00
|
|
|
Txt = <<"Denied by ACL">>,
|
2016-07-30 16:48:52 +02:00
|
|
|
xmpp:make_error(IQ, xmpp:err_forbidden(Txt, Lang))
|
2015-10-26 12:10:55 +01:00
|
|
|
end;
|
2016-07-30 16:48:52 +02:00
|
|
|
process_iq(_From, #iq{type = T} = IQ, _State) when T == get; T == set ->
|
|
|
|
xmpp:make_error(IQ, xmpp:err_not_allowed());
|
|
|
|
process_iq(_From, #iq{}, _State) ->
|
2015-10-26 12:10:55 +01:00
|
|
|
not_request.
|
|
|
|
|
|
|
|
-spec create_slot(state(), jid(), binary(), pos_integer(), binary(), binary())
|
|
|
|
-> {ok, slot()} | {ok, binary(), binary()} | {error, xmlel()}.
|
|
|
|
|
|
|
|
create_slot(#state{service_url = undefined, max_size = MaxSize},
|
|
|
|
JID, File, Size, _ContentType, Lang) when MaxSize /= infinity,
|
|
|
|
Size > MaxSize ->
|
2016-11-26 08:05:22 +01:00
|
|
|
Text = {<<"File larger than ~w bytes">>, [MaxSize]},
|
2015-10-26 12:10:55 +01:00
|
|
|
?INFO_MSG("Rejecting file ~s from ~s (too large: ~B bytes)",
|
2015-11-24 16:44:13 +01:00
|
|
|
[File, jid:to_string(JID), Size]),
|
2016-07-30 16:48:52 +02:00
|
|
|
{error, xmpp:err_not_acceptable(Text, Lang)};
|
2015-10-26 12:10:55 +01:00
|
|
|
create_slot(#state{service_url = undefined,
|
|
|
|
jid_in_url = JIDinURL,
|
|
|
|
secret_length = SecretLength,
|
|
|
|
server_host = ServerHost,
|
|
|
|
docroot = DocRoot},
|
|
|
|
JID, File, Size, _ContentType, Lang) ->
|
|
|
|
UserStr = make_user_string(JID, JIDinURL),
|
|
|
|
UserDir = <<DocRoot/binary, $/, UserStr/binary>>,
|
|
|
|
case ejabberd_hooks:run_fold(http_upload_slot_request, ServerHost, allow,
|
|
|
|
[JID, UserDir, Size, Lang]) of
|
2015-11-09 19:23:52 +01:00
|
|
|
allow ->
|
|
|
|
RandStr = make_rand_string(SecretLength),
|
|
|
|
FileStr = make_file_string(File),
|
|
|
|
?INFO_MSG("Got HTTP upload slot for ~s (file: ~s)",
|
2015-11-24 16:44:13 +01:00
|
|
|
[jid:to_string(JID), File]),
|
2015-11-09 19:23:52 +01:00
|
|
|
{ok, [UserStr, RandStr, FileStr]};
|
|
|
|
deny ->
|
2016-07-30 16:48:52 +02:00
|
|
|
{error, xmpp:err_service_unavailable()};
|
2016-09-08 16:08:48 +02:00
|
|
|
#stanza_error{} = Error ->
|
2015-11-09 19:23:52 +01:00
|
|
|
{error, Error}
|
2015-10-26 12:10:55 +01:00
|
|
|
end;
|
|
|
|
create_slot(#state{service_url = ServiceURL},
|
|
|
|
#jid{luser = U, lserver = S} = JID, File, Size, ContentType,
|
2016-03-31 10:00:29 +02:00
|
|
|
Lang) ->
|
2015-10-26 12:10:55 +01:00
|
|
|
Options = [{body_format, binary}, {full_result, false}],
|
|
|
|
HttpOptions = [{timeout, ?SERVICE_REQUEST_TIMEOUT}],
|
2016-09-24 22:34:28 +02:00
|
|
|
SizeStr = integer_to_binary(Size),
|
2015-10-26 12:10:55 +01:00
|
|
|
GetRequest = binary_to_list(ServiceURL) ++
|
2015-11-24 16:44:13 +01:00
|
|
|
"?jid=" ++ ?URL_ENC(jid:to_string({U, S, <<"">>})) ++
|
2015-10-26 12:10:55 +01:00
|
|
|
"&name=" ++ ?URL_ENC(File) ++
|
|
|
|
"&size=" ++ ?URL_ENC(SizeStr) ++
|
|
|
|
"&content_type=" ++ ?URL_ENC(ContentType),
|
|
|
|
case httpc:request(get, {GetRequest, []}, HttpOptions, Options) of
|
2015-11-09 19:23:52 +01:00
|
|
|
{ok, {Code, Body}} when Code >= 200, Code =< 299 ->
|
|
|
|
case binary:split(Body, <<$\n>>, [global, trim]) of
|
|
|
|
[<<"http", _/binary>> = PutURL,
|
|
|
|
<<"http", _/binary>> = GetURL] ->
|
|
|
|
?INFO_MSG("Got HTTP upload slot for ~s (file: ~s)",
|
2015-11-24 16:44:13 +01:00
|
|
|
[jid:to_string(JID), File]),
|
2015-11-09 19:23:52 +01:00
|
|
|
{ok, PutURL, GetURL};
|
|
|
|
Lines ->
|
|
|
|
?ERROR_MSG("Can't parse data received for ~s from <~s>: ~p",
|
2015-11-24 16:44:13 +01:00
|
|
|
[jid:to_string(JID), ServiceURL, Lines]),
|
2016-03-31 10:00:29 +02:00
|
|
|
Txt = <<"Failed to parse HTTP response">>,
|
2016-07-30 16:48:52 +02:00
|
|
|
{error, xmpp:err_service_unavailable(Txt, Lang)}
|
2015-11-09 19:23:52 +01:00
|
|
|
end;
|
|
|
|
{ok, {402, _Body}} ->
|
|
|
|
?INFO_MSG("Got status code 402 for ~s from <~s>",
|
2015-11-24 16:44:13 +01:00
|
|
|
[jid:to_string(JID), ServiceURL]),
|
2016-07-30 16:48:52 +02:00
|
|
|
{error, xmpp:err_resource_constraint()};
|
2015-11-09 19:23:52 +01:00
|
|
|
{ok, {403, _Body}} ->
|
|
|
|
?INFO_MSG("Got status code 403 for ~s from <~s>",
|
2015-11-24 16:44:13 +01:00
|
|
|
[jid:to_string(JID), ServiceURL]),
|
2016-07-30 16:48:52 +02:00
|
|
|
{error, xmpp:err_not_allowed()};
|
2015-11-09 19:23:52 +01:00
|
|
|
{ok, {413, _Body}} ->
|
|
|
|
?INFO_MSG("Got status code 413 for ~s from <~s>",
|
2015-11-24 16:44:13 +01:00
|
|
|
[jid:to_string(JID), ServiceURL]),
|
2016-07-30 16:48:52 +02:00
|
|
|
{error, xmpp:err_not_acceptable()};
|
2015-11-09 19:23:52 +01:00
|
|
|
{ok, {Code, _Body}} ->
|
|
|
|
?ERROR_MSG("Got unexpected status code for ~s from <~s>: ~B",
|
2015-11-24 16:44:13 +01:00
|
|
|
[jid:to_string(JID), ServiceURL, Code]),
|
2016-07-30 16:48:52 +02:00
|
|
|
{error, xmpp:err_service_unavailable()};
|
2015-11-09 19:23:52 +01:00
|
|
|
{error, Reason} ->
|
|
|
|
?ERROR_MSG("Error requesting upload slot for ~s from <~s>: ~p",
|
2015-11-24 16:44:13 +01:00
|
|
|
[jid:to_string(JID), ServiceURL, Reason]),
|
2016-07-30 16:48:52 +02:00
|
|
|
{error, xmpp:err_service_unavailable()}
|
2015-10-26 12:10:55 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
-spec add_slot(slot(), pos_integer(), timer:tref(), state()) -> state().
|
|
|
|
|
|
|
|
add_slot(Slot, Size, Timer, #state{slots = Slots} = State) ->
|
2016-01-16 01:30:22 +01:00
|
|
|
NewSlots = maps:put(Slot, {Size, Timer}, Slots),
|
2015-10-26 12:10:55 +01:00
|
|
|
State#state{slots = NewSlots}.
|
|
|
|
|
|
|
|
-spec get_slot(slot(), state()) -> {ok, {pos_integer(), timer:tref()}} | error.
|
|
|
|
|
|
|
|
get_slot(Slot, #state{slots = Slots}) ->
|
2016-01-16 01:30:22 +01:00
|
|
|
maps:find(Slot, Slots).
|
2015-10-26 12:10:55 +01:00
|
|
|
|
|
|
|
-spec del_slot(slot(), state()) -> state().
|
|
|
|
|
|
|
|
del_slot(Slot, #state{slots = Slots} = State) ->
|
2016-01-16 01:30:22 +01:00
|
|
|
NewSlots = maps:remove(Slot, Slots),
|
2015-10-26 12:10:55 +01:00
|
|
|
State#state{slots = NewSlots}.
|
|
|
|
|
2016-08-09 09:56:32 +02:00
|
|
|
-spec mk_slot(slot(), state(), binary()) -> upload_slot();
|
|
|
|
(binary(), binary(), binary()) -> upload_slot().
|
2015-10-26 12:10:55 +01:00
|
|
|
|
2016-07-30 16:48:52 +02:00
|
|
|
mk_slot(Slot, #state{put_url = PutPrefix, get_url = GetPrefix}, XMLNS) ->
|
2015-10-26 12:10:55 +01:00
|
|
|
PutURL = str:join([PutPrefix | Slot], <<$/>>),
|
|
|
|
GetURL = str:join([GetPrefix | Slot], <<$/>>),
|
2016-08-09 09:56:32 +02:00
|
|
|
mk_slot(PutURL, GetURL, XMLNS);
|
|
|
|
mk_slot(PutURL, GetURL, XMLNS) ->
|
2016-07-30 16:48:52 +02:00
|
|
|
#upload_slot{get = GetURL, put = PutURL, xmlns = XMLNS}.
|
2015-10-26 12:10:55 +01:00
|
|
|
|
|
|
|
-spec make_user_string(jid(), sha1 | node) -> binary().
|
|
|
|
|
|
|
|
make_user_string(#jid{luser = U, lserver = S}, sha1) ->
|
|
|
|
p1_sha:sha(<<U/binary, $@, S/binary>>);
|
|
|
|
make_user_string(#jid{luser = U}, node) ->
|
|
|
|
re:replace(U, <<"[^a-zA-Z0-9_.-]">>, <<$_>>, [global, {return, binary}]).
|
|
|
|
|
|
|
|
-spec make_file_string(binary()) -> binary().
|
|
|
|
|
|
|
|
make_file_string(File) ->
|
|
|
|
re:replace(File, <<"[^a-zA-Z0-9_.-]">>, <<$_>>, [global, {return, binary}]).
|
|
|
|
|
|
|
|
-spec make_rand_string(non_neg_integer()) -> binary().
|
|
|
|
|
|
|
|
make_rand_string(Length) ->
|
|
|
|
list_to_binary(make_rand_string([], Length)).
|
|
|
|
|
|
|
|
-spec make_rand_string(string(), non_neg_integer()) -> string().
|
|
|
|
|
|
|
|
make_rand_string(S, 0) -> S;
|
|
|
|
make_rand_string(S, N) -> make_rand_string([make_rand_char() | S], N - 1).
|
|
|
|
|
|
|
|
-spec make_rand_char() -> char().
|
|
|
|
|
|
|
|
make_rand_char() ->
|
|
|
|
map_int_to_char(crypto:rand_uniform(0, 62)).
|
|
|
|
|
|
|
|
-spec map_int_to_char(0..61) -> char().
|
|
|
|
|
|
|
|
map_int_to_char(N) when N =< 9 -> N + 48; % Digit.
|
|
|
|
map_int_to_char(N) when N =< 35 -> N + 55; % Upper-case character.
|
|
|
|
map_int_to_char(N) when N =< 61 -> N + 61. % Lower-case character.
|
|
|
|
|
|
|
|
-spec yield_content_type(binary()) -> binary().
|
|
|
|
|
|
|
|
yield_content_type(<<"">>) -> ?DEFAULT_CONTENT_TYPE;
|
|
|
|
yield_content_type(Type) -> Type.
|
|
|
|
|
2016-07-30 16:48:52 +02:00
|
|
|
-spec iq_disco_info(binary(), binary(), binary(), [xdata()]) -> [xmlel()].
|
2016-03-09 00:27:06 +01:00
|
|
|
|
2016-07-30 16:48:52 +02:00
|
|
|
iq_disco_info(Host, Lang, Name, AddInfo) ->
|
2016-03-09 00:27:06 +01:00
|
|
|
Form = case gen_mod:get_module_opt(Host, ?MODULE, max_size,
|
|
|
|
fun(I) when is_integer(I), I > 0 -> I;
|
|
|
|
(infinity) -> infinity
|
|
|
|
end,
|
|
|
|
104857600) of
|
|
|
|
infinity ->
|
2016-07-30 16:48:52 +02:00
|
|
|
AddInfo;
|
2016-03-09 00:27:06 +01:00
|
|
|
MaxSize ->
|
2016-09-24 22:34:28 +02:00
|
|
|
MaxSizeStr = integer_to_binary(MaxSize),
|
2016-07-30 16:48:52 +02:00
|
|
|
Fields = [#xdata_field{type = hidden,
|
|
|
|
var = <<"FORM_TYPE">>,
|
|
|
|
values = [?NS_HTTP_UPLOAD]},
|
|
|
|
#xdata_field{var = <<"max-file-size">>,
|
|
|
|
values = [MaxSizeStr]}],
|
|
|
|
[#xdata{type = result, fields = Fields}|AddInfo]
|
2016-03-09 00:27:06 +01:00
|
|
|
end,
|
2016-07-30 16:48:52 +02:00
|
|
|
#disco_info{identities = [#identity{category = <<"store">>,
|
|
|
|
type = <<"file">>,
|
|
|
|
name = translate:translate(Lang, Name)}],
|
|
|
|
features = [?NS_HTTP_UPLOAD, ?NS_HTTP_UPLOAD_OLD],
|
|
|
|
xdata = Form}.
|
2015-10-26 12:10:55 +01:00
|
|
|
|
|
|
|
%% HTTP request handling.
|
|
|
|
|
2016-01-06 00:12:36 +01:00
|
|
|
-spec parse_http_request(#request{}) -> {atom(), slot()}.
|
|
|
|
|
|
|
|
parse_http_request(#request{host = Host, path = Path}) ->
|
|
|
|
PrefixLength = length(Path) - 3,
|
|
|
|
{ProcURL, Slot} = if PrefixLength > 0 ->
|
|
|
|
Prefix = lists:sublist(Path, PrefixLength),
|
|
|
|
{str:join([Host | Prefix], $/),
|
|
|
|
lists:nthtail(PrefixLength, Path)};
|
|
|
|
true ->
|
|
|
|
{Host, Path}
|
|
|
|
end,
|
|
|
|
{gen_mod:get_module_proc(ProcURL, ?PROCNAME), Slot}.
|
|
|
|
|
2015-11-02 23:46:31 +01:00
|
|
|
-spec store_file(binary(), binary(),
|
2015-10-26 22:30:58 +01:00
|
|
|
integer() | undefined,
|
|
|
|
integer() | undefined,
|
2015-11-09 19:12:08 +01:00
|
|
|
binary(), slot(), boolean())
|
2015-10-26 22:30:58 +01:00
|
|
|
-> ok | {ok, [{binary(), binary()}], binary()} | {error, term()}.
|
|
|
|
|
2016-01-05 22:06:34 +01:00
|
|
|
store_file(Path, Data, FileMode, DirMode, GetPrefix, Slot, Thumbnail) ->
|
2015-10-26 12:10:55 +01:00
|
|
|
case do_store_file(Path, Data, FileMode, DirMode) of
|
|
|
|
ok when Thumbnail ->
|
|
|
|
case identify(Path) of
|
|
|
|
{ok, MediaInfo} ->
|
|
|
|
case convert(Path, MediaInfo) of
|
|
|
|
{ok, OutPath} ->
|
2016-01-05 22:06:34 +01:00
|
|
|
[UserDir, RandDir | _] = Slot,
|
2015-10-26 12:10:55 +01:00
|
|
|
FileName = filename:basename(OutPath),
|
|
|
|
URL = str:join([GetPrefix, UserDir,
|
|
|
|
RandDir, FileName], <<$/>>),
|
|
|
|
ThumbEl = thumb_el(OutPath, URL),
|
|
|
|
{ok,
|
|
|
|
[{<<"Content-Type">>,
|
|
|
|
<<"text/xml; charset=utf-8">>}],
|
2016-02-03 19:03:17 +01:00
|
|
|
fxml:element_to_binary(ThumbEl)};
|
2015-11-09 19:12:08 +01:00
|
|
|
pass ->
|
|
|
|
ok
|
2015-10-26 12:10:55 +01:00
|
|
|
end;
|
2015-11-09 18:41:31 +01:00
|
|
|
pass ->
|
2015-10-26 12:10:55 +01:00
|
|
|
ok
|
|
|
|
end;
|
|
|
|
ok ->
|
|
|
|
ok;
|
|
|
|
Err ->
|
|
|
|
Err
|
|
|
|
end.
|
|
|
|
|
2015-10-26 22:30:58 +01:00
|
|
|
-spec do_store_file(file:filename_all(), binary(),
|
|
|
|
integer() | undefined,
|
|
|
|
integer() | undefined)
|
2015-10-26 12:10:55 +01:00
|
|
|
-> ok | {error, term()}.
|
|
|
|
|
|
|
|
do_store_file(Path, Data, FileMode, DirMode) ->
|
|
|
|
try
|
|
|
|
ok = filelib:ensure_dir(Path),
|
|
|
|
{ok, Io} = file:open(Path, [write, exclusive, raw]),
|
|
|
|
Ok = file:write(Io, Data),
|
|
|
|
ok = file:close(Io),
|
|
|
|
if is_integer(FileMode) ->
|
|
|
|
ok = file:change_mode(Path, FileMode);
|
|
|
|
FileMode == undefined ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
if is_integer(DirMode) ->
|
|
|
|
RandDir = filename:dirname(Path),
|
|
|
|
UserDir = filename:dirname(RandDir),
|
|
|
|
ok = file:change_mode(RandDir, DirMode),
|
|
|
|
ok = file:change_mode(UserDir, DirMode);
|
|
|
|
DirMode == undefined ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
ok = Ok % Raise an exception if file:write/2 failed.
|
|
|
|
catch
|
2015-11-09 19:23:52 +01:00
|
|
|
_:{badmatch, {error, Error}} ->
|
2015-11-17 00:12:20 +01:00
|
|
|
{error, Error};
|
2015-11-09 19:23:52 +01:00
|
|
|
_:Error ->
|
|
|
|
{error, Error}
|
2015-10-26 12:10:55 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
-spec guess_content_type(binary()) -> binary().
|
|
|
|
|
|
|
|
guess_content_type(FileName) ->
|
|
|
|
mod_http_fileserver:content_type(FileName,
|
|
|
|
?DEFAULT_CONTENT_TYPE,
|
|
|
|
?CONTENT_TYPES).
|
|
|
|
|
|
|
|
-spec http_response(binary(), 100..599)
|
|
|
|
-> {pos_integer(), [{binary(), binary()}], binary()}.
|
|
|
|
|
|
|
|
http_response(Host, Code) ->
|
|
|
|
http_response(Host, Code, []).
|
|
|
|
|
|
|
|
-spec http_response(binary(), 100..599, [{binary(), binary()}])
|
|
|
|
-> {pos_integer(), [{binary(), binary()}], binary()}.
|
|
|
|
|
|
|
|
http_response(Host, Code, ExtraHeaders) ->
|
|
|
|
Message = <<(code_to_message(Code))/binary, $\n>>,
|
|
|
|
http_response(Host, Code, ExtraHeaders, Message).
|
|
|
|
|
|
|
|
-spec http_response(binary(), 100..599, [{binary(), binary()}], binary())
|
|
|
|
-> {pos_integer(), [{binary(), binary()}], binary()}.
|
|
|
|
|
|
|
|
http_response(Host, Code, ExtraHeaders, Body) ->
|
|
|
|
ServerHeader = {<<"Server">>, <<"ejabberd ", (?VERSION)/binary>>},
|
|
|
|
CustomHeaders =
|
|
|
|
gen_mod:get_module_opt(Host, ?MODULE, custom_headers,
|
|
|
|
fun(Headers) ->
|
|
|
|
lists:map(fun({K, V}) ->
|
|
|
|
{iolist_to_binary(K),
|
|
|
|
iolist_to_binary(V)}
|
|
|
|
end, Headers)
|
|
|
|
end,
|
|
|
|
[]),
|
|
|
|
Headers = case proplists:is_defined(<<"Content-Type">>, ExtraHeaders) of
|
2015-11-09 19:23:52 +01:00
|
|
|
true ->
|
|
|
|
[ServerHeader | ExtraHeaders];
|
|
|
|
false ->
|
|
|
|
[ServerHeader, {<<"Content-Type">>, <<"text/plain">>} |
|
|
|
|
ExtraHeaders]
|
2015-10-26 12:10:55 +01:00
|
|
|
end ++ CustomHeaders,
|
|
|
|
{Code, Headers, Body}.
|
|
|
|
|
|
|
|
-spec code_to_message(100..599) -> binary().
|
|
|
|
|
|
|
|
code_to_message(201) -> <<"Upload successful.">>;
|
|
|
|
code_to_message(403) -> <<"Forbidden.">>;
|
|
|
|
code_to_message(404) -> <<"Not found.">>;
|
|
|
|
code_to_message(405) -> <<"Method not allowed.">>;
|
|
|
|
code_to_message(413) -> <<"File size doesn't match requested size.">>;
|
|
|
|
code_to_message(500) -> <<"Internal server error.">>;
|
|
|
|
code_to_message(_Code) -> <<"">>.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
2015-11-09 19:12:08 +01:00
|
|
|
%% Image manipulation stuff.
|
2015-10-26 12:10:55 +01:00
|
|
|
%%--------------------------------------------------------------------
|
2015-11-09 19:12:08 +01:00
|
|
|
|
2015-11-09 18:41:31 +01:00
|
|
|
-spec identify(binary()) -> {ok, media_info()} | pass.
|
2015-10-26 12:10:55 +01:00
|
|
|
|
|
|
|
identify(Path) ->
|
2015-11-09 19:12:08 +01:00
|
|
|
Cmd = io_lib:format("identify -format 'ok %m %h %w' ~s", [Path]),
|
2015-10-26 22:10:32 +01:00
|
|
|
Res = string:strip(os:cmd(Cmd), right, $\n),
|
2015-10-26 12:10:55 +01:00
|
|
|
case string:tokens(Res, " ") of
|
|
|
|
["ok", T, H, W] ->
|
2015-11-09 19:12:08 +01:00
|
|
|
{ok, #media_info{type = list_to_binary(string:to_lower(T)),
|
|
|
|
height = list_to_integer(H),
|
|
|
|
width = list_to_integer(W)}};
|
2015-10-26 12:10:55 +01:00
|
|
|
_ ->
|
2015-11-09 19:12:08 +01:00
|
|
|
?DEBUG("Cannot identify type of ~s: ~s", [Path, Res]),
|
2015-11-09 18:41:31 +01:00
|
|
|
pass
|
2015-10-26 12:10:55 +01:00
|
|
|
end.
|
|
|
|
|
2015-11-02 23:46:31 +01:00
|
|
|
-spec convert(binary(), media_info()) -> {ok, binary()} | pass.
|
2015-10-26 12:10:55 +01:00
|
|
|
|
2015-10-26 13:05:28 +01:00
|
|
|
convert(Path, #media_info{type = T, width = W, height = H}) ->
|
2015-11-09 19:12:08 +01:00
|
|
|
if W * H >= 25000000 ->
|
|
|
|
?DEBUG("The image ~s is more than 25 Mpix", [Path]),
|
2015-10-26 12:10:55 +01:00
|
|
|
pass;
|
2015-11-09 19:12:08 +01:00
|
|
|
W =< 300, H =< 300 ->
|
2015-10-26 12:10:55 +01:00
|
|
|
{ok, Path};
|
2015-11-02 23:46:31 +01:00
|
|
|
T == <<"gif">>; T == <<"jpeg">>; T == <<"png">>; T == <<"webp">> ->
|
2015-10-26 12:10:55 +01:00
|
|
|
Dir = filename:dirname(Path),
|
2015-11-02 23:46:31 +01:00
|
|
|
FileName = <<(randoms:get_string())/binary, $., T/binary>>,
|
2015-10-26 12:10:55 +01:00
|
|
|
OutPath = filename:join(Dir, FileName),
|
2015-11-09 19:12:08 +01:00
|
|
|
Cmd = io_lib:format("convert -resize 300 ~s ~s", [Path, OutPath]),
|
2015-10-26 12:10:55 +01:00
|
|
|
case os:cmd(Cmd) of
|
|
|
|
"" ->
|
|
|
|
{ok, OutPath};
|
|
|
|
Err ->
|
2015-11-09 19:12:08 +01:00
|
|
|
?ERROR_MSG("Failed to convert ~s to ~s: ~s",
|
2015-10-26 22:10:32 +01:00
|
|
|
[Path, OutPath, string:strip(Err, right, $\n)]),
|
2015-10-26 12:10:55 +01:00
|
|
|
pass
|
|
|
|
end;
|
|
|
|
true ->
|
2015-11-09 19:12:08 +01:00
|
|
|
?DEBUG("Won't call 'convert' for unknown type ~s", [T]),
|
2015-10-26 12:10:55 +01:00
|
|
|
pass
|
|
|
|
end.
|
|
|
|
|
2015-11-02 23:46:31 +01:00
|
|
|
-spec thumb_el(binary(), binary()) -> xmlel().
|
2015-10-26 12:10:55 +01:00
|
|
|
|
|
|
|
thumb_el(Path, URI) ->
|
2015-11-02 23:46:31 +01:00
|
|
|
ContentType = guess_content_type(Path),
|
2016-07-30 16:48:52 +02:00
|
|
|
xmpp:encode(
|
|
|
|
case identify(Path) of
|
|
|
|
{ok, #media_info{height = H, width = W}} ->
|
|
|
|
#thumbnail{'media-type' = ContentType, uri = URI,
|
|
|
|
height = H, width = W};
|
|
|
|
pass ->
|
|
|
|
#thumbnail{uri = URI, 'media-type' = ContentType}
|
|
|
|
end).
|
2015-10-26 12:10:55 +01:00
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Remove user.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
|
|
|
|
-spec remove_user(binary(), binary()) -> ok.
|
|
|
|
|
|
|
|
remove_user(User, Server) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
ServerHost = jid:nameprep(Server),
|
2015-10-26 12:10:55 +01:00
|
|
|
DocRoot = gen_mod:get_module_opt(ServerHost, ?MODULE, docroot,
|
|
|
|
fun iolist_to_binary/1,
|
|
|
|
<<"@HOME@/upload">>),
|
|
|
|
JIDinURL = gen_mod:get_module_opt(ServerHost, ?MODULE, jid_in_url,
|
|
|
|
fun(sha1) -> sha1;
|
|
|
|
(node) -> node
|
|
|
|
end,
|
|
|
|
sha1),
|
2016-02-20 20:13:30 +01:00
|
|
|
DocRoot1 = expand_host(expand_home(DocRoot), ServerHost),
|
2015-11-24 16:44:13 +01:00
|
|
|
UserStr = make_user_string(jid:make(User, Server, <<"">>), JIDinURL),
|
2016-02-20 20:13:30 +01:00
|
|
|
UserDir = str:join([DocRoot1, UserStr], <<$/>>),
|
2015-10-26 12:10:55 +01:00
|
|
|
case del_tree(UserDir) of
|
|
|
|
ok ->
|
|
|
|
?INFO_MSG("Removed HTTP upload directory of ~s@~s", [User, Server]);
|
|
|
|
{error, enoent} ->
|
|
|
|
?DEBUG("Found no HTTP upload directory of ~s@~s", [User, Server]);
|
|
|
|
{error, Error} ->
|
|
|
|
?ERROR_MSG("Cannot remove HTTP upload directory of ~s@~s: ~p",
|
2015-11-17 00:12:20 +01:00
|
|
|
[User, Server, ?FORMAT(Error)])
|
2015-10-26 12:10:55 +01:00
|
|
|
end,
|
|
|
|
ok.
|
|
|
|
|
|
|
|
-spec del_tree(file:filename_all()) -> ok | {error, term()}.
|
|
|
|
|
|
|
|
del_tree(Dir) when is_binary(Dir) ->
|
|
|
|
del_tree(binary_to_list(Dir));
|
|
|
|
del_tree(Dir) ->
|
|
|
|
try
|
|
|
|
{ok, Entries} = file:list_dir(Dir),
|
|
|
|
lists:foreach(fun(Path) ->
|
|
|
|
case filelib:is_dir(Path) of
|
2015-11-09 19:23:52 +01:00
|
|
|
true ->
|
|
|
|
ok = del_tree(Path);
|
|
|
|
false ->
|
|
|
|
ok = file:delete(Path)
|
2015-10-26 12:10:55 +01:00
|
|
|
end
|
|
|
|
end, [Dir ++ "/" ++ Entry || Entry <- Entries]),
|
|
|
|
ok = file:del_dir(Dir)
|
|
|
|
catch
|
2015-11-09 19:23:52 +01:00
|
|
|
_:{badmatch, {error, Error}} ->
|
2015-11-17 00:12:20 +01:00
|
|
|
{error, Error};
|
2015-11-09 19:23:52 +01:00
|
|
|
_:Error ->
|
|
|
|
{error, Error}
|
2015-10-26 12:10:55 +01:00
|
|
|
end.
|