2018-02-26 13:03:07 +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.
|
|
|
|
|
2018-03-08 21:38:02 +01:00
|
|
|
#include "media.h"
|
2018-02-26 13:03:07 +01:00
|
|
|
|
2019-05-30 04:20:17 +02:00
|
|
|
using namespace ircd::m::media::thumbnail; //TODO: XXX
|
2019-08-22 08:57:07 +02:00
|
|
|
using namespace ircd;
|
2019-05-30 04:20:17 +02:00
|
|
|
|
|
|
|
decltype(ircd::m::media::thumbnail::enable)
|
|
|
|
ircd::m::media::thumbnail::enable
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.media.thumbnail.enable" },
|
|
|
|
{ "default", true },
|
|
|
|
};
|
|
|
|
|
2019-07-26 06:38:35 +02:00
|
|
|
decltype(ircd::m::media::thumbnail::enable_remote)
|
|
|
|
ircd::m::media::thumbnail::enable_remote
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.media.thumbnail.enable_remote" },
|
|
|
|
{ "default", true },
|
|
|
|
};
|
|
|
|
|
2019-05-30 04:20:17 +02:00
|
|
|
decltype(ircd::m::media::thumbnail::width_min)
|
|
|
|
ircd::m::media::thumbnail::width_min
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.media.thumbnail.width.min" },
|
2019-05-30 09:17:45 +02:00
|
|
|
{ "default", 16L },
|
2019-05-30 04:20:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::m::media::thumbnail::width_max)
|
|
|
|
ircd::m::media::thumbnail::width_max
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.media.thumbnail.width.max" },
|
|
|
|
{ "default", 1536L },
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::m::media::thumbnail::height_min)
|
|
|
|
ircd::m::media::thumbnail::height_min
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.media.thumbnail.height.min" },
|
2019-05-30 09:17:45 +02:00
|
|
|
{ "default", 16L },
|
2019-05-30 04:20:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::m::media::thumbnail::height_max)
|
|
|
|
ircd::m::media::thumbnail::height_max
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.media.thumbnail.height.max" },
|
|
|
|
{ "default", 1536L },
|
|
|
|
};
|
|
|
|
|
2019-07-06 03:20:39 +02:00
|
|
|
decltype(ircd::m::media::thumbnail::mime_whitelist)
|
|
|
|
ircd::m::media::thumbnail::mime_whitelist
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.media.thumbnail.mime.whitelist" },
|
|
|
|
{ "default", "image/jpeg image/png image/webp" },
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::m::media::thumbnail::mime_blacklist)
|
|
|
|
ircd::m::media::thumbnail::mime_blacklist
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.media.thumbnail.mime.blacklist" },
|
|
|
|
{ "default", "" },
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource
|
2018-02-26 13:03:07 +01:00
|
|
|
thumbnail_resource__legacy
|
|
|
|
{
|
|
|
|
"/_matrix/media/v1/thumbnail/",
|
|
|
|
{
|
2018-04-12 23:44:30 +02:00
|
|
|
"(11.7.1.4) thumbnails (legacy version)",
|
2018-02-26 13:03:07 +01:00
|
|
|
resource::DIRECTORY,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource
|
2018-02-26 13:03:07 +01:00
|
|
|
thumbnail_resource
|
|
|
|
{
|
|
|
|
"/_matrix/media/r0/thumbnail/",
|
|
|
|
{
|
2018-04-12 23:44:30 +02:00
|
|
|
"(11.7.1.4) thumbnails",
|
2018-02-26 13:03:07 +01:00
|
|
|
resource::DIRECTORY,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
static m::resource::response
|
2018-03-08 21:38:02 +01:00
|
|
|
get__thumbnail_local(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request,
|
2019-08-22 08:57:07 +02:00
|
|
|
const m::media::mxc &,
|
2019-05-30 09:52:36 +02:00
|
|
|
const m::room &room);
|
2018-02-26 13:03:07 +01:00
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response
|
2018-02-26 13:03:07 +01:00
|
|
|
get__thumbnail(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request)
|
2018-02-26 13:03:07 +01:00
|
|
|
{
|
|
|
|
if(request.parv.size() < 1)
|
2019-06-15 10:01:47 +02:00
|
|
|
throw m::NEED_MORE_PARAMS
|
2018-02-26 13:03:07 +01:00
|
|
|
{
|
2019-06-15 10:01:47 +02:00
|
|
|
"Server name parameter required"
|
2018-02-26 13:03:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if(request.parv.size() < 2)
|
2019-06-15 10:01:47 +02:00
|
|
|
throw m::NEED_MORE_PARAMS
|
2018-02-26 13:03:07 +01:00
|
|
|
{
|
2019-06-15 10:01:47 +02:00
|
|
|
"Media ID parameter required"
|
2018-02-26 13:03:07 +01:00
|
|
|
};
|
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
const m::media::mxc mxc
|
2018-02-26 13:03:07 +01:00
|
|
|
{
|
2019-08-22 08:57:07 +02:00
|
|
|
request.parv[0], request.parv[1]
|
2018-02-26 13:03:07 +01:00
|
|
|
};
|
|
|
|
|
2018-04-30 16:30:34 +02:00
|
|
|
// Thumbnail 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-07-26 06:38:35 +02:00
|
|
|
if(!m::media::thumbnail::enable_remote)
|
|
|
|
{
|
|
|
|
const m::room::id::buf room_id
|
|
|
|
{
|
2019-08-22 08:57:07 +02:00
|
|
|
m::media::file::room_id(mxc)
|
2019-07-26 06:38:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if(!exists(room_id))
|
2019-09-29 01:12:07 +02:00
|
|
|
return m::resource::response
|
2019-07-26 06:38:35 +02:00
|
|
|
{
|
|
|
|
client, http::NOT_FOUND
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-08 21:38:02 +01:00
|
|
|
const m::room::id::buf room_id
|
2018-02-26 13:03:07 +01:00
|
|
|
{
|
2019-08-22 08:57:07 +02:00
|
|
|
m::media::file::download(mxc, user_id)
|
2018-04-23 05:46:14 +02:00
|
|
|
};
|
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
return get__thumbnail_local(client, request, mxc, room_id);
|
2018-02-26 13:03:07 +01:00
|
|
|
}
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
static m::resource::method
|
2018-02-26 13:03:07 +01:00
|
|
|
method_get__legacy
|
|
|
|
{
|
|
|
|
thumbnail_resource__legacy, "GET", get__thumbnail
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
static m::resource::method
|
2018-02-26 13:03:07 +01:00
|
|
|
method_get
|
|
|
|
{
|
|
|
|
thumbnail_resource, "GET", get__thumbnail
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
static m::resource::response
|
2018-03-08 21:38:02 +01:00
|
|
|
get__thumbnail_local(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request,
|
2019-08-22 08:57:07 +02:00
|
|
|
const m::media::mxc &mxc,
|
2019-05-30 09:52:36 +02:00
|
|
|
const m::room &room)
|
2018-03-08 21:38:02 +01:00
|
|
|
{
|
2019-05-30 09:52:36 +02:00
|
|
|
const auto &method
|
|
|
|
{
|
|
|
|
request.query.get("method", "scale"_sv)
|
|
|
|
};
|
|
|
|
|
|
|
|
std::pair<size_t, size_t> dimension
|
|
|
|
{
|
|
|
|
request.query.get<size_t>("width", 0),
|
|
|
|
request.query.get<size_t>("height", 0)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(dimension.first)
|
|
|
|
{
|
|
|
|
dimension.first = std::max(dimension.first, size_t(width_min));
|
|
|
|
dimension.first = std::min(dimension.first, size_t(width_max));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(dimension.second)
|
|
|
|
{
|
|
|
|
dimension.second = std::max(dimension.second, size_t(height_min));
|
|
|
|
dimension.second = std::min(dimension.second, size_t(height_max));
|
|
|
|
}
|
|
|
|
|
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-03-08 21:38:02 +01:00
|
|
|
// Get the file's total size
|
|
|
|
size_t file_size{0};
|
2018-12-30 02:33:51 +01:00
|
|
|
state.get("ircd.file.stat", "size", [&file_size]
|
2018-03-08 21:38:02 +01:00
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
file_size = at<"content"_>(event).get<size_t>("value");
|
|
|
|
});
|
|
|
|
|
|
|
|
// Get the MIME type
|
|
|
|
char type_buf[64];
|
2018-04-13 00:58:30 +02:00
|
|
|
string_view content_type
|
|
|
|
{
|
|
|
|
"application/octet-stream"
|
|
|
|
};
|
|
|
|
|
2018-12-30 02:33:51 +01:00
|
|
|
state.get("ircd.file.stat", "type", [&type_buf, &content_type]
|
2018-03-08 21:38:02 +01:00
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
const auto &value
|
|
|
|
{
|
|
|
|
unquote(at<"content"_>(event).at("value"))
|
|
|
|
};
|
|
|
|
|
|
|
|
content_type =
|
|
|
|
{
|
|
|
|
type_buf, copy(type_buf, value)
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2019-05-30 02:44:10 +02:00
|
|
|
const unique_buffer<mutable_buffer> buf
|
2018-03-08 21:38:02 +01:00
|
|
|
{
|
2019-05-30 02:44:10 +02:00
|
|
|
file_size
|
2018-03-08 21:38:02 +01:00
|
|
|
};
|
|
|
|
|
2019-05-30 02:44:10 +02:00
|
|
|
size_t copied(0);
|
|
|
|
const auto sink{[&buf, &copied]
|
2018-04-25 03:26:35 +02:00
|
|
|
(const const_buffer &block)
|
2018-03-08 21:38:02 +01:00
|
|
|
{
|
2019-05-30 02:44:10 +02:00
|
|
|
copied += copy(buf + copied, block);
|
2018-04-23 05:44:15 +02:00
|
|
|
}};
|
|
|
|
|
2018-04-26 03:44:02 +02:00
|
|
|
const size_t read_size
|
2018-04-23 05:44:15 +02:00
|
|
|
{
|
2019-08-22 08:57:07 +02:00
|
|
|
m::media::file::read(room, sink)
|
2018-04-23 05:44:15 +02:00
|
|
|
};
|
2018-03-08 21:38:02 +01:00
|
|
|
|
2019-05-30 02:44:10 +02:00
|
|
|
if(unlikely(read_size != file_size || file_size != copied))
|
|
|
|
throw ircd::error
|
|
|
|
{
|
|
|
|
"File %s/%s [%s] size mismatch: expected %zu got %zu copied %zu",
|
2019-08-22 08:57:07 +02:00
|
|
|
mxc.server,
|
|
|
|
mxc.mediaid,
|
2019-05-30 02:44:10 +02:00
|
|
|
string_view{room.room_id},
|
|
|
|
file_size,
|
|
|
|
read_size,
|
|
|
|
copied
|
|
|
|
};
|
|
|
|
|
2019-07-26 06:09:43 +02:00
|
|
|
const bool available
|
2019-06-04 11:22:09 +02:00
|
|
|
{
|
2019-08-22 08:57:07 +02:00
|
|
|
m::media::magick_support
|
2019-06-04 11:22:09 +02:00
|
|
|
};
|
|
|
|
|
2019-07-06 03:20:39 +02:00
|
|
|
const auto mime_type
|
|
|
|
{
|
|
|
|
split(content_type, ';').first
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool permitted
|
|
|
|
{
|
|
|
|
// If there's a blacklist, mime type must not in the blacklist.
|
|
|
|
(!mime_blacklist || !has(mime_blacklist, mime_type))
|
|
|
|
|
|
|
|
// If there's a whitelist, mime type must be in the whitelist.
|
|
|
|
&& (!mime_whitelist || has(mime_whitelist, mime_type))
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool valid_args
|
|
|
|
{
|
|
|
|
// Both dimension parameters given in query string
|
|
|
|
(dimension.first && dimension.second)
|
|
|
|
|
|
|
|
// Known thumbnailing method in query string
|
|
|
|
&& (method == "scale" || method == "crop")
|
|
|
|
};
|
|
|
|
|
2019-05-30 09:52:36 +02:00
|
|
|
const bool fallback // Reasons to just send the original image
|
|
|
|
{
|
2019-06-04 11:22:09 +02:00
|
|
|
// Disabled by configuration
|
2019-07-06 03:20:39 +02:00
|
|
|
!enable
|
2019-06-04 11:22:09 +02:00
|
|
|
|
2019-07-06 03:20:39 +02:00
|
|
|
// Access denied for this operation
|
|
|
|
|| !permitted
|
2019-06-04 11:22:09 +02:00
|
|
|
|
|
|
|
// The thumbnailer is not loaded or available on this system.
|
2019-07-06 03:20:39 +02:00
|
|
|
|| !available
|
|
|
|
|
|
|
|
// Arguments invalid.
|
|
|
|
|| !valid_args
|
2019-05-30 09:52:36 +02:00
|
|
|
};
|
|
|
|
|
2019-07-06 03:20:39 +02:00
|
|
|
if(fallback && available && enable)
|
|
|
|
log::dwarning
|
|
|
|
{
|
|
|
|
"Not thumbnailing %s/%s [%s] '%s' bytes:%zu :%s",
|
2019-08-22 08:57:07 +02:00
|
|
|
mxc.server,
|
|
|
|
mxc.mediaid,
|
2019-07-06 03:20:39 +02:00
|
|
|
string_view{room.room_id},
|
|
|
|
content_type,
|
|
|
|
file_size,
|
|
|
|
!permitted?
|
|
|
|
"Not permitted":
|
|
|
|
!valid_args?
|
|
|
|
"Invalid arguments":
|
|
|
|
"Unknown reason",
|
|
|
|
};
|
|
|
|
|
2019-05-30 09:52:36 +02:00
|
|
|
if(fallback)
|
2019-09-29 01:12:07 +02:00
|
|
|
return m::resource::response
|
2019-05-30 02:44:10 +02:00
|
|
|
{
|
|
|
|
client, buf, content_type
|
|
|
|
};
|
|
|
|
|
2019-05-30 09:52:36 +02:00
|
|
|
const auto closure{[&client, &content_type]
|
|
|
|
(const const_buffer &buf)
|
2019-05-30 02:44:10 +02:00
|
|
|
{
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response
|
2019-05-30 02:44:10 +02:00
|
|
|
{
|
2019-05-30 09:52:36 +02:00
|
|
|
client, buf, content_type
|
|
|
|
};
|
|
|
|
}};
|
|
|
|
|
|
|
|
if(method == "crop")
|
|
|
|
magick::thumbcrop
|
|
|
|
{
|
|
|
|
buf, dimension, closure
|
|
|
|
};
|
|
|
|
else
|
|
|
|
magick::thumbnail
|
|
|
|
{
|
|
|
|
buf, dimension, closure
|
|
|
|
};
|
2018-04-25 03:26:35 +02:00
|
|
|
|
2019-05-30 04:20:17 +02:00
|
|
|
return {}; // responded from closure.
|
2018-02-26 13:03:07 +01:00
|
|
|
}
|