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-02-18 02:17:19 +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>
|
|
|
|
|
2017-12-25 01:15:42 +01:00
|
|
|
using namespace ircd;
|
|
|
|
|
2018-02-18 02:17:19 +01:00
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
2017-12-25 01:15:42 +01:00
|
|
|
{
|
2018-02-18 02:17:19 +01:00
|
|
|
"11.7 :Content repository (upload)"
|
2017-12-25 01:15:42 +01:00
|
|
|
};
|
|
|
|
|
2018-02-18 02:17:19 +01:00
|
|
|
resource
|
|
|
|
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)",
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
resource::response
|
2018-02-18 02:17:19 +01:00
|
|
|
post__upload(client &client,
|
|
|
|
const 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
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto filename
|
2017-12-25 01:15:42 +01:00
|
|
|
{
|
|
|
|
request.query["filename"]
|
|
|
|
};
|
|
|
|
|
2018-02-18 02:17:19 +01:00
|
|
|
char pathbuf[32];
|
|
|
|
const auto path
|
2017-12-25 01:15:42 +01:00
|
|
|
{
|
2018-02-18 02:17:19 +01:00
|
|
|
rand::string(rand::dict::alpha, pathbuf)
|
2017-12-25 01:15:42 +01:00
|
|
|
};
|
|
|
|
|
2018-02-18 02:17:19 +01:00
|
|
|
sha256 hash;
|
|
|
|
size_t offset{0};
|
|
|
|
while(offset < size(request.content))
|
2017-12-25 01:15:42 +01:00
|
|
|
{
|
2018-02-18 02:17:19 +01:00
|
|
|
const string_view pending
|
|
|
|
{
|
|
|
|
data(request.content) + offset, size(request.content) - offset
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto appended
|
|
|
|
{
|
|
|
|
fs::append(path, pending, offset)
|
|
|
|
};
|
|
|
|
|
|
|
|
hash.update(appended);
|
|
|
|
offset += size(appended);
|
2017-12-25 01:15:42 +01:00
|
|
|
}
|
2018-02-18 02:17:19 +01:00
|
|
|
assert(offset == client.content_consumed);
|
2017-12-25 01:15:42 +01:00
|
|
|
|
2018-02-18 02:17:19 +01:00
|
|
|
char buffer[4_KiB];
|
|
|
|
while(client.content_consumed < request.head.content_length)
|
2017-12-25 01:15:42 +01:00
|
|
|
{
|
2018-02-18 02:17:19 +01:00
|
|
|
const size_t remain
|
|
|
|
{
|
|
|
|
request.head.content_length - client.content_consumed
|
|
|
|
};
|
|
|
|
|
|
|
|
const mutable_buffer buf
|
|
|
|
{
|
|
|
|
buffer, std::min(remain, sizeof(buf))
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view read
|
|
|
|
{
|
|
|
|
data(buf), read_few(*client.sock, buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
client.content_consumed += size(read); do
|
|
|
|
{
|
|
|
|
const auto appended
|
|
|
|
{
|
|
|
|
fs::append(path, read, offset)
|
|
|
|
};
|
|
|
|
|
|
|
|
hash.update(appended);
|
|
|
|
offset += size(appended);
|
|
|
|
}
|
|
|
|
while(offset < client.content_consumed);
|
|
|
|
assert(offset == client.content_consumed);
|
|
|
|
}
|
|
|
|
assert(offset == request.head.content_length);
|
|
|
|
|
|
|
|
char hashbuf[32];
|
|
|
|
hash.digest(hashbuf);
|
|
|
|
|
|
|
|
char uribuf[256], b58buf[64];
|
|
|
|
const string_view content_uri
|
|
|
|
{
|
|
|
|
fmt::sprintf
|
|
|
|
{
|
|
|
|
uribuf, "mxc://%s/%s", my_host(), b58encode(b58buf, hashbuf)
|
|
|
|
}
|
2017-12-25 01:15:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return resource::response
|
|
|
|
{
|
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
|
|
|
|
|
|
|
struct resource::method::opts
|
|
|
|
const method_post_opts
|
2017-12-25 01:15:42 +01:00
|
|
|
{
|
2018-02-18 02:17:19 +01:00
|
|
|
resource::method::REQUIRES_AUTH |
|
|
|
|
resource::method::CONTENT_DISCRETION,
|
|
|
|
|
|
|
|
8_MiB //TODO: conf; (this is the payload max option)
|
|
|
|
};
|
|
|
|
|
|
|
|
resource::method
|
|
|
|
method_post
|
|
|
|
{
|
|
|
|
upload_resource, "POST", post__upload, method_post_opts
|
|
|
|
};
|
|
|
|
|
|
|
|
resource::method
|
|
|
|
method_post__legacy
|
|
|
|
{
|
|
|
|
upload_resource__legacy, "POST", post__upload, method_post_opts
|
2017-12-25 01:15:42 +01:00
|
|
|
};
|