51 lines
1.3 KiB
C++
51 lines
1.3 KiB
C++
#include "datadragonimagecache.h"
|
|
|
|
#include <Log.h>
|
|
|
|
#include <sys/stat.h>
|
|
#include <QFile>
|
|
|
|
static bool mkdirs(const std::string& path) {
|
|
size_t offset = 0;
|
|
while(offset < path.size()) {
|
|
offset = path.find('/', offset+1);
|
|
int res = mkdir(path.substr(0, offset).c_str(), S_IRWXU | S_IRWXG); // 770
|
|
if(res == -1 && errno != EEXIST) {
|
|
// mkdirs failed
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
DataDragonImageCache::DataDragonImageCache(const std::string& folderextra, const std::string& imageext) : imageext(imageext) {
|
|
// init cache dir
|
|
const char* homevar = getenv("HOME");
|
|
if(homevar == nullptr) {
|
|
Log::warn << "$HOME is not set! Defaulting to ./";
|
|
homevar = "./";
|
|
}
|
|
cacheDir = std::string(homevar) + "/.cache/lolautoaccept/" + folderextra + "/";
|
|
|
|
mkdirs(cacheDir);
|
|
}
|
|
DataDragonImageCache::~DataDragonImageCache() {}
|
|
|
|
cv::Mat DataDragonImageCache::getImage(const std::string& name) {
|
|
auto img = cv::imread(getFilepath(name));
|
|
if(!img.empty())
|
|
cv::cvtColor(img, img, cv::COLOR_BGR2RGB);
|
|
return img;
|
|
}
|
|
|
|
void DataDragonImageCache::addImageRaw(const QByteArray& arr, const std::string& name) {
|
|
QFile file(QString::fromStdString(getFilepath(name)));
|
|
file.open(QIODevice::WriteOnly);
|
|
file.write(arr);
|
|
}
|
|
|
|
std::string DataDragonImageCache::getFilepath(const std::string& name) const {
|
|
return cacheDir + name + imageext;
|
|
}
|