2018-11-25 06:00:45 +01:00
|
|
|
/* This file is part of libravatarserv.
|
|
|
|
* Copyright © 2018 tastytea <tastytea@tastytea.de>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* 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
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
2018-11-27 09:59:02 +01:00
|
|
|
#include <sstream>
|
|
|
|
#include <array>
|
2018-11-29 13:40:50 +01:00
|
|
|
#include <algorithm>
|
2018-11-25 06:00:45 +01:00
|
|
|
#include "libravatarserv.hpp"
|
|
|
|
|
2018-11-27 04:35:14 +01:00
|
|
|
using std::cout;
|
2018-11-25 06:00:45 +01:00
|
|
|
using std::cerr;
|
|
|
|
using std::endl;
|
|
|
|
using namespace image;
|
|
|
|
|
|
|
|
const Image image::get(const string &digest, const uint16_t size)
|
|
|
|
{
|
|
|
|
uint8_t error = 0;
|
|
|
|
Magick::Image img;
|
|
|
|
fs::path filename;
|
|
|
|
|
|
|
|
const auto &entry = hash::table.find(digest);
|
|
|
|
if (entry == hash::table.end())
|
|
|
|
{
|
2018-11-30 07:00:37 +01:00
|
|
|
filename = settings::avatar_dir / "default";
|
2018-11-25 06:00:45 +01:00
|
|
|
if (!fs::is_regular_file(filename))
|
|
|
|
{
|
|
|
|
cerr << "Error: User not found and no default image set.\n";
|
|
|
|
return { 2, img };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-11-30 07:00:37 +01:00
|
|
|
filename = settings::avatar_dir / entry->second;
|
2018-11-25 06:00:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
img.read(filename);
|
|
|
|
img.resize(Magick::Geometry(size, size));
|
|
|
|
}
|
|
|
|
catch (const Magick::Exception &e)
|
|
|
|
{
|
|
|
|
cerr << "Error: " << e.what() << endl;
|
|
|
|
error = 2;
|
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
|
|
|
cerr << "Error: " << e.what() << endl;
|
|
|
|
error = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
return { error, img };
|
|
|
|
}
|
2018-11-27 04:35:14 +01:00
|
|
|
|
|
|
|
void image::write(Image &image)
|
|
|
|
{
|
2018-11-29 13:40:50 +01:00
|
|
|
string magick = image.image.magick();
|
|
|
|
std::transform(magick.begin(), magick.end(), magick.begin(), ::tolower);
|
|
|
|
|
2018-11-29 13:45:46 +01:00
|
|
|
cout << "Content-Type: image/" << magick << endl << endl;
|
2018-11-27 04:35:14 +01:00
|
|
|
cout.flush(); // We need to flush before we use /dev/stdout directly.
|
|
|
|
image.image.write("/dev/stdout");
|
|
|
|
}
|
2018-11-27 09:59:02 +01:00
|
|
|
|
|
|
|
Image image::identicon(const string &digest)
|
|
|
|
{
|
|
|
|
const string sha256 = hash::sha256(digest);
|
|
|
|
Magick::Image img(Magick::Geometry(4, 4), Magick::Color("white"));
|
2018-11-27 10:14:44 +01:00
|
|
|
// The 16 named colors specified in HTML 4.01 minus white, silver and gray.
|
2018-11-27 09:59:02 +01:00
|
|
|
const std::array<Magick::Color, 13> colors =
|
|
|
|
{
|
|
|
|
Magick::Color("black"),
|
|
|
|
Magick::Color("red"),
|
|
|
|
Magick::Color("maroon"),
|
|
|
|
Magick::Color("yellow"),
|
|
|
|
Magick::Color("olive"),
|
|
|
|
Magick::Color("lime"),
|
|
|
|
Magick::Color("green"),
|
|
|
|
Magick::Color("aqua"),
|
|
|
|
Magick::Color("teal"),
|
|
|
|
Magick::Color("blue"),
|
|
|
|
Magick::Color("navy"),
|
|
|
|
Magick::Color("fuchsia"),
|
|
|
|
Magick::Color("purple")
|
|
|
|
};
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2018-11-27 12:01:03 +01:00
|
|
|
std::uint64_t random = 0xffffffffffffffff;
|
2018-11-27 09:59:02 +01:00
|
|
|
for (uint64_t chunk = 0; chunk < 4; ++chunk)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << std::hex << sha256.substr(chunk * 16, 16);
|
|
|
|
random = (random / 2) + (static_cast<std::uint64_t>(ss.get()) / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
Magick::Color dotcolor = colors[random % 13];
|
|
|
|
for (uint8_t row = 0; row < 4; ++row)
|
|
|
|
{
|
|
|
|
for (uint8_t column = 0; column < 4; ++column)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
uint16_t px;
|
|
|
|
ss << std::hex << sha256.substr(row * 4 + column, 4);
|
|
|
|
ss >> px;
|
|
|
|
if (px > 0x8000)
|
|
|
|
{
|
|
|
|
img.pixelColor(column, row, dotcolor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
|
|
|
cerr << "Error: " << e.what() << endl;
|
|
|
|
return { 5, img };
|
|
|
|
}
|
|
|
|
|
|
|
|
img.magick("png");
|
|
|
|
return { 0, img };
|
|
|
|
}
|