2018-02-04 03:22:01 +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.
|
2017-12-25 01:15:42 +01:00
|
|
|
|
2018-03-08 21:38:02 +01:00
|
|
|
#include "media.h"
|
2017-12-25 01:15:42 +01:00
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
using namespace ircd;
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource
|
2018-02-18 02:17:19 +01:00
|
|
|
upload_resource__legacy
|
2017-12-25 01:15:42 +01:00
|
|
|
{
|
2018-02-18 02:17:19 +01:00
|
|
|
"/_matrix/media/v1/upload/",
|
|
|
|
{
|
|
|
|
"(11.7.1.1) upload (legacy compat)",
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource
|
2017-12-25 01:15:42 +01:00
|
|
|
upload_resource
|
|
|
|
{
|
2018-02-18 02:17:19 +01:00
|
|
|
"/_matrix/media/r0/upload/",
|
2017-12-25 01:15:42 +01:00
|
|
|
{
|
2018-02-18 02:17:19 +01:00
|
|
|
"(11.7.1.1) upload",
|
2017-12-25 01:15:42 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response
|
2018-02-18 02:17:19 +01:00
|
|
|
post__upload(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request)
|
2017-12-25 01:15:42 +01:00
|
|
|
{
|
2018-02-18 02:17:19 +01:00
|
|
|
const auto &content_type
|
|
|
|
{
|
|
|
|
request.head.content_type
|
|
|
|
};
|
|
|
|
|
2018-04-21 12:24:13 +02:00
|
|
|
const auto &server
|
2017-12-25 01:15:42 +01:00
|
|
|
{
|
2018-04-21 12:24:13 +02:00
|
|
|
my_host()
|
2017-12-25 01:15:42 +01:00
|
|
|
};
|
|
|
|
|
2018-04-21 12:24:13 +02:00
|
|
|
const auto &filename
|
2017-12-25 01:15:42 +01:00
|
|
|
{
|
2018-04-21 12:24:13 +02:00
|
|
|
request.query["filename"]
|
2017-12-25 01:15:42 +01:00
|
|
|
};
|
|
|
|
|
2018-04-21 12:24:13 +02:00
|
|
|
char randbuf[32];
|
|
|
|
const auto randstr
|
2017-12-25 01:15:42 +01:00
|
|
|
{
|
2018-04-21 12:24:13 +02:00
|
|
|
rand::string(rand::dict::alpha, randbuf)
|
|
|
|
};
|
2017-12-25 01:15:42 +01:00
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
const m::media::mxc mxc
|
|
|
|
{
|
|
|
|
server, randstr
|
|
|
|
};
|
|
|
|
|
2018-04-21 12:24:13 +02:00
|
|
|
const m::room::id::buf room_id
|
2017-12-25 01:15:42 +01:00
|
|
|
{
|
2019-08-22 08:57:07 +02:00
|
|
|
m::media::file::room_id(mxc)
|
2018-04-21 12:24:13 +02:00
|
|
|
};
|
2018-02-18 02:17:19 +01:00
|
|
|
|
2018-05-07 01:04:51 +02:00
|
|
|
m::vm::copts vmopts;
|
2018-04-21 12:24:13 +02:00
|
|
|
vmopts.history = false;
|
|
|
|
const m::room room
|
|
|
|
{
|
|
|
|
room_id, &vmopts
|
|
|
|
};
|
2018-02-18 02:17:19 +01:00
|
|
|
|
2018-04-21 12:24:13 +02:00
|
|
|
create(room, request.user_id, "file");
|
2018-02-18 02:17:19 +01:00
|
|
|
|
2018-04-21 12:24:13 +02:00
|
|
|
const unique_buffer<mutable_buffer> buf
|
|
|
|
{
|
|
|
|
request.head.content_length
|
|
|
|
};
|
2018-02-18 02:17:19 +01:00
|
|
|
|
2018-04-21 12:24:13 +02:00
|
|
|
copy(buf, request.content);
|
2018-09-05 05:26:55 +02:00
|
|
|
client.content_consumed += read_all(*client.sock, buf + client.content_consumed);
|
2018-04-21 12:24:13 +02:00
|
|
|
assert(client.content_consumed == request.head.content_length);
|
2018-02-18 02:17:19 +01:00
|
|
|
|
2018-04-21 12:24:13 +02:00
|
|
|
const size_t written
|
2018-02-27 05:51:42 +01:00
|
|
|
{
|
2019-08-22 08:57:07 +02:00
|
|
|
m::media::file::write(room, request.user_id, buf, content_type)
|
2018-02-27 05:51:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
char uribuf[256];
|
2018-02-18 02:17:19 +01:00
|
|
|
const string_view content_uri
|
|
|
|
{
|
2019-08-22 08:57:07 +02:00
|
|
|
mxc.uri(uribuf)
|
2017-12-25 01:15:42 +01:00
|
|
|
};
|
|
|
|
|
2018-04-21 12:24:13 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2019-08-22 08:57:07 +02:00
|
|
|
m::media::log, "%s uploaded %zu bytes uri: `%s' file_room: %s :%s",
|
2018-04-21 12:24:13 +02:00
|
|
|
request.user_id,
|
|
|
|
request.head.content_length,
|
|
|
|
content_uri,
|
|
|
|
string_view{room.room_id},
|
2019-08-22 08:57:07 +02:00
|
|
|
filename,
|
2018-04-21 12:24:13 +02:00
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
return m::resource::response
|
2017-12-25 01:15:42 +01:00
|
|
|
{
|
2018-02-18 02:17:19 +01:00
|
|
|
client, http::CREATED, json::members
|
|
|
|
{
|
|
|
|
{ "content_uri", content_uri }
|
|
|
|
}
|
2017-12-25 01:15:42 +01:00
|
|
|
};
|
|
|
|
}
|
2018-02-18 02:17:19 +01:00
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
static const struct m::resource::method::opts
|
2018-04-12 23:44:30 +02:00
|
|
|
method_post_opts
|
2017-12-25 01:15:42 +01:00
|
|
|
{
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::method::REQUIRES_AUTH |
|
|
|
|
m::resource::method::CONTENT_DISCRETION,
|
2018-02-18 02:17:19 +01:00
|
|
|
|
2018-04-16 01:42:13 +02:00
|
|
|
-1s, // TODO: no coarse timer
|
|
|
|
|
2018-02-18 02:17:19 +01:00
|
|
|
8_MiB //TODO: conf; (this is the payload max option)
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
static m::resource::method
|
2018-02-18 02:17:19 +01:00
|
|
|
method_post
|
|
|
|
{
|
|
|
|
upload_resource, "POST", post__upload, method_post_opts
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
static m::resource::method
|
2018-02-18 02:17:19 +01:00
|
|
|
method_post__legacy
|
|
|
|
{
|
|
|
|
upload_resource__legacy, "POST", post__upload, method_post_opts
|
2017-12-25 01:15:42 +01:00
|
|
|
};
|