This repository has been archived on 2020-05-10. You can view files and clone it, but cannot push or open issues or pull requests.
mastodon-cpp/src/easy/entities/attachment.cpp

202 lines
4.3 KiB
C++
Raw Normal View History

2018-03-21 20:29:33 +01:00
/* This file is part of mastodon-cpp.
2019-03-20 06:15:43 +01:00
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
2019-03-29 14:44:39 +01:00
*
2018-03-21 20:29:33 +01:00
* This program is free software: you can redistribute it and/or modify
2019-08-15 22:53:38 +02:00
* it under the terms of the GNU Affero General Public License as published by
2018-03-21 20:29:33 +01:00
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2019-08-15 22:53:38 +02:00
* GNU Affero General Public License for more details.
2018-03-21 20:29:33 +01:00
*
2019-08-15 22:53:38 +02:00
* You should have received a copy of the GNU Affero General Public License
2018-03-21 20:29:33 +01:00
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sstream>
2018-03-22 00:33:36 +01:00
#include "attachment.hpp"
2019-02-22 11:35:06 +01:00
#include "debug.hpp"
2018-03-21 20:29:33 +01:00
using namespace Mastodon;
using Attachment = Easy::Attachment;
2018-12-04 11:26:28 +01:00
bool Attachment::valid() const
{
return Entity::check_valid(
{
"id",
"type",
"url",
"preview_url"
});
}
2018-03-21 20:29:33 +01:00
const string Attachment::description() const
{
2018-03-25 22:46:38 +02:00
return get_string("description");
2018-03-21 20:29:33 +01:00
}
2018-06-14 01:53:28 +02:00
Attachment Attachment::description(const string &description)
{
set("description", Json::Value(description));
return *this;
}
2018-06-14 04:11:28 +02:00
const string Attachment::file() const
2018-06-14 01:53:28 +02:00
{
return get_string("file");
}
Attachment Attachment::file(const string &file)
{
set("file", Json::Value(file));
return *this;
}
const std::array<double, 2> Attachment::focus() const
2018-03-21 23:28:48 +01:00
{
2018-03-25 22:46:38 +02:00
const Json::Value x = get("meta.focus.x");
const Json::Value y = get("meta.focus.y");
if (x.isDouble() && y.isDouble())
2018-03-21 23:28:48 +01:00
{
return
2018-03-22 00:33:36 +01:00
{{
x.asDouble(),
y.asDouble()
2018-03-22 00:33:36 +01:00
}};
2018-03-21 23:28:48 +01:00
}
return {};
}
2018-06-14 01:53:28 +02:00
Attachment Attachment::focus(const std::array<double, 2> &focus)
{
set("meta.focus.x", Json::Value(focus[0]));
set("meta.focus.y", Json::Value(focus[1]));
return *this;
}
const string Attachment::id() const
2018-03-21 23:28:48 +01:00
{
return get_string("id");
2018-03-21 23:28:48 +01:00
}
const Attachment::Meta Attachment::meta() const
2018-03-21 20:29:33 +01:00
{
return Meta(get("meta"));
2018-03-21 20:29:33 +01:00
}
const string Attachment::preview_url() const
{
2018-03-25 22:46:38 +02:00
return get_string("preview_url");
2018-03-21 20:29:33 +01:00
}
const string Attachment::remote_url() const
{
2018-03-25 22:46:38 +02:00
return get_string("remote_url");
2018-03-21 20:29:33 +01:00
}
const string Attachment::text_url() const
{
2018-03-25 22:46:38 +02:00
return get_string("text_url");
2018-03-21 20:29:33 +01:00
}
2018-12-04 11:26:28 +01:00
Easy::attachment_type Attachment::type() const
2018-03-21 20:29:33 +01:00
{
2018-03-25 22:46:38 +02:00
const string strtype = get_string("type");
if (strtype.compare("image") == 0)
2018-04-01 05:14:02 +02:00
return attachment_type::Image;
2018-03-25 22:46:38 +02:00
else if (strtype.compare("video") == 0)
2018-04-01 05:14:02 +02:00
return attachment_type::Video;
2018-03-25 22:46:38 +02:00
else if (strtype.compare("gifv") == 0)
2018-04-01 05:14:02 +02:00
return attachment_type::Gifv;
2018-03-25 22:46:38 +02:00
else if (strtype.compare("unknown") == 0)
2018-04-01 05:14:02 +02:00
return attachment_type::Unknown;
2018-03-25 22:46:38 +02:00
2018-03-31 00:12:34 +02:00
ttdebug << "Could not get data: type\n";
2018-04-01 05:14:02 +02:00
return attachment_type::Undefined;
2018-03-21 20:29:33 +01:00
}
const string Attachment::url() const
{
2018-03-25 22:46:38 +02:00
return get_string("url");
2018-03-21 20:29:33 +01:00
}
2018-03-21 23:28:48 +01:00
bool Attachment::Meta::valid() const
{
return true;
}
double Attachment::Meta::aspect() const
{
return get_double("original.aspect");
}
double Attachment::Meta::aspect_small() const
{
return get_double("small.aspect");
}
uint64_t Attachment::Meta::bitrate() const
{
return get_uint64("original.bitrate");
}
const std::chrono::duration<double> Attachment::Meta::duration() const
{
const double sec = get_double("original.duration");
return std::chrono::duration<double>(sec);
}
double Attachment::Meta::frame_rate() const
{
string strframes = get_string("original.frame_rate");
if (!strframes.empty())
{
std::size_t pos = strframes.find('/');
if (pos != std::string::npos)
{
std::uint16_t frames = std::stoul(strframes.substr(0, pos));
std::uint16_t divider = std::stoul(strframes.substr(pos + 1));
return frames / divider;
}
}
return 0.0;
}
uint64_t Attachment::Meta::height() const
{
return get_uint64("original.height");
}
uint64_t Attachment::Meta::height_small() const
{
return get_uint64("small.height");
}
const string Attachment::Meta::size() const
{
return get_string("original.size");
}
const string Attachment::Meta::size_small() const
{
return get_string("small.size");
}
uint64_t Attachment::Meta::width() const
2018-03-21 23:28:48 +01:00
{
return get_uint64("original.width");
2018-03-21 23:28:48 +01:00
}
uint64_t Attachment::Meta::width_small() const
2018-03-21 23:28:48 +01:00
{
return get_uint64("small.width");
2018-03-21 23:28:48 +01:00
}