2018-04-12 23:44:30 +02: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.
|
|
|
|
|
|
|
|
#include "media.h"
|
|
|
|
|
2019-08-02 03:59:16 +02:00
|
|
|
struct ircd::m::media::magick
|
|
|
|
{
|
|
|
|
module modules
|
|
|
|
{
|
|
|
|
"magick"
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
ircd::mapi::header
|
|
|
|
IRCD_MODULE
|
2019-07-26 06:09:43 +02:00
|
|
|
{
|
2019-08-22 08:57:07 +02:00
|
|
|
"11.7 :Content respository",
|
|
|
|
ircd::m::media::init,
|
|
|
|
ircd::m::media::fini
|
|
|
|
};
|
2019-07-26 06:09:43 +02:00
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
decltype(ircd::m::media::magick_support)
|
|
|
|
ircd::m::media::magick_support;
|
2018-09-05 05:33:22 +02:00
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
decltype(ircd::m::media::log)
|
|
|
|
ircd::m::media::log
|
2018-09-05 05:33:22 +02:00
|
|
|
{
|
2019-08-05 00:10:13 +02:00
|
|
|
"m.media"
|
2018-09-05 05:33:22 +02:00
|
|
|
};
|
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
decltype(ircd::m::media::blocks_cache_enable)
|
|
|
|
ircd::m::media::blocks_cache_enable
|
2018-09-15 15:41:00 +02:00
|
|
|
{
|
|
|
|
{ "name", "ircd.media.blocks.cache.enable" },
|
|
|
|
{ "default", true },
|
|
|
|
};
|
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
decltype(ircd::m::media::blocks_cache_comp_enable)
|
|
|
|
ircd::m::media::blocks_cache_comp_enable
|
2018-09-15 15:41:00 +02:00
|
|
|
{
|
|
|
|
{ "name", "ircd.media.blocks.cache_comp.enable" },
|
|
|
|
{ "default", false },
|
|
|
|
};
|
|
|
|
|
2018-04-30 16:31:11 +02:00
|
|
|
// Blocks column
|
2019-08-22 08:57:07 +02:00
|
|
|
decltype(ircd::m::media::blocks_descriptor)
|
|
|
|
ircd::m::media::blocks_descriptor
|
2018-04-30 16:31:11 +02:00
|
|
|
{
|
|
|
|
// name
|
|
|
|
"blocks",
|
|
|
|
|
|
|
|
// explain
|
|
|
|
R"(
|
|
|
|
Key-value store of blocks belonging to files. The key is a hash of
|
|
|
|
the block. The key is plaintext sha256-b58 and the block is binary
|
|
|
|
up to 32768 bytes.
|
|
|
|
)",
|
|
|
|
|
|
|
|
// typing
|
|
|
|
{
|
|
|
|
typeid(string_view), typeid(string_view)
|
|
|
|
},
|
|
|
|
|
2018-09-05 05:33:22 +02:00
|
|
|
{}, // options
|
|
|
|
{}, // comparaor
|
|
|
|
{}, // prefix transform
|
2018-11-30 23:43:51 +01:00
|
|
|
false, // drop column
|
2018-09-18 06:45:09 +02:00
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
bool(blocks_cache_enable)? -1 : 0,
|
2018-09-18 06:45:09 +02:00
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
bool(blocks_cache_comp_enable)? -1 : 0,
|
2018-09-18 06:45:09 +02:00
|
|
|
|
|
|
|
// bloom_bits
|
2018-10-16 17:46:03 +02:00
|
|
|
10,
|
2018-09-18 06:45:09 +02:00
|
|
|
|
|
|
|
// expect hit
|
|
|
|
true,
|
|
|
|
|
|
|
|
// block_size
|
|
|
|
32_KiB,
|
2018-11-30 00:42:27 +01:00
|
|
|
|
|
|
|
// meta block size
|
|
|
|
512,
|
|
|
|
|
|
|
|
// compression
|
|
|
|
""s // no compression
|
2018-04-30 16:31:11 +02:00
|
|
|
};
|
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
decltype(ircd::m::media::description)
|
|
|
|
ircd::m::media::description
|
2018-04-30 16:31:11 +02:00
|
|
|
{
|
|
|
|
{ "default" }, // requirement of RocksDB
|
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
blocks_descriptor,
|
2018-04-30 16:31:11 +02:00
|
|
|
};
|
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
decltype(ircd::m::media::blocks_cache_size)
|
|
|
|
ircd::m::media::blocks_cache_size
|
2018-04-12 23:44:30 +02:00
|
|
|
{
|
2018-04-30 16:31:11 +02:00
|
|
|
{
|
2018-09-05 09:01:39 +02:00
|
|
|
{ "name", "ircd.media.blocks.cache.size" },
|
|
|
|
{ "default", long(48_MiB) },
|
|
|
|
}, []
|
2018-06-01 14:38:50 +02:00
|
|
|
{
|
2018-09-05 09:07:49 +02:00
|
|
|
if(!blocks)
|
|
|
|
return;
|
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
const size_t &value{blocks_cache_size};
|
2018-09-05 09:01:39 +02:00
|
|
|
db::capacity(db::cache(blocks), value);
|
2018-04-30 16:31:11 +02:00
|
|
|
}
|
2018-04-12 23:44:30 +02:00
|
|
|
};
|
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
decltype(ircd::m::media::blocks_cache_comp_size)
|
|
|
|
ircd::m::media::blocks_cache_comp_size
|
2018-04-25 03:26:35 +02:00
|
|
|
{
|
2018-09-05 09:01:39 +02:00
|
|
|
{
|
|
|
|
{ "name", "ircd.media.blocks.cache_comp.size" },
|
|
|
|
{ "default", long(16_MiB) },
|
|
|
|
}, []
|
|
|
|
{
|
2018-09-05 09:07:49 +02:00
|
|
|
if(!blocks)
|
|
|
|
return;
|
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
const size_t &value{blocks_cache_comp_size};
|
2018-09-05 09:01:39 +02:00
|
|
|
db::capacity(db::cache_compressed(blocks), value);
|
|
|
|
}
|
2018-04-25 03:26:35 +02:00
|
|
|
};
|
|
|
|
|
2019-09-17 21:41:20 +02:00
|
|
|
decltype(ircd::m::media::blocks_prefetch)
|
|
|
|
ircd::m::media::blocks_prefetch
|
|
|
|
{
|
|
|
|
{ "name", "ircd.media.file.prefetch.blocks" },
|
|
|
|
{ "default", 32L },
|
|
|
|
};
|
|
|
|
|
2019-09-17 03:12:51 +02:00
|
|
|
decltype(ircd::m::media::events_prefetch)
|
|
|
|
ircd::m::media::events_prefetch
|
|
|
|
{
|
|
|
|
{ "name", "ircd.media.file.prefetch.events" },
|
|
|
|
{ "default", 16L },
|
|
|
|
};
|
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
decltype(ircd::m::media::database)
|
|
|
|
ircd::m::media::database;
|
2018-09-30 04:57:14 +02:00
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
decltype(ircd::m::media::blocks)
|
|
|
|
ircd::m::media::blocks;
|
2019-07-26 06:02:48 +02:00
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
decltype(ircd::m::media::downloading)
|
|
|
|
ircd::m::media::downloading;
|
2018-09-30 04:57:14 +02:00
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
decltype(ircd::m::media::downloading_dock)
|
|
|
|
ircd::m::media::downloading_dock;
|
2018-04-26 06:56:43 +02:00
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
//
|
|
|
|
// init
|
|
|
|
//
|
2018-04-26 06:56:43 +02:00
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
void
|
|
|
|
ircd::m::media::init()
|
|
|
|
{
|
|
|
|
static const std::string dbopts;
|
|
|
|
database = std::make_shared<db::database>("media", dbopts, description);
|
|
|
|
blocks = db::column{*database, "blocks"};
|
|
|
|
|
|
|
|
// The conf setter callbacks must be manually executed after
|
|
|
|
// the database was just loaded to set the cache size.
|
|
|
|
conf::reset("ircd.media.blocks.cache.size");
|
|
|
|
conf::reset("ircd.media.blocks.cache_comp.size");
|
|
|
|
|
|
|
|
// conditions to load the magick.so module
|
|
|
|
const bool enable_magick
|
|
|
|
{
|
|
|
|
// used by the thumbnailer
|
|
|
|
thumbnail::enable
|
|
|
|
|
|
|
|
// support is available
|
|
|
|
&& mods::available("magick")
|
|
|
|
};
|
|
|
|
|
|
|
|
if(enable_magick)
|
|
|
|
magick_support.reset(new media::magick{});
|
|
|
|
else
|
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
log, "GraphicsMagick support is disabled or unavailable."
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::media::fini()
|
|
|
|
{
|
|
|
|
magick_support.reset();
|
|
|
|
|
|
|
|
// The database close contains pthread_join()'s within RocksDB which
|
|
|
|
// deadlock under certain conditions when called during a dlclose()
|
|
|
|
// (i.e static destruction of this module). Therefor we must manually
|
|
|
|
// close the db here first.
|
|
|
|
database = std::shared_ptr<db::database>{};
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// media::file
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::room::id::buf
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::media::file::download(const mxc &mxc,
|
|
|
|
const m::user::id &user_id,
|
|
|
|
const net::hostport &remote)
|
2018-04-26 06:56:43 +02:00
|
|
|
{
|
|
|
|
const m::room::id::buf room_id
|
|
|
|
{
|
2019-08-22 08:57:07 +02:00
|
|
|
file::room_id(mxc)
|
2018-04-26 06:56:43 +02:00
|
|
|
};
|
|
|
|
|
2019-03-08 00:02:43 +01:00
|
|
|
thread_local char rembuf[256];
|
|
|
|
if(remote && my_host(string(rembuf, remote)))
|
|
|
|
return room_id;
|
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
if(!remote && my_host(mxc.server))
|
2019-03-08 00:02:43 +01:00
|
|
|
return room_id;
|
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
download(mxc, user_id, remote, room_id);
|
2018-04-26 06:56:43 +02:00
|
|
|
return room_id;
|
|
|
|
}
|
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
ircd::m::room
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::media::file::download(const mxc &mxc,
|
|
|
|
const m::user::id &user_id,
|
|
|
|
const net::hostport &remote,
|
|
|
|
const m::room::id &room_id)
|
2018-04-26 06:56:43 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
auto iit
|
|
|
|
{
|
|
|
|
downloading.emplace(room_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!iit.second)
|
|
|
|
{
|
2019-07-06 01:41:56 +02:00
|
|
|
downloading_dock.wait([&room_id]
|
2018-04-26 06:56:43 +02:00
|
|
|
{
|
2019-07-06 01:41:56 +02:00
|
|
|
return !downloading.count(room_id);
|
|
|
|
});
|
|
|
|
|
2018-04-26 06:56:43 +02:00
|
|
|
return room_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
const unwind uw{[&iit]
|
|
|
|
{
|
|
|
|
downloading.erase(iit.first);
|
|
|
|
downloading_dock.notify_all();
|
|
|
|
}};
|
|
|
|
|
2018-05-10 02:07:37 +02:00
|
|
|
if(exists(room_id))
|
2018-04-26 06:56:43 +02:00
|
|
|
return room_id;
|
|
|
|
|
|
|
|
const unique_buffer<mutable_buffer> buf
|
|
|
|
{
|
|
|
|
16_KiB
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto pair
|
|
|
|
{
|
2019-08-22 08:57:07 +02:00
|
|
|
download(buf, mxc, remote)
|
2018-04-26 06:56:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto &head
|
|
|
|
{
|
|
|
|
pair.first
|
|
|
|
};
|
|
|
|
|
|
|
|
const const_buffer &content
|
|
|
|
{
|
|
|
|
pair.second
|
|
|
|
};
|
|
|
|
|
|
|
|
char mime_type_buf[64];
|
|
|
|
const auto &content_type
|
|
|
|
{
|
|
|
|
magic::mime(mime_type_buf, content)
|
|
|
|
};
|
|
|
|
|
2018-09-05 05:34:50 +02:00
|
|
|
if(content_type != head.content_type) log::dwarning
|
2018-04-26 06:56:43 +02:00
|
|
|
{
|
2019-08-22 08:57:07 +02:00
|
|
|
log, "Server %s claims thumbnail %s is '%s' but we think it is '%s'",
|
2018-04-26 06:56:43 +02:00
|
|
|
string(remote),
|
2019-08-22 08:57:07 +02:00
|
|
|
mxc.mediaid,
|
2018-04-26 06:56:43 +02:00
|
|
|
head.content_type,
|
2019-08-22 08:57:07 +02:00
|
|
|
content_type,
|
2018-04-26 06:56:43 +02:00
|
|
|
};
|
|
|
|
|
2018-05-07 01:04:51 +02:00
|
|
|
m::vm::copts vmopts;
|
2018-04-26 06:56:43 +02:00
|
|
|
vmopts.history = false;
|
|
|
|
const m::room room
|
|
|
|
{
|
|
|
|
room_id, &vmopts
|
|
|
|
};
|
|
|
|
|
2018-04-30 16:30:34 +02:00
|
|
|
create(room, user_id, "file");
|
2019-12-03 22:05:32 +01:00
|
|
|
const unwind_exceptional purge{[&room]
|
2019-06-15 21:25:14 +02:00
|
|
|
{
|
|
|
|
m::room::purge(room);
|
|
|
|
}};
|
2018-04-26 06:56:43 +02:00
|
|
|
|
|
|
|
const size_t written
|
|
|
|
{
|
2019-08-22 08:57:07 +02:00
|
|
|
file::write(room, user_id, content, content_type)
|
2018-04-26 06:56:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return room;
|
|
|
|
}
|
|
|
|
catch(const ircd::server::unavailable &e)
|
|
|
|
{
|
2019-03-08 00:02:14 +01:00
|
|
|
thread_local char rembuf[256];
|
|
|
|
throw m::error
|
2018-04-26 06:56:43 +02:00
|
|
|
{
|
2019-03-08 00:02:14 +01:00
|
|
|
http::BAD_GATEWAY, "M_MEDIA_UNAVAILABLE",
|
|
|
|
"Server '%s' is not available for media for '%s/%s' :%s",
|
|
|
|
string(rembuf, remote),
|
2019-08-22 08:57:07 +02:00
|
|
|
mxc.server,
|
|
|
|
mxc.mediaid,
|
2019-03-08 00:02:14 +01:00
|
|
|
e.what()
|
2018-04-26 06:56:43 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
decltype(ircd::m::media::download_timeout)
|
|
|
|
ircd::m::media::download_timeout
|
2018-09-05 05:34:10 +02:00
|
|
|
{
|
|
|
|
{ "name", "ircd.media.download.timeout" },
|
|
|
|
{ "default", 15L },
|
|
|
|
};
|
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
std::pair
|
|
|
|
<
|
|
|
|
ircd::http::response::head,
|
|
|
|
ircd::unique_buffer<ircd::mutable_buffer>
|
|
|
|
>
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::media::file::download(const mutable_buffer &head_buf,
|
|
|
|
const mxc &mxc,
|
|
|
|
net::hostport remote,
|
|
|
|
server::request::opts *const opts)
|
2018-04-26 06:56:43 +02:00
|
|
|
{
|
2019-03-08 00:02:14 +01:00
|
|
|
thread_local char rembuf[256];
|
2019-08-22 08:57:07 +02:00
|
|
|
assert(remote || !my_host(mxc.server));
|
2019-03-08 00:02:43 +01:00
|
|
|
assert(!remote || !my_host(string(rembuf, remote)));
|
2019-03-08 00:02:14 +01:00
|
|
|
|
2018-04-26 06:56:43 +02:00
|
|
|
if(!remote)
|
2019-08-22 08:57:07 +02:00
|
|
|
remote = mxc.server;
|
2018-04-26 06:56:43 +02:00
|
|
|
|
|
|
|
window_buffer wb{head_buf};
|
|
|
|
thread_local char uri[4_KiB];
|
|
|
|
http::request
|
|
|
|
{
|
|
|
|
wb, host(remote), "GET", fmt::sprintf
|
|
|
|
{
|
2019-08-22 08:57:07 +02:00
|
|
|
uri, "/_matrix/media/r0/download/%s/%s",
|
|
|
|
mxc.server,
|
|
|
|
mxc.mediaid,
|
2018-04-26 06:56:43 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const const_buffer out_head
|
|
|
|
{
|
|
|
|
wb.completed()
|
|
|
|
};
|
|
|
|
|
|
|
|
// Remaining space in buffer is used for received head
|
|
|
|
const mutable_buffer in_head
|
|
|
|
{
|
|
|
|
data(head_buf) + size(out_head), size(head_buf) - size(out_head)
|
|
|
|
};
|
|
|
|
|
|
|
|
//TODO: --- This should use the progress callback to build blocks
|
|
|
|
server::request remote_request
|
|
|
|
{
|
2019-04-11 06:17:55 +02:00
|
|
|
remote, { out_head }, { in_head, {} }, opts
|
2018-04-26 06:56:43 +02:00
|
|
|
};
|
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
if(!remote_request.wait(seconds(download_timeout), std::nothrow))
|
2019-03-08 00:02:14 +01:00
|
|
|
throw m::error
|
2018-04-26 06:56:43 +02:00
|
|
|
{
|
2019-03-08 00:02:14 +01:00
|
|
|
http::GATEWAY_TIMEOUT, "M_MEDIA_DOWNLOAD_TIMEOUT",
|
|
|
|
"Server '%s' did not respond with media for '%s/%s' in time",
|
|
|
|
string(rembuf, remote),
|
2019-08-22 08:57:07 +02:00
|
|
|
mxc.server,
|
|
|
|
mxc.mediaid
|
2018-04-26 06:56:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto &code
|
|
|
|
{
|
|
|
|
remote_request.get()
|
|
|
|
};
|
|
|
|
|
|
|
|
if(code != http::OK)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
parse::buffer pb{remote_request.in.head};
|
|
|
|
parse::capstan pc{pb};
|
|
|
|
pc.read += size(remote_request.in.head);
|
2019-04-12 18:57:57 +02:00
|
|
|
return std::pair<http::response::head, unique_buffer<mutable_buffer>>
|
2018-04-26 06:56:43 +02:00
|
|
|
{
|
2019-04-12 18:57:57 +02:00
|
|
|
pc, std::move(remote_request.in.dynamic)
|
2018-04-26 06:56:43 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-04-13 00:58:30 +02:00
|
|
|
size_t
|
2019-08-22 08:57:07 +02:00
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::media::file::write(const m::room &room,
|
|
|
|
const m::user::id &user_id,
|
|
|
|
const const_buffer &content,
|
|
|
|
const string_view &content_type)
|
2018-04-13 00:58:30 +02:00
|
|
|
{
|
|
|
|
//TODO: TXN
|
2019-06-24 09:25:53 +02:00
|
|
|
send(room, user_id, "ircd.file.stat", "size", json::members
|
2018-04-13 00:58:30 +02:00
|
|
|
{
|
|
|
|
{ "value", long(size(content)) }
|
|
|
|
});
|
|
|
|
|
|
|
|
//TODO: TXN
|
2019-06-24 09:25:53 +02:00
|
|
|
send(room, user_id, "ircd.file.stat", "type", json::members
|
2018-04-13 00:58:30 +02:00
|
|
|
{
|
|
|
|
{ "value", content_type }
|
|
|
|
});
|
|
|
|
|
|
|
|
size_t off{0}, wrote{0};
|
|
|
|
while(off < size(content))
|
|
|
|
{
|
|
|
|
const size_t blksz
|
|
|
|
{
|
|
|
|
std::min(size(content) - off, size_t(32_KiB))
|
|
|
|
};
|
|
|
|
|
|
|
|
const const_buffer &block
|
|
|
|
{
|
|
|
|
data(content) + off, blksz
|
|
|
|
};
|
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
block::set(room, user_id, block);
|
2018-04-30 16:31:11 +02:00
|
|
|
wrote += size(block);
|
2018-04-13 00:58:30 +02:00
|
|
|
off += blksz;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(off == size(content));
|
2018-04-26 03:44:02 +02:00
|
|
|
assert(wrote == off);
|
2018-04-13 00:58:30 +02:00
|
|
|
return wrote;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2019-08-22 08:57:07 +02:00
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::media::file::read(const m::room &room,
|
|
|
|
const closure &closure)
|
2018-04-13 00:58:30 +02:00
|
|
|
{
|
2019-08-22 08:57:07 +02:00
|
|
|
static const event::fetch::opts fopts
|
2018-12-30 02:33:51 +01:00
|
|
|
{
|
2019-08-22 08:57:07 +02:00
|
|
|
event::keys::include { "content", "type" }
|
2018-12-30 02:33:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
size_t ret{0};
|
2019-08-30 23:26:07 +02:00
|
|
|
room::events it
|
2018-12-30 02:33:51 +01:00
|
|
|
{
|
|
|
|
room, 1, &fopts
|
|
|
|
};
|
|
|
|
|
2019-09-17 03:12:51 +02:00
|
|
|
if(!it)
|
|
|
|
return ret;
|
|
|
|
|
2019-09-17 21:41:20 +02:00
|
|
|
size_t events_fetched(0), events_prefetched(0);
|
|
|
|
room::events epf
|
|
|
|
{
|
|
|
|
room, 1, &fopts
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t blocks_fetched(0), blocks_prefetched(0);
|
|
|
|
room::events bpf
|
2019-09-17 03:12:51 +02:00
|
|
|
{
|
|
|
|
room, 1, &fopts
|
|
|
|
};
|
|
|
|
|
2019-09-17 21:41:20 +02:00
|
|
|
for(; it; ++it)
|
2018-04-13 00:58:30 +02:00
|
|
|
{
|
2019-09-17 21:41:20 +02:00
|
|
|
for(; bpf && blocks_prefetched < blocks_fetched + blocks_prefetch; ++bpf)
|
|
|
|
{
|
|
|
|
for(; epf && events_prefetched < events_fetched + events_prefetch; ++epf)
|
|
|
|
events_prefetched += epf.prefetch();
|
|
|
|
|
|
|
|
++events_fetched;
|
|
|
|
const m::event &event
|
|
|
|
{
|
|
|
|
*bpf
|
|
|
|
};
|
|
|
|
|
|
|
|
if(at<"type"_>(event) != "ircd.file.block")
|
|
|
|
continue;
|
2019-09-17 03:12:51 +02:00
|
|
|
|
2019-09-17 21:41:20 +02:00
|
|
|
const json::string &hash
|
|
|
|
{
|
|
|
|
at<"content"_>(event).at("hash")
|
|
|
|
};
|
|
|
|
|
|
|
|
blocks_prefetched += block::prefetch(hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!blocks_fetched)
|
|
|
|
ctx::yield();
|
|
|
|
|
|
|
|
++blocks_fetched;
|
2019-09-17 03:12:51 +02:00
|
|
|
const m::event &event
|
|
|
|
{
|
|
|
|
*it
|
|
|
|
};
|
|
|
|
|
2018-04-13 00:58:30 +02:00
|
|
|
if(at<"type"_>(event) != "ircd.file.block")
|
|
|
|
continue;
|
|
|
|
|
2019-07-06 01:41:56 +02:00
|
|
|
const json::string &hash
|
2018-04-13 00:58:30 +02:00
|
|
|
{
|
2019-07-06 01:41:56 +02:00
|
|
|
at<"content"_>(event).at("hash")
|
2018-04-13 00:58:30 +02:00
|
|
|
};
|
|
|
|
|
2019-09-17 23:02:46 +02:00
|
|
|
const auto &block_size
|
2018-04-13 00:58:30 +02:00
|
|
|
{
|
|
|
|
at<"content"_>(event).get<size_t>("size")
|
|
|
|
};
|
|
|
|
|
2019-09-17 23:02:46 +02:00
|
|
|
const auto handle{[&](const const_buffer &block)
|
2018-04-13 00:58:30 +02:00
|
|
|
{
|
2019-09-17 23:02:46 +02:00
|
|
|
if(unlikely(size(block) != block_size))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"File [%s] block [%s] event %s idx:%lu block size %zu != %zu",
|
|
|
|
string_view{room.room_id},
|
|
|
|
hash,
|
|
|
|
string_view{event.event_id},
|
|
|
|
it.event_idx(),
|
|
|
|
block_size,
|
|
|
|
size(block)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(size(block) == block_size);
|
|
|
|
ret += size(block);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
log::debug
|
2019-09-17 21:41:20 +02:00
|
|
|
{
|
2019-09-17 23:02:46 +02:00
|
|
|
log, "File %s read %s block[fetched:%zu prefetched:%zu] events[fetched:%zu prefetched:%zu] size:%zu total:%zu",
|
2019-09-17 21:41:20 +02:00
|
|
|
string_view{room.room_id},
|
|
|
|
hash,
|
2019-09-17 23:02:46 +02:00
|
|
|
blocks_fetched,
|
|
|
|
blocks_prefetched,
|
|
|
|
events_fetched,
|
|
|
|
events_prefetched,
|
|
|
|
block_size,
|
|
|
|
ret,
|
2019-09-17 21:41:20 +02:00
|
|
|
};
|
2019-09-17 23:02:46 +02:00
|
|
|
#endif
|
2019-09-17 21:41:20 +02:00
|
|
|
|
2019-09-17 23:02:46 +02:00
|
|
|
closure(block);
|
|
|
|
}};
|
2019-09-17 21:41:20 +02:00
|
|
|
|
2019-09-17 23:02:46 +02:00
|
|
|
if(unlikely(!block::get(hash, handle)))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"File [%s] block %s missing in event %s idx:%lu",
|
|
|
|
string_view{room.room_id},
|
|
|
|
hash,
|
|
|
|
string_view{event.event_id},
|
|
|
|
it.event_idx(),
|
|
|
|
};
|
2018-04-13 00:58:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2018-04-25 02:41:51 +02:00
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
//
|
|
|
|
// media::file
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::room::id::buf
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::media::file::room_id(const mxc &mxc)
|
|
|
|
{
|
|
|
|
m::room::id::buf ret;
|
|
|
|
room_id(ret, mxc);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::room::id
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::media::file::room_id(room::id::buf &out,
|
|
|
|
const mxc &mxc)
|
2018-04-30 16:31:11 +02:00
|
|
|
{
|
2019-08-22 08:57:07 +02:00
|
|
|
thread_local char buf[512];
|
|
|
|
const auto path
|
|
|
|
{
|
|
|
|
mxc.path(buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
const sha256::buf hash
|
|
|
|
{
|
|
|
|
sha256{path}
|
|
|
|
};
|
|
|
|
|
|
|
|
out =
|
|
|
|
{
|
|
|
|
b58encode(buf, hash), my_host()
|
|
|
|
};
|
|
|
|
|
|
|
|
return out;
|
2018-04-30 16:31:11 +02:00
|
|
|
}
|
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
//
|
|
|
|
// media::block
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::media::block::set(const m::room &room,
|
|
|
|
const m::user::id &user_id,
|
|
|
|
const const_buffer &block)
|
2018-04-30 16:31:11 +02:00
|
|
|
{
|
2018-05-11 06:08:58 +02:00
|
|
|
static constexpr const auto bufsz
|
2018-04-30 16:31:11 +02:00
|
|
|
{
|
|
|
|
b58encode_size(sha256::digest_size)
|
|
|
|
};
|
|
|
|
|
|
|
|
char b58buf[bufsz];
|
|
|
|
const auto hash
|
|
|
|
{
|
2019-08-22 08:57:07 +02:00
|
|
|
set(mutable_buffer{b58buf}, block)
|
2018-04-30 16:31:11 +02:00
|
|
|
};
|
|
|
|
|
2019-06-24 09:25:53 +02:00
|
|
|
return send(room, user_id, "ircd.file.block", json::members
|
2018-04-30 16:31:11 +02:00
|
|
|
{
|
|
|
|
{ "size", long(size(block)) },
|
|
|
|
{ "hash", hash }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
ircd::string_view
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::media::block::set(const mutable_buffer &b58buf,
|
|
|
|
const const_buffer &block)
|
2018-04-30 16:31:11 +02:00
|
|
|
{
|
|
|
|
const sha256::buf hash
|
|
|
|
{
|
|
|
|
sha256{block}
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view b58hash
|
|
|
|
{
|
|
|
|
b58encode(b58buf, hash)
|
|
|
|
};
|
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
set(b58hash, block);
|
2018-04-30 16:31:11 +02:00
|
|
|
return b58hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-08-22 08:57:07 +02:00
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::media::block::set(const string_view &b58hash,
|
|
|
|
const const_buffer &block)
|
2018-04-30 16:31:11 +02:00
|
|
|
{
|
2019-08-22 08:57:07 +02:00
|
|
|
db::write(blocks, b58hash, block);
|
2018-04-30 16:31:11 +02:00
|
|
|
}
|
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
ircd::const_buffer
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::media::block::get(const mutable_buffer &out,
|
|
|
|
const string_view &b58hash)
|
2018-04-25 02:41:51 +02:00
|
|
|
{
|
2019-08-22 08:57:07 +02:00
|
|
|
return db::read(blocks, b58hash, out);
|
2018-04-25 02:41:51 +02:00
|
|
|
}
|
|
|
|
|
2019-09-17 22:52:44 +02:00
|
|
|
bool
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::media::block::get(const string_view &b58hash,
|
|
|
|
const closure &closure)
|
|
|
|
{
|
|
|
|
const db::gopts opts;
|
|
|
|
return blocks(b58hash, std::nothrow, closure, opts);
|
|
|
|
}
|
|
|
|
|
2019-09-17 20:35:50 +02:00
|
|
|
bool
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::media::block::prefetch(const string_view &b58hash)
|
|
|
|
{
|
|
|
|
return db::prefetch(blocks, b58hash);
|
|
|
|
}
|
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
//
|
|
|
|
// media::mxc
|
|
|
|
//
|
|
|
|
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::media::mxc::mxc(const string_view &server,
|
|
|
|
const string_view &mediaid)
|
|
|
|
:server
|
|
|
|
{
|
|
|
|
split(lstrip(server, "mxc://"), '/').first
|
|
|
|
}
|
|
|
|
,mediaid
|
|
|
|
{
|
|
|
|
mediaid?: rsplit(server, '/').second
|
|
|
|
}
|
2018-04-25 02:41:51 +02:00
|
|
|
{
|
2019-08-22 08:57:07 +02:00
|
|
|
if(empty(server))
|
2018-04-25 02:41:51 +02:00
|
|
|
throw m::BAD_REQUEST
|
|
|
|
{
|
2019-08-22 08:57:07 +02:00
|
|
|
"Invalid MXC: missing server parameter."
|
2018-04-25 02:41:51 +02:00
|
|
|
};
|
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
if(empty(mediaid))
|
|
|
|
throw m::BAD_REQUEST
|
2018-08-29 07:42:11 +02:00
|
|
|
{
|
2019-08-22 08:57:07 +02:00
|
|
|
"Invalid MXC: missing mediaid parameter."
|
|
|
|
};
|
|
|
|
}
|
2018-08-29 07:42:11 +02:00
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::media::mxc::mxc(const string_view &uri)
|
|
|
|
:server
|
|
|
|
{
|
|
|
|
split(lstrip(uri, "mxc://"), '/').first
|
|
|
|
}
|
|
|
|
,mediaid
|
|
|
|
{
|
|
|
|
rsplit(uri, '/').second
|
|
|
|
}
|
|
|
|
{
|
|
|
|
if(empty(server))
|
|
|
|
throw m::BAD_REQUEST
|
|
|
|
{
|
|
|
|
"Invalid MXC: missing server parameter."
|
|
|
|
};
|
|
|
|
|
|
|
|
if(empty(mediaid))
|
|
|
|
throw m::BAD_REQUEST
|
|
|
|
{
|
|
|
|
"Invalid MXC: missing mediaid parameter."
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::media::mxc::uri(const mutable_buffer &out)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return fmt::sprintf
|
2018-04-25 02:41:51 +02:00
|
|
|
{
|
2019-08-22 08:57:07 +02:00
|
|
|
out, "mxc://%s/%s", server, mediaid
|
2018-04-25 02:41:51 +02:00
|
|
|
};
|
2019-08-22 08:57:07 +02:00
|
|
|
}
|
2018-04-25 02:41:51 +02:00
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
ircd::string_view
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::media::mxc::path(const mutable_buffer &out)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return fmt::sprintf
|
2018-04-25 02:41:51 +02:00
|
|
|
{
|
2019-08-22 08:57:07 +02:00
|
|
|
out, "%s/%s", server, mediaid
|
2018-04-25 02:41:51 +02:00
|
|
|
};
|
|
|
|
}
|