Read images from file, allow forcing magick

This commit is contained in:
tastytea 2022-06-11 05:15:22 +02:00
parent 17205298cd
commit 6aa3b40faa
Signed by: tastytea
SSH Key Fingerprint: SHA256:FBkvrOlhq5use1XEttyUGT4bUTDVA1ar9SgIc9P03cM
3 changed files with 41 additions and 13 deletions

View File

@ -20,26 +20,23 @@
#include <identiconpp.hpp>
#include <Magick++/Blob.h>
#include <Magick++/Geometry.h>
#include <Magick++/Image.h>
#include <algorithm>
#include <cstdint>
#include <ios>
#include <iostream>
#include <string>
#include <string_view>
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 get(const fs::path &filepath, const size_t size) {
Magick::Image img;
img.read(filepath);
img.resize(Magick::Geometry(size, size));
return img;
}
Magick::Image generate_identicon(const request_t &req) {
@ -70,4 +67,19 @@ Magick::Image generate_identicon(const request_t &req) {
return img;
}
void write(Magick::Image &img,
const std::optional<std::string_view> magick_force) {
std::string magick{magick_force.value_or(img.magick())};
std::transform(magick.begin(), magick.end(), magick.begin(), ::tolower);
img.magick(magick);
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()));
}
} // namespace libravatarserv::image

View File

@ -17,17 +17,25 @@
#ifndef LIBRAVATARSERV_IMAGE_HPP
#define LIBRAVATARSERV_IMAGE_HPP
#include "fs-compat.hpp"
#include "types.hpp"
#include <Magick++/Image.h>
#include <optional>
namespace libravatarserv::image {
void write(Magick::Image &img);
// read image from file
[[nodiscard]] Magick::Image get(const fs::path &filepath, size_t size);
// Generate identicon from hash
[[nodiscard]] Magick::Image generate_identicon(const request_t &req);
// write image with headers to stdout
void write(Magick::Image &img,
std::optional<std::string_view> magick_force = nullptr);
} // namespace libravatarserv::image
#endif // LIBRAVATARSERV_IMAGE_HPP

View File

@ -22,6 +22,7 @@
#include "request.hpp"
#include "version.hpp"
#include <Magick++/Exception.h>
#include <Magick++/Image.h>
#include <cstring>
@ -49,7 +50,14 @@ int main(int /*argc*/, char * /*argv*/[]) {
Magick::Image img;
if (!email.empty()) {
// img = image::get(cfg.avatar_dir / email, req.size);
try {
img = image::get(cfg.avatar_dir / email,
static_cast<size_t>(req.size));
} catch (const Magick::Exception &e) {
std::cout << "Status: 500 Internal Server Error\n\n";
std::cerr << "ERROR: " << e.what() << '\n';
return 1;
}
} else {
if (cfg.redirect) {
std::cout << "Status: 307 Temporary Redirect\n";