2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2017-10-25 18:42:23 +02:00
|
|
|
|
2018-03-08 21:38:02 +01:00
|
|
|
#include "media.h"
|
2017-10-25 18:42:23 +02:00
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
using namespace ircd;
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource
|
2017-10-25 18:42:23 +02:00
|
|
|
download_resource
|
|
|
|
{
|
2018-02-26 14:42:46 +01:00
|
|
|
"/_matrix/media/r0/download/",
|
2017-10-25 18:42:23 +02:00
|
|
|
{
|
2018-02-26 14:42:46 +01:00
|
|
|
"(11.7.1.2) download",
|
|
|
|
resource::DIRECTORY,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
static m::resource::response
|
2018-04-21 09:51:51 +02:00
|
|
|
get__download_local(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request,
|
2023-02-22 22:14:46 +01:00
|
|
|
const m::media::mxc &mxc,
|
2018-04-21 09:51:51 +02:00
|
|
|
const m::room &room);
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
static m::resource::response
|
2018-02-26 14:42:46 +01:00
|
|
|
get__download(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request)
|
2017-10-25 18:42:23 +02:00
|
|
|
{
|
|
|
|
if(request.parv.size() < 2)
|
|
|
|
throw http::error
|
|
|
|
{
|
|
|
|
http::MULTIPLE_CHOICES, "/ download / domain / file"
|
|
|
|
};
|
|
|
|
|
2023-02-22 22:14:46 +01:00
|
|
|
char url_buf[2][256];
|
|
|
|
const m::media::mxc mxc
|
2017-10-25 18:42:23 +02:00
|
|
|
{
|
2023-02-22 22:14:46 +01:00
|
|
|
url::decode(url_buf[0], request.parv[0]),
|
|
|
|
url::decode(url_buf[1], request.parv[1]),
|
2017-10-25 18:42:23 +02:00
|
|
|
};
|
|
|
|
|
2018-04-30 16:30:34 +02:00
|
|
|
// Download doesn't require auth so if there is no user_id detected
|
|
|
|
// then we download on behalf of @ircd.
|
|
|
|
const m::user::id &user_id
|
|
|
|
{
|
|
|
|
request.user_id?
|
|
|
|
m::user::id{request.user_id}:
|
2019-10-01 05:50:58 +02:00
|
|
|
m::me()
|
2018-04-30 16:30:34 +02:00
|
|
|
};
|
|
|
|
|
2019-08-08 00:04:12 +02:00
|
|
|
const bool allow_remote
|
|
|
|
{
|
|
|
|
request.query.get<bool>("allow_remote", true)
|
|
|
|
};
|
|
|
|
|
2018-04-21 09:51:51 +02:00
|
|
|
const m::room::id::buf room_id
|
|
|
|
{
|
2023-02-22 22:14:46 +01:00
|
|
|
m::media::file::download(mxc, user_id)
|
2018-04-21 09:51:51 +02:00
|
|
|
};
|
|
|
|
|
2023-02-22 22:14:46 +01:00
|
|
|
return get__download_local(client, request, mxc, room_id);
|
2018-04-21 09:51:51 +02:00
|
|
|
}
|
2017-10-25 18:42:23 +02:00
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
static m::resource::response
|
2018-04-21 09:51:51 +02:00
|
|
|
get__download_local(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request,
|
2023-02-22 22:14:46 +01:00
|
|
|
const m::media::mxc &mxc,
|
2018-04-21 09:51:51 +02:00
|
|
|
const m::room &room)
|
|
|
|
{
|
2018-12-30 02:33:51 +01:00
|
|
|
static const m::event::fetch::opts fopts
|
|
|
|
{
|
|
|
|
m::event::keys::include {"content"}
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room::state state
|
|
|
|
{
|
|
|
|
room, &fopts
|
|
|
|
};
|
|
|
|
|
2018-04-21 09:51:51 +02:00
|
|
|
// Get the file's total size
|
|
|
|
size_t file_size{0};
|
2020-08-21 12:37:10 +02:00
|
|
|
state.get("ircd.file.stat.size", "", [&file_size]
|
2018-04-21 09:51:51 +02:00
|
|
|
(const m::event &event)
|
|
|
|
{
|
2020-08-21 12:37:10 +02:00
|
|
|
file_size = at<"content"_>(event).get<size_t>("bytes");
|
2018-04-21 09:51:51 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Get the MIME type
|
|
|
|
char type_buf[64];
|
|
|
|
string_view content_type
|
2017-10-25 18:42:23 +02:00
|
|
|
{
|
2018-04-21 09:51:51 +02:00
|
|
|
"application/octet-stream"
|
2017-10-25 18:42:23 +02:00
|
|
|
};
|
2018-04-21 09:51:51 +02:00
|
|
|
|
2020-08-21 12:37:10 +02:00
|
|
|
state.get("ircd.file.stat.type", "", [&type_buf, &content_type]
|
2018-04-21 09:51:51 +02:00
|
|
|
(const m::event &event)
|
|
|
|
{
|
2020-08-21 12:37:10 +02:00
|
|
|
const json::string &value
|
2018-04-21 09:51:51 +02:00
|
|
|
{
|
2020-08-21 12:37:10 +02:00
|
|
|
at<"content"_>(event).at("mime_type")
|
2018-04-21 09:51:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
content_type =
|
|
|
|
{
|
|
|
|
type_buf, copy(type_buf, value)
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-06-13 03:06:55 +02:00
|
|
|
static const auto &addl_headers
|
|
|
|
{
|
|
|
|
"Cache-Control: public, max-age=31536000, immutable\r\n"_sv
|
|
|
|
};
|
|
|
|
|
2018-04-21 09:51:51 +02:00
|
|
|
// Send HTTP head to client
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response
|
2018-04-21 09:51:51 +02:00
|
|
|
{
|
2020-06-13 03:06:55 +02:00
|
|
|
client,
|
|
|
|
http::OK,
|
|
|
|
content_type,
|
|
|
|
file_size,
|
|
|
|
addl_headers,
|
2018-04-21 09:51:51 +02:00
|
|
|
};
|
|
|
|
|
2018-12-30 02:33:51 +01:00
|
|
|
size_t sent{0}, read
|
2018-04-21 09:51:51 +02:00
|
|
|
{
|
2019-08-22 08:57:07 +02:00
|
|
|
m::media::file::read(room, [&client, &sent]
|
2018-12-30 02:33:51 +01:00
|
|
|
(const string_view &block)
|
|
|
|
{
|
|
|
|
sent += write_all(*client.sock, block);
|
|
|
|
})
|
|
|
|
};
|
2018-04-21 09:51:51 +02:00
|
|
|
|
2020-03-13 22:51:44 +01:00
|
|
|
if(unlikely(read != file_size))
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
m::media::log, "File %s/%s [%s] size mismatch: expected %zu got %zu",
|
2023-02-22 22:14:46 +01:00
|
|
|
mxc.server,
|
|
|
|
mxc.mediaid,
|
2020-03-13 22:51:44 +01:00
|
|
|
string_view{room.room_id},
|
|
|
|
file_size,
|
|
|
|
read
|
|
|
|
};
|
2018-04-25 03:26:35 +02:00
|
|
|
|
|
|
|
// Have to kill client here after failing content length expectation.
|
|
|
|
if(unlikely(read != file_size))
|
|
|
|
client.close(net::dc::RST, net::close_ignore);
|
|
|
|
|
2018-04-21 09:51:51 +02:00
|
|
|
return {};
|
2017-10-25 18:42:23 +02:00
|
|
|
}
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
static m::resource::method
|
2018-02-26 14:42:46 +01:00
|
|
|
method_get
|
2017-10-25 18:42:23 +02:00
|
|
|
{
|
2020-05-19 00:39:41 +02:00
|
|
|
download_resource,
|
|
|
|
"GET",
|
|
|
|
get__download,
|
|
|
|
{
|
|
|
|
m::resource::method::flag(0), // flags
|
|
|
|
45s, // timeout
|
|
|
|
}
|
2018-02-26 14:42:46 +01:00
|
|
|
};
|