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-02 11:08:06 +01:00
|
|
|
#include <boost/gil/image.hpp>
|
|
|
|
#include <boost/gil/typedefs.hpp>
|
|
|
|
#include <boost/gil/extension/io/jpeg_io.hpp>
|
|
|
|
// #include <boost/gil/extension/numeric/sampler.hpp>
|
|
|
|
// #include <boost/gil/extension/numeric/resample.hpp>
|
|
|
|
|
2018-03-08 21:38:02 +01:00
|
|
|
#include "media.h"
|
2018-02-26 13:03:07 +01:00
|
|
|
|
|
|
|
resource
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
resource
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-08 21:38:02 +01:00
|
|
|
static resource::response
|
|
|
|
get__thumbnail_local(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const string_view &server,
|
|
|
|
const string_view &file,
|
|
|
|
const m::room &room);
|
2018-02-26 13:03:07 +01:00
|
|
|
|
|
|
|
resource::response
|
|
|
|
get__thumbnail(client &client,
|
|
|
|
const resource::request &request)
|
|
|
|
{
|
|
|
|
if(request.parv.size() < 1)
|
|
|
|
throw http::error
|
|
|
|
{
|
|
|
|
http::MULTIPLE_CHOICES, "Server name parameter required"
|
|
|
|
};
|
|
|
|
|
|
|
|
if(request.parv.size() < 2)
|
|
|
|
throw http::error
|
|
|
|
{
|
|
|
|
http::MULTIPLE_CHOICES, "Media ID parameter required"
|
|
|
|
};
|
|
|
|
|
2018-04-23 05:46:14 +02:00
|
|
|
auto &server
|
2018-02-26 13:03:07 +01:00
|
|
|
{
|
|
|
|
request.parv[0]
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &file
|
|
|
|
{
|
|
|
|
request.parv[1]
|
|
|
|
};
|
|
|
|
|
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}:
|
|
|
|
m::me.user_id
|
|
|
|
};
|
|
|
|
|
2018-03-08 21:38:02 +01:00
|
|
|
const m::room::id::buf room_id
|
2018-02-26 13:03:07 +01:00
|
|
|
{
|
2018-04-30 16:30:34 +02:00
|
|
|
download(server, file, user_id)
|
2018-04-23 05:46:14 +02:00
|
|
|
};
|
|
|
|
|
2018-04-26 06:56:43 +02:00
|
|
|
return get__thumbnail_local(client, request, server, file, room_id);
|
2018-02-26 13:03:07 +01:00
|
|
|
}
|
|
|
|
|
2018-04-12 23:44:30 +02:00
|
|
|
static resource::method
|
2018-02-26 13:03:07 +01:00
|
|
|
method_get__legacy
|
|
|
|
{
|
|
|
|
thumbnail_resource__legacy, "GET", get__thumbnail
|
|
|
|
};
|
|
|
|
|
2018-04-12 23:44:30 +02:00
|
|
|
static resource::method
|
2018-02-26 13:03:07 +01:00
|
|
|
method_get
|
|
|
|
{
|
|
|
|
thumbnail_resource, "GET", get__thumbnail
|
|
|
|
};
|
|
|
|
|
2018-03-08 21:38:02 +01:00
|
|
|
static resource::response
|
|
|
|
get__thumbnail_local(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const string_view &hostname,
|
|
|
|
const string_view &mediaid,
|
|
|
|
const m::room &room)
|
|
|
|
{
|
|
|
|
// Get the file's total size
|
|
|
|
size_t file_size{0};
|
|
|
|
room.get("ircd.file.stat", "size", [&file_size]
|
|
|
|
(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-03-08 21:38:02 +01:00
|
|
|
room.get("ircd.file.stat", "type", [&type_buf, &content_type]
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
const auto &value
|
|
|
|
{
|
|
|
|
unquote(at<"content"_>(event).at("value"))
|
|
|
|
};
|
|
|
|
|
|
|
|
content_type =
|
|
|
|
{
|
|
|
|
type_buf, copy(type_buf, value)
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
// Send HTTP head to client
|
2018-04-23 05:44:15 +02:00
|
|
|
const resource::response response
|
2018-03-08 21:38:02 +01:00
|
|
|
{
|
2018-03-09 22:15:32 +01:00
|
|
|
client, http::OK, content_type, file_size
|
2018-03-08 21:38:02 +01:00
|
|
|
};
|
|
|
|
|
2018-04-26 03:44:02 +02:00
|
|
|
size_t sent_size{0};
|
|
|
|
const auto sink{[&client, &sent_size]
|
2018-04-25 03:26:35 +02:00
|
|
|
(const const_buffer &block)
|
2018-03-08 21:38:02 +01:00
|
|
|
{
|
2018-04-26 03:44:02 +02:00
|
|
|
sent_size += client.write_all(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
|
|
|
{
|
|
|
|
read_each_block(room, sink)
|
|
|
|
};
|
2018-03-08 21:38:02 +01:00
|
|
|
|
2018-04-26 03:44:02 +02:00
|
|
|
if(unlikely(read_size != file_size)) log::error
|
2018-04-25 03:26:35 +02:00
|
|
|
{
|
|
|
|
media_log, "File %s/%s [%s] size mismatch: expected %zu got %zu",
|
|
|
|
hostname,
|
|
|
|
mediaid,
|
|
|
|
string_view{room.room_id},
|
|
|
|
file_size,
|
2018-04-26 03:44:02 +02:00
|
|
|
read_size
|
2018-04-25 03:26:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Have to kill client here after failing content length expectation.
|
2018-04-26 03:44:02 +02:00
|
|
|
if(unlikely(read_size != file_size))
|
2018-04-25 03:26:35 +02:00
|
|
|
client.close(net::dc::RST, net::close_ignore);
|
|
|
|
|
2018-04-26 03:44:02 +02:00
|
|
|
assert(read_size == sent_size);
|
2018-04-23 05:44:15 +02:00
|
|
|
return response;
|
2018-02-26 13:03:07 +01:00
|
|
|
}
|