This commit is contained in:
parent
06a1bfccd9
commit
785cd7a35f
@ -22,7 +22,6 @@
|
|||||||
#include <cryptopp/hex.h>
|
#include <cryptopp/hex.h>
|
||||||
#include "libravatarserv.hpp"
|
#include "libravatarserv.hpp"
|
||||||
|
|
||||||
using global::avatar_dir;
|
|
||||||
using namespace hash;
|
using namespace hash;
|
||||||
|
|
||||||
const string hash::md5(const string &text)
|
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))
|
if (fs::is_regular_file(path))
|
||||||
{
|
{
|
||||||
|
@ -23,7 +23,6 @@
|
|||||||
using std::cout;
|
using std::cout;
|
||||||
using std::cerr;
|
using std::cerr;
|
||||||
using std::endl;
|
using std::endl;
|
||||||
using global::avatar_dir;
|
|
||||||
using namespace image;
|
using namespace image;
|
||||||
|
|
||||||
const Image image::get(const string &digest, const uint16_t size)
|
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);
|
const auto &entry = hash::table.find(digest);
|
||||||
if (entry == hash::table.end())
|
if (entry == hash::table.end())
|
||||||
{
|
{
|
||||||
filename = avatar_dir / "default";
|
filename = settings::avatar_dir / "default";
|
||||||
if (!fs::is_regular_file(filename))
|
if (!fs::is_regular_file(filename))
|
||||||
{
|
{
|
||||||
cerr << "Error: User not found and no default image set.\n";
|
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
|
else
|
||||||
{
|
{
|
||||||
filename = avatar_dir / entry->second;
|
filename = settings::avatar_dir / entry->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
|
@ -15,21 +15,17 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <cstdlib>
|
|
||||||
#include <basedir.h>
|
|
||||||
#include "version.hpp"
|
#include "version.hpp"
|
||||||
#include "libravatarserv.hpp"
|
#include "libravatarserv.hpp"
|
||||||
|
|
||||||
using std::cout;
|
using std::cout;
|
||||||
using std::cerr;
|
using std::cerr;
|
||||||
using std::endl;
|
using std::endl;
|
||||||
using global::avatar_dir;
|
|
||||||
using global::settings;
|
|
||||||
|
|
||||||
// Global variables
|
// Global variables
|
||||||
std::map<const string, const string> hash::table;
|
std::map<const string, const string> hash::table;
|
||||||
fs::path global::avatar_dir = "";
|
fs::path settings::avatar_dir = "";
|
||||||
global::Settings global::settings;
|
settings::Settings settings::settings;
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
@ -52,14 +48,14 @@ int main()
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!find_avatar_dir())
|
if (!settings::find_avatar_dir())
|
||||||
{
|
{
|
||||||
cout << "Status: 503 Service Unavailable\n\n";
|
cout << "Status: 503 Service Unavailable\n\n";
|
||||||
cerr << "Error: No avatars found.\n";
|
cerr << "Error: No avatars found.\n";
|
||||||
return 3;
|
return 3;
|
||||||
}
|
}
|
||||||
hash::fill_table();
|
hash::fill_table();
|
||||||
read_settings();
|
settings::read_settings();
|
||||||
|
|
||||||
image::Image image = image::get(avatar.digest, avatar.size);
|
image::Image image = image::get(avatar.digest, avatar.size);
|
||||||
if (image.error == 0)
|
if (image.error == 0)
|
||||||
@ -70,14 +66,14 @@ int main()
|
|||||||
{
|
{
|
||||||
cerr << "Error " << std::to_string(image.error)
|
cerr << "Error " << std::to_string(image.error)
|
||||||
<< ": Could not open file.\n";
|
<< ": Could not open file.\n";
|
||||||
if (global::settings.redirect_nouser)
|
if (settings::settings.redirect_nouser)
|
||||||
{
|
{
|
||||||
http::send_redirect(avatar);
|
http::send_redirect(avatar);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if (avatar.fallback.empty())
|
if (avatar.fallback.empty())
|
||||||
{
|
{
|
||||||
avatar.fallback = settings.default_fallback;
|
avatar.fallback = settings::settings.default_fallback;
|
||||||
}
|
}
|
||||||
if (avatar.fallback.substr(0, 3) == "404")
|
if (avatar.fallback.substr(0, 3) == "404")
|
||||||
{
|
{
|
||||||
@ -120,7 +116,7 @@ int main()
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (global::settings.redirect_nofallback)
|
if (settings::settings.redirect_nofallback)
|
||||||
{
|
{
|
||||||
http::send_redirect(avatar);
|
http::send_redirect(avatar);
|
||||||
}
|
}
|
||||||
@ -133,64 +129,3 @@ int main()
|
|||||||
|
|
||||||
return 0;
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -36,11 +36,9 @@ using std::uint16_t;
|
|||||||
using std::uint8_t;
|
using std::uint8_t;
|
||||||
|
|
||||||
int main();
|
int main();
|
||||||
bool find_avatar_dir();
|
|
||||||
void read_settings();
|
|
||||||
|
|
||||||
namespace global
|
namespace settings
|
||||||
{
|
{ // settings.cpp
|
||||||
extern fs::path avatar_dir;
|
extern fs::path avatar_dir;
|
||||||
extern struct Settings
|
extern struct Settings
|
||||||
{
|
{
|
||||||
@ -48,6 +46,9 @@ namespace global
|
|||||||
bool redirect_nofallback = false;
|
bool redirect_nofallback = false;
|
||||||
bool redirect_nouser = false;
|
bool redirect_nouser = false;
|
||||||
} settings;
|
} settings;
|
||||||
|
|
||||||
|
bool find_avatar_dir();
|
||||||
|
void read_settings();
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace http // http.cpp
|
namespace http // http.cpp
|
||||||
|
82
src/settings.cpp
Normal file
82
src/settings.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user