-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmission.h
60 lines (50 loc) · 1.83 KB
/
submission.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#pragma once
#include "file.h"
#include <SQLiteCpp/SQLiteCpp.h>
#include <json.h>
#include <algorithm>
#include <cctype>
#include <string_view>
#define SUBMISSIONS_DATABASE "data/submissions.db"
#define SUBMISSION_DIR_FRAGMENT "data/submissions/"
#define SUBMISSION_DATA_FILE_FRAGMENT "/data.zip"
#define SUBMISSION_DATA_FILE_COMPRESSED_FRAGMENT "/data.zip.zst"
#define SUBMISSION_CHANGELOG_FILE_FRAGMENT "/changelog.json"
namespace submission
{
inline SQLite::Database &get_database()
{
static auto &database = []() -> SQLite::Database &
{
static SQLite::Database database(file::get_data_root() + SUBMISSIONS_DATABASE,
SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE | SQLite::OPEN_FULLMUTEX);
database.exec("CREATE TABLE IF NOT EXISTS submissions("
"id TEXT PRIMARY KEY NOT NULL,"
"author TEXT NOT NULL,"
"description TEXT,"
"lastupdated INT NOT NULL,"
"name TEXT NOT NULL,"
"sha256 TEXT,"
"version TEXT NOT NULL)");
return database;
}();
return database;
}
inline bool submission_id_sanitycheck(std::string_view submission_id)
{
if (submission_id.length() != 16
|| std::find_if(submission_id.begin(), submission_id.end(),
[](auto c) { return !std::isalnum(c) || std::isupper(c); })
!= submission_id.end())
return false;
return true;
}
inline nlohmann::json get_changelog_json(const std::string &submission_id)
{
return file::read_json_file(SUBMISSION_DIR_FRAGMENT + submission_id + SUBMISSION_CHANGELOG_FILE_FRAGMENT);
}
inline void write_changelog_json(const std::string &submission_id, const nlohmann::json &json)
{
file::write_file(SUBMISSION_DIR_FRAGMENT + submission_id + SUBMISSION_CHANGELOG_FILE_FRAGMENT, json.dump(4));
}
}