implement fallbacks

This commit is contained in:
tastytea 2022-06-11 04:46:54 +02:00
parent cb354e666d
commit 17205298cd
Signed by: tastytea
SSH Key Fingerprint: SHA256:FBkvrOlhq5use1XEttyUGT4bUTDVA1ar9SgIc9P03cM
3 changed files with 153 additions and 7 deletions

73
src/image.cpp Normal file
View File

@ -0,0 +1,73 @@
/* This file is part of libravatarserv.
* Copyright © 2022 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 "image.hpp"
#include "types.hpp"
#include <identiconpp.hpp>
#include <Magick++/Blob.h>
#include <Magick++/Image.h>
#include <algorithm>
#include <cstdint>
#include <ios>
#include <iostream>
namespace libravatarserv::image {
void write(Magick::Image &img) {
string magick{img.magick()};
std::transform(magick.begin(), magick.end(), magick.begin(), ::tolower);
Magick::Blob buffer;
img.write(&buffer);
std::cout << "Content-Type: image/" << magick << '\n';
std::cout << "Content-Length: " << buffer.length() << "\n\n";
std::cout.write(static_cast<const char *>(buffer.data()),
static_cast<std::streamsize>(buffer.length()));
}
Magick::Image generate_identicon(const request_t &req) {
const auto padwidth{[&req]() -> std::uint8_t {
if (req.size < 60) {
return 0;
}
return static_cast<std::uint8_t>(std::max(req.size / 10, 10));
}()};
Identiconpp identicon(5, 5, Identiconpp::algorithm::sigil, "fefefeff",
{
// The same colors ivatar uses.
"2d4fffff", // Blue
"feb42cff", // Yellow
"e279eaff", // Bright pink
"1eb3fdff", // Cyan
"E84D41ff", // Red
"31CB73ff", // Green
"8D45AAff" // Dark pink
},
{padwidth, padwidth});
Magick::Image img{
identicon.generate(req.hash, static_cast<std::uint8_t>(req.size))};
img.magick("PNG");
return img;
}
} // namespace libravatarserv::image

33
src/image.hpp Normal file
View File

@ -0,0 +1,33 @@
/* This file is part of libravatarserv.
* Copyright © 2022 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/>.
*/
#ifndef LIBRAVATARSERV_IMAGE_HPP
#define LIBRAVATARSERV_IMAGE_HPP
#include "types.hpp"
#include <Magick++/Image.h>
namespace libravatarserv::image {
void write(Magick::Image &img);
// Generate identicon from hash
[[nodiscard]] Magick::Image generate_identicon(const request_t &req);
} // namespace libravatarserv::image
#endif // LIBRAVATARSERV_IMAGE_HPP

View File

@ -15,11 +15,15 @@
*/
#include "config.hpp"
#include "fs-compat.hpp"
#include "hash.hpp"
#include "helpers.hpp"
#include "image.hpp"
#include "request.hpp"
#include "version.hpp"
#include <Magick++/Image.h>
#include <cstring>
#include <iostream>
#include <stdexcept>
@ -40,14 +44,50 @@ int main(int /*argc*/, char * /*argv*/[]) {
auto cfg{config::read()};
auto req{request::parse(uri)};
if (req.fallback.empty()) {
req.fallback = cfg.default_fallback;
}
const std::string email{hash::find_email(req, cfg.avatar_dir)};
std::string email{hash::find_email(req, cfg.avatar_dir)};
std::cout << " HASH: " << req.hash << "\n SIZE: " << req.size
<< "\n FALLBACK: " << req.fallback << "\n EMAIL: " << email
<< '\n';
Magick::Image img;
if (!email.empty()) {
// img = image::get(cfg.avatar_dir / email, req.size);
} else {
if (cfg.redirect) {
std::cout << "Status: 307 Temporary Redirect\n";
std::cout << "Location: https://seccdn.libravatar.org/avatar/"
<< req.hash << "?s=" << req.size
<< "&d=" << req.fallback << "\n\n";
return 0;
}
if (req.fallback.empty()) {
req.fallback = cfg.default_fallback;
}
if (req.fallback == "404") {
std::cout << "Status: 404 Not Found\n\n";
return 0;
}
if (req.fallback == "mp" || req.fallback == "mm") {
if (!fs::exists(cfg.avatar_dir / "mp")) {
std::cout << "Status: 404 Not Found\n\n";
return 0;
}
email = "mp";
} else if (req.fallback == "identicon" || req.fallback == "retro") {
try {
img = image::generate_identicon(req);
} catch (const std::exception &e) {
std::cout << "Status: 500 Internal Server Error\n\n";
std::cerr
<< "ERROR: could not generate identicon: " << e.what()
<< "\n";
return 1;
}
} else {
std::cout << "Status: 501 Not Implemented\n\n";
}
}
image::write(img);
} catch (const std::runtime_error &e) {
if (std::strlen(e.what()) != 0) {