forked from tastytea/libravatarserv
80 lines
2.1 KiB
C++
80 lines
2.1 KiB
C++
/* 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>
|
|
#include <sstream>
|
|
#include <array>
|
|
#include <algorithm>
|
|
#include <Magick++/Geometry.h>
|
|
#include <Magick++/Color.h>
|
|
#include <Magick++/Exception.h>
|
|
#include "libravatarserv.hpp"
|
|
|
|
using std::cout;
|
|
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())
|
|
{
|
|
filename = settings::avatar_dir / "default";
|
|
if (!fs::is_regular_file(filename))
|
|
{
|
|
cerr << "Warning: User not found and no default image set.\n";
|
|
return { 2, img };
|
|
}
|
|
}
|
|
else
|
|
{
|
|
filename = settings::avatar_dir / entry->second;
|
|
}
|
|
|
|
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 };
|
|
}
|
|
|
|
void image::write(Image &image)
|
|
{
|
|
string magick = image.image.magick();
|
|
std::transform(magick.begin(), magick.end(), magick.begin(), ::tolower);
|
|
|
|
cout << "Content-Type: image/" << magick << endl << endl;
|
|
cout.flush(); // We need to flush before we use /dev/stdout directly.
|
|
image.image.write("/dev/stdout");
|
|
}
|