Refactored settings-stuff into own file.

This commit is contained in:
tastytea 2018-11-30 07:00:37 +01:00
parent 06a1bfccd9
commit 785cd7a35f
Signed by untrusted user: tastytea
GPG Key ID: CFC39497F1B26E07
5 changed files with 98 additions and 81 deletions

View File

@ -22,7 +22,6 @@
#include <cryptopp/hex.h>
#include "libravatarserv.hpp"
using global::avatar_dir;
using namespace hash;
const string hash::md5(const string &text)
@ -57,7 +56,8 @@ bool hash::fill_table()
for (const fs::path &path : fs::recursive_directory_iterator(avatar_dir))
for (const fs::path &path :
fs::recursive_directory_iterator(settings::avatar_dir))
{
if (fs::is_regular_file(path))
{

View File

@ -23,7 +23,6 @@
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)
@ -35,7 +34,7 @@ const Image image::get(const string &digest, const uint16_t size)
const auto &entry = hash::table.find(digest);
if (entry == hash::table.end())
{
filename = avatar_dir / "default";
filename = settings::avatar_dir / "default";
if (!fs::is_regular_file(filename))
{
cerr << "Error: User not found and no default image set.\n";
@ -44,7 +43,7 @@ const Image image::get(const string &digest, const uint16_t size)
}
else
{
filename = avatar_dir / entry->second;
filename = settings::avatar_dir / entry->second;
}
try

View File

@ -15,21 +15,17 @@
*/
#include <iostream>
#include <cstdlib>
#include <basedir.h>
#include "version.hpp"
#include "libravatarserv.hpp"
using std::cout;
using std::cerr;
using std::endl;
using global::avatar_dir;
using global::settings;
// Global variables
std::map<const string, const string> hash::table;
fs::path global::avatar_dir = "";
global::Settings global::settings;
fs::path settings::avatar_dir = "";
settings::Settings settings::settings;
int main()
{
@ -52,14 +48,14 @@ int main()
return 1;
}
if (!find_avatar_dir())
if (!settings::find_avatar_dir())
{
cout << "Status: 503 Service Unavailable\n\n";
cerr << "Error: No avatars found.\n";
return 3;
}
hash::fill_table();
read_settings();
settings::read_settings();
image::Image image = image::get(avatar.digest, avatar.size);
if (image.error == 0)
@ -70,14 +66,14 @@ int main()
{
cerr << "Error " << std::to_string(image.error)
<< ": Could not open file.\n";
if (global::settings.redirect_nouser)
if (settings::settings.redirect_nouser)
{
http::send_redirect(avatar);
return 0;
}
if (avatar.fallback.empty())
{
avatar.fallback = settings.default_fallback;
avatar.fallback = settings::settings.default_fallback;
}
if (avatar.fallback.substr(0, 3) == "404")
{
@ -120,7 +116,7 @@ int main()
}
else
{
if (global::settings.redirect_nofallback)
if (settings::settings.redirect_nofallback)
{
http::send_redirect(avatar);
}
@ -133,64 +129,3 @@ int main()
return 0;
}
bool find_avatar_dir()
{
const char *envdir = std::getenv("LIBRAVATARSERV_DIR");
if (envdir == nullptr)
{
// TODO: Remove AVATAR_DIR in 1.0.0
// DEPRECATED because it is potentially used by another program
envdir = std::getenv("AVATAR_DIR");
}
if (envdir != nullptr && fs::is_directory(envdir))
{
avatar_dir = envdir;
}
else
{
xdgHandle xdg;
xdgInitHandle(&xdg);
auto data_dirs = xdgDataDirectories(&xdg);
const uint8_t size = sizeof(data_dirs) / sizeof(*data_dirs);
for (uint8_t index = 0; index <= size; ++index)
{
const string searchdir = data_dirs[index]
+ string("/libravatarserv");
if (fs::is_directory(searchdir))
{
avatar_dir = searchdir;
break;
}
}
xdgWipeHandle(&xdg);
if (avatar_dir.empty())
{
return false;
}
}
return true;
}
void read_settings()
{
const char *env = std::getenv("LIBRAVATARSERV_DEFAULT_FALLBACK");
if (env != nullptr)
{
global::settings.default_fallback = env;
}
env = std::getenv("LIBRAVATARSERV_REDIRECT_NOFALLBACK");
if (env != nullptr && env[0] == '1')
{
global::settings.redirect_nofallback = true;
}
env = std::getenv("LIBRAVATARSERV_REDIRECT_NOUSER");
if (env != nullptr && env[0] == '1')
{
global::settings.redirect_nouser = true;
}
}

View File

@ -36,11 +36,9 @@ using std::uint16_t;
using std::uint8_t;
int main();
bool find_avatar_dir();
void read_settings();
namespace global
{
namespace settings
{ // settings.cpp
extern fs::path avatar_dir;
extern struct Settings
{
@ -48,6 +46,9 @@ namespace global
bool redirect_nofallback = false;
bool redirect_nouser = false;
} settings;
bool find_avatar_dir();
void read_settings();
}
namespace http // http.cpp

82
src/settings.cpp Normal file
View File

@ -0,0 +1,82 @@
/* 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 <cstdlib>
#include <basedir.h>
#include "libravatarserv.hpp"
using namespace settings;
bool settings::find_avatar_dir()
{
const char *envdir = std::getenv("LIBRAVATARSERV_DIR");
if (envdir == nullptr)
{
// TODO: Remove AVATAR_DIR in 1.0.0
// DEPRECATED because it is potentially used by another program
envdir = std::getenv("AVATAR_DIR");
}
if (envdir != nullptr && fs::is_directory(envdir))
{
avatar_dir = envdir;
}
else
{
xdgHandle xdg;
xdgInitHandle(&xdg);
auto data_dirs = xdgDataDirectories(&xdg);
const uint8_t size = sizeof(data_dirs) / sizeof(*data_dirs);
for (uint8_t index = 0; index <= size; ++index)
{
const string searchdir = data_dirs[index]
+ string("/libravatarserv");
if (fs::is_directory(searchdir))
{
avatar_dir = searchdir;
break;
}
}
xdgWipeHandle(&xdg);
if (avatar_dir.empty())
{
return false;
}
}
return true;
}
void settings::read_settings()
{
const char *env = std::getenv("LIBRAVATARSERV_DEFAULT_FALLBACK");
if (env != nullptr)
{
settings.default_fallback = env;
}
env = std::getenv("LIBRAVATARSERV_REDIRECT_NOFALLBACK");
if (env != nullptr && env[0] == '1')
{
settings.redirect_nofallback = true;
}
env = std::getenv("LIBRAVATARSERV_REDIRECT_NOUSER");
if (env != nullptr && env[0] == '1')
{
settings.redirect_nouser = true;
}
}