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
|
|
|
%%%
|
|
|
|
%%%
|
2024-01-22 16:40:01 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2015-2024 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').
|
2019-06-14 11:33:26 +02:00
|
|
|
-behaviour(gen_server).
|
|
|
|
-behaviour(gen_mod).
|
2022-11-07 12:32:38 +01:00
|
|
|
-protocol({xep, 363, '0.2', '15.10', "", ""}).
|
2015-10-28 23:52:33 +01:00
|
|
|
|
2015-10-26 12:10:55 +01:00
|
|
|
-define(SERVICE_REQUEST_TIMEOUT, 5000). % 5 seconds.
|
2018-06-29 09:32:53 +02:00
|
|
|
-define(CALL_TIMEOUT, 60000). % 1 minute.
|
2018-07-16 00:17:11 +02:00
|
|
|
-define(SLOT_TIMEOUT, timer:hours(5)).
|
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">>},
|
2018-04-24 18:16:16 +02:00
|
|
|
{<<".m4a">>, <<"audio/mp4">>},
|
2015-10-26 12:10:55 +01:00
|
|
|
{<<".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">>}]).
|
|
|
|
|
|
|
|
%% gen_mod/supervisor callbacks.
|
2017-02-14 10:39:26 +01:00
|
|
|
-export([start/2,
|
2015-10-26 12:10:55 +01:00
|
|
|
stop/1,
|
2019-08-06 11:31:33 +02:00
|
|
|
reload/3,
|
2016-07-06 13:58:48 +02:00
|
|
|
depends/2,
|
2020-01-08 10:24:51 +01:00
|
|
|
mod_doc/0,
|
2018-01-23 08:54:52 +01:00
|
|
|
mod_opt_type/1,
|
|
|
|
mod_options/1]).
|
2015-10-26 12:10:55 +01:00
|
|
|
|
|
|
|
%% 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_http.hrl").
|
2020-09-03 13:45:57 +02:00
|
|
|
-include_lib("xmpp/include/xmpp.hrl").
|
2015-10-26 12:10:55 +01:00
|
|
|
-include("logger.hrl").
|
2018-01-08 09:29:17 +01:00
|
|
|
-include("translate.hrl").
|
2015-10-26 12:10:55 +01:00
|
|
|
|
|
|
|
-record(state,
|
2019-08-06 11:31:33 +02:00
|
|
|
{server_host = <<>> :: binary(),
|
|
|
|
hosts = [] :: [binary()],
|
|
|
|
name = <<>> :: binary(),
|
|
|
|
access = none :: atom(),
|
|
|
|
max_size = infinity :: pos_integer() | infinity,
|
|
|
|
secret_length = 40 :: pos_integer(),
|
|
|
|
jid_in_url = sha1 :: sha1 | node,
|
2015-10-26 12:10:55 +01:00
|
|
|
file_mode :: integer() | undefined,
|
|
|
|
dir_mode :: integer() | undefined,
|
2019-08-06 11:31:33 +02:00
|
|
|
docroot = <<>> :: binary(),
|
|
|
|
put_url = <<>> :: binary(),
|
|
|
|
get_url = <<>> :: binary(),
|
2015-10-26 12:10:55 +01:00
|
|
|
service_url :: binary() | undefined,
|
2019-08-06 11:31:33 +02:00
|
|
|
thumbnail = false :: boolean(),
|
|
|
|
custom_headers = [] :: [{binary(), binary()}],
|
2019-06-27 14:22:27 +02:00
|
|
|
slots = #{} :: slots(),
|
2019-08-06 11:31:33 +02:00
|
|
|
external_secret = <<>> :: binary()}).
|
2015-10-26 12:10:55 +01:00
|
|
|
|
|
|
|
-record(media_info,
|
2019-04-14 16:05:16 +02:00
|
|
|
{path :: binary(),
|
|
|
|
type :: atom(),
|
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(), ...].
|
2019-06-27 14:22:27 +02:00
|
|
|
-type slots() :: #{slot() => {pos_integer(), reference()}}.
|
2015-10-26 12:10:55 +01:00
|
|
|
-type media_info() :: #media_info{}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% gen_mod/supervisor callbacks.
|
|
|
|
%%--------------------------------------------------------------------
|
2019-08-06 11:31:33 +02:00
|
|
|
-spec start(binary(), gen_mod:opts()) -> {ok, pid()} | {error, term()}.
|
2015-10-26 12:10:55 +01:00
|
|
|
start(ServerHost, Opts) ->
|
2017-02-14 10:39:26 +01:00
|
|
|
Proc = get_proc_name(ServerHost, ?MODULE),
|
2019-08-06 11:31:33 +02:00
|
|
|
case gen_mod:start_child(?MODULE, ServerHost, Opts, Proc) of
|
|
|
|
{ok, _} = Ret -> Ret;
|
|
|
|
{error, {already_started, _}} = Err ->
|
2018-09-17 21:46:37 +02:00
|
|
|
?ERROR_MSG("Multiple virtual hosts can't use a single 'put_url' "
|
|
|
|
"without the @HOST@ keyword", []),
|
2019-08-06 11:31:33 +02:00
|
|
|
Err;
|
|
|
|
Err ->
|
|
|
|
Err
|
2018-09-17 21:46:37 +02:00
|
|
|
end.
|
2015-10-26 12:10:55 +01:00
|
|
|
|
2017-02-18 07:36:27 +01:00
|
|
|
-spec stop(binary()) -> ok | {error, any()}.
|
2015-10-26 12:10:55 +01:00
|
|
|
stop(ServerHost) ->
|
2017-02-14 10:39:26 +01:00
|
|
|
Proc = get_proc_name(ServerHost, ?MODULE),
|
|
|
|
gen_mod:stop_child(Proc).
|
2015-10-26 12:10:55 +01:00
|
|
|
|
2019-08-06 11:31:33 +02:00
|
|
|
-spec reload(binary(), gen_mod:opts(), gen_mod:opts()) -> ok | {ok, pid()} | {error, term()}.
|
|
|
|
reload(ServerHost, NewOpts, OldOpts) ->
|
|
|
|
NewURL = mod_http_upload_opt:put_url(NewOpts),
|
|
|
|
OldURL = mod_http_upload_opt:put_url(OldOpts),
|
|
|
|
OldProc = get_proc_name(ServerHost, ?MODULE, OldURL),
|
|
|
|
NewProc = get_proc_name(ServerHost, ?MODULE, NewURL),
|
|
|
|
if OldProc /= NewProc ->
|
|
|
|
gen_mod:stop_child(OldProc),
|
|
|
|
start(ServerHost, NewOpts);
|
|
|
|
true ->
|
|
|
|
gen_server:cast(NewProc, {reload, NewOpts, OldOpts})
|
|
|
|
end.
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec mod_opt_type(atom()) -> econf:validator().
|
2015-10-26 12:10:55 +01:00
|
|
|
mod_opt_type(name) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:binary();
|
2015-10-26 12:10:55 +01:00
|
|
|
mod_opt_type(access) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:acl();
|
2015-10-26 12:10:55 +01:00
|
|
|
mod_opt_type(max_size) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:pos_int(infinity);
|
2015-10-26 12:10:55 +01:00
|
|
|
mod_opt_type(secret_length) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:int(8, 1000);
|
2015-10-26 12:10:55 +01:00
|
|
|
mod_opt_type(jid_in_url) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:enum([sha1, node]);
|
2015-10-26 12:10:55 +01:00
|
|
|
mod_opt_type(file_mode) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:octal();
|
2015-10-26 12:10:55 +01:00
|
|
|
mod_opt_type(dir_mode) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:octal();
|
2015-10-26 12:10:55 +01:00
|
|
|
mod_opt_type(docroot) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:binary();
|
2015-10-26 12:10:55 +01:00
|
|
|
mod_opt_type(put_url) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:url();
|
2015-10-26 12:10:55 +01:00
|
|
|
mod_opt_type(get_url) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:url();
|
2015-10-26 12:10:55 +01:00
|
|
|
mod_opt_type(service_url) ->
|
2019-06-14 16:38:55 +02:00
|
|
|
econf:url();
|
2015-10-26 12:10:55 +01:00
|
|
|
mod_opt_type(custom_headers) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:map(econf:binary(), econf:binary());
|
2015-10-26 12:10:55 +01:00
|
|
|
mod_opt_type(rm_on_unregister) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:bool();
|
2015-10-26 12:10:55 +01:00
|
|
|
mod_opt_type(thumbnail) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:and_then(
|
|
|
|
econf:bool(),
|
|
|
|
fun(true) ->
|
|
|
|
case eimp:supported_formats() of
|
|
|
|
[] -> econf:fail(eimp_error);
|
|
|
|
[_|_] -> true
|
|
|
|
end;
|
|
|
|
(false) ->
|
|
|
|
false
|
|
|
|
end);
|
2018-07-02 15:53:44 +02:00
|
|
|
mod_opt_type(external_secret) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:binary();
|
|
|
|
mod_opt_type(host) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:host();
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(hosts) ->
|
2019-08-02 12:55:48 +02:00
|
|
|
econf:hosts();
|
|
|
|
mod_opt_type(vcard) ->
|
|
|
|
econf:vcard_temp().
|
2018-01-23 08:54:52 +01:00
|
|
|
|
2019-06-14 16:38:55 +02:00
|
|
|
-spec mod_options(binary()) -> [{thumbnail, boolean()} |
|
2019-06-14 11:33:26 +02:00
|
|
|
{atom(), any()}].
|
|
|
|
mod_options(Host) ->
|
|
|
|
[{host, <<"upload.", Host/binary>>},
|
2018-01-23 08:54:52 +01:00
|
|
|
{hosts, []},
|
|
|
|
{name, ?T("HTTP File Upload")},
|
2019-08-02 12:55:48 +02:00
|
|
|
{vcard, undefined},
|
2018-01-23 08:54:52 +01:00
|
|
|
{access, local},
|
|
|
|
{max_size, 104857600},
|
|
|
|
{secret_length, 40},
|
|
|
|
{jid_in_url, sha1},
|
|
|
|
{file_mode, undefined},
|
|
|
|
{dir_mode, undefined},
|
|
|
|
{docroot, <<"@HOME@/upload">>},
|
2019-06-14 11:33:26 +02:00
|
|
|
{put_url, <<"https://", Host/binary, ":5443/upload">>},
|
2018-01-23 08:54:52 +01:00
|
|
|
{get_url, undefined},
|
|
|
|
{service_url, undefined},
|
2018-07-02 15:53:44 +02:00
|
|
|
{external_secret, <<"">>},
|
2018-01-23 08:54:52 +01:00
|
|
|
{custom_headers, []},
|
|
|
|
{rm_on_unregister, true},
|
2018-05-11 17:56:31 +02:00
|
|
|
{thumbnail, false}].
|
2015-10-26 12:10:55 +01:00
|
|
|
|
2020-01-08 10:24:51 +01:00
|
|
|
mod_doc() ->
|
|
|
|
#{desc =>
|
|
|
|
[?T("This module allows for requesting permissions to "
|
|
|
|
"upload a file via HTTP as described in "
|
|
|
|
"https://xmpp.org/extensions/xep-0363.html"
|
|
|
|
"[XEP-0363: HTTP File Upload]. If the request is accepted, "
|
|
|
|
"the client receives a URL for uploading the file and "
|
|
|
|
"another URL from which that file can later be downloaded."), "",
|
2021-08-23 13:40:19 +02:00
|
|
|
?T("In order to use this module, it must be enabled "
|
|
|
|
"in 'listen' -> 'ejabberd_http' -> "
|
2024-03-14 12:38:03 +01:00
|
|
|
"_`listen-options.md#request_handlers|request_handlers`_.")],
|
2020-01-08 10:24:51 +01:00
|
|
|
opts =>
|
|
|
|
[{host,
|
|
|
|
#{desc => ?T("Deprecated. Use 'hosts' instead.")}},
|
|
|
|
{hosts,
|
|
|
|
#{value => ?T("[Host, ...]"),
|
|
|
|
desc =>
|
|
|
|
?T("This option defines the Jabber IDs of the service. "
|
|
|
|
"If the 'hosts' option is not specified, the only Jabber ID will "
|
|
|
|
"be the hostname of the virtual host with the prefix \"upload.\". "
|
|
|
|
"The keyword '@HOST@' is replaced with the real virtual host name.")}},
|
|
|
|
{name,
|
|
|
|
#{value => ?T("Name"),
|
|
|
|
desc =>
|
|
|
|
?T("A name of the service in the Service Discovery. "
|
|
|
|
"This will only be displayed by special XMPP clients. "
|
|
|
|
"The default value is \"HTTP File Upload\".")}},
|
|
|
|
{access,
|
|
|
|
#{value => ?T("AccessName"),
|
|
|
|
desc =>
|
|
|
|
?T("This option defines the access rule to limit who is "
|
|
|
|
"permitted to use the HTTP upload service. "
|
|
|
|
"The default value is 'local'. If no access rule of "
|
|
|
|
"that name exists, no user will be allowed to use the service.")}},
|
|
|
|
{max_size,
|
|
|
|
#{value => ?T("Size"),
|
|
|
|
desc =>
|
|
|
|
?T("This option limits the acceptable file size. "
|
|
|
|
"Either a number of bytes (larger than zero) or "
|
|
|
|
"'infinity' must be specified. "
|
|
|
|
"The default value is '104857600'.")}},
|
|
|
|
{secret_length,
|
|
|
|
#{value => ?T("Length"),
|
|
|
|
desc =>
|
|
|
|
?T("This option defines the length of the random "
|
|
|
|
"string included in the GET and PUT URLs generated "
|
|
|
|
"by 'mod_http_upload'. The minimum length is 8 characters, "
|
|
|
|
"but it is recommended to choose a larger value. "
|
|
|
|
"The default value is '40'.")}},
|
|
|
|
{jid_in_url,
|
|
|
|
#{value => "node | sha1",
|
|
|
|
desc =>
|
|
|
|
?T("When this option is set to 'node', the node identifier "
|
|
|
|
"of the user's JID (i.e., the user name) is included in "
|
|
|
|
"the GET and PUT URLs generated by 'mod_http_upload'. "
|
|
|
|
"Otherwise, a SHA-1 hash of the user's bare JID is "
|
|
|
|
"included instead. The default value is 'sha1'.")}},
|
|
|
|
{thumbnail,
|
|
|
|
#{value => "true | false",
|
|
|
|
desc =>
|
|
|
|
?T("This option specifies whether ejabberd should create "
|
|
|
|
"thumbnails of uploaded images. If a thumbnail is created, "
|
|
|
|
"a <thumbnail/> element that contains the download <uri/> "
|
|
|
|
"and some metadata is returned with the PUT response. "
|
|
|
|
"The default value is 'false'.")}},
|
|
|
|
{file_mode,
|
|
|
|
#{value => ?T("Permission"),
|
|
|
|
desc =>
|
|
|
|
?T("This option defines the permission bits of uploaded files. "
|
|
|
|
"The bits are specified as an octal number (see the chmod(1) "
|
|
|
|
"manual page) within double quotes. For example: \"0644\". "
|
|
|
|
"The default is undefined, which means no explicit permissions "
|
|
|
|
"will be set.")}},
|
|
|
|
{dir_mode,
|
|
|
|
#{value => ?T("Permission"),
|
|
|
|
desc =>
|
|
|
|
?T("This option defines the permission bits of the 'docroot' "
|
|
|
|
"directory and any directories created during file uploads. "
|
|
|
|
"The bits are specified as an octal number (see the chmod(1) "
|
|
|
|
"manual page) within double quotes. For example: \"0755\". "
|
|
|
|
"The default is undefined, which means no explicit permissions "
|
|
|
|
"will be set.")}},
|
|
|
|
{docroot,
|
|
|
|
#{value => ?T("Path"),
|
|
|
|
desc =>
|
|
|
|
?T("Uploaded files are stored below the directory specified "
|
|
|
|
"(as an absolute path) with this option. The keyword "
|
|
|
|
"@HOME@ is replaced with the home directory of the user "
|
|
|
|
"running ejabberd, and the keyword @HOST@ with the virtual "
|
|
|
|
"host name. The default value is \"@HOME@/upload\".")}},
|
|
|
|
{put_url,
|
|
|
|
#{value => ?T("URL"),
|
|
|
|
desc =>
|
|
|
|
?T("This option specifies the initial part of the PUT URLs "
|
|
|
|
"used for file uploads. The keyword @HOST@ is replaced "
|
|
|
|
"with the virtual host name. NOTE: different virtual "
|
|
|
|
"hosts cannot use the same PUT URL. "
|
2021-09-14 11:24:49 +02:00
|
|
|
"The default value is \"https://@HOST@:5443/upload\".")}},
|
2020-01-08 10:24:51 +01:00
|
|
|
{get_url,
|
|
|
|
#{value => ?T("URL"),
|
|
|
|
desc =>
|
|
|
|
?T("This option specifies the initial part of the GET URLs "
|
2021-09-14 11:24:49 +02:00
|
|
|
"used for downloading the files. The default value is 'undefined'. "
|
|
|
|
"When this option is 'undefined', this option is set "
|
2020-01-08 10:24:51 +01:00
|
|
|
"to the same value as 'put_url'. The keyword @HOST@ is "
|
|
|
|
"replaced with the virtual host name. NOTE: if GET requests "
|
|
|
|
"are handled by 'mod_http_upload', the 'get_url' must match the "
|
|
|
|
"'put_url'. Setting it to a different value only makes "
|
2021-08-21 18:31:21 +02:00
|
|
|
"sense if an external web server or _`mod_http_fileserver`_ "
|
2020-01-08 10:24:51 +01:00
|
|
|
"is used to serve the uploaded files.")}},
|
|
|
|
{service_url,
|
|
|
|
#{desc => ?T("Deprecated.")}},
|
|
|
|
{custom_headers,
|
|
|
|
#{value => "{Name: Value}",
|
|
|
|
desc =>
|
|
|
|
?T("This option specifies additional header fields to be "
|
|
|
|
"included in all HTTP responses. By default no custom "
|
|
|
|
"headers are included.")}},
|
|
|
|
{external_secret,
|
|
|
|
#{value => ?T("Text"),
|
|
|
|
desc =>
|
|
|
|
?T("This option makes it possible to offload all HTTP "
|
|
|
|
"Upload processing to a separate HTTP server. "
|
|
|
|
"Both ejabberd and the HTTP server should share this "
|
|
|
|
"secret and behave exactly as described at "
|
|
|
|
"https://modules.prosody.im/mod_http_upload_external.html"
|
|
|
|
"[Prosody's mod_http_upload_external] in the "
|
|
|
|
"'Implementation' section. There is no default value.")}},
|
|
|
|
{rm_on_unregister,
|
|
|
|
#{value => "true | false",
|
|
|
|
desc =>
|
|
|
|
?T("This option specifies whether files uploaded by a user "
|
|
|
|
"should be removed when that user is unregistered. "
|
|
|
|
"The default value is 'true'.")}},
|
|
|
|
{vcard,
|
|
|
|
#{value => ?T("vCard"),
|
|
|
|
desc =>
|
|
|
|
?T("A custom vCard of the service that will be displayed "
|
|
|
|
"by some XMPP clients in Service Discovery. The value of "
|
|
|
|
"'vCard' is a YAML map constructed from an XML representation "
|
|
|
|
"of vCard. Since the representation has no attributes, "
|
|
|
|
"the mapping is straightforward."),
|
|
|
|
example =>
|
2024-03-29 16:36:47 +01:00
|
|
|
["# This XML representation of vCard:",
|
|
|
|
"# <vCard xmlns='vcard-temp'>",
|
|
|
|
"# <FN>Conferences</FN>",
|
|
|
|
"# <ADR>",
|
|
|
|
"# <WORK/>",
|
|
|
|
"# <STREET>Elm Street</STREET>",
|
|
|
|
"# </ADR>",
|
|
|
|
"# </vCard>",
|
|
|
|
"# ",
|
|
|
|
"# is translated to:",
|
|
|
|
"vcard:",
|
|
|
|
" fn: Conferences",
|
|
|
|
" adr:",
|
|
|
|
" -",
|
|
|
|
" work: true",
|
|
|
|
" street: Elm Street"]}}],
|
2020-01-08 10:24:51 +01:00
|
|
|
example =>
|
|
|
|
["listen:",
|
|
|
|
" -",
|
|
|
|
" port: 5443",
|
|
|
|
" module: ejabberd_http",
|
|
|
|
" tls: true",
|
|
|
|
" request_handlers:",
|
|
|
|
" /upload: mod_http_upload",
|
|
|
|
"",
|
|
|
|
"modules:",
|
|
|
|
" mod_http_upload:",
|
|
|
|
" docroot: /ejabberd/upload",
|
2024-03-29 16:09:50 +01:00
|
|
|
" put_url: \"https://@HOST@:5443/upload\""]}.
|
2020-01-08 10:24:51 +01:00
|
|
|
|
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.
|
|
|
|
%%--------------------------------------------------------------------
|
2017-02-18 07:36:27 +01:00
|
|
|
-spec init(list()) -> {ok, state()}.
|
2019-08-04 20:46:18 +02:00
|
|
|
init([ServerHost|_]) ->
|
2015-10-26 12:10:55 +01:00
|
|
|
process_flag(trap_exit, true),
|
2019-08-04 20:46:18 +02:00
|
|
|
Opts = gen_mod:get_module_opts(ServerHost, ?MODULE),
|
2019-06-14 11:33:26 +02:00
|
|
|
Hosts = gen_mod:get_opt_hosts(Opts),
|
2019-08-06 11:31:33 +02:00
|
|
|
case mod_http_upload_opt:rm_on_unregister(Opts) of
|
|
|
|
true ->
|
|
|
|
ejabberd_hooks:add(remove_user, ServerHost, ?MODULE,
|
|
|
|
remove_user, 50);
|
|
|
|
false ->
|
|
|
|
ok
|
2015-10-26 12:10:55 +01:00
|
|
|
end,
|
2019-08-06 11:31:33 +02:00
|
|
|
State = init_state(ServerHost, Hosts, Opts),
|
|
|
|
{ok, State}.
|
2015-10-26 12:10:55 +01:00
|
|
|
|
|
|
|
-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()}.
|
2017-09-26 21:40:56 +02:00
|
|
|
handle_call({use_slot, Slot, Size}, _From,
|
|
|
|
#state{file_mode = FileMode,
|
|
|
|
dir_mode = DirMode,
|
|
|
|
get_url = GetPrefix,
|
|
|
|
thumbnail = Thumbnail,
|
|
|
|
custom_headers = CustomHeaders,
|
|
|
|
docroot = DocRoot} = State) ->
|
2015-10-26 12:10:55 +01:00
|
|
|
case get_slot(Slot, State) of
|
2018-07-16 00:17:11 +02:00
|
|
|
{ok, {Size, TRef}} ->
|
2018-07-17 20:50:58 +02:00
|
|
|
misc:cancel_timer(TRef),
|
2015-11-09 19:23:52 +01:00
|
|
|
NewState = del_slot(Slot, State),
|
|
|
|
Path = str:join([DocRoot | Slot], <<$/>>),
|
2017-09-26 21:40:56 +02:00
|
|
|
{reply,
|
|
|
|
{ok, Path, FileMode, DirMode, GetPrefix, Thumbnail, CustomHeaders},
|
2015-11-09 19:23:52 +01:00
|
|
|
NewState};
|
2018-07-16 00:17:11 +02:00
|
|
|
{ok, {_WrongSize, _TRef}} ->
|
2016-05-08 15:36:51 +02:00
|
|
|
{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;
|
2017-09-26 21:40:56 +02:00
|
|
|
handle_call(get_conf, _From,
|
|
|
|
#state{docroot = DocRoot,
|
|
|
|
custom_headers = CustomHeaders} = State) ->
|
|
|
|
{reply, {ok, DocRoot, CustomHeaders}, State};
|
2015-10-26 12:10:55 +01:00
|
|
|
handle_call(Request, From, State) ->
|
2019-07-12 10:55:36 +02:00
|
|
|
?WARNING_MSG("Unexpected call from ~p: ~p", [From, Request]),
|
2015-10-26 12:10:55 +01:00
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
-spec handle_cast(_, state()) -> {noreply, state()}.
|
2019-08-06 11:31:33 +02:00
|
|
|
handle_cast({reload, NewOpts, OldOpts},
|
|
|
|
#state{server_host = ServerHost} = State) ->
|
|
|
|
case {mod_http_upload_opt:rm_on_unregister(NewOpts),
|
|
|
|
mod_http_upload_opt:rm_on_unregister(OldOpts)} of
|
|
|
|
{true, false} ->
|
|
|
|
ejabberd_hooks:add(remove_user, ServerHost, ?MODULE,
|
|
|
|
remove_user, 50);
|
|
|
|
{false, true} ->
|
|
|
|
ejabberd_hooks:delete(remove_user, ServerHost, ?MODULE,
|
|
|
|
remove_user, 50);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
NewHosts = gen_mod:get_opt_hosts(NewOpts),
|
|
|
|
OldHosts = gen_mod:get_opt_hosts(OldOpts),
|
|
|
|
lists:foreach(fun ejabberd_router:unregister_route/1, OldHosts -- NewHosts),
|
|
|
|
NewState = init_state(State#state{hosts = NewHosts -- OldHosts}, NewOpts),
|
|
|
|
{noreply, NewState};
|
2015-10-26 12:10:55 +01:00
|
|
|
handle_cast(Request, State) ->
|
2019-07-12 10:55:36 +02:00
|
|
|
?WARNING_MSG("Unexpected cast: ~p", [Request]),
|
2015-10-26 12:10:55 +01:00
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
-spec handle_info(timeout | _, state()) -> {noreply, state()}.
|
2017-02-26 09:53:41 +01:00
|
|
|
handle_info({route, #iq{lang = Lang} = Packet}, State) ->
|
|
|
|
try xmpp:decode_els(Packet) of
|
|
|
|
IQ ->
|
|
|
|
{Reply, NewState} = case process_iq(IQ, State) of
|
|
|
|
R when is_record(R, iq) ->
|
|
|
|
{R, State};
|
|
|
|
{R, S} ->
|
|
|
|
{R, S};
|
|
|
|
not_request ->
|
|
|
|
{none, State}
|
|
|
|
end,
|
|
|
|
if Reply /= none ->
|
|
|
|
ejabberd_router:route(Reply);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
{noreply, NewState}
|
|
|
|
catch _:{xmpp_codec, Why} ->
|
|
|
|
Txt = xmpp:io_format_error(Why),
|
|
|
|
Err = xmpp:err_bad_request(Txt, Lang),
|
|
|
|
ejabberd_router:route_error(Packet, Err),
|
|
|
|
{noreply, State}
|
|
|
|
end;
|
2018-07-16 00:17:11 +02:00
|
|
|
handle_info({timeout, _TRef, Slot}, State) ->
|
2015-10-26 12:10:55 +01:00
|
|
|
NewState = del_slot(Slot, State),
|
|
|
|
{noreply, NewState};
|
|
|
|
handle_info(Info, State) ->
|
2019-07-12 10:55:36 +02:00
|
|
|
?WARNING_MSG("Unexpected info: ~p", [Info]),
|
2015-10-26 12:10:55 +01:00
|
|
|
{noreply, State}.
|
|
|
|
|
2016-04-19 09:15:09 +02:00
|
|
|
-spec terminate(normal | shutdown | {shutdown, _} | _, state()) -> ok.
|
2017-08-08 16:46:26 +02:00
|
|
|
terminate(Reason, #state{server_host = ServerHost, hosts = Hosts}) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Stopping HTTP upload process for ~ts: ~p", [ServerHost, Reason]),
|
2019-08-06 11:31:33 +02:00
|
|
|
ejabberd_hooks:delete(remove_user, ServerHost, ?MODULE, remove_user, 50),
|
2017-08-08 16:46:26 +02:00
|
|
|
lists:foreach(fun ejabberd_router:unregister_route/1, Hosts).
|
2015-10-26 12:10:55 +01:00
|
|
|
|
|
|
|
-spec code_change({down, _} | _, state(), _) -> {ok, state()}.
|
|
|
|
code_change(_OldVsn, #state{server_host = ServerHost} = State, _Extra) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Updating HTTP upload process for ~ts", [ServerHost]),
|
2015-10-26 12:10:55 +01:00
|
|
|
{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' ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Rejecting ~ts request from ~ts for ~ts: Too few path components",
|
2018-07-17 22:28:31 +02:00
|
|
|
[Method, encode_addr(IP), Host]),
|
2017-09-26 21:40:56 +02:00
|
|
|
http_response(404);
|
2016-01-06 00:12:36 +01:00
|
|
|
process(_LocalPath, #request{method = 'PUT', host = Host, ip = IP,
|
2018-05-14 18:30:21 +02:00
|
|
|
length = Length} = Request) ->
|
2016-01-06 00:12:36 +01:00
|
|
|
{Proc, Slot} = parse_http_request(Request),
|
2019-02-28 00:28:46 +01:00
|
|
|
try gen_server:call(Proc, {use_slot, Slot, Length}, ?CALL_TIMEOUT) of
|
2017-09-26 21:40:56 +02:00
|
|
|
{ok, Path, FileMode, DirMode, GetPrefix, Thumbnail, CustomHeaders} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Storing file from ~ts for ~ts: ~ts",
|
2018-07-17 22:28:31 +02:00
|
|
|
[encode_addr(IP), Host, Path]),
|
2018-05-14 18:30:21 +02:00
|
|
|
case store_file(Path, Request, FileMode, DirMode,
|
2015-11-11 22:51:40 +01:00
|
|
|
GetPrefix, Slot, Thumbnail) of
|
2015-11-09 19:23:52 +01:00
|
|
|
ok ->
|
2017-09-26 21:40:56 +02:00
|
|
|
http_response(201, CustomHeaders);
|
2015-11-09 19:23:52 +01:00
|
|
|
{ok, Headers, OutData} ->
|
2020-02-21 12:19:02 +01:00
|
|
|
http_response(201, ejabberd_http:apply_custom_headers(Headers, CustomHeaders), OutData);
|
2018-07-04 07:55:52 +02:00
|
|
|
{error, closed} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Cannot store file ~ts from ~ts for ~ts: connection closed",
|
2018-07-17 22:28:31 +02:00
|
|
|
[Path, encode_addr(IP), Host]),
|
2018-07-04 07:55:52 +02:00
|
|
|
http_response(404);
|
2015-11-09 19:23:52 +01:00
|
|
|
{error, Error} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Cannot store file ~ts from ~ts for ~ts: ~ts",
|
2018-07-17 22:28:31 +02:00
|
|
|
[Path, encode_addr(IP), Host, format_error(Error)]),
|
2017-09-26 21:40:56 +02:00
|
|
|
http_response(500)
|
2015-11-09 19:23:52 +01:00
|
|
|
end;
|
2016-05-08 15:36:51 +02:00
|
|
|
{error, size_mismatch} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("Rejecting file ~ts from ~ts for ~ts: Unexpected size (~B)",
|
2018-07-17 22:28:31 +02:00
|
|
|
[lists:last(Slot), encode_addr(IP), Host, Length]),
|
2017-09-26 21:40:56 +02:00
|
|
|
http_response(413);
|
2016-05-08 15:36:51 +02:00
|
|
|
{error, invalid_slot} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("Rejecting file ~ts from ~ts for ~ts: Invalid slot",
|
2018-07-17 22:28:31 +02:00
|
|
|
[lists:last(Slot), encode_addr(IP), Host]),
|
2019-02-28 00:28:46 +01:00
|
|
|
http_response(403)
|
|
|
|
catch
|
|
|
|
exit:{noproc, _} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("Cannot handle PUT request from ~ts for ~ts: "
|
2019-02-28 00:28:46 +01:00
|
|
|
"Upload not configured for this host",
|
|
|
|
[encode_addr(IP), Host]),
|
|
|
|
http_response(404);
|
|
|
|
_:Error ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Cannot handle PUT request from ~ts for ~ts: ~p",
|
2018-07-17 22:28:31 +02:00
|
|
|
[encode_addr(IP), Host, Error]),
|
2017-09-26 21:40:56 +02:00
|
|
|
http_response(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),
|
2019-02-28 00:28:46 +01:00
|
|
|
try gen_server:call(Proc, get_conf, ?CALL_TIMEOUT) of
|
2017-09-26 21:40:56 +02:00
|
|
|
{ok, DocRoot, CustomHeaders} ->
|
2015-11-11 22:51:40 +01:00
|
|
|
Path = str:join([DocRoot | Slot], <<$/>>),
|
2018-05-17 11:00:06 +02:00
|
|
|
case file:open(Path, [read]) of
|
2018-05-14 18:30:21 +02:00
|
|
|
{ok, Fd} ->
|
|
|
|
file:close(Fd),
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("Serving ~ts to ~ts", [Path, encode_addr(IP)]),
|
2015-11-09 19:23:52 +01:00
|
|
|
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],
|
2020-02-21 12:19:02 +01:00
|
|
|
Headers3 = ejabberd_http:apply_custom_headers(Headers2, CustomHeaders),
|
2018-05-14 18:30:21 +02:00
|
|
|
http_response(200, Headers3, {file, Path});
|
2015-11-09 19:23:52 +01:00
|
|
|
{error, eacces} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("Cannot serve ~ts to ~ts: Permission denied",
|
2018-07-17 22:28:31 +02:00
|
|
|
[Path, encode_addr(IP)]),
|
2017-09-26 21:40:56 +02:00
|
|
|
http_response(403);
|
2015-11-09 19:23:52 +01:00
|
|
|
{error, enoent} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("Cannot serve ~ts to ~ts: No such file",
|
2018-07-17 22:28:31 +02:00
|
|
|
[Path, encode_addr(IP)]),
|
2017-09-26 21:40:56 +02:00
|
|
|
http_response(404);
|
2015-11-09 19:23:52 +01:00
|
|
|
{error, eisdir} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("Cannot serve ~ts to ~ts: Is a directory",
|
2018-07-17 22:28:31 +02:00
|
|
|
[Path, encode_addr(IP)]),
|
2017-09-26 21:40:56 +02:00
|
|
|
http_response(404);
|
2015-11-09 19:23:52 +01:00
|
|
|
{error, Error} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("Cannot serve ~ts to ~ts: ~ts",
|
2018-07-17 22:28:31 +02:00
|
|
|
[Path, encode_addr(IP), format_error(Error)]),
|
2017-09-26 21:40:56 +02:00
|
|
|
http_response(500)
|
2019-02-28 00:28:46 +01:00
|
|
|
end
|
|
|
|
catch
|
|
|
|
exit:{noproc, _} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("Cannot handle ~ts request from ~ts for ~ts: "
|
2019-02-28 00:28:46 +01:00
|
|
|
"Upload not configured for this host",
|
|
|
|
[Method, encode_addr(IP), Host]),
|
|
|
|
http_response(404);
|
|
|
|
_:Error ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Cannot handle ~ts request from ~ts for ~ts: ~p",
|
2018-07-17 22:28:31 +02:00
|
|
|
[Method, encode_addr(IP), Host, Error]),
|
2017-09-26 21:40:56 +02:00
|
|
|
http_response(500)
|
2015-10-26 12:10:55 +01:00
|
|
|
end;
|
2017-09-26 21:40:56 +02:00
|
|
|
process(_LocalPath, #request{method = 'OPTIONS', host = Host,
|
|
|
|
ip = IP} = Request) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Responding to OPTIONS request from ~ts for ~ts",
|
2018-07-17 22:28:31 +02:00
|
|
|
[encode_addr(IP), Host]),
|
2017-09-26 21:40:56 +02:00
|
|
|
{Proc, _Slot} = parse_http_request(Request),
|
2019-02-28 00:28:46 +01:00
|
|
|
try gen_server:call(Proc, get_conf, ?CALL_TIMEOUT) of
|
2017-09-26 21:40:56 +02:00
|
|
|
{ok, _DocRoot, CustomHeaders} ->
|
2018-07-17 19:42:57 +02:00
|
|
|
AllowHeader = {<<"Allow">>, <<"OPTIONS, HEAD, GET, PUT">>},
|
2020-02-21 12:19:02 +01:00
|
|
|
http_response(200, ejabberd_http:apply_custom_headers([AllowHeader], CustomHeaders))
|
2019-02-28 00:28:46 +01:00
|
|
|
catch
|
|
|
|
exit:{noproc, _} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("Cannot handle OPTIONS request from ~ts for ~ts: "
|
2019-02-28 00:28:46 +01:00
|
|
|
"Upload not configured for this host",
|
|
|
|
[encode_addr(IP), Host]),
|
|
|
|
http_response(404);
|
|
|
|
_:Error ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Cannot handle OPTIONS request from ~ts for ~ts: ~p",
|
2018-07-17 22:28:31 +02:00
|
|
|
[encode_addr(IP), Host, Error]),
|
2017-09-26 21:40:56 +02:00
|
|
|
http_response(500)
|
|
|
|
end;
|
2015-10-26 12:10:55 +01:00
|
|
|
process(_LocalPath, #request{method = Method, host = Host, ip = IP}) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Rejecting ~ts request from ~ts for ~ts",
|
2018-07-17 22:28:31 +02:00
|
|
|
[Method, encode_addr(IP), Host]),
|
2017-09-26 21:40:56 +02:00
|
|
|
http_response(405, [{<<"Allow">>, <<"OPTIONS, HEAD, GET, PUT">>}]).
|
2015-10-26 12:10:55 +01:00
|
|
|
|
2019-08-06 11:31:33 +02:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% State initialization
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
-spec init_state(binary(), [binary()], gen_mod:opts()) -> state().
|
|
|
|
init_state(ServerHost, Hosts, Opts) ->
|
|
|
|
init_state(#state{server_host = ServerHost, hosts = Hosts}, Opts).
|
|
|
|
|
|
|
|
-spec init_state(state(), gen_mod:opts()) -> state().
|
|
|
|
init_state(#state{server_host = ServerHost, hosts = Hosts} = State, Opts) ->
|
|
|
|
Name = mod_http_upload_opt:name(Opts),
|
|
|
|
Access = mod_http_upload_opt:access(Opts),
|
|
|
|
MaxSize = mod_http_upload_opt:max_size(Opts),
|
|
|
|
SecretLength = mod_http_upload_opt:secret_length(Opts),
|
|
|
|
JIDinURL = mod_http_upload_opt:jid_in_url(Opts),
|
|
|
|
DocRoot = mod_http_upload_opt:docroot(Opts),
|
|
|
|
FileMode = mod_http_upload_opt:file_mode(Opts),
|
|
|
|
DirMode = mod_http_upload_opt:dir_mode(Opts),
|
|
|
|
PutURL = mod_http_upload_opt:put_url(Opts),
|
|
|
|
GetURL = case mod_http_upload_opt:get_url(Opts) of
|
|
|
|
undefined -> PutURL;
|
|
|
|
URL -> URL
|
|
|
|
end,
|
|
|
|
ServiceURL = mod_http_upload_opt:service_url(Opts),
|
|
|
|
Thumbnail = mod_http_upload_opt:thumbnail(Opts),
|
|
|
|
ExternalSecret = mod_http_upload_opt:external_secret(Opts),
|
|
|
|
CustomHeaders = mod_http_upload_opt:custom_headers(Opts),
|
|
|
|
DocRoot1 = expand_home(str:strip(DocRoot, right, $/)),
|
|
|
|
DocRoot2 = expand_host(DocRoot1, ServerHost),
|
|
|
|
case DirMode of
|
|
|
|
undefined ->
|
|
|
|
ok;
|
|
|
|
Mode ->
|
|
|
|
file:change_mode(DocRoot2, Mode)
|
|
|
|
end,
|
|
|
|
lists:foreach(
|
|
|
|
fun(Host) ->
|
|
|
|
ejabberd_router:register_route(Host, ServerHost)
|
|
|
|
end, Hosts),
|
|
|
|
State#state{server_host = ServerHost, hosts = Hosts, name = Name,
|
|
|
|
access = Access, max_size = MaxSize,
|
|
|
|
secret_length = SecretLength, jid_in_url = JIDinURL,
|
|
|
|
file_mode = FileMode, dir_mode = DirMode,
|
|
|
|
thumbnail = Thumbnail,
|
|
|
|
docroot = DocRoot2,
|
|
|
|
put_url = expand_host(str:strip(PutURL, right, $/), ServerHost),
|
|
|
|
get_url = expand_host(str:strip(GetURL, right, $/), ServerHost),
|
|
|
|
service_url = ServiceURL,
|
|
|
|
external_secret = ExternalSecret,
|
|
|
|
custom_headers = CustomHeaders}.
|
|
|
|
|
2015-10-26 12:10:55 +01:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Exported utility functions.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
-spec get_proc_name(binary(), atom()) -> atom().
|
|
|
|
get_proc_name(ServerHost, ModuleName) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
PutURL = mod_http_upload_opt:put_url(ServerHost),
|
2019-08-06 11:31:33 +02:00
|
|
|
get_proc_name(ServerHost, ModuleName, PutURL).
|
|
|
|
|
|
|
|
-spec get_proc_name(binary(), atom(), binary()) -> atom().
|
|
|
|
get_proc_name(ServerHost, ModuleName, PutURL) ->
|
2019-05-12 11:57:17 +02:00
|
|
|
%% Once we depend on OTP >= 20.0, we can use binaries with http_uri.
|
2022-05-06 10:14:50 +02:00
|
|
|
{ok, _Scheme, _UserInfo, Host0, _Port, Path0, _Query} =
|
2020-06-01 10:35:28 +02:00
|
|
|
misc:uri_parse(expand_host(PutURL, ServerHost)),
|
2019-05-12 11:57:17 +02:00
|
|
|
Host = jid:nameprep(iolist_to_binary(Host0)),
|
|
|
|
Path = str:strip(iolist_to_binary(Path0), right, $/),
|
|
|
|
ProcPrefix = <<Host/binary, Path/binary>>,
|
2016-01-06 00:12:36 +01:00
|
|
|
gen_mod:get_module_proc(ProcPrefix, ModuleName).
|
2015-10-26 12:10:55 +01:00
|
|
|
|
|
|
|
-spec expand_home(binary()) -> binary().
|
2017-03-27 23:52:49 +02:00
|
|
|
expand_home(Input) ->
|
2015-10-26 12:10:55 +01:00
|
|
|
{ok, [[Home]]} = init:get_argument(home),
|
2017-04-11 12:13:58 +02:00
|
|
|
misc:expand_keyword(<<"@HOME@">>, Input, Home).
|
2015-10-26 12:10:55 +01:00
|
|
|
|
2016-02-20 20:13:30 +01:00
|
|
|
-spec expand_host(binary(), binary()) -> binary().
|
2017-03-27 23:52:49 +02:00
|
|
|
expand_host(Input, Host) ->
|
2017-04-11 12:13:58 +02:00
|
|
|
misc:expand_keyword(<<"@HOST@">>, Input, Host).
|
2016-02-20 20:13:30 +01:00
|
|
|
|
2015-10-26 12:10:55 +01:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Internal functions.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
|
|
|
|
%% XMPP request handling.
|
|
|
|
|
2017-02-16 09:00:26 +01:00
|
|
|
-spec process_iq(iq(), state()) -> {iq(), state()} | iq() | not_request.
|
|
|
|
process_iq(#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));
|
2017-02-26 09:53:41 +01:00
|
|
|
process_iq(#iq{type = get, sub_els = [#disco_items{}]} = IQ, _State) ->
|
|
|
|
xmpp:make_iq_result(IQ, #disco_items{});
|
2019-08-02 12:55:48 +02:00
|
|
|
process_iq(#iq{type = get, sub_els = [#vcard_temp{}], lang = Lang} = IQ,
|
|
|
|
#state{server_host = ServerHost}) ->
|
|
|
|
VCard = case mod_http_upload_opt:vcard(ServerHost) of
|
|
|
|
undefined ->
|
|
|
|
#vcard_temp{fn = <<"ejabberd/mod_http_upload">>,
|
|
|
|
url = ejabberd_config:get_uri(),
|
|
|
|
desc = misc:get_descr(
|
|
|
|
Lang, ?T("ejabberd HTTP Upload service"))};
|
|
|
|
V ->
|
|
|
|
V
|
|
|
|
end,
|
|
|
|
xmpp:make_iq_result(IQ, VCard);
|
2017-04-21 18:36:53 +02:00
|
|
|
process_iq(#iq{type = get, sub_els = [#upload_request{filename = File,
|
|
|
|
size = Size,
|
|
|
|
'content-type' = CType,
|
|
|
|
xmlns = XMLNS}]} = IQ,
|
|
|
|
State) ->
|
|
|
|
process_slot_request(IQ, File, Size, CType, XMLNS, State);
|
|
|
|
process_iq(#iq{type = get, sub_els = [#upload_request_0{filename = File,
|
|
|
|
size = Size,
|
|
|
|
'content-type' = CType,
|
|
|
|
xmlns = XMLNS}]} = IQ,
|
|
|
|
State) ->
|
|
|
|
process_slot_request(IQ, File, Size, CType, XMLNS, State);
|
|
|
|
process_iq(#iq{type = T, lang = Lang} = IQ, _State) when T == get; T == set ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("No module is handling this query"),
|
2017-04-21 18:36:53 +02:00
|
|
|
xmpp:make_error(IQ, xmpp:err_service_unavailable(Txt, Lang));
|
|
|
|
process_iq(#iq{}, _State) ->
|
|
|
|
not_request.
|
|
|
|
|
|
|
|
-spec process_slot_request(iq(), binary(), pos_integer(), binary(), binary(),
|
|
|
|
state()) -> {iq(), state()} | iq().
|
|
|
|
process_slot_request(#iq{lang = Lang, from = From} = IQ,
|
|
|
|
File, Size, CType, XMLNS,
|
|
|
|
#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),
|
2018-06-04 22:18:56 +02:00
|
|
|
case create_slot(State, From, File, Size, ContentType, XMLNS,
|
|
|
|
Lang) of
|
2016-07-30 16:48:52 +02:00
|
|
|
{ok, Slot} ->
|
2018-07-02 15:53:44 +02:00
|
|
|
Query = make_query_string(Slot, Size, State),
|
2018-07-15 21:53:50 +02:00
|
|
|
NewState = add_slot(Slot, Size, State),
|
2018-07-02 15:53:44 +02:00
|
|
|
NewSlot = mk_slot(Slot, State, XMLNS, Query),
|
2016-08-09 09:56:32 +02:00
|
|
|
{xmpp:make_iq_result(IQ, NewSlot), NewState};
|
2016-07-30 16:48:52 +02:00
|
|
|
{ok, PutURL, GetURL} ->
|
2018-07-02 15:53:44 +02:00
|
|
|
Slot = mk_slot(PutURL, GetURL, XMLNS, <<"">>),
|
2016-07-30 16:48:52 +02:00
|
|
|
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 ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Denying HTTP upload slot request from ~ts",
|
2017-02-26 08:07:12 +01:00
|
|
|
[jid:encode(From)]),
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Access denied by service policy"),
|
2016-07-30 16:48:52 +02:00
|
|
|
xmpp:make_error(IQ, xmpp:err_forbidden(Txt, Lang))
|
2017-04-21 18:36:53 +02:00
|
|
|
end.
|
2015-10-26 12:10:55 +01:00
|
|
|
|
2018-06-04 22:18:56 +02:00
|
|
|
-spec create_slot(state(), jid(), binary(), pos_integer(), binary(), binary(),
|
|
|
|
binary())
|
2019-06-27 10:32:54 +02:00
|
|
|
-> {ok, slot()} | {ok, binary(), binary()} | {error, xmpp_element()}.
|
2015-10-26 12:10:55 +01:00
|
|
|
create_slot(#state{service_url = undefined, max_size = MaxSize},
|
2018-06-04 22:18:56 +02:00
|
|
|
JID, File, Size, _ContentType, XMLNS, Lang)
|
|
|
|
when MaxSize /= infinity,
|
|
|
|
Size > MaxSize ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Text = {?T("File larger than ~w bytes"), [MaxSize]},
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("Rejecting file ~ts from ~ts (too large: ~B bytes)",
|
2017-02-26 08:07:12 +01:00
|
|
|
[File, jid:encode(JID), Size]),
|
2018-06-04 22:18:56 +02:00
|
|
|
Error = xmpp:err_not_acceptable(Text, Lang),
|
|
|
|
Els = xmpp:get_els(Error),
|
|
|
|
Els1 = [#upload_file_too_large{'max-file-size' = MaxSize,
|
|
|
|
xmlns = XMLNS} | Els],
|
|
|
|
Error1 = xmpp:set_els(Error, Els1),
|
|
|
|
{error, Error1};
|
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},
|
2018-06-04 22:18:56 +02:00
|
|
|
JID, File, Size, _ContentType, _XMLNS, Lang) ->
|
2015-10-26 12:10:55 +01:00
|
|
|
UserStr = make_user_string(JID, JIDinURL),
|
|
|
|
UserDir = <<DocRoot/binary, $/, UserStr/binary>>,
|
|
|
|
case ejabberd_hooks:run_fold(http_upload_slot_request, ServerHost, allow,
|
2018-07-10 21:19:15 +02:00
|
|
|
[ServerHost, JID, UserDir, Size, Lang]) of
|
2015-11-09 19:23:52 +01:00
|
|
|
allow ->
|
2018-07-05 10:51:49 +02:00
|
|
|
RandStr = p1_rand:get_alphanum_string(SecretLength),
|
2015-11-09 19:23:52 +01:00
|
|
|
FileStr = make_file_string(File),
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("Got HTTP upload slot for ~ts (file: ~ts, size: ~B)",
|
2018-06-29 08:58:33 +02:00
|
|
|
[jid:encode(JID), File, Size]),
|
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},
|
2018-06-04 22:18:56 +02:00
|
|
|
#jid{luser = U, lserver = S} = JID,
|
|
|
|
File, Size, ContentType, _XMLNS, 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),
|
2018-07-17 22:28:31 +02:00
|
|
|
JidStr = jid:encode({U, S, <<"">>}),
|
|
|
|
GetRequest = <<ServiceURL/binary,
|
|
|
|
"?jid=", (misc:url_encode(JidStr))/binary,
|
|
|
|
"&name=", (misc:url_encode(File))/binary,
|
|
|
|
"&size=", (misc:url_encode(SizeStr))/binary,
|
|
|
|
"&content_type=", (misc:url_encode(ContentType))/binary>>,
|
|
|
|
case httpc:request(get, {binary_to_list(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] ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("Got HTTP upload slot for ~ts (file: ~ts, size: ~B)",
|
2018-06-29 08:58:33 +02:00
|
|
|
[jid:encode(JID), File, Size]),
|
2015-11-09 19:23:52 +01:00
|
|
|
{ok, PutURL, GetURL};
|
|
|
|
Lines ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Can't parse data received for ~ts from <~ts>: ~p",
|
2017-02-26 08:07:12 +01:00
|
|
|
[jid:encode(JID), ServiceURL, Lines]),
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("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}} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("Got status code 402 for ~ts from <~ts>",
|
2017-02-26 08:07:12 +01:00
|
|
|
[jid:encode(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}} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("Got status code 403 for ~ts from <~ts>",
|
2017-02-26 08:07:12 +01:00
|
|
|
[jid:encode(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}} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("Got status code 413 for ~ts from <~ts>",
|
2017-02-26 08:07:12 +01:00
|
|
|
[jid:encode(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}} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Unexpected status code for ~ts from <~ts>: ~B",
|
2017-02-26 08:07:12 +01:00
|
|
|
[jid:encode(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} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Error requesting upload slot for ~ts from <~ts>: ~p",
|
2017-02-26 08:07:12 +01:00
|
|
|
[jid:encode(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.
|
|
|
|
|
2018-07-15 21:53:50 +02:00
|
|
|
-spec add_slot(slot(), pos_integer(), state()) -> state().
|
|
|
|
add_slot(Slot, Size, #state{external_secret = <<>>, slots = Slots} = State) ->
|
2018-07-16 00:17:11 +02:00
|
|
|
TRef = erlang:start_timer(?SLOT_TIMEOUT, self(), Slot),
|
|
|
|
NewSlots = maps:put(Slot, {Size, TRef}, Slots),
|
2018-07-15 21:53:50 +02:00
|
|
|
State#state{slots = NewSlots};
|
|
|
|
add_slot(_Slot, _Size, State) ->
|
|
|
|
State.
|
2015-10-26 12:10:55 +01:00
|
|
|
|
2018-07-16 00:17:11 +02:00
|
|
|
-spec get_slot(slot(), state()) -> {ok, {pos_integer(), reference()}} | error.
|
2015-10-26 12:10:55 +01:00
|
|
|
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}.
|
|
|
|
|
2018-07-02 15:53:44 +02:00
|
|
|
-spec mk_slot(slot(), state(), binary(), binary()) -> upload_slot();
|
|
|
|
(binary(), binary(), binary(), binary()) -> upload_slot().
|
|
|
|
mk_slot(Slot, #state{put_url = PutPrefix, get_url = GetPrefix}, XMLNS, Query) ->
|
2015-10-26 12:10:55 +01:00
|
|
|
PutURL = str:join([PutPrefix | Slot], <<$/>>),
|
|
|
|
GetURL = str:join([GetPrefix | Slot], <<$/>>),
|
2018-07-02 15:53:44 +02:00
|
|
|
mk_slot(PutURL, GetURL, XMLNS, Query);
|
|
|
|
mk_slot(PutURL, GetURL, XMLNS, Query) ->
|
|
|
|
PutURL1 = <<(misc:url_encode(PutURL))/binary, Query/binary>>,
|
|
|
|
GetURL1 = misc:url_encode(GetURL),
|
|
|
|
case XMLNS of
|
|
|
|
?NS_HTTP_UPLOAD_0 ->
|
|
|
|
#upload_slot_0{get = GetURL1, put = PutURL1, xmlns = XMLNS};
|
|
|
|
_ ->
|
|
|
|
#upload_slot{get = GetURL1, put = PutURL1, xmlns = XMLNS}
|
|
|
|
end.
|
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) ->
|
2017-03-14 00:31:51 +01:00
|
|
|
str:sha(<<U/binary, $@, S/binary>>);
|
2015-10-26 12:10:55 +01:00
|
|
|
make_user_string(#jid{luser = U}, node) ->
|
2018-04-03 21:00:15 +02:00
|
|
|
replace_special_chars(U).
|
2015-10-26 12:10:55 +01:00
|
|
|
|
|
|
|
-spec make_file_string(binary()) -> binary().
|
|
|
|
make_file_string(File) ->
|
2018-04-03 21:00:15 +02:00
|
|
|
replace_special_chars(File).
|
|
|
|
|
2018-07-02 15:53:44 +02:00
|
|
|
-spec make_query_string(slot(), non_neg_integer(), state()) -> binary().
|
|
|
|
make_query_string(Slot, Size, #state{external_secret = Key}) when Key /= <<>> ->
|
|
|
|
UrlPath = str:join(Slot, <<$/>>),
|
|
|
|
SizeStr = integer_to_binary(Size),
|
|
|
|
Data = <<UrlPath/binary, " ", SizeStr/binary>>,
|
2020-06-01 10:35:28 +02:00
|
|
|
HMAC = str:to_hexlist(misc:crypto_hmac(sha256, Key, Data)),
|
2018-07-02 15:53:44 +02:00
|
|
|
<<"?v=", HMAC/binary>>;
|
|
|
|
make_query_string(_Slot, _Size, _State) ->
|
|
|
|
<<>>.
|
|
|
|
|
2018-04-03 21:00:15 +02:00
|
|
|
-spec replace_special_chars(binary()) -> binary().
|
|
|
|
replace_special_chars(S) ->
|
|
|
|
re:replace(S, <<"[^\\p{Xan}_.-]">>, <<$_>>,
|
|
|
|
[unicode, global, {return, binary}]).
|
2015-10-26 12:10:55 +01:00
|
|
|
|
|
|
|
-spec yield_content_type(binary()) -> binary().
|
|
|
|
yield_content_type(<<"">>) -> ?DEFAULT_CONTENT_TYPE;
|
|
|
|
yield_content_type(Type) -> Type.
|
|
|
|
|
2018-07-17 22:28:31 +02:00
|
|
|
-spec encode_addr(inet:ip_address() | {inet:ip_address(), inet:port_number()} |
|
|
|
|
undefined) -> binary().
|
|
|
|
encode_addr(IP) ->
|
|
|
|
ejabberd_config:may_hide_data(misc:ip_to_list(IP)).
|
|
|
|
|
2017-02-18 07:36:27 +01:00
|
|
|
-spec iq_disco_info(binary(), binary(), binary(), [xdata()]) -> disco_info().
|
2016-07-30 16:48:52 +02:00
|
|
|
iq_disco_info(Host, Lang, Name, AddInfo) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
Form = case mod_http_upload_opt:max_size(Host) of
|
2016-03-09 00:27:06 +01:00
|
|
|
infinity ->
|
2016-07-30 16:48:52 +02:00
|
|
|
AddInfo;
|
2016-03-09 00:27:06 +01:00
|
|
|
MaxSize ->
|
2018-06-27 12:36:58 +02:00
|
|
|
lists:foldl(
|
|
|
|
fun(NS, Acc) ->
|
|
|
|
Fs = http_upload:encode(
|
|
|
|
[{'max-file-size', MaxSize}], NS, Lang),
|
|
|
|
[#xdata{type = result, fields = Fs}|Acc]
|
|
|
|
end, AddInfo, [?NS_HTTP_UPLOAD_0, ?NS_HTTP_UPLOAD])
|
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)}],
|
2017-04-21 18:36:53 +02:00
|
|
|
features = [?NS_HTTP_UPLOAD,
|
|
|
|
?NS_HTTP_UPLOAD_0,
|
|
|
|
?NS_HTTP_UPLOAD_OLD,
|
2019-08-02 12:55:48 +02:00
|
|
|
?NS_VCARD,
|
2017-04-21 18:36:53 +02:00
|
|
|
?NS_DISCO_INFO,
|
|
|
|
?NS_DISCO_ITEMS],
|
2016-07-30 16:48:52 +02:00
|
|
|
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()}.
|
2019-05-12 11:57:17 +02:00
|
|
|
parse_http_request(#request{host = Host0, path = Path}) ->
|
|
|
|
Host = jid:nameprep(Host0),
|
2016-01-06 00:12:36 +01:00
|
|
|
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,
|
2017-02-14 10:39:26 +01:00
|
|
|
{gen_mod:get_module_proc(ProcURL, ?MODULE), Slot}.
|
2016-01-06 00:12:36 +01:00
|
|
|
|
2018-05-14 18:30:21 +02:00
|
|
|
-spec store_file(binary(), http_request(),
|
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()}.
|
2018-05-14 18:30:21 +02:00
|
|
|
store_file(Path, Request, FileMode, DirMode, GetPrefix, Slot, Thumbnail) ->
|
|
|
|
case do_store_file(Path, Request, FileMode, DirMode) of
|
2015-10-26 12:10:55 +01:00
|
|
|
ok when Thumbnail ->
|
2019-04-14 16:05:16 +02:00
|
|
|
case read_image(Path) of
|
|
|
|
{ok, Data, MediaInfo} ->
|
|
|
|
case convert(Data, MediaInfo) of
|
|
|
|
{ok, #media_info{path = OutPath} = OutMediaInfo} ->
|
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], <<$/>>),
|
2017-09-25 11:41:12 +02:00
|
|
|
ThumbEl = thumb_el(OutMediaInfo, URL),
|
2015-10-26 12:10:55 +01:00
|
|
|
{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.
|
|
|
|
|
2018-05-14 18:30:21 +02:00
|
|
|
-spec do_store_file(file:filename_all(), http_request(),
|
2015-10-26 22:30:58 +01:00
|
|
|
integer() | undefined,
|
|
|
|
integer() | undefined)
|
2015-10-26 12:10:55 +01:00
|
|
|
-> ok | {error, term()}.
|
2018-05-14 18:30:21 +02:00
|
|
|
do_store_file(Path, Request, FileMode, DirMode) ->
|
2015-10-26 12:10:55 +01:00
|
|
|
try
|
|
|
|
ok = filelib:ensure_dir(Path),
|
2018-05-14 18:30:21 +02:00
|
|
|
ok = ejabberd_http:recv_file(Request, Path),
|
2015-10-26 12:10:55 +01:00
|
|
|
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
|
2018-05-14 18:30:21 +02:00
|
|
|
end
|
2015-10-26 12:10:55 +01:00
|
|
|
catch
|
2015-11-09 19:23:52 +01:00
|
|
|
_:{badmatch, {error, 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).
|
|
|
|
|
2017-09-26 21:40:56 +02:00
|
|
|
-spec http_response(100..599)
|
2015-10-26 12:10:55 +01:00
|
|
|
-> {pos_integer(), [{binary(), binary()}], binary()}.
|
2017-09-26 21:40:56 +02:00
|
|
|
http_response(Code) ->
|
|
|
|
http_response(Code, []).
|
2015-10-26 12:10:55 +01:00
|
|
|
|
2017-09-26 21:40:56 +02:00
|
|
|
-spec http_response(100..599, [{binary(), binary()}])
|
2015-10-26 12:10:55 +01:00
|
|
|
-> {pos_integer(), [{binary(), binary()}], binary()}.
|
2017-09-26 21:40:56 +02:00
|
|
|
http_response(Code, ExtraHeaders) ->
|
2015-10-26 12:10:55 +01:00
|
|
|
Message = <<(code_to_message(Code))/binary, $\n>>,
|
2017-09-26 21:40:56 +02:00
|
|
|
http_response(Code, ExtraHeaders, Message).
|
2015-10-26 12:10:55 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-type http_body() :: binary() | {file, file:filename_all()}.
|
2018-05-14 18:30:21 +02:00
|
|
|
-spec http_response(100..599, [{binary(), binary()}], http_body())
|
2019-06-14 11:33:26 +02:00
|
|
|
-> {pos_integer(), [{binary(), binary()}], http_body()}.
|
2017-09-26 21:40:56 +02:00
|
|
|
http_response(Code, ExtraHeaders, Body) ->
|
2015-10-26 12:10:55 +01:00
|
|
|
Headers = case proplists:is_defined(<<"Content-Type">>, ExtraHeaders) of
|
2015-11-09 19:23:52 +01:00
|
|
|
true ->
|
2017-03-28 00:03:17 +02:00
|
|
|
ExtraHeaders;
|
2015-11-09 19:23:52 +01:00
|
|
|
false ->
|
2017-03-28 00:03:17 +02:00
|
|
|
[{<<"Content-Type">>, <<"text/plain">>} | ExtraHeaders]
|
2017-09-26 21:40:56 +02:00
|
|
|
end,
|
2015-10-26 12:10:55 +01:00
|
|
|
{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) -> <<"">>.
|
|
|
|
|
2018-06-26 18:32:29 +02:00
|
|
|
-spec format_error(atom()) -> string().
|
|
|
|
format_error(Reason) ->
|
|
|
|
case file:format_error(Reason) of
|
|
|
|
"unknown POSIX error" ->
|
|
|
|
case inet:format_error(Reason) of
|
|
|
|
"unknown POSIX error" ->
|
|
|
|
atom_to_list(Reason);
|
|
|
|
Txt ->
|
|
|
|
Txt
|
|
|
|
end;
|
|
|
|
Txt ->
|
|
|
|
Txt
|
|
|
|
end.
|
|
|
|
|
2015-10-26 12:10:55 +01:00
|
|
|
%%--------------------------------------------------------------------
|
2015-11-09 19:12:08 +01:00
|
|
|
%% Image manipulation stuff.
|
2015-10-26 12:10:55 +01:00
|
|
|
%%--------------------------------------------------------------------
|
2019-04-14 16:05:16 +02:00
|
|
|
-spec read_image(binary()) -> {ok, binary(), media_info()} | pass.
|
|
|
|
read_image(Path) ->
|
|
|
|
case file:read_file(Path) of
|
|
|
|
{ok, Data} ->
|
|
|
|
case eimp:identify(Data) of
|
|
|
|
{ok, Info} ->
|
|
|
|
{ok, Data,
|
|
|
|
#media_info{
|
|
|
|
path = Path,
|
2018-05-14 18:30:21 +02:00
|
|
|
type = proplists:get_value(type, Info),
|
|
|
|
width = proplists:get_value(width, Info),
|
|
|
|
height = proplists:get_value(height, Info)}};
|
2019-04-14 16:05:16 +02:00
|
|
|
{error, Why} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Cannot identify type of ~ts: ~ts",
|
2019-04-14 16:05:16 +02:00
|
|
|
[Path, eimp:format_error(Why)]),
|
|
|
|
pass
|
|
|
|
end;
|
|
|
|
{error, Reason} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Failed to read file ~ts: ~ts",
|
2018-06-26 18:32:29 +02:00
|
|
|
[Path, format_error(Reason)]),
|
2015-11-09 18:41:31 +01:00
|
|
|
pass
|
2015-10-26 12:10:55 +01:00
|
|
|
end.
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec convert(binary(), media_info()) -> {ok, media_info()} | pass.
|
2019-04-14 16:05:16 +02:00
|
|
|
convert(InData, #media_info{path = Path, type = T, width = W, height = H} = Info) ->
|
2015-11-09 19:12:08 +01:00
|
|
|
if W * H >= 25000000 ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("The image ~ts 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 ->
|
2019-06-14 11:33:26 +02:00
|
|
|
{ok, Info};
|
2017-09-25 11:41:12 +02:00
|
|
|
true ->
|
2015-10-26 12:10:55 +01:00
|
|
|
Dir = filename:dirname(Path),
|
2017-09-25 11:41:12 +02:00
|
|
|
Ext = atom_to_binary(T, latin1),
|
2018-07-05 10:51:49 +02:00
|
|
|
FileName = <<(p1_rand:get_string())/binary, $., Ext/binary>>,
|
2015-10-26 12:10:55 +01:00
|
|
|
OutPath = filename:join(Dir, FileName),
|
2017-09-25 11:41:12 +02:00
|
|
|
{W1, H1} = if W > H -> {300, round(H*300/W)};
|
|
|
|
H > W -> {round(W*300/H), 300};
|
|
|
|
true -> {300, 300}
|
|
|
|
end,
|
2019-04-14 16:05:16 +02:00
|
|
|
OutInfo = #media_info{path = OutPath, type = T, width = W1, height = H1},
|
|
|
|
case eimp:convert(InData, T, [{scale, {W1, H1}}]) of
|
|
|
|
{ok, OutData} ->
|
|
|
|
case file:write_file(OutPath, OutData) of
|
|
|
|
ok ->
|
|
|
|
{ok, OutInfo};
|
2017-09-25 11:41:12 +02:00
|
|
|
{error, Why} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Failed to write to ~ts: ~ts",
|
2019-04-14 16:05:16 +02:00
|
|
|
[OutPath, format_error(Why)]),
|
2017-09-25 11:41:12 +02:00
|
|
|
pass
|
|
|
|
end;
|
|
|
|
{error, Why} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Failed to convert ~ts to ~ts: ~ts",
|
2019-04-14 16:05:16 +02:00
|
|
|
[Path, OutPath, eimp:format_error(Why)]),
|
2015-10-26 12:10:55 +01:00
|
|
|
pass
|
2017-09-25 11:41:12 +02:00
|
|
|
end
|
2015-10-26 12:10:55 +01:00
|
|
|
end.
|
|
|
|
|
2017-09-25 11:41:12 +02:00
|
|
|
-spec thumb_el(media_info(), binary()) -> xmlel().
|
|
|
|
thumb_el(#media_info{type = T, height = H, width = W}, URI) ->
|
|
|
|
MimeType = <<"image/", (atom_to_binary(T, latin1))/binary>>,
|
|
|
|
Thumb = #thumbnail{'media-type' = MimeType, uri = URI,
|
|
|
|
height = H, width = W},
|
|
|
|
xmpp:encode(Thumb).
|
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),
|
2019-06-14 11:33:26 +02:00
|
|
|
DocRoot = mod_http_upload_opt:docroot(ServerHost),
|
|
|
|
JIDinURL = mod_http_upload_opt:jid_in_url(ServerHost),
|
2016-02-20 20:13:30 +01:00
|
|
|
DocRoot1 = expand_host(expand_home(DocRoot), ServerHost),
|
2017-02-25 08:01:01 +01:00
|
|
|
UserStr = make_user_string(jid:make(User, Server), JIDinURL),
|
2016-02-20 20:13:30 +01:00
|
|
|
UserDir = str:join([DocRoot1, UserStr], <<$/>>),
|
2019-09-20 11:36:31 +02:00
|
|
|
case misc:delete_dir(UserDir) of
|
2015-10-26 12:10:55 +01:00
|
|
|
ok ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("Removed HTTP upload directory of ~ts@~ts", [User, Server]);
|
2015-10-26 12:10:55 +01:00
|
|
|
{error, enoent} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Found no HTTP upload directory of ~ts@~ts", [User, Server]);
|
2015-10-26 12:10:55 +01:00
|
|
|
{error, Error} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Cannot remove HTTP upload directory of ~ts@~ts: ~ts",
|
2018-06-26 18:32:29 +02:00
|
|
|
[User, Server, format_error(Error)])
|
2015-10-26 12:10:55 +01:00
|
|
|
end,
|
|
|
|
ok.
|