mirror of
https://github.com/matrix-construct/construct
synced 2024-09-29 12:18:54 +02:00
modules/media: Optimize various event fetches.
This commit is contained in:
parent
afba6c2b5c
commit
0e58aceee9
3 changed files with 46 additions and 12 deletions
|
@ -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
|
||||
{
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue