libravatarserv/src/image.cpp

72 lines
1.9 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 "libravatarserv.hpp"
using std::cout;
using std::cerr;
using std::endl;
using global::avatar_dir;
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 = avatar_dir / "default";
if (!fs::is_regular_file(filename))
{
cerr << "Error: User not found and no default image set.\n";
return { 2, img };
}
}
else
{
filename = 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)
{
cout << "Content-type: image/png\n\n";
cout.flush(); // We need to flush before we use /dev/stdout directly.
image.image.write("/dev/stdout");
}