0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-27 22:38:21 +02:00

modules/media: Optimize various event fetches.

This commit is contained in:
Jason Volk 2018-12-29 17:33:51 -08:00
parent afba6c2b5c
commit 0e58aceee9
3 changed files with 46 additions and 12 deletions

View file

@ -81,9 +81,19 @@ get__download_local(client &client,
const string_view &file,
const m::room &room)
{
static const m::event::fetch::opts fopts
{
m::event::keys::include {"content"}
};
const m::room::state state
{
room, &fopts
};
// Get the file's total size
size_t file_size{0};
room.get("ircd.file.stat", "size", [&file_size]
state.get("ircd.file.stat", "size", [&file_size]
(const m::event &event)
{
file_size = at<"content"_>(event).get<size_t>("value");
@ -96,7 +106,7 @@ get__download_local(client &client,
"application/octet-stream"
};
room.get("ircd.file.stat", "type", [&type_buf, &content_type]
state.get("ircd.file.stat", "type", [&type_buf, &content_type]
(const m::event &event)
{
const auto &value
@ -116,12 +126,14 @@ get__download_local(client &client,
client, http::OK, content_type, file_size
};
size_t sent{0}, read;
read = read_each_block(room, [&client, &sent]
(const string_view &block)
size_t sent{0}, read
{
sent += write_all(*client.sock, block);
});
read_each_block(room, [&client, &sent]
(const string_view &block)
{
sent += write_all(*client.sock, block);
})
};
if(unlikely(read != file_size)) log::error
{

View file

@ -371,14 +371,26 @@ size_t
read_each_block(const m::room &room,
const std::function<void (const const_buffer &)> &closure)
{
static const m::event::fetch::opts fopts
{
m::event::keys::include
{
"content", "type"
}
};
size_t ret{0};
m::room::messages it
{
room, 1, &fopts
};
// Block buffer
const unique_buffer<mutable_buffer> buf
{
64_KiB
};
size_t ret{0};
m::room::messages it{room, 1};
for(; bool(it); ++it)
{
const m::event &event{*it};
@ -404,7 +416,7 @@ read_each_block(const m::room &room,
{
"File [%s] block [%s] (%s) blksz %zu != %zu",
string_view{room.room_id},
string_view{at<"event_id"_>(event)},
string_view{m::get(std::nothrow, it.event_idx(), "event_id", buf)},
hash,
blksz,
size(block)

View file

@ -105,9 +105,19 @@ get__thumbnail_local(client &client,
const string_view &mediaid,
const m::room &room)
{
static const m::event::fetch::opts fopts
{
m::event::keys::include {"content"}
};
const m::room::state state
{
room, &fopts
};
// Get the file's total size
size_t file_size{0};
room.get("ircd.file.stat", "size", [&file_size]
state.get("ircd.file.stat", "size", [&file_size]
(const m::event &event)
{
file_size = at<"content"_>(event).get<size_t>("value");
@ -120,7 +130,7 @@ get__thumbnail_local(client &client,
"application/octet-stream"
};
room.get("ircd.file.stat", "type", [&type_buf, &content_type]
state.get("ircd.file.stat", "type", [&type_buf, &content_type]
(const m::event &event)
{
const auto &value