Rewrote Frontend
This commit is contained in:
@@ -55,14 +55,15 @@ namespace api {
|
||||
|
||||
void admin::delete_user(req_type req, cbk_type cbk) {
|
||||
Json::Value& json = *req->jsonObject();
|
||||
msd::channel<std::string> chan;
|
||||
try {
|
||||
uint64_t user_id = dto::json_get<uint64_t>(json, "user").value();
|
||||
|
||||
db::MapperUser user_mapper(drogon::app().getDbClient());
|
||||
auto user = user_mapper.findByPrimaryKey(user_id);
|
||||
auth::revoke_all(user);
|
||||
fs::delete_node(fs::get_node(user.getValueOfRootId()).value(), true);
|
||||
user_mapper.deleteOne(user);
|
||||
fs::delete_node(fs::get_node(user.getValueOfRootId()).value(), chan, true);
|
||||
user_mapper.deleteOne(user);
|
||||
cbk(dto::Responses::get_success_res());
|
||||
} catch (const std::exception&) {
|
||||
cbk(dto::Responses::get_badreq_res("Validation error"));
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include <botan/base32.h>
|
||||
#include <botan/base64.h>
|
||||
#include <qrcodegen.hpp>
|
||||
#include <lodepng.h>
|
||||
#include <opencv2/opencv.hpp>
|
||||
|
||||
#include "controllers/controllers.h"
|
||||
#include "db/db.h"
|
||||
@@ -24,21 +24,14 @@ std::string create_totp_qrcode(const db::User& user, const std::string& b32_secr
|
||||
const int mod_count = code.getSize();
|
||||
|
||||
const int row_size = qrcode_pixel_size * mod_count;
|
||||
std::vector<uint8_t> secret, image, row;
|
||||
row.reserve(row_size);
|
||||
image.reserve(row_size * row_size);
|
||||
cv::Mat image(mod_count, mod_count, CV_8UC1), scaled_image;
|
||||
std::vector<uint8_t> image_encoded;
|
||||
for (int y = 0; y < mod_count; y++) for (int x = 0; x < mod_count; x++)
|
||||
image.at<uint8_t>(x, y) = code.getModule(x, y) ? 0 : 0xff;
|
||||
cv::resize(image, scaled_image, cv::Size(), qrcode_pixel_size, qrcode_pixel_size, cv::INTER_NEAREST);
|
||||
cv::imencode(".png", scaled_image, image_encoded);
|
||||
|
||||
for (int y = 0; y < mod_count; y++) {
|
||||
row.clear();
|
||||
for (int x = 0; x < mod_count; x++)
|
||||
row.insert(row.end(), qrcode_pixel_size, code.getModule(x, y) ? 0 : 0xff);
|
||||
for (int i = 0; i < qrcode_pixel_size; i++)
|
||||
image.insert(image.end(), row.begin(), row.end());
|
||||
}
|
||||
|
||||
lodepng::encode(secret, image, row_size, row_size, LCT_GREY, 8);
|
||||
|
||||
return "data:image/png;base64," + Botan::base64_encode(secret);
|
||||
return "data:image/png;base64," + Botan::base64_encode(image_encoded);
|
||||
}
|
||||
|
||||
namespace api {
|
||||
@@ -64,7 +57,7 @@ namespace api {
|
||||
std::string code = create_totp_qrcode(user, b32_secret);
|
||||
cbk(dto::Responses::get_tfa_setup_res(b32_secret, code));
|
||||
}
|
||||
} catch (const std::exception&) {
|
||||
} catch (const std::exception& e) {
|
||||
cbk(dto::Responses::get_badreq_res("Validation error"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
|
||||
#include <jwt-cpp/traits/kazuho-picojson/traits.h>
|
||||
#include <jwt-cpp/jwt.h>
|
||||
#include <mailio/smtp.hpp>
|
||||
#include <SMTPMail.h>
|
||||
|
||||
#include "controllers/controllers.h"
|
||||
#include "db/db.h"
|
||||
@@ -43,15 +43,17 @@ namespace api {
|
||||
char totp[16];
|
||||
std::snprintf(totp, 16, "%06d", Botan::TOTP(Botan::OctetString(totp_secret)).generate_totp(t));
|
||||
|
||||
mailio::message msg;
|
||||
msg.from(mailio::mail_address("Fileserver", "fileserver@mattv.de"));
|
||||
msg.add_recipient(mailio::mail_address(user.getValueOfName(), user.getValueOfName()));
|
||||
msg.subject("Subject: Fileserver - Email 2fa code");
|
||||
msg.content("Your code is: " + std::string(totp) +"\r\nIt is valid for 5 Minutes");
|
||||
|
||||
mailio::smtps conn("mail.mattv.de", 587);
|
||||
conn.authenticate("no-reply@mattv.de", "noreplyLONGPASS123", mailio::smtps::auth_method_t::START_TLS);
|
||||
conn.submit(msg);
|
||||
drogon::app().getPlugin<SMTPMail>()->sendEmail(
|
||||
"mail.mattv.de",
|
||||
587,
|
||||
"fileserver@mattv.de",
|
||||
user.getValueOfName(),
|
||||
"MFileserver - Email 2fa code",
|
||||
"Your code is: " + std::string(totp) +"\r\nIt is valid for 5 Minutes",
|
||||
"no-reply@mattv.de",
|
||||
"noreplyLONGPASS123",
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
std::string auth::get_token(const db::User& user) {
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#ifndef BACKEND_CONTROLLERS_H
|
||||
#define BACKEND_CONTROLLERS_H
|
||||
#include <drogon/drogon.h>
|
||||
#include <drogon/utils/coroutine.h>
|
||||
#include <botan/rng.h>
|
||||
#include <coroutine>
|
||||
#include <variant>
|
||||
|
||||
#include <drogon/drogon.h>
|
||||
#include <botan/rng.h>
|
||||
#include <msd/channel.hpp>
|
||||
|
||||
#include "db/db.h"
|
||||
|
||||
using req_type = const drogon::HttpRequestPtr&;
|
||||
@@ -86,14 +86,32 @@ public:
|
||||
METHOD_ADD(fs::create_node_req<true>, "/createFile", drogon::Post, "Login");
|
||||
METHOD_ADD(fs::delete_node_req, "/delete/{}", drogon::Post, "Login");
|
||||
METHOD_ADD(fs::upload, "/upload/{}", drogon::Post, "Login");
|
||||
METHOD_ADD(fs::create_zip, "/create_zip", drogon::Post, "Login");
|
||||
METHOD_ADD(fs::download, "/download", drogon::Post, "Login");
|
||||
METHOD_ADD(fs::download_multi, "/download_multi", drogon::Post, "Login");
|
||||
METHOD_ADD(fs::download_preview, "/download_preview/{}", drogon::Get, "Login");
|
||||
METHOD_ADD(fs::download_base64, "/download_base64/{}", drogon::Get, "Login");
|
||||
METHOD_ADD(fs::get_type, "/get_type/{}", drogon::Get, "Login");
|
||||
METHOD_LIST_END
|
||||
|
||||
enum class create_node_error {
|
||||
INVALID_NAME,
|
||||
INVALID_PARENT,
|
||||
FILE_PARENT
|
||||
};
|
||||
|
||||
struct mutex_stream {
|
||||
std::stringstream ss;
|
||||
std::mutex mutex;
|
||||
bool done = false;
|
||||
};
|
||||
|
||||
static std::optional<db::INode> get_node(uint64_t node);
|
||||
static std::optional<db::INode> get_node_and_validate(const db::User& user, uint64_t node);
|
||||
static std::vector<db::INode> get_children(const db::INode& parent);
|
||||
static std::variant<db::INode, std::string> create_node(std::string name, const db::User& owner, bool file, const std::optional<uint64_t> &parent, bool force = false);
|
||||
static void delete_node(db::INode node, bool allow_root = false);
|
||||
static std::variant<db::INode, fs::create_node_error, std::tuple<bool, uint64_t>>
|
||||
create_node(std::string name, const db::User& owner, bool file, const std::optional<uint64_t> &parent, bool force = false);
|
||||
static void delete_node(db::INode node, msd::channel<std::string>& chan, bool allow_root = false);
|
||||
|
||||
|
||||
void root(req_type, cbk_type);
|
||||
@@ -102,7 +120,12 @@ public:
|
||||
template<bool file> void create_node_req(req_type req, cbk_type cbk);
|
||||
void delete_node_req(req_type, cbk_type, uint64_t node);
|
||||
void upload(req_type, cbk_type, uint64_t node);
|
||||
void create_zip(req_type, cbk_type);
|
||||
void download(req_type, cbk_type);
|
||||
void download_multi(req_type, cbk_type);
|
||||
void download_preview(req_type, cbk_type, uint64_t node);
|
||||
void download_base64(req_type, cbk_type, uint64_t node);
|
||||
void get_type(req_type, cbk_type, uint64_t node);
|
||||
};
|
||||
|
||||
class user : public drogon::HttpController<user> {
|
||||
|
||||
@@ -3,12 +3,100 @@
|
||||
#pragma ide diagnostic ignored "readability-convert-member-functions-to-static"
|
||||
|
||||
#include <filesystem>
|
||||
#include <unordered_map>
|
||||
#include <fstream>
|
||||
|
||||
#include <opencv2/opencv.hpp>
|
||||
#include <botan/base64.h>
|
||||
#include <trantor/net/EventLoopThread.h>
|
||||
#include <zip/zip.h>
|
||||
|
||||
#include "controllers.h"
|
||||
#include "dto/dto.h"
|
||||
|
||||
char windows_invalid_chars[] = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F<>:\"/\\|";
|
||||
|
||||
std::string generate_path(db::INode node) {
|
||||
// https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#common_image_file_types
|
||||
const std::unordered_map<std::string, std::string> mime_type_map = {
|
||||
{ ".apng" , "image/apng" },
|
||||
{ ".avif" , "image/avif" },
|
||||
{ ".bmp" , "image/bmp" },
|
||||
{ ".gif" , "image/gif" },
|
||||
{ ".jpg" , "image/jpeg" },
|
||||
{ ".jpeg" , "image/jpeg" },
|
||||
{ ".jfif" , "image/jpeg" },
|
||||
{ ".pjpeg", "image/jpeg" },
|
||||
{ ".pjp" , "image/jpeg" },
|
||||
{ ".png" , "image/png" },
|
||||
{ ".svg" , "image/svg" },
|
||||
{ ".webp" , "image/webp" },
|
||||
|
||||
{ ".aac" , "audio/aac" },
|
||||
{ ".flac" , "audio/flac" },
|
||||
{ ".mp3" , "audio/mp3" },
|
||||
{ ".m4a" , "audio/mp4" },
|
||||
{ ".oga" , "audio/ogg" },
|
||||
{ ".ogg" , "audio/ogg" },
|
||||
{ ".wav" , "audio/wav" },
|
||||
|
||||
{ ".3gp" , "video/3gpp" },
|
||||
{ ".mpg" , "video/mpeg" },
|
||||
{ ".mpeg" , "video/mpeg" },
|
||||
{ ".mp4" , "video/mp4" },
|
||||
{ ".m4v" , "video/mp4" },
|
||||
{ ".m4p" , "video/mp4" },
|
||||
{ ".ogv" , "video/ogg" },
|
||||
{ ".mov" , "video/quicktime" },
|
||||
{ ".webm" , "video/webm" },
|
||||
{ ".mkv" , "video/x-matroska" },
|
||||
{ ".mk3d" , "video/x-matroska" },
|
||||
{ ".mks" , "video/x-matroska" },
|
||||
};
|
||||
|
||||
uint64_t next_temp_id = 0;
|
||||
std::unordered_map<std::string, std::string> zip_to_temp_map;
|
||||
std::unordered_map<std::string, std::tuple<std::string, uint64_t, uint64_t>> in_progress_zips;
|
||||
|
||||
trantor::EventLoop* get_zip_loop() {
|
||||
static bool init_done = false;
|
||||
static trantor::EventLoopThread loop("ZipEventLoop");
|
||||
if (!init_done) {
|
||||
init_done = true;
|
||||
loop.run();
|
||||
loop.getLoop()->runEvery(30*60, []{
|
||||
for (const auto& entry : std::filesystem::directory_iterator("./temp")) {
|
||||
if (!entry.is_regular_file()) continue;
|
||||
const std::string file_name = "./temp/" + entry.path().filename().string();
|
||||
const auto& progress_pos = std::find_if(in_progress_zips.begin(), in_progress_zips.end(),
|
||||
[&file_name](const std::pair<std::string, std::tuple<std::string, uint64_t, uint64_t>>& entry) {
|
||||
return std::get<0>(entry.second) == file_name;
|
||||
}
|
||||
);
|
||||
if (progress_pos != in_progress_zips.end()) return;
|
||||
const auto& zip_map_pos = std::find_if(zip_to_temp_map.begin(), zip_to_temp_map.end(),
|
||||
[&file_name](const std::pair<std::string, std::string>& entry){
|
||||
return entry.second == file_name;
|
||||
}
|
||||
);
|
||||
if (zip_map_pos != zip_to_temp_map.end()) return;
|
||||
std::filesystem::remove(entry.path());
|
||||
}
|
||||
});
|
||||
}
|
||||
return loop.getLoop();
|
||||
}
|
||||
|
||||
trantor::EventLoop* get_delete_loop() {
|
||||
static bool init_done = false;
|
||||
static trantor::EventLoopThread loop("DeleteEventLoop");
|
||||
if (!init_done) {
|
||||
init_done = true;
|
||||
loop.run();
|
||||
}
|
||||
return loop.getLoop();
|
||||
}
|
||||
|
||||
void generate_path(db::INode node, std::string& str) {
|
||||
db::MapperInode inode_mapper(drogon::app().getDbClient());
|
||||
std::stack<db::INode> path;
|
||||
path.push(node);
|
||||
@@ -16,14 +104,101 @@ std::string generate_path(db::INode node) {
|
||||
node = inode_mapper.findByPrimaryKey(node.getValueOfParentId());
|
||||
path.push(node);
|
||||
}
|
||||
std::stringstream ss;
|
||||
while (!path.empty()) {
|
||||
const db::INode& seg = path.top();
|
||||
ss << seg.getValueOfName();
|
||||
if (seg.getValueOfIsFile() == 0) ss << '/';
|
||||
str += seg.getValueOfName();
|
||||
if (seg.getValueOfIsFile() == 0) str += "/";
|
||||
path.pop();
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
Json::Value generate_path(db::INode node) {
|
||||
Json::Value segments = Json::Value(Json::ValueType::arrayValue);
|
||||
db::MapperInode inode_mapper(drogon::app().getDbClient());
|
||||
std::stack<db::INode> path;
|
||||
path.push(node);
|
||||
while (node.getParentId() != nullptr) {
|
||||
node = inode_mapper.findByPrimaryKey(node.getValueOfParentId());
|
||||
path.push(node);
|
||||
}
|
||||
while (!path.empty()) {
|
||||
const db::INode& seg = path.top();
|
||||
if (seg.getParentId() == nullptr) {
|
||||
Json::Value json_seg;
|
||||
json_seg["path"] = "/";
|
||||
json_seg["node"] = seg.getValueOfId();
|
||||
segments.append(json_seg);
|
||||
} else {
|
||||
Json::Value json_seg;
|
||||
json_seg["path"] = seg.getValueOfName();
|
||||
json_seg["node"] = seg.getValueOfId();
|
||||
segments.append(json_seg);
|
||||
if (seg.getValueOfIsFile() == 0) {
|
||||
json_seg.removeMember("node");
|
||||
json_seg["path"] = "/";
|
||||
segments.append(json_seg);
|
||||
}
|
||||
}
|
||||
path.pop();
|
||||
}
|
||||
Json::Value resp;
|
||||
resp["segments"] = segments;
|
||||
return resp;
|
||||
}
|
||||
|
||||
uint64_t calc_total_size(const db::INode& base) {
|
||||
uint64_t size = 0;
|
||||
std::stack<db::INode> queue;
|
||||
queue.push(base);
|
||||
while (!queue.empty()) {
|
||||
const db::INode& node = queue.top();
|
||||
if (node.getValueOfIsFile() == 0) {
|
||||
auto children = api::fs::get_children(node);
|
||||
queue.pop();
|
||||
for (const auto& child : children) {
|
||||
if (child.getValueOfIsFile() == 0) queue.push(child);
|
||||
else if (child.getSize()) size += child.getValueOfSize();
|
||||
}
|
||||
} else {
|
||||
size += node.getValueOfSize();
|
||||
queue.pop();
|
||||
}
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
void add_to_zip(struct zip_t* zip, const std::string& key, const db::INode& node, const std::string& path) {
|
||||
if (node.getValueOfIsFile() == 0) {
|
||||
std::string new_path = path + node.getValueOfName() + "/";
|
||||
zip_entry_opencasesensitive(zip, new_path.c_str());
|
||||
zip_entry_close(zip);
|
||||
auto children = api::fs::get_children(node);
|
||||
for (const auto& child : children)
|
||||
add_to_zip(zip, key, child, new_path);
|
||||
} else {
|
||||
zip_entry_opencasesensitive(zip, (path + node.getValueOfName()).c_str());
|
||||
std::ifstream file("./files/" + std::to_string(node.getValueOfId()), std::ifstream::binary);
|
||||
std::vector<char> buffer(64*1024);
|
||||
while (!file.eof()) {
|
||||
file.read(buffer.data(), (std::streamsize)buffer.size());
|
||||
auto read = file.gcount();
|
||||
zip_entry_write(zip, buffer.data(), read);
|
||||
std::get<1>(in_progress_zips[key]) += read;
|
||||
}
|
||||
zip_entry_close(zip);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename InputIt>
|
||||
std::string join_string(InputIt first, InputIt last, const std::string& separator = ",") {
|
||||
std::ostringstream result;
|
||||
if (first != last) {
|
||||
result << *first;
|
||||
while (++first != last) {
|
||||
result << separator << *first;
|
||||
}
|
||||
}
|
||||
return result.str();
|
||||
}
|
||||
|
||||
namespace api {
|
||||
@@ -48,26 +223,31 @@ namespace api {
|
||||
return inode_mapper.findBy(db::Criteria(db::INode::Cols::_parent_id, db::CompareOps::EQ, parent.getValueOfId()));
|
||||
}
|
||||
|
||||
std::variant<db::INode, std::string> fs::create_node(std::string name, const db::User& owner, bool file, const std::optional<uint64_t> &parent, bool force) {
|
||||
std::variant<db::INode, fs::create_node_error, std::tuple<bool, uint64_t>>
|
||||
fs::create_node(std::string name, const db::User& owner, bool file, const std::optional<uint64_t> &parent, bool force) {
|
||||
// Stolen from https://github.com/boostorg/filesystem/blob/develop/src/portability.cpp
|
||||
if (!force)
|
||||
if (name.empty() || name[0] == ' ' || name.find_first_of(windows_invalid_chars, 0, sizeof(windows_invalid_chars)) != std::string::npos || *(name.end() - 1) == ' ' || *(name.end() - 1) == '.' || name == "." || name == "..")
|
||||
return {"Invalid name"};
|
||||
return {create_node_error::INVALID_NAME};
|
||||
|
||||
db::INode node;
|
||||
node.setIsFile(file ? 1 : 0);
|
||||
node.setName(name);
|
||||
node.setOwnerId(owner.getValueOfId());
|
||||
node.setHasPreview(0);
|
||||
if (parent.has_value()) {
|
||||
auto parent_node = get_node_and_validate(owner, *parent);
|
||||
if (!parent_node.has_value())
|
||||
return {"Invalid parent"};
|
||||
return {create_node_error::INVALID_PARENT};
|
||||
if (parent_node->getValueOfIsFile() != 0)
|
||||
return {"Can't use file as parent"};
|
||||
return {create_node_error::FILE_PARENT};
|
||||
auto children = get_children(*parent_node);
|
||||
for (const auto& child : children)
|
||||
if (child.getValueOfName() == name)
|
||||
return {"File/Folder already exists"};
|
||||
return {std::make_tuple(
|
||||
child.getValueOfIsFile() != 0,
|
||||
child.getValueOfId()
|
||||
)};
|
||||
node.setParentId(*parent);
|
||||
}
|
||||
db::MapperInode inode_mapper(drogon::app().getDbClient());
|
||||
@@ -75,18 +255,56 @@ namespace api {
|
||||
return {node};
|
||||
}
|
||||
|
||||
void fs::delete_node(db::INode node, bool allow_root) {
|
||||
void fs::delete_node(db::INode node, msd::channel<std::string>& chan, bool allow_root) {
|
||||
if (node.getValueOfParentId() == 0 && (!allow_root)) return;
|
||||
if (node.getValueOfIsFile() == 0) {
|
||||
auto children = get_children(node);
|
||||
for (const auto& child : children) delete_node(child, false);
|
||||
} else {
|
||||
|
||||
db::MapperInode inode_mapper(drogon::app().getDbClient());
|
||||
|
||||
const auto delete_file = [&chan, &inode_mapper](const db::INode& node) {
|
||||
std::string entry = "Deleting ";
|
||||
generate_path(node, entry);
|
||||
entry >> chan;
|
||||
std::filesystem::path p("./files");
|
||||
p /= std::to_string(node.getValueOfId());
|
||||
std::filesystem::remove(p);
|
||||
if (node.getValueOfHasPreview() != 0)
|
||||
std::filesystem::remove(p.string() + "_preview.png");
|
||||
inode_mapper.deleteOne(node);
|
||||
std::string(" Done\n") >> chan;
|
||||
};
|
||||
|
||||
std::stack<db::INode> queue, files, folders;
|
||||
|
||||
if (node.getValueOfIsFile() == 0) queue.push(node);
|
||||
else files.push(node);
|
||||
|
||||
while (!queue.empty()) {
|
||||
while (!files.empty()) {
|
||||
delete_file(files.top());
|
||||
files.pop();
|
||||
}
|
||||
std::string entry = "Deleting ";
|
||||
generate_path(queue.top(), entry);
|
||||
entry += "\n";
|
||||
entry >> chan;
|
||||
auto children = get_children(queue.top());
|
||||
folders.push(queue.top());
|
||||
queue.pop();
|
||||
for (const auto& child : children) {
|
||||
if (child.getValueOfIsFile() == 0) queue.push(child);
|
||||
else files.push(child);
|
||||
}
|
||||
}
|
||||
|
||||
while (!files.empty()) {
|
||||
delete_file(files.top());
|
||||
files.pop();
|
||||
}
|
||||
|
||||
while (!folders.empty()) {
|
||||
inode_mapper.deleteOne(folders.top());
|
||||
folders.pop();
|
||||
}
|
||||
db::MapperInode inode_mapper(drogon::app().getDbClient());
|
||||
inode_mapper.deleteOne(node);
|
||||
}
|
||||
|
||||
void fs::root(req_type req, cbk_type cbk) {
|
||||
@@ -98,23 +316,11 @@ namespace api {
|
||||
db::User user = dto::get_user(req);
|
||||
auto inode = get_node_and_validate(user, node);
|
||||
if (!inode.has_value())
|
||||
cbk(dto::Responses::get_badreq_res("Unknown node"));
|
||||
else if (inode->getValueOfIsFile() == 0) {
|
||||
std::vector<uint64_t> children;
|
||||
for (const db::INode& child : get_children(*inode)) children.push_back(child.getValueOfId());
|
||||
cbk(dto::Responses::get_node_folder_res(
|
||||
inode->getValueOfId(),
|
||||
inode->getValueOfName(),
|
||||
inode->getParentId(),
|
||||
children
|
||||
));
|
||||
} else
|
||||
cbk(dto::Responses::get_node_file_res(
|
||||
inode->getValueOfId(),
|
||||
inode->getValueOfName(),
|
||||
inode->getParentId(),
|
||||
inode->getValueOfSize()
|
||||
));
|
||||
return cbk(dto::Responses::get_badreq_res("Unknown node"));
|
||||
auto dto_node = dto::Responses::GetNodeEntry(*inode);
|
||||
std::vector<dto::Responses::GetNodeEntry> children;
|
||||
if (!dto_node.is_file) for (const db::INode& child : get_children(*inode)) children.emplace_back(child);
|
||||
cbk(dto::Responses::get_node_res(dto_node, children));
|
||||
}
|
||||
|
||||
void fs::path(req_type req, cbk_type cbk, uint64_t node) {
|
||||
@@ -122,8 +328,10 @@ namespace api {
|
||||
auto inode = get_node_and_validate(user, node);
|
||||
if (!inode.has_value())
|
||||
cbk(dto::Responses::get_badreq_res("Unknown node"));
|
||||
else
|
||||
cbk(dto::Responses::get_path_res( generate_path(*inode)));
|
||||
else {
|
||||
auto path = generate_path(*inode);
|
||||
cbk(dto::Responses::get_success_res(path));
|
||||
}
|
||||
}
|
||||
|
||||
template<bool file>
|
||||
@@ -135,10 +343,18 @@ namespace api {
|
||||
std::string name = dto::json_get<std::string>(json, "name").value();
|
||||
|
||||
auto new_node = create_node(name, user, file, std::make_optional(parent));
|
||||
if (std::holds_alternative<std::string>(new_node))
|
||||
cbk(dto::Responses::get_badreq_res(std::get<std::string>(new_node)));
|
||||
else
|
||||
if (std::holds_alternative<db::INode>(new_node))
|
||||
cbk(dto::Responses::get_new_node_res(std::get<db::INode>(new_node).getValueOfId()));
|
||||
else if (std::holds_alternative<create_node_error>(new_node))
|
||||
switch (std::get<create_node_error>(new_node)) {
|
||||
case create_node_error::INVALID_NAME: return cbk(dto::Responses::get_badreq_res("Invalid name"));
|
||||
case create_node_error::INVALID_PARENT: return cbk(dto::Responses::get_badreq_res("Invalid parent"));
|
||||
case create_node_error::FILE_PARENT: return cbk(dto::Responses::get_badreq_res("Parent is file"));
|
||||
}
|
||||
else {
|
||||
auto tuple = std::get<std::tuple<bool, uint64_t>>(new_node);
|
||||
cbk(dto::Responses::get_node_exists_res(std::get<1>(tuple), std::get<0>(tuple)));
|
||||
}
|
||||
} catch (const std::exception&) {
|
||||
cbk(dto::Responses::get_badreq_res("Validation error"));
|
||||
}
|
||||
@@ -152,12 +368,27 @@ namespace api {
|
||||
else if (inode->getValueOfParentId() == 0)
|
||||
cbk(dto::Responses::get_badreq_res("Can't delete root"));
|
||||
else {
|
||||
delete_node(*inode);
|
||||
cbk(dto::Responses::get_success_res());
|
||||
auto chan = std::make_shared<msd::channel<std::string>>();
|
||||
std::string("Waiting in queue...\n") >> (*chan);
|
||||
get_delete_loop()->queueInLoop([chan, inode=*inode]{
|
||||
delete_node(inode, *chan);
|
||||
chan->close();
|
||||
});
|
||||
cbk(drogon::HttpResponse::newStreamResponse([chan](char* buf, std::size_t size) -> std::size_t{
|
||||
if (buf == nullptr) return 0;
|
||||
if (chan->closed() && chan->empty()) return 0;
|
||||
std::string buffer;
|
||||
buffer << *chan;
|
||||
if (buffer.empty()) return 0;
|
||||
std::size_t read = std::min(size, buffer.size());
|
||||
std::memcpy(buf, buffer.data(), read); // NOLINT(bugprone-not-null-terminated-result)
|
||||
return read;
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
void fs::upload(req_type req, cbk_type cbk, uint64_t node) {
|
||||
constexpr int image_height = 256;
|
||||
db::User user = dto::get_user(req);
|
||||
|
||||
auto inode = get_node_and_validate(user, node);
|
||||
@@ -178,13 +409,73 @@ namespace api {
|
||||
p /= std::to_string(inode->getValueOfId());
|
||||
|
||||
file.saveAs(p.string());
|
||||
try {
|
||||
if (file.fileLength() > 100 * 1024 * 1024) throw std::exception();
|
||||
std::filesystem::path filename(inode->getValueOfName());
|
||||
const std::string& mime = mime_type_map.at(filename.extension().string());
|
||||
if (!mime.starts_with("image")) throw std::exception();
|
||||
|
||||
cv::_InputArray image_arr(file.fileData(), (int) file.fileLength());
|
||||
cv::Mat image = cv::imdecode(image_arr, cv::IMREAD_COLOR);
|
||||
if (!image.empty()) {
|
||||
float h_ration = ((float) image_height) / ((float) image.rows);
|
||||
cv::Mat preview;
|
||||
cv::resize(image, preview, cv::Size((int) (((float) image.cols) * h_ration), image_height), 0, 0, cv::INTER_AREA);
|
||||
cv::imwrite(p.string() + "_preview.png", preview);
|
||||
inode->setHasPreview(1);
|
||||
}
|
||||
} catch (const std::exception&) {}
|
||||
inode->setSize(file.fileLength());
|
||||
|
||||
db::MapperInode inode_mapper(drogon::app().getDbClient());
|
||||
inode_mapper.update(*inode);
|
||||
|
||||
cbk(dto::Responses::get_success_res());
|
||||
}
|
||||
|
||||
void fs::create_zip(req_type req, cbk_type cbk) {
|
||||
db::User user = dto::get_user(req);
|
||||
Json::Value& json = *req->jsonObject();
|
||||
try {
|
||||
if (!json.isMember("nodes")) throw std::exception();
|
||||
Json::Value node_arr = json["nodes"];
|
||||
if (!node_arr.isArray()) throw std::exception();
|
||||
std::vector<uint64_t> node_ids;
|
||||
for (const auto& node : node_arr)
|
||||
node_ids.push_back(node.asUInt64());
|
||||
|
||||
std::vector<db::INode> nodes;
|
||||
std::transform(node_ids.begin(), node_ids.end(), std::back_inserter(nodes), [&user](uint64_t node) {
|
||||
return api::fs::get_node_and_validate(user, node).value();
|
||||
});
|
||||
|
||||
std::string key = join_string(node_ids.begin(), node_ids.end());
|
||||
|
||||
if (zip_to_temp_map.contains(key)) return cbk(dto::Responses::get_create_zip_done_res());
|
||||
if (in_progress_zips.contains(key)) {
|
||||
auto progress = in_progress_zips.at(key);
|
||||
return cbk(dto::Responses::get_create_zip_done_res(std::get<1>(progress), std::get<2>(progress)));
|
||||
}
|
||||
uint64_t size = 0;
|
||||
for (const auto& node : nodes) size += calc_total_size(node);
|
||||
std::string file_name = "./temp/fs_" + std::to_string(next_temp_id++) + ".zip";
|
||||
in_progress_zips.emplace(key, std::make_tuple(file_name, 0, size));
|
||||
get_zip_loop()->queueInLoop([key = std::move(key), nodes = std::move(nodes), file_name = std::move(file_name)]{
|
||||
{
|
||||
struct zip_t* zip = zip_open(file_name.c_str(), ZIP_DEFAULT_COMPRESSION_LEVEL, 'w');
|
||||
for (const db::INode& node : nodes)
|
||||
add_to_zip(zip, key, node, "");
|
||||
zip_close(zip);
|
||||
}
|
||||
zip_to_temp_map.emplace(key, file_name);
|
||||
in_progress_zips.erase(key);
|
||||
});
|
||||
return cbk(dto::Responses::get_create_zip_done_res(0, size));
|
||||
} catch (const std::exception&) {
|
||||
cbk(dto::Responses::get_badreq_res("Validation error"));
|
||||
}
|
||||
}
|
||||
|
||||
void fs::download(req_type req, cbk_type cbk) {
|
||||
db::User user = dto::get_user(req);
|
||||
|
||||
@@ -193,19 +484,114 @@ namespace api {
|
||||
cbk(dto::Responses::get_badreq_res("Invalid node"));
|
||||
return;
|
||||
}
|
||||
auto inode = get_node_and_validate(user, *node_id);
|
||||
auto inode = get_node_and_validate(user, *node_id);
|
||||
if (!inode.has_value()) {
|
||||
cbk(dto::Responses::get_badreq_res("Invalid node"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (inode->getValueOfIsFile() != 0) {
|
||||
std::filesystem::path p("./files");
|
||||
p /= std::to_string(inode->getValueOfId());
|
||||
|
||||
cbk(drogon::HttpResponse::newFileResponse(
|
||||
p.string(),
|
||||
inode->getValueOfName()
|
||||
));
|
||||
} else {
|
||||
try {
|
||||
std::string key = std::to_string(inode->getValueOfId());
|
||||
std::string file = zip_to_temp_map.at(key);
|
||||
zip_to_temp_map.erase(key);
|
||||
cbk(drogon::HttpResponse::newFileResponse(
|
||||
file,
|
||||
inode->getValueOfName() + ".zip"
|
||||
));
|
||||
} catch (const std::exception&) {
|
||||
cbk(dto::Responses::get_badreq_res("Invalid node"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void fs::download_multi(req_type req, cbk_type cbk) {
|
||||
db::User user = dto::get_user(req);
|
||||
|
||||
auto node_ids_str = req->getOptionalParameter<std::string>("id");
|
||||
if (!node_ids_str.has_value())
|
||||
return cbk(dto::Responses::get_badreq_res("No nodes"));
|
||||
|
||||
std::stringstream node_ids_ss(*node_ids_str);
|
||||
std::string temp;
|
||||
try {
|
||||
while (std::getline(node_ids_ss, temp, ','))
|
||||
if (!get_node_and_validate(user, std::stoull(temp)).has_value()) throw std::exception();
|
||||
|
||||
std::string file = zip_to_temp_map.at(*node_ids_str);
|
||||
zip_to_temp_map.erase(*node_ids_str);
|
||||
cbk(drogon::HttpResponse::newFileResponse(
|
||||
file,
|
||||
"files.zip"
|
||||
));
|
||||
} catch (const std::exception&) {
|
||||
cbk(dto::Responses::get_badreq_res("Invalid nodes"));
|
||||
}
|
||||
}
|
||||
|
||||
void fs::download_preview(req_type req, cbk_type cbk, uint64_t node) {
|
||||
db::User user = dto::get_user(req);
|
||||
|
||||
auto inode = get_node_and_validate(user, node);
|
||||
if (!inode.has_value())
|
||||
return cbk(dto::Responses::get_badreq_res("Unknown node"));
|
||||
if (inode->getValueOfHasPreview() == 0)
|
||||
return cbk(dto::Responses::get_badreq_res("No preview"));
|
||||
|
||||
std::filesystem::path p("./files");
|
||||
p /= std::to_string(inode->getValueOfId()) + "_preview.png";
|
||||
std::ifstream file(p, std::ios::in | std::ios::binary);
|
||||
std::vector<uint8_t> image((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
||||
|
||||
cbk(dto::Responses::get_download_base64_res("data:image/png;base64," + Botan::base64_encode(image)));
|
||||
}
|
||||
|
||||
void fs::download_base64(req_type req, cbk_type cbk, uint64_t node) {
|
||||
db::User user = dto::get_user(req);
|
||||
|
||||
auto inode = get_node_and_validate(user, node);
|
||||
if (!inode.has_value())
|
||||
return cbk(dto::Responses::get_badreq_res("Unknown node"));
|
||||
|
||||
|
||||
std::filesystem::path p("./files"), name(inode->getValueOfName());
|
||||
p /= std::to_string(inode->getValueOfId());
|
||||
|
||||
cbk(drogon::HttpResponse::newFileResponse(
|
||||
p.string(),
|
||||
inode->getValueOfName()
|
||||
));
|
||||
try {
|
||||
std::string mime = mime_type_map.at(name.extension().string());
|
||||
std::ifstream file(p, std::ios::in | std::ios::binary);
|
||||
std::vector<uint8_t> content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
||||
|
||||
cbk(dto::Responses::get_download_base64_res("data:" + mime + ";base64," + Botan::base64_encode(content)));
|
||||
} catch (const std::exception&) {
|
||||
cbk(dto::Responses::get_badreq_res("Invalid file type"));
|
||||
}
|
||||
}
|
||||
|
||||
void fs::get_type(req_type req, cbk_type cbk, uint64_t node){
|
||||
db::User user = dto::get_user(req);
|
||||
|
||||
auto inode = get_node_and_validate(user, node);
|
||||
if (!inode.has_value())
|
||||
return cbk(dto::Responses::get_badreq_res("Unknown node"));
|
||||
|
||||
|
||||
std::filesystem::path p("./files"), name(inode->getValueOfName());
|
||||
p /= std::to_string(inode->getValueOfId());
|
||||
|
||||
try {
|
||||
cbk(dto::Responses::get_type_res(mime_type_map.at(name.extension().string())));
|
||||
} catch (const std::exception&) {
|
||||
cbk(dto::Responses::get_badreq_res("Invalid file type"));
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma clang diagnostic pop
|
||||
@@ -18,9 +18,10 @@ namespace api {
|
||||
void user::delete_user(req_type req, cbk_type cbk) {
|
||||
db::MapperUser user_mapper(drogon::app().getDbClient());
|
||||
|
||||
msd::channel<std::string> chan;
|
||||
db::User user = dto::get_user(req);
|
||||
auth::revoke_all(user);
|
||||
fs::delete_node((fs::get_node(user.getValueOfRootId())).value(), true);
|
||||
fs::delete_node((fs::get_node(user.getValueOfRootId())).value(), chan, true);
|
||||
user_mapper.deleteOne(user);
|
||||
|
||||
cbk(dto::Responses::get_success_res());
|
||||
|
||||
@@ -6,9 +6,9 @@
|
||||
#include <drogon/utils/coroutine.h>
|
||||
#include <drogon/drogon.h>
|
||||
|
||||
#include "model/Inode.h"
|
||||
#include "model/Tokens.h"
|
||||
#include "model/User.h"
|
||||
#include "Inode.h"
|
||||
#include "Tokens.h"
|
||||
#include "User.h"
|
||||
|
||||
const std::string jwt_secret = "CUM";
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,275 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* Inode.h
|
||||
* DO NOT EDIT. This file is generated by drogon_ctl
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <drogon/orm/Result.h>
|
||||
#include <drogon/orm/Row.h>
|
||||
#include <drogon/orm/Field.h>
|
||||
#include <drogon/orm/SqlBinder.h>
|
||||
#include <drogon/orm/Mapper.h>
|
||||
#ifdef __cpp_impl_coroutine
|
||||
#include <drogon/orm/CoroMapper.h>
|
||||
#endif
|
||||
#include <trantor/utils/Date.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include <json/json.h>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <tuple>
|
||||
#include <stdint.h>
|
||||
#include <iostream>
|
||||
|
||||
namespace drogon
|
||||
{
|
||||
namespace orm
|
||||
{
|
||||
class DbClient;
|
||||
using DbClientPtr = std::shared_ptr<DbClient>;
|
||||
}
|
||||
}
|
||||
namespace drogon_model
|
||||
{
|
||||
namespace sqlite3
|
||||
{
|
||||
|
||||
class Inode
|
||||
{
|
||||
public:
|
||||
struct Cols
|
||||
{
|
||||
static const std::string _id;
|
||||
static const std::string _is_file;
|
||||
static const std::string _name;
|
||||
static const std::string _parent_id;
|
||||
static const std::string _owner_id;
|
||||
static const std::string _size;
|
||||
};
|
||||
|
||||
const static int primaryKeyNumber;
|
||||
const static std::string tableName;
|
||||
const static bool hasPrimaryKey;
|
||||
const static std::string primaryKeyName;
|
||||
using PrimaryKeyType = uint64_t;
|
||||
const PrimaryKeyType &getPrimaryKey() const;
|
||||
|
||||
/**
|
||||
* @brief constructor
|
||||
* @param r One row of records in the SQL query result.
|
||||
* @param indexOffset Set the offset to -1 to access all columns by column names,
|
||||
* otherwise access all columns by offsets.
|
||||
* @note If the SQL is not a style of 'select * from table_name ...' (select all
|
||||
* columns by an asterisk), please set the offset to -1.
|
||||
*/
|
||||
explicit Inode(const drogon::orm::Row &r, const ssize_t indexOffset = 0) noexcept;
|
||||
|
||||
/**
|
||||
* @brief constructor
|
||||
* @param pJson The json object to construct a new instance.
|
||||
*/
|
||||
explicit Inode(const Json::Value &pJson) noexcept(false);
|
||||
|
||||
/**
|
||||
* @brief constructor
|
||||
* @param pJson The json object to construct a new instance.
|
||||
* @param pMasqueradingVector The aliases of table columns.
|
||||
*/
|
||||
Inode(const Json::Value &pJson, const std::vector<std::string> &pMasqueradingVector) noexcept(false);
|
||||
|
||||
Inode() = default;
|
||||
|
||||
void updateByJson(const Json::Value &pJson) noexcept(false);
|
||||
void updateByMasqueradedJson(const Json::Value &pJson,
|
||||
const std::vector<std::string> &pMasqueradingVector) noexcept(false);
|
||||
static bool validateJsonForCreation(const Json::Value &pJson, std::string &err);
|
||||
static bool validateMasqueradedJsonForCreation(const Json::Value &,
|
||||
const std::vector<std::string> &pMasqueradingVector,
|
||||
std::string &err);
|
||||
static bool validateJsonForUpdate(const Json::Value &pJson, std::string &err);
|
||||
static bool validateMasqueradedJsonForUpdate(const Json::Value &,
|
||||
const std::vector<std::string> &pMasqueradingVector,
|
||||
std::string &err);
|
||||
static bool validJsonOfField(size_t index,
|
||||
const std::string &fieldName,
|
||||
const Json::Value &pJson,
|
||||
std::string &err,
|
||||
bool isForCreation);
|
||||
|
||||
/** For column id */
|
||||
///Get the value of the column id, returns the default value if the column is null
|
||||
const uint64_t &getValueOfId() const noexcept;
|
||||
///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null
|
||||
const std::shared_ptr<uint64_t> &getId() const noexcept;
|
||||
///Set the value of the column id
|
||||
void setId(const uint64_t &pId) noexcept;
|
||||
|
||||
/** For column is_file */
|
||||
///Get the value of the column is_file, returns the default value if the column is null
|
||||
const uint64_t &getValueOfIsFile() const noexcept;
|
||||
///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null
|
||||
const std::shared_ptr<uint64_t> &getIsFile() const noexcept;
|
||||
///Set the value of the column is_file
|
||||
void setIsFile(const uint64_t &pIsFile) noexcept;
|
||||
|
||||
/** For column name */
|
||||
///Get the value of the column name, returns the default value if the column is null
|
||||
const std::string &getValueOfName() const noexcept;
|
||||
///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null
|
||||
const std::shared_ptr<std::string> &getName() const noexcept;
|
||||
///Set the value of the column name
|
||||
void setName(const std::string &pName) noexcept;
|
||||
void setName(std::string &&pName) noexcept;
|
||||
void setNameToNull() noexcept;
|
||||
|
||||
/** For column parent_id */
|
||||
///Get the value of the column parent_id, returns the default value if the column is null
|
||||
const uint64_t &getValueOfParentId() const noexcept;
|
||||
///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null
|
||||
const std::shared_ptr<uint64_t> &getParentId() const noexcept;
|
||||
///Set the value of the column parent_id
|
||||
void setParentId(const uint64_t &pParentId) noexcept;
|
||||
void setParentIdToNull() noexcept;
|
||||
|
||||
/** For column owner_id */
|
||||
///Get the value of the column owner_id, returns the default value if the column is null
|
||||
const uint64_t &getValueOfOwnerId() const noexcept;
|
||||
///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null
|
||||
const std::shared_ptr<uint64_t> &getOwnerId() const noexcept;
|
||||
///Set the value of the column owner_id
|
||||
void setOwnerId(const uint64_t &pOwnerId) noexcept;
|
||||
|
||||
/** For column size */
|
||||
///Get the value of the column size, returns the default value if the column is null
|
||||
const uint64_t &getValueOfSize() const noexcept;
|
||||
///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null
|
||||
const std::shared_ptr<uint64_t> &getSize() const noexcept;
|
||||
///Set the value of the column size
|
||||
void setSize(const uint64_t &pSize) noexcept;
|
||||
void setSizeToNull() noexcept;
|
||||
|
||||
|
||||
static size_t getColumnNumber() noexcept { return 6; }
|
||||
static const std::string &getColumnName(size_t index) noexcept(false);
|
||||
|
||||
Json::Value toJson() const;
|
||||
Json::Value toMasqueradedJson(const std::vector<std::string> &pMasqueradingVector) const;
|
||||
/// Relationship interfaces
|
||||
private:
|
||||
friend drogon::orm::Mapper<Inode>;
|
||||
#ifdef __cpp_impl_coroutine
|
||||
friend drogon::orm::CoroMapper<Inode>;
|
||||
#endif
|
||||
static const std::vector<std::string> &insertColumns() noexcept;
|
||||
void outputArgs(drogon::orm::internal::SqlBinder &binder) const;
|
||||
const std::vector<std::string> updateColumns() const;
|
||||
void updateArgs(drogon::orm::internal::SqlBinder &binder) const;
|
||||
///For mysql or sqlite3
|
||||
void updateId(const uint64_t id);
|
||||
std::shared_ptr<uint64_t> id_;
|
||||
std::shared_ptr<uint64_t> isFile_;
|
||||
std::shared_ptr<std::string> name_;
|
||||
std::shared_ptr<uint64_t> parentId_;
|
||||
std::shared_ptr<uint64_t> ownerId_;
|
||||
std::shared_ptr<uint64_t> size_;
|
||||
struct MetaData
|
||||
{
|
||||
const std::string colName_;
|
||||
const std::string colType_;
|
||||
const std::string colDatabaseType_;
|
||||
const ssize_t colLength_;
|
||||
const bool isAutoVal_;
|
||||
const bool isPrimaryKey_;
|
||||
const bool notNull_;
|
||||
};
|
||||
static const std::vector<MetaData> metaData_;
|
||||
bool dirtyFlag_[6]={ false };
|
||||
public:
|
||||
static const std::string &sqlForFindingByPrimaryKey()
|
||||
{
|
||||
static const std::string sql="select * from " + tableName + " where id = ?";
|
||||
return sql;
|
||||
}
|
||||
|
||||
static const std::string &sqlForDeletingByPrimaryKey()
|
||||
{
|
||||
static const std::string sql="delete from " + tableName + " where id = ?";
|
||||
return sql;
|
||||
}
|
||||
std::string sqlForInserting(bool &needSelection) const
|
||||
{
|
||||
std::string sql="insert into " + tableName + " (";
|
||||
size_t parametersCount = 0;
|
||||
needSelection = false;
|
||||
if(dirtyFlag_[1])
|
||||
{
|
||||
sql += "is_file,";
|
||||
++parametersCount;
|
||||
}
|
||||
if(dirtyFlag_[2])
|
||||
{
|
||||
sql += "name,";
|
||||
++parametersCount;
|
||||
}
|
||||
if(dirtyFlag_[3])
|
||||
{
|
||||
sql += "parent_id,";
|
||||
++parametersCount;
|
||||
}
|
||||
if(dirtyFlag_[4])
|
||||
{
|
||||
sql += "owner_id,";
|
||||
++parametersCount;
|
||||
}
|
||||
if(dirtyFlag_[5])
|
||||
{
|
||||
sql += "size,";
|
||||
++parametersCount;
|
||||
}
|
||||
if(parametersCount > 0)
|
||||
{
|
||||
sql[sql.length()-1]=')';
|
||||
sql += " values (";
|
||||
}
|
||||
else
|
||||
sql += ") values (";
|
||||
|
||||
if(dirtyFlag_[1])
|
||||
{
|
||||
sql.append("?,");
|
||||
|
||||
}
|
||||
if(dirtyFlag_[2])
|
||||
{
|
||||
sql.append("?,");
|
||||
|
||||
}
|
||||
if(dirtyFlag_[3])
|
||||
{
|
||||
sql.append("?,");
|
||||
|
||||
}
|
||||
if(dirtyFlag_[4])
|
||||
{
|
||||
sql.append("?,");
|
||||
|
||||
}
|
||||
if(dirtyFlag_[5])
|
||||
{
|
||||
sql.append("?,");
|
||||
|
||||
}
|
||||
if(parametersCount > 0)
|
||||
{
|
||||
sql.resize(sql.length() - 1);
|
||||
}
|
||||
sql.append(1, ')');
|
||||
LOG_TRACE << sql;
|
||||
return sql;
|
||||
}
|
||||
};
|
||||
} // namespace sqlite3
|
||||
} // namespace drogon_model
|
||||
@@ -1,631 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* Tokens.cc
|
||||
* DO NOT EDIT. This file is generated by drogon_ctl
|
||||
*
|
||||
*/
|
||||
|
||||
#include "Tokens.h"
|
||||
#include <drogon/utils/Utilities.h>
|
||||
#include <string>
|
||||
|
||||
using namespace drogon;
|
||||
using namespace drogon::orm;
|
||||
using namespace drogon_model::sqlite3;
|
||||
|
||||
const std::string Tokens::Cols::_id = "id";
|
||||
const std::string Tokens::Cols::_owner_id = "owner_id";
|
||||
const std::string Tokens::Cols::_exp = "exp";
|
||||
const std::string Tokens::primaryKeyName = "id";
|
||||
const bool Tokens::hasPrimaryKey = true;
|
||||
const std::string Tokens::tableName = "tokens";
|
||||
|
||||
const std::vector<typename Tokens::MetaData> Tokens::metaData_={
|
||||
{"id","uint64_t","integer",8,1,1,1},
|
||||
{"owner_id","uint64_t","integer",8,0,0,1},
|
||||
{"exp","uint64_t","integer",8,0,0,1}
|
||||
};
|
||||
const std::string &Tokens::getColumnName(size_t index) noexcept(false)
|
||||
{
|
||||
assert(index < metaData_.size());
|
||||
return metaData_[index].colName_;
|
||||
}
|
||||
Tokens::Tokens(const Row &r, const ssize_t indexOffset) noexcept
|
||||
{
|
||||
if(indexOffset < 0)
|
||||
{
|
||||
if(!r["id"].isNull())
|
||||
{
|
||||
id_=std::make_shared<uint64_t>(r["id"].as<uint64_t>());
|
||||
}
|
||||
if(!r["owner_id"].isNull())
|
||||
{
|
||||
ownerId_=std::make_shared<uint64_t>(r["owner_id"].as<uint64_t>());
|
||||
}
|
||||
if(!r["exp"].isNull())
|
||||
{
|
||||
exp_=std::make_shared<uint64_t>(r["exp"].as<uint64_t>());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
size_t offset = (size_t)indexOffset;
|
||||
if(offset + 3 > r.size())
|
||||
{
|
||||
LOG_FATAL << "Invalid SQL result for this model";
|
||||
return;
|
||||
}
|
||||
size_t index;
|
||||
index = offset + 0;
|
||||
if(!r[index].isNull())
|
||||
{
|
||||
id_=std::make_shared<uint64_t>(r[index].as<uint64_t>());
|
||||
}
|
||||
index = offset + 1;
|
||||
if(!r[index].isNull())
|
||||
{
|
||||
ownerId_=std::make_shared<uint64_t>(r[index].as<uint64_t>());
|
||||
}
|
||||
index = offset + 2;
|
||||
if(!r[index].isNull())
|
||||
{
|
||||
exp_=std::make_shared<uint64_t>(r[index].as<uint64_t>());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Tokens::Tokens(const Json::Value &pJson, const std::vector<std::string> &pMasqueradingVector) noexcept(false)
|
||||
{
|
||||
if(pMasqueradingVector.size() != 3)
|
||||
{
|
||||
LOG_ERROR << "Bad masquerading vector";
|
||||
return;
|
||||
}
|
||||
if(!pMasqueradingVector[0].empty() && pJson.isMember(pMasqueradingVector[0]))
|
||||
{
|
||||
dirtyFlag_[0] = true;
|
||||
if(!pJson[pMasqueradingVector[0]].isNull())
|
||||
{
|
||||
id_=std::make_shared<uint64_t>((uint64_t)pJson[pMasqueradingVector[0]].asUInt64());
|
||||
}
|
||||
}
|
||||
if(!pMasqueradingVector[1].empty() && pJson.isMember(pMasqueradingVector[1]))
|
||||
{
|
||||
dirtyFlag_[1] = true;
|
||||
if(!pJson[pMasqueradingVector[1]].isNull())
|
||||
{
|
||||
ownerId_=std::make_shared<uint64_t>((uint64_t)pJson[pMasqueradingVector[1]].asUInt64());
|
||||
}
|
||||
}
|
||||
if(!pMasqueradingVector[2].empty() && pJson.isMember(pMasqueradingVector[2]))
|
||||
{
|
||||
dirtyFlag_[2] = true;
|
||||
if(!pJson[pMasqueradingVector[2]].isNull())
|
||||
{
|
||||
exp_=std::make_shared<uint64_t>((uint64_t)pJson[pMasqueradingVector[2]].asUInt64());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Tokens::Tokens(const Json::Value &pJson) noexcept(false)
|
||||
{
|
||||
if(pJson.isMember("id"))
|
||||
{
|
||||
dirtyFlag_[0]=true;
|
||||
if(!pJson["id"].isNull())
|
||||
{
|
||||
id_=std::make_shared<uint64_t>((uint64_t)pJson["id"].asUInt64());
|
||||
}
|
||||
}
|
||||
if(pJson.isMember("owner_id"))
|
||||
{
|
||||
dirtyFlag_[1]=true;
|
||||
if(!pJson["owner_id"].isNull())
|
||||
{
|
||||
ownerId_=std::make_shared<uint64_t>((uint64_t)pJson["owner_id"].asUInt64());
|
||||
}
|
||||
}
|
||||
if(pJson.isMember("exp"))
|
||||
{
|
||||
dirtyFlag_[2]=true;
|
||||
if(!pJson["exp"].isNull())
|
||||
{
|
||||
exp_=std::make_shared<uint64_t>((uint64_t)pJson["exp"].asUInt64());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Tokens::updateByMasqueradedJson(const Json::Value &pJson,
|
||||
const std::vector<std::string> &pMasqueradingVector) noexcept(false)
|
||||
{
|
||||
if(pMasqueradingVector.size() != 3)
|
||||
{
|
||||
LOG_ERROR << "Bad masquerading vector";
|
||||
return;
|
||||
}
|
||||
if(!pMasqueradingVector[0].empty() && pJson.isMember(pMasqueradingVector[0]))
|
||||
{
|
||||
if(!pJson[pMasqueradingVector[0]].isNull())
|
||||
{
|
||||
id_=std::make_shared<uint64_t>((uint64_t)pJson[pMasqueradingVector[0]].asUInt64());
|
||||
}
|
||||
}
|
||||
if(!pMasqueradingVector[1].empty() && pJson.isMember(pMasqueradingVector[1]))
|
||||
{
|
||||
dirtyFlag_[1] = true;
|
||||
if(!pJson[pMasqueradingVector[1]].isNull())
|
||||
{
|
||||
ownerId_=std::make_shared<uint64_t>((uint64_t)pJson[pMasqueradingVector[1]].asUInt64());
|
||||
}
|
||||
}
|
||||
if(!pMasqueradingVector[2].empty() && pJson.isMember(pMasqueradingVector[2]))
|
||||
{
|
||||
dirtyFlag_[2] = true;
|
||||
if(!pJson[pMasqueradingVector[2]].isNull())
|
||||
{
|
||||
exp_=std::make_shared<uint64_t>((uint64_t)pJson[pMasqueradingVector[2]].asUInt64());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Tokens::updateByJson(const Json::Value &pJson) noexcept(false)
|
||||
{
|
||||
if(pJson.isMember("id"))
|
||||
{
|
||||
if(!pJson["id"].isNull())
|
||||
{
|
||||
id_=std::make_shared<uint64_t>((uint64_t)pJson["id"].asUInt64());
|
||||
}
|
||||
}
|
||||
if(pJson.isMember("owner_id"))
|
||||
{
|
||||
dirtyFlag_[1] = true;
|
||||
if(!pJson["owner_id"].isNull())
|
||||
{
|
||||
ownerId_=std::make_shared<uint64_t>((uint64_t)pJson["owner_id"].asUInt64());
|
||||
}
|
||||
}
|
||||
if(pJson.isMember("exp"))
|
||||
{
|
||||
dirtyFlag_[2] = true;
|
||||
if(!pJson["exp"].isNull())
|
||||
{
|
||||
exp_=std::make_shared<uint64_t>((uint64_t)pJson["exp"].asUInt64());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const uint64_t &Tokens::getValueOfId() const noexcept
|
||||
{
|
||||
const static uint64_t defaultValue = uint64_t();
|
||||
if(id_)
|
||||
return *id_;
|
||||
return defaultValue;
|
||||
}
|
||||
const std::shared_ptr<uint64_t> &Tokens::getId() const noexcept
|
||||
{
|
||||
return id_;
|
||||
}
|
||||
void Tokens::setId(const uint64_t &pId) noexcept
|
||||
{
|
||||
id_ = std::make_shared<uint64_t>(pId);
|
||||
dirtyFlag_[0] = true;
|
||||
}
|
||||
const typename Tokens::PrimaryKeyType & Tokens::getPrimaryKey() const
|
||||
{
|
||||
assert(id_);
|
||||
return *id_;
|
||||
}
|
||||
|
||||
const uint64_t &Tokens::getValueOfOwnerId() const noexcept
|
||||
{
|
||||
const static uint64_t defaultValue = uint64_t();
|
||||
if(ownerId_)
|
||||
return *ownerId_;
|
||||
return defaultValue;
|
||||
}
|
||||
const std::shared_ptr<uint64_t> &Tokens::getOwnerId() const noexcept
|
||||
{
|
||||
return ownerId_;
|
||||
}
|
||||
void Tokens::setOwnerId(const uint64_t &pOwnerId) noexcept
|
||||
{
|
||||
ownerId_ = std::make_shared<uint64_t>(pOwnerId);
|
||||
dirtyFlag_[1] = true;
|
||||
}
|
||||
|
||||
const uint64_t &Tokens::getValueOfExp() const noexcept
|
||||
{
|
||||
const static uint64_t defaultValue = uint64_t();
|
||||
if(exp_)
|
||||
return *exp_;
|
||||
return defaultValue;
|
||||
}
|
||||
const std::shared_ptr<uint64_t> &Tokens::getExp() const noexcept
|
||||
{
|
||||
return exp_;
|
||||
}
|
||||
void Tokens::setExp(const uint64_t &pExp) noexcept
|
||||
{
|
||||
exp_ = std::make_shared<uint64_t>(pExp);
|
||||
dirtyFlag_[2] = true;
|
||||
}
|
||||
|
||||
void Tokens::updateId(const uint64_t id)
|
||||
{
|
||||
id_ = std::make_shared<uint64_t>(id);
|
||||
}
|
||||
|
||||
const std::vector<std::string> &Tokens::insertColumns() noexcept
|
||||
{
|
||||
static const std::vector<std::string> inCols={
|
||||
"owner_id",
|
||||
"exp"
|
||||
};
|
||||
return inCols;
|
||||
}
|
||||
|
||||
void Tokens::outputArgs(drogon::orm::internal::SqlBinder &binder) const
|
||||
{
|
||||
if(dirtyFlag_[1])
|
||||
{
|
||||
if(getOwnerId())
|
||||
{
|
||||
binder << getValueOfOwnerId();
|
||||
}
|
||||
else
|
||||
{
|
||||
binder << nullptr;
|
||||
}
|
||||
}
|
||||
if(dirtyFlag_[2])
|
||||
{
|
||||
if(getExp())
|
||||
{
|
||||
binder << getValueOfExp();
|
||||
}
|
||||
else
|
||||
{
|
||||
binder << nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const std::vector<std::string> Tokens::updateColumns() const
|
||||
{
|
||||
std::vector<std::string> ret;
|
||||
if(dirtyFlag_[1])
|
||||
{
|
||||
ret.push_back(getColumnName(1));
|
||||
}
|
||||
if(dirtyFlag_[2])
|
||||
{
|
||||
ret.push_back(getColumnName(2));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void Tokens::updateArgs(drogon::orm::internal::SqlBinder &binder) const
|
||||
{
|
||||
if(dirtyFlag_[1])
|
||||
{
|
||||
if(getOwnerId())
|
||||
{
|
||||
binder << getValueOfOwnerId();
|
||||
}
|
||||
else
|
||||
{
|
||||
binder << nullptr;
|
||||
}
|
||||
}
|
||||
if(dirtyFlag_[2])
|
||||
{
|
||||
if(getExp())
|
||||
{
|
||||
binder << getValueOfExp();
|
||||
}
|
||||
else
|
||||
{
|
||||
binder << nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
Json::Value Tokens::toJson() const
|
||||
{
|
||||
Json::Value ret;
|
||||
if(getId())
|
||||
{
|
||||
ret["id"]=(Json::UInt64)getValueOfId();
|
||||
}
|
||||
else
|
||||
{
|
||||
ret["id"]=Json::Value();
|
||||
}
|
||||
if(getOwnerId())
|
||||
{
|
||||
ret["owner_id"]=(Json::UInt64)getValueOfOwnerId();
|
||||
}
|
||||
else
|
||||
{
|
||||
ret["owner_id"]=Json::Value();
|
||||
}
|
||||
if(getExp())
|
||||
{
|
||||
ret["exp"]=(Json::UInt64)getValueOfExp();
|
||||
}
|
||||
else
|
||||
{
|
||||
ret["exp"]=Json::Value();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
Json::Value Tokens::toMasqueradedJson(
|
||||
const std::vector<std::string> &pMasqueradingVector) const
|
||||
{
|
||||
Json::Value ret;
|
||||
if(pMasqueradingVector.size() == 3)
|
||||
{
|
||||
if(!pMasqueradingVector[0].empty())
|
||||
{
|
||||
if(getId())
|
||||
{
|
||||
ret[pMasqueradingVector[0]]=(Json::UInt64)getValueOfId();
|
||||
}
|
||||
else
|
||||
{
|
||||
ret[pMasqueradingVector[0]]=Json::Value();
|
||||
}
|
||||
}
|
||||
if(!pMasqueradingVector[1].empty())
|
||||
{
|
||||
if(getOwnerId())
|
||||
{
|
||||
ret[pMasqueradingVector[1]]=(Json::UInt64)getValueOfOwnerId();
|
||||
}
|
||||
else
|
||||
{
|
||||
ret[pMasqueradingVector[1]]=Json::Value();
|
||||
}
|
||||
}
|
||||
if(!pMasqueradingVector[2].empty())
|
||||
{
|
||||
if(getExp())
|
||||
{
|
||||
ret[pMasqueradingVector[2]]=(Json::UInt64)getValueOfExp();
|
||||
}
|
||||
else
|
||||
{
|
||||
ret[pMasqueradingVector[2]]=Json::Value();
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
LOG_ERROR << "Masquerade failed";
|
||||
if(getId())
|
||||
{
|
||||
ret["id"]=(Json::UInt64)getValueOfId();
|
||||
}
|
||||
else
|
||||
{
|
||||
ret["id"]=Json::Value();
|
||||
}
|
||||
if(getOwnerId())
|
||||
{
|
||||
ret["owner_id"]=(Json::UInt64)getValueOfOwnerId();
|
||||
}
|
||||
else
|
||||
{
|
||||
ret["owner_id"]=Json::Value();
|
||||
}
|
||||
if(getExp())
|
||||
{
|
||||
ret["exp"]=(Json::UInt64)getValueOfExp();
|
||||
}
|
||||
else
|
||||
{
|
||||
ret["exp"]=Json::Value();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool Tokens::validateJsonForCreation(const Json::Value &pJson, std::string &err)
|
||||
{
|
||||
if(pJson.isMember("id"))
|
||||
{
|
||||
if(!validJsonOfField(0, "id", pJson["id"], err, true))
|
||||
return false;
|
||||
}
|
||||
if(pJson.isMember("owner_id"))
|
||||
{
|
||||
if(!validJsonOfField(1, "owner_id", pJson["owner_id"], err, true))
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
err="The owner_id column cannot be null";
|
||||
return false;
|
||||
}
|
||||
if(pJson.isMember("exp"))
|
||||
{
|
||||
if(!validJsonOfField(2, "exp", pJson["exp"], err, true))
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
err="The exp column cannot be null";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool Tokens::validateMasqueradedJsonForCreation(const Json::Value &pJson,
|
||||
const std::vector<std::string> &pMasqueradingVector,
|
||||
std::string &err)
|
||||
{
|
||||
if(pMasqueradingVector.size() != 3)
|
||||
{
|
||||
err = "Bad masquerading vector";
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
if(!pMasqueradingVector[0].empty())
|
||||
{
|
||||
if(pJson.isMember(pMasqueradingVector[0]))
|
||||
{
|
||||
if(!validJsonOfField(0, pMasqueradingVector[0], pJson[pMasqueradingVector[0]], err, true))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if(!pMasqueradingVector[1].empty())
|
||||
{
|
||||
if(pJson.isMember(pMasqueradingVector[1]))
|
||||
{
|
||||
if(!validJsonOfField(1, pMasqueradingVector[1], pJson[pMasqueradingVector[1]], err, true))
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
err="The " + pMasqueradingVector[1] + " column cannot be null";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if(!pMasqueradingVector[2].empty())
|
||||
{
|
||||
if(pJson.isMember(pMasqueradingVector[2]))
|
||||
{
|
||||
if(!validJsonOfField(2, pMasqueradingVector[2], pJson[pMasqueradingVector[2]], err, true))
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
err="The " + pMasqueradingVector[2] + " column cannot be null";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(const Json::LogicError &e)
|
||||
{
|
||||
err = e.what();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool Tokens::validateJsonForUpdate(const Json::Value &pJson, std::string &err)
|
||||
{
|
||||
if(pJson.isMember("id"))
|
||||
{
|
||||
if(!validJsonOfField(0, "id", pJson["id"], err, false))
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
err = "The value of primary key must be set in the json object for update";
|
||||
return false;
|
||||
}
|
||||
if(pJson.isMember("owner_id"))
|
||||
{
|
||||
if(!validJsonOfField(1, "owner_id", pJson["owner_id"], err, false))
|
||||
return false;
|
||||
}
|
||||
if(pJson.isMember("exp"))
|
||||
{
|
||||
if(!validJsonOfField(2, "exp", pJson["exp"], err, false))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool Tokens::validateMasqueradedJsonForUpdate(const Json::Value &pJson,
|
||||
const std::vector<std::string> &pMasqueradingVector,
|
||||
std::string &err)
|
||||
{
|
||||
if(pMasqueradingVector.size() != 3)
|
||||
{
|
||||
err = "Bad masquerading vector";
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
if(!pMasqueradingVector[0].empty() && pJson.isMember(pMasqueradingVector[0]))
|
||||
{
|
||||
if(!validJsonOfField(0, pMasqueradingVector[0], pJson[pMasqueradingVector[0]], err, false))
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
err = "The value of primary key must be set in the json object for update";
|
||||
return false;
|
||||
}
|
||||
if(!pMasqueradingVector[1].empty() && pJson.isMember(pMasqueradingVector[1]))
|
||||
{
|
||||
if(!validJsonOfField(1, pMasqueradingVector[1], pJson[pMasqueradingVector[1]], err, false))
|
||||
return false;
|
||||
}
|
||||
if(!pMasqueradingVector[2].empty() && pJson.isMember(pMasqueradingVector[2]))
|
||||
{
|
||||
if(!validJsonOfField(2, pMasqueradingVector[2], pJson[pMasqueradingVector[2]], err, false))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch(const Json::LogicError &e)
|
||||
{
|
||||
err = e.what();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool Tokens::validJsonOfField(size_t index,
|
||||
const std::string &fieldName,
|
||||
const Json::Value &pJson,
|
||||
std::string &err,
|
||||
bool isForCreation)
|
||||
{
|
||||
switch(index)
|
||||
{
|
||||
case 0:
|
||||
if(pJson.isNull())
|
||||
{
|
||||
err="The " + fieldName + " column cannot be null";
|
||||
return false;
|
||||
}
|
||||
if(isForCreation)
|
||||
{
|
||||
err="The automatic primary key cannot be set";
|
||||
return false;
|
||||
}
|
||||
if(!pJson.isUInt64())
|
||||
{
|
||||
err="Type error in the "+fieldName+" field";
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
if(pJson.isNull())
|
||||
{
|
||||
err="The " + fieldName + " column cannot be null";
|
||||
return false;
|
||||
}
|
||||
if(!pJson.isUInt64())
|
||||
{
|
||||
err="Type error in the "+fieldName+" field";
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
if(pJson.isNull())
|
||||
{
|
||||
err="The " + fieldName + " column cannot be null";
|
||||
return false;
|
||||
}
|
||||
if(!pJson.isUInt64())
|
||||
{
|
||||
err="Type error in the "+fieldName+" field";
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
err="Internal error in the server";
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -1,211 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* Tokens.h
|
||||
* DO NOT EDIT. This file is generated by drogon_ctl
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <drogon/orm/Result.h>
|
||||
#include <drogon/orm/Row.h>
|
||||
#include <drogon/orm/Field.h>
|
||||
#include <drogon/orm/SqlBinder.h>
|
||||
#include <drogon/orm/Mapper.h>
|
||||
#ifdef __cpp_impl_coroutine
|
||||
#include <drogon/orm/CoroMapper.h>
|
||||
#endif
|
||||
#include <trantor/utils/Date.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include <json/json.h>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <tuple>
|
||||
#include <stdint.h>
|
||||
#include <iostream>
|
||||
|
||||
namespace drogon
|
||||
{
|
||||
namespace orm
|
||||
{
|
||||
class DbClient;
|
||||
using DbClientPtr = std::shared_ptr<DbClient>;
|
||||
}
|
||||
}
|
||||
namespace drogon_model
|
||||
{
|
||||
namespace sqlite3
|
||||
{
|
||||
|
||||
class Tokens
|
||||
{
|
||||
public:
|
||||
struct Cols
|
||||
{
|
||||
static const std::string _id;
|
||||
static const std::string _owner_id;
|
||||
static const std::string _exp;
|
||||
};
|
||||
|
||||
const static int primaryKeyNumber;
|
||||
const static std::string tableName;
|
||||
const static bool hasPrimaryKey;
|
||||
const static std::string primaryKeyName;
|
||||
using PrimaryKeyType = uint64_t;
|
||||
const PrimaryKeyType &getPrimaryKey() const;
|
||||
|
||||
/**
|
||||
* @brief constructor
|
||||
* @param r One row of records in the SQL query result.
|
||||
* @param indexOffset Set the offset to -1 to access all columns by column names,
|
||||
* otherwise access all columns by offsets.
|
||||
* @note If the SQL is not a style of 'select * from table_name ...' (select all
|
||||
* columns by an asterisk), please set the offset to -1.
|
||||
*/
|
||||
explicit Tokens(const drogon::orm::Row &r, const ssize_t indexOffset = 0) noexcept;
|
||||
|
||||
/**
|
||||
* @brief constructor
|
||||
* @param pJson The json object to construct a new instance.
|
||||
*/
|
||||
explicit Tokens(const Json::Value &pJson) noexcept(false);
|
||||
|
||||
/**
|
||||
* @brief constructor
|
||||
* @param pJson The json object to construct a new instance.
|
||||
* @param pMasqueradingVector The aliases of table columns.
|
||||
*/
|
||||
Tokens(const Json::Value &pJson, const std::vector<std::string> &pMasqueradingVector) noexcept(false);
|
||||
|
||||
Tokens() = default;
|
||||
|
||||
void updateByJson(const Json::Value &pJson) noexcept(false);
|
||||
void updateByMasqueradedJson(const Json::Value &pJson,
|
||||
const std::vector<std::string> &pMasqueradingVector) noexcept(false);
|
||||
static bool validateJsonForCreation(const Json::Value &pJson, std::string &err);
|
||||
static bool validateMasqueradedJsonForCreation(const Json::Value &,
|
||||
const std::vector<std::string> &pMasqueradingVector,
|
||||
std::string &err);
|
||||
static bool validateJsonForUpdate(const Json::Value &pJson, std::string &err);
|
||||
static bool validateMasqueradedJsonForUpdate(const Json::Value &,
|
||||
const std::vector<std::string> &pMasqueradingVector,
|
||||
std::string &err);
|
||||
static bool validJsonOfField(size_t index,
|
||||
const std::string &fieldName,
|
||||
const Json::Value &pJson,
|
||||
std::string &err,
|
||||
bool isForCreation);
|
||||
|
||||
/** For column id */
|
||||
///Get the value of the column id, returns the default value if the column is null
|
||||
const uint64_t &getValueOfId() const noexcept;
|
||||
///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null
|
||||
const std::shared_ptr<uint64_t> &getId() const noexcept;
|
||||
///Set the value of the column id
|
||||
void setId(const uint64_t &pId) noexcept;
|
||||
|
||||
/** For column owner_id */
|
||||
///Get the value of the column owner_id, returns the default value if the column is null
|
||||
const uint64_t &getValueOfOwnerId() const noexcept;
|
||||
///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null
|
||||
const std::shared_ptr<uint64_t> &getOwnerId() const noexcept;
|
||||
///Set the value of the column owner_id
|
||||
void setOwnerId(const uint64_t &pOwnerId) noexcept;
|
||||
|
||||
/** For column exp */
|
||||
///Get the value of the column exp, returns the default value if the column is null
|
||||
const uint64_t &getValueOfExp() const noexcept;
|
||||
///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null
|
||||
const std::shared_ptr<uint64_t> &getExp() const noexcept;
|
||||
///Set the value of the column exp
|
||||
void setExp(const uint64_t &pExp) noexcept;
|
||||
|
||||
|
||||
static size_t getColumnNumber() noexcept { return 3; }
|
||||
static const std::string &getColumnName(size_t index) noexcept(false);
|
||||
|
||||
Json::Value toJson() const;
|
||||
Json::Value toMasqueradedJson(const std::vector<std::string> &pMasqueradingVector) const;
|
||||
/// Relationship interfaces
|
||||
private:
|
||||
friend drogon::orm::Mapper<Tokens>;
|
||||
#ifdef __cpp_impl_coroutine
|
||||
friend drogon::orm::CoroMapper<Tokens>;
|
||||
#endif
|
||||
static const std::vector<std::string> &insertColumns() noexcept;
|
||||
void outputArgs(drogon::orm::internal::SqlBinder &binder) const;
|
||||
const std::vector<std::string> updateColumns() const;
|
||||
void updateArgs(drogon::orm::internal::SqlBinder &binder) const;
|
||||
///For mysql or sqlite3
|
||||
void updateId(const uint64_t id);
|
||||
std::shared_ptr<uint64_t> id_;
|
||||
std::shared_ptr<uint64_t> ownerId_;
|
||||
std::shared_ptr<uint64_t> exp_;
|
||||
struct MetaData
|
||||
{
|
||||
const std::string colName_;
|
||||
const std::string colType_;
|
||||
const std::string colDatabaseType_;
|
||||
const ssize_t colLength_;
|
||||
const bool isAutoVal_;
|
||||
const bool isPrimaryKey_;
|
||||
const bool notNull_;
|
||||
};
|
||||
static const std::vector<MetaData> metaData_;
|
||||
bool dirtyFlag_[3]={ false };
|
||||
public:
|
||||
static const std::string &sqlForFindingByPrimaryKey()
|
||||
{
|
||||
static const std::string sql="select * from " + tableName + " where id = ?";
|
||||
return sql;
|
||||
}
|
||||
|
||||
static const std::string &sqlForDeletingByPrimaryKey()
|
||||
{
|
||||
static const std::string sql="delete from " + tableName + " where id = ?";
|
||||
return sql;
|
||||
}
|
||||
std::string sqlForInserting(bool &needSelection) const
|
||||
{
|
||||
std::string sql="insert into " + tableName + " (";
|
||||
size_t parametersCount = 0;
|
||||
needSelection = false;
|
||||
if(dirtyFlag_[1])
|
||||
{
|
||||
sql += "owner_id,";
|
||||
++parametersCount;
|
||||
}
|
||||
if(dirtyFlag_[2])
|
||||
{
|
||||
sql += "exp,";
|
||||
++parametersCount;
|
||||
}
|
||||
if(parametersCount > 0)
|
||||
{
|
||||
sql[sql.length()-1]=')';
|
||||
sql += " values (";
|
||||
}
|
||||
else
|
||||
sql += ") values (";
|
||||
|
||||
if(dirtyFlag_[1])
|
||||
{
|
||||
sql.append("?,");
|
||||
|
||||
}
|
||||
if(dirtyFlag_[2])
|
||||
{
|
||||
sql.append("?,");
|
||||
|
||||
}
|
||||
if(parametersCount > 0)
|
||||
{
|
||||
sql.resize(sql.length() - 1);
|
||||
}
|
||||
sql.append(1, ')');
|
||||
LOG_TRACE << sql;
|
||||
return sql;
|
||||
}
|
||||
};
|
||||
} // namespace sqlite3
|
||||
} // namespace drogon_model
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,361 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* User.h
|
||||
* DO NOT EDIT. This file is generated by drogon_ctl
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <drogon/orm/Result.h>
|
||||
#include <drogon/orm/Row.h>
|
||||
#include <drogon/orm/Field.h>
|
||||
#include <drogon/orm/SqlBinder.h>
|
||||
#include <drogon/orm/Mapper.h>
|
||||
#ifdef __cpp_impl_coroutine
|
||||
#include <drogon/orm/CoroMapper.h>
|
||||
#endif
|
||||
#include <trantor/utils/Date.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include <json/json.h>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <tuple>
|
||||
#include <stdint.h>
|
||||
#include <iostream>
|
||||
|
||||
namespace drogon
|
||||
{
|
||||
namespace orm
|
||||
{
|
||||
class DbClient;
|
||||
using DbClientPtr = std::shared_ptr<DbClient>;
|
||||
}
|
||||
}
|
||||
namespace drogon_model
|
||||
{
|
||||
namespace sqlite3
|
||||
{
|
||||
|
||||
class User
|
||||
{
|
||||
public:
|
||||
struct Cols
|
||||
{
|
||||
static const std::string _id;
|
||||
static const std::string _gitlab;
|
||||
static const std::string _name;
|
||||
static const std::string _password;
|
||||
static const std::string _role;
|
||||
static const std::string _root_id;
|
||||
static const std::string _tfa_type;
|
||||
static const std::string _tfa_secret;
|
||||
static const std::string _gitlab_at;
|
||||
static const std::string _gitlab_rt;
|
||||
};
|
||||
|
||||
const static int primaryKeyNumber;
|
||||
const static std::string tableName;
|
||||
const static bool hasPrimaryKey;
|
||||
const static std::string primaryKeyName;
|
||||
using PrimaryKeyType = uint64_t;
|
||||
const PrimaryKeyType &getPrimaryKey() const;
|
||||
|
||||
/**
|
||||
* @brief constructor
|
||||
* @param r One row of records in the SQL query result.
|
||||
* @param indexOffset Set the offset to -1 to access all columns by column names,
|
||||
* otherwise access all columns by offsets.
|
||||
* @note If the SQL is not a style of 'select * from table_name ...' (select all
|
||||
* columns by an asterisk), please set the offset to -1.
|
||||
*/
|
||||
explicit User(const drogon::orm::Row &r, const ssize_t indexOffset = 0) noexcept;
|
||||
|
||||
/**
|
||||
* @brief constructor
|
||||
* @param pJson The json object to construct a new instance.
|
||||
*/
|
||||
explicit User(const Json::Value &pJson) noexcept(false);
|
||||
|
||||
/**
|
||||
* @brief constructor
|
||||
* @param pJson The json object to construct a new instance.
|
||||
* @param pMasqueradingVector The aliases of table columns.
|
||||
*/
|
||||
User(const Json::Value &pJson, const std::vector<std::string> &pMasqueradingVector) noexcept(false);
|
||||
|
||||
User() = default;
|
||||
|
||||
void updateByJson(const Json::Value &pJson) noexcept(false);
|
||||
void updateByMasqueradedJson(const Json::Value &pJson,
|
||||
const std::vector<std::string> &pMasqueradingVector) noexcept(false);
|
||||
static bool validateJsonForCreation(const Json::Value &pJson, std::string &err);
|
||||
static bool validateMasqueradedJsonForCreation(const Json::Value &,
|
||||
const std::vector<std::string> &pMasqueradingVector,
|
||||
std::string &err);
|
||||
static bool validateJsonForUpdate(const Json::Value &pJson, std::string &err);
|
||||
static bool validateMasqueradedJsonForUpdate(const Json::Value &,
|
||||
const std::vector<std::string> &pMasqueradingVector,
|
||||
std::string &err);
|
||||
static bool validJsonOfField(size_t index,
|
||||
const std::string &fieldName,
|
||||
const Json::Value &pJson,
|
||||
std::string &err,
|
||||
bool isForCreation);
|
||||
|
||||
/** For column id */
|
||||
///Get the value of the column id, returns the default value if the column is null
|
||||
const uint64_t &getValueOfId() const noexcept;
|
||||
///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null
|
||||
const std::shared_ptr<uint64_t> &getId() const noexcept;
|
||||
///Set the value of the column id
|
||||
void setId(const uint64_t &pId) noexcept;
|
||||
|
||||
/** For column gitlab */
|
||||
///Get the value of the column gitlab, returns the default value if the column is null
|
||||
const uint64_t &getValueOfGitlab() const noexcept;
|
||||
///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null
|
||||
const std::shared_ptr<uint64_t> &getGitlab() const noexcept;
|
||||
///Set the value of the column gitlab
|
||||
void setGitlab(const uint64_t &pGitlab) noexcept;
|
||||
|
||||
/** For column name */
|
||||
///Get the value of the column name, returns the default value if the column is null
|
||||
const std::string &getValueOfName() const noexcept;
|
||||
///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null
|
||||
const std::shared_ptr<std::string> &getName() const noexcept;
|
||||
///Set the value of the column name
|
||||
void setName(const std::string &pName) noexcept;
|
||||
void setName(std::string &&pName) noexcept;
|
||||
|
||||
/** For column password */
|
||||
///Get the value of the column password, returns the default value if the column is null
|
||||
const std::string &getValueOfPassword() const noexcept;
|
||||
///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null
|
||||
const std::shared_ptr<std::string> &getPassword() const noexcept;
|
||||
///Set the value of the column password
|
||||
void setPassword(const std::string &pPassword) noexcept;
|
||||
void setPassword(std::string &&pPassword) noexcept;
|
||||
|
||||
/** For column role */
|
||||
///Get the value of the column role, returns the default value if the column is null
|
||||
const uint64_t &getValueOfRole() const noexcept;
|
||||
///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null
|
||||
const std::shared_ptr<uint64_t> &getRole() const noexcept;
|
||||
///Set the value of the column role
|
||||
void setRole(const uint64_t &pRole) noexcept;
|
||||
|
||||
/** For column root_id */
|
||||
///Get the value of the column root_id, returns the default value if the column is null
|
||||
const uint64_t &getValueOfRootId() const noexcept;
|
||||
///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null
|
||||
const std::shared_ptr<uint64_t> &getRootId() const noexcept;
|
||||
///Set the value of the column root_id
|
||||
void setRootId(const uint64_t &pRootId) noexcept;
|
||||
|
||||
/** For column tfa_type */
|
||||
///Get the value of the column tfa_type, returns the default value if the column is null
|
||||
const uint64_t &getValueOfTfaType() const noexcept;
|
||||
///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null
|
||||
const std::shared_ptr<uint64_t> &getTfaType() const noexcept;
|
||||
///Set the value of the column tfa_type
|
||||
void setTfaType(const uint64_t &pTfaType) noexcept;
|
||||
|
||||
/** For column tfa_secret */
|
||||
///Get the value of the column tfa_secret, returns the default value if the column is null
|
||||
const std::vector<char> &getValueOfTfaSecret() const noexcept;
|
||||
///Return the column value by std::string with binary data
|
||||
std::string getValueOfTfaSecretAsString() const noexcept;
|
||||
///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null
|
||||
const std::shared_ptr<std::vector<char>> &getTfaSecret() const noexcept;
|
||||
///Set the value of the column tfa_secret
|
||||
void setTfaSecret(const std::vector<char> &pTfaSecret) noexcept;
|
||||
void setTfaSecret(const std::string &pTfaSecret) noexcept;
|
||||
void setTfaSecretToNull() noexcept;
|
||||
|
||||
/** For column gitlab_at */
|
||||
///Get the value of the column gitlab_at, returns the default value if the column is null
|
||||
const std::string &getValueOfGitlabAt() const noexcept;
|
||||
///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null
|
||||
const std::shared_ptr<std::string> &getGitlabAt() const noexcept;
|
||||
///Set the value of the column gitlab_at
|
||||
void setGitlabAt(const std::string &pGitlabAt) noexcept;
|
||||
void setGitlabAt(std::string &&pGitlabAt) noexcept;
|
||||
void setGitlabAtToNull() noexcept;
|
||||
|
||||
/** For column gitlab_rt */
|
||||
///Get the value of the column gitlab_rt, returns the default value if the column is null
|
||||
const std::string &getValueOfGitlabRt() const noexcept;
|
||||
///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null
|
||||
const std::shared_ptr<std::string> &getGitlabRt() const noexcept;
|
||||
///Set the value of the column gitlab_rt
|
||||
void setGitlabRt(const std::string &pGitlabRt) noexcept;
|
||||
void setGitlabRt(std::string &&pGitlabRt) noexcept;
|
||||
void setGitlabRtToNull() noexcept;
|
||||
|
||||
|
||||
static size_t getColumnNumber() noexcept { return 10; }
|
||||
static const std::string &getColumnName(size_t index) noexcept(false);
|
||||
|
||||
Json::Value toJson() const;
|
||||
Json::Value toMasqueradedJson(const std::vector<std::string> &pMasqueradingVector) const;
|
||||
/// Relationship interfaces
|
||||
private:
|
||||
friend drogon::orm::Mapper<User>;
|
||||
#ifdef __cpp_impl_coroutine
|
||||
friend drogon::orm::CoroMapper<User>;
|
||||
#endif
|
||||
static const std::vector<std::string> &insertColumns() noexcept;
|
||||
void outputArgs(drogon::orm::internal::SqlBinder &binder) const;
|
||||
const std::vector<std::string> updateColumns() const;
|
||||
void updateArgs(drogon::orm::internal::SqlBinder &binder) const;
|
||||
///For mysql or sqlite3
|
||||
void updateId(const uint64_t id);
|
||||
std::shared_ptr<uint64_t> id_;
|
||||
std::shared_ptr<uint64_t> gitlab_;
|
||||
std::shared_ptr<std::string> name_;
|
||||
std::shared_ptr<std::string> password_;
|
||||
std::shared_ptr<uint64_t> role_;
|
||||
std::shared_ptr<uint64_t> rootId_;
|
||||
std::shared_ptr<uint64_t> tfaType_;
|
||||
std::shared_ptr<std::vector<char>> tfaSecret_;
|
||||
std::shared_ptr<std::string> gitlabAt_;
|
||||
std::shared_ptr<std::string> gitlabRt_;
|
||||
struct MetaData
|
||||
{
|
||||
const std::string colName_;
|
||||
const std::string colType_;
|
||||
const std::string colDatabaseType_;
|
||||
const ssize_t colLength_;
|
||||
const bool isAutoVal_;
|
||||
const bool isPrimaryKey_;
|
||||
const bool notNull_;
|
||||
};
|
||||
static const std::vector<MetaData> metaData_;
|
||||
bool dirtyFlag_[10]={ false };
|
||||
public:
|
||||
static const std::string &sqlForFindingByPrimaryKey()
|
||||
{
|
||||
static const std::string sql="select * from " + tableName + " where id = ?";
|
||||
return sql;
|
||||
}
|
||||
|
||||
static const std::string &sqlForDeletingByPrimaryKey()
|
||||
{
|
||||
static const std::string sql="delete from " + tableName + " where id = ?";
|
||||
return sql;
|
||||
}
|
||||
std::string sqlForInserting(bool &needSelection) const
|
||||
{
|
||||
std::string sql="insert into " + tableName + " (";
|
||||
size_t parametersCount = 0;
|
||||
needSelection = false;
|
||||
if(dirtyFlag_[1])
|
||||
{
|
||||
sql += "gitlab,";
|
||||
++parametersCount;
|
||||
}
|
||||
if(dirtyFlag_[2])
|
||||
{
|
||||
sql += "name,";
|
||||
++parametersCount;
|
||||
}
|
||||
if(dirtyFlag_[3])
|
||||
{
|
||||
sql += "password,";
|
||||
++parametersCount;
|
||||
}
|
||||
if(dirtyFlag_[4])
|
||||
{
|
||||
sql += "role,";
|
||||
++parametersCount;
|
||||
}
|
||||
if(dirtyFlag_[5])
|
||||
{
|
||||
sql += "root_id,";
|
||||
++parametersCount;
|
||||
}
|
||||
if(dirtyFlag_[6])
|
||||
{
|
||||
sql += "tfa_type,";
|
||||
++parametersCount;
|
||||
}
|
||||
if(dirtyFlag_[7])
|
||||
{
|
||||
sql += "tfa_secret,";
|
||||
++parametersCount;
|
||||
}
|
||||
if(dirtyFlag_[8])
|
||||
{
|
||||
sql += "gitlab_at,";
|
||||
++parametersCount;
|
||||
}
|
||||
if(dirtyFlag_[9])
|
||||
{
|
||||
sql += "gitlab_rt,";
|
||||
++parametersCount;
|
||||
}
|
||||
if(parametersCount > 0)
|
||||
{
|
||||
sql[sql.length()-1]=')';
|
||||
sql += " values (";
|
||||
}
|
||||
else
|
||||
sql += ") values (";
|
||||
|
||||
if(dirtyFlag_[1])
|
||||
{
|
||||
sql.append("?,");
|
||||
|
||||
}
|
||||
if(dirtyFlag_[2])
|
||||
{
|
||||
sql.append("?,");
|
||||
|
||||
}
|
||||
if(dirtyFlag_[3])
|
||||
{
|
||||
sql.append("?,");
|
||||
|
||||
}
|
||||
if(dirtyFlag_[4])
|
||||
{
|
||||
sql.append("?,");
|
||||
|
||||
}
|
||||
if(dirtyFlag_[5])
|
||||
{
|
||||
sql.append("?,");
|
||||
|
||||
}
|
||||
if(dirtyFlag_[6])
|
||||
{
|
||||
sql.append("?,");
|
||||
|
||||
}
|
||||
if(dirtyFlag_[7])
|
||||
{
|
||||
sql.append("?,");
|
||||
|
||||
}
|
||||
if(dirtyFlag_[8])
|
||||
{
|
||||
sql.append("?,");
|
||||
|
||||
}
|
||||
if(dirtyFlag_[9])
|
||||
{
|
||||
sql.append("?,");
|
||||
|
||||
}
|
||||
if(parametersCount > 0)
|
||||
{
|
||||
sql.resize(sql.length() - 1);
|
||||
}
|
||||
sql.append(1, ')');
|
||||
LOG_TRACE << sql;
|
||||
return sql;
|
||||
}
|
||||
};
|
||||
} // namespace sqlite3
|
||||
} // namespace drogon_model
|
||||
@@ -1,5 +0,0 @@
|
||||
{
|
||||
"rdbms":"sqlite3",
|
||||
"filename":"run/sqlite.db",
|
||||
"tables":[]
|
||||
}
|
||||
@@ -30,6 +30,16 @@ namespace dto {
|
||||
db::UserRole role;
|
||||
};
|
||||
|
||||
struct GetNodeEntry {
|
||||
explicit GetNodeEntry(const db::INode& node) : id(node.getValueOfId()), name(node.getValueOfName()), is_file(node.getValueOfIsFile() != 0), has_preview(node.getValueOfHasPreview() != 0), parent(node.getParentId()) {
|
||||
if (node.getValueOfIsFile() != 0) size = node.getValueOfSize();
|
||||
}
|
||||
uint64_t id, size;
|
||||
std::string name;
|
||||
bool is_file, has_preview;
|
||||
std::shared_ptr<uint64_t> parent;
|
||||
};
|
||||
|
||||
drogon::HttpResponsePtr get_error_res(drogon::HttpStatusCode, const std::string &msg);
|
||||
drogon::HttpResponsePtr get_success_res();
|
||||
drogon::HttpResponsePtr get_success_res(Json::Value &);
|
||||
@@ -46,10 +56,13 @@ namespace dto {
|
||||
drogon::HttpResponsePtr get_admin_users_res(const std::vector<GetUsersEntry>& users);
|
||||
|
||||
drogon::HttpResponsePtr get_root_res(uint64_t root);
|
||||
drogon::HttpResponsePtr get_node_folder_res(uint64_t id, const std::string& name, const std::shared_ptr<uint64_t>& parent, const std::vector<uint64_t>& children);
|
||||
drogon::HttpResponsePtr get_node_file_res(uint64_t id, const std::string& name, const std::shared_ptr<uint64_t>& parent, uint64_t size);
|
||||
drogon::HttpResponsePtr get_path_res(const std::string& path);
|
||||
drogon::HttpResponsePtr get_node_res(const GetNodeEntry& node, const std::vector<GetNodeEntry>& children);
|
||||
drogon::HttpResponsePtr get_new_node_res(uint64_t id);
|
||||
drogon::HttpResponsePtr get_node_exists_res(uint64_t id, bool file);
|
||||
drogon::HttpResponsePtr get_download_base64_res(const std::string& data);
|
||||
drogon::HttpResponsePtr get_type_res(const std::string& type);
|
||||
drogon::HttpResponsePtr get_create_zip_done_res();
|
||||
drogon::HttpResponsePtr get_create_zip_done_res(uint64_t progress, uint64_t total);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -63,30 +63,24 @@ namespace dto::Responses {
|
||||
return get_success_res(json);
|
||||
}
|
||||
|
||||
drogon::HttpResponsePtr get_node_folder_res(uint64_t id, const std::string &name, const std::shared_ptr<uint64_t> &parent, const std::vector<uint64_t> &children) {
|
||||
Json::Value parse_node(const GetNodeEntry& node) {
|
||||
Json::Value json;
|
||||
json["id"] = id;
|
||||
json["name"] = name;
|
||||
json["isFile"] = false;
|
||||
json["parent"] = (parent != nullptr) ? *parent : Json::Value::nullSingleton();
|
||||
for (uint64_t child : children)
|
||||
json["children"].append(child);
|
||||
return get_success_res(json);
|
||||
json["id"] = node.id;
|
||||
json["name"] = node.name;
|
||||
json["isFile"] = node.is_file;
|
||||
json["preview"] = node.has_preview;
|
||||
json["parent"] = (node.parent != nullptr) ? *node.parent : Json::Value::nullSingleton();
|
||||
if (node.is_file) json["size"] = node.size;
|
||||
return json;
|
||||
}
|
||||
|
||||
drogon::HttpResponsePtr get_node_file_res(uint64_t id, const std::string &name, const std::shared_ptr<uint64_t> &parent, uint64_t size) {
|
||||
Json::Value json;
|
||||
json["id"] = id;
|
||||
json["name"] = name;
|
||||
json["isFile"] = true;
|
||||
json["parent"] = (parent != nullptr) ? *parent : Json::Value::nullSingleton();
|
||||
json["size"] = size;
|
||||
return get_success_res(json);
|
||||
}
|
||||
|
||||
drogon::HttpResponsePtr get_path_res(const std::string& path) {
|
||||
Json::Value json;
|
||||
json["path"] = path;
|
||||
drogon::HttpResponsePtr get_node_res(const GetNodeEntry& node, const std::vector<GetNodeEntry>& children) {
|
||||
Json::Value json = parse_node(node);
|
||||
if (!node.is_file) {
|
||||
json["children"] = Json::Value(Json::arrayValue);
|
||||
for (const GetNodeEntry& child : children)
|
||||
json["children"].append(parse_node(child));
|
||||
}
|
||||
return get_success_res(json);
|
||||
}
|
||||
|
||||
@@ -95,4 +89,38 @@ namespace dto::Responses {
|
||||
json["id"] = id;
|
||||
return get_success_res(json);
|
||||
}
|
||||
|
||||
drogon::HttpResponsePtr get_node_exists_res(uint64_t id, bool file) {
|
||||
Json::Value json;
|
||||
json["id"] = id;
|
||||
json["exists"] = true;
|
||||
json["isFile"] = file;
|
||||
return get_success_res(json);
|
||||
}
|
||||
|
||||
drogon::HttpResponsePtr get_download_base64_res(const std::string &data) {
|
||||
Json::Value json;
|
||||
json["data"] = data;
|
||||
return get_success_res(json);
|
||||
}
|
||||
|
||||
drogon::HttpResponsePtr get_type_res(const std::string &type) {
|
||||
Json::Value json;
|
||||
json["type"] = type;
|
||||
return get_success_res(json);
|
||||
}
|
||||
|
||||
drogon::HttpResponsePtr get_create_zip_done_res() {
|
||||
Json::Value json;
|
||||
json["done"] = true;
|
||||
return get_success_res(json);
|
||||
}
|
||||
|
||||
drogon::HttpResponsePtr get_create_zip_done_res(uint64_t progress, uint64_t total) {
|
||||
Json::Value json;
|
||||
json["done"] = false;
|
||||
json["progress"] = progress;
|
||||
json["total"] = total;
|
||||
return get_success_res(json);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ void cleanup_tokens(db::MapperToken& mapper) {
|
||||
|
||||
void Login::doFilter(const drogon::HttpRequestPtr& req, drogon::FilterCallback&& cb, drogon::FilterChainCallback&& ccb) {
|
||||
std::string token_str;
|
||||
if (req->path() == "/api/fs/download") {
|
||||
if (req->path() == "/api/fs/download" || req->path() == "/api/fs/download_multi") {
|
||||
token_str = req->getParameter("jwtToken");
|
||||
} else {
|
||||
std::string auth_header = req->getHeader("Authorization");
|
||||
|
||||
@@ -13,6 +13,9 @@ void cleanup() {
|
||||
std::cout << "Cleanup up uploads...";
|
||||
std::filesystem::remove_all("uploads");
|
||||
std::cout << " [Done]" << std::endl;
|
||||
std::cout << "Removing temp folder..." << std::flush;
|
||||
std::filesystem::remove_all("temp");
|
||||
std::cout << " [Done]" << std::endl;
|
||||
std::cout << "Goodbye!" << std::endl;
|
||||
}
|
||||
|
||||
@@ -47,6 +50,15 @@ int main(int argc, char* argv[]) {
|
||||
std::filesystem::create_directory("logs");
|
||||
std::cout << " [Done]" << std::endl;
|
||||
}
|
||||
if (std::filesystem::exists("temp")) {
|
||||
std::cout << "Removing existing temp folder..." << std::flush;
|
||||
std::filesystem::remove_all("temp");
|
||||
std::cout << " [Done]" << std::endl;
|
||||
}
|
||||
std::cout << "Creating temp folder..." << std::flush;
|
||||
std::filesystem::create_directory("temp");
|
||||
std::cout << " [Done]" << std::endl;
|
||||
|
||||
|
||||
auto* loop = drogon::app().getLoop();
|
||||
loop->queueInLoop([]{
|
||||
@@ -76,7 +88,8 @@ int main(int argc, char* argv[]) {
|
||||
" 'name' TEXT,\n"
|
||||
" 'parent_id' INTEGER,\n"
|
||||
" 'owner_id' INTEGER NOT NULL,\n"
|
||||
" 'size' INTEGER\n"
|
||||
" 'size' INTEGER,\n"
|
||||
" 'has_preview' INTEGER NOT NULL\n"
|
||||
")");
|
||||
std::cout << " [Done]" << std::endl;
|
||||
std::cout << "Started!" << std::endl;
|
||||
@@ -101,8 +114,12 @@ int main(int argc, char* argv[]) {
|
||||
Json::Value access_logger;
|
||||
access_logger["name"] = "drogon::plugin::AccessLogger";
|
||||
|
||||
Json::Value smtp_mail;
|
||||
smtp_mail["name"] = "SMTPMail";
|
||||
|
||||
Json::Value config;
|
||||
config["plugins"].append(access_logger);
|
||||
config["plugins"].append(smtp_mail);
|
||||
|
||||
drogon::app()
|
||||
.setClientMaxBodySize(std::numeric_limits<size_t>::max())
|
||||
@@ -123,8 +140,10 @@ int main(int argc, char* argv[]) {
|
||||
.setIntSignalHandler(cleanup)
|
||||
.setTermSignalHandler(cleanup)
|
||||
|
||||
.addListener("0.0.0.0", 5678)
|
||||
.setThreadNum(2);
|
||||
.enableRelaunchOnError()
|
||||
|
||||
.addListener("0.0.0.0", 2345)
|
||||
.setThreadNum(8);
|
||||
std::cout << "Setup done!" << std::endl;
|
||||
|
||||
drogon::app().run();
|
||||
|
||||
Reference in New Issue
Block a user