|
|
|
|
@@ -1,217 +1,217 @@
|
|
|
|
|
#include "urldatabase.h"
|
|
|
|
|
#include "urldatabase.h".h"
|
|
|
|
|
#include <QSqlQuery>
|
|
|
|
|
#include <QSqlError>
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
|
|
ImageUrlDatabase::ImageUrlDatabase(QObject *parent) : QObject(parent)
|
|
|
|
|
ImageManager* ImageManager::m_instance = nullptr;
|
|
|
|
|
QMutex ImageManager::m_mutex;
|
|
|
|
|
|
|
|
|
|
ImageManager::ImageManager(QObject *parent) : QObject(parent)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ImageUrlDatabase::~ImageUrlDatabase()
|
|
|
|
|
ImageManager::~ImageManager()
|
|
|
|
|
{
|
|
|
|
|
if (m_db.isOpen()) {
|
|
|
|
|
m_db.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ImageUrlDatabase::openDatabase(const QString &path)
|
|
|
|
|
ImageManager* ImageManager::instance()
|
|
|
|
|
{
|
|
|
|
|
m_db = QSqlDatabase::addDatabase("QSQLITE");
|
|
|
|
|
m_db.setDatabaseName(path);
|
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
|
if (!m_instance) {
|
|
|
|
|
m_instance = new ImageManager();
|
|
|
|
|
}
|
|
|
|
|
return m_instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ImageManager::initialize(const QString &dbPath)
|
|
|
|
|
{
|
|
|
|
|
m_db = QSqlDatabase::addDatabase("QSQLITE", "image_manager_connection");
|
|
|
|
|
m_db.setDatabaseName(dbPath);
|
|
|
|
|
|
|
|
|
|
if (!m_db.open()) {
|
|
|
|
|
qWarning() << "Failed to open database:" << m_db.lastError().text();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建表结构
|
|
|
|
|
QSqlQuery query;
|
|
|
|
|
return createTables();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ImageManager::createTables()
|
|
|
|
|
{
|
|
|
|
|
QSqlQuery query(m_db);
|
|
|
|
|
|
|
|
|
|
// 创建域名表
|
|
|
|
|
if (!query.exec("CREATE TABLE IF NOT EXISTS domains ("
|
|
|
|
|
"domain_id INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
|
|
|
"domain TEXT NOT NULL UNIQUE)")) {
|
|
|
|
|
qWarning() << "Failed to create domains table:" << query.lastError().text();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建用户表
|
|
|
|
|
if (!query.exec("CREATE TABLE IF NOT EXISTS users ("
|
|
|
|
|
"user_id INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
|
|
|
"username TEXT NOT NULL UNIQUE, "
|
|
|
|
|
"created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)")) {
|
|
|
|
|
"username TEXT NOT NULL UNIQUE)")) {
|
|
|
|
|
qWarning() << "Failed to create users table:" << query.lastError().text();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建服务器表
|
|
|
|
|
if (!query.exec("CREATE TABLE IF NOT EXISTS servers ("
|
|
|
|
|
"server_id INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
|
|
|
"domain TEXT NOT NULL UNIQUE, "
|
|
|
|
|
"created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)")) {
|
|
|
|
|
qWarning() << "Failed to create servers table:" << query.lastError().text();
|
|
|
|
|
// 创建域名-用户关联表(确保唯一)
|
|
|
|
|
if (!query.exec("CREATE TABLE IF NOT EXISTS domain_users ("
|
|
|
|
|
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
|
|
|
"domain_id INTEGER NOT NULL, "
|
|
|
|
|
"user_id INTEGER NOT NULL, "
|
|
|
|
|
"UNIQUE(domain_id, user_id), "
|
|
|
|
|
"FOREIGN KEY(domain_id) REFERENCES domains(domain_id), "
|
|
|
|
|
"FOREIGN KEY(user_id) REFERENCES users(user_id))")) {
|
|
|
|
|
qWarning() << "Failed to create domain_users table:" << query.lastError().text();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建图片URL表
|
|
|
|
|
if (!query.exec("CREATE TABLE IF NOT EXISTS image_urls ("
|
|
|
|
|
"url_id INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
|
|
|
"user_id INTEGER NOT NULL, "
|
|
|
|
|
"server_id INTEGER NOT NULL, "
|
|
|
|
|
"domain_user_id INTEGER NOT NULL, "
|
|
|
|
|
"image_url TEXT NOT NULL, "
|
|
|
|
|
"last_accessed TIMESTAMP, "
|
|
|
|
|
"created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, "
|
|
|
|
|
"FOREIGN KEY (user_id) REFERENCES users(user_id), "
|
|
|
|
|
"FOREIGN KEY (server_id) REFERENCES servers(server_id), "
|
|
|
|
|
"UNIQUE(user_id, server_id, image_url))")) {
|
|
|
|
|
"upload_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, "
|
|
|
|
|
"FOREIGN KEY(domain_user_id) REFERENCES domain_users(id), "
|
|
|
|
|
"UNIQUE(domain_user_id, image_url))")) {
|
|
|
|
|
qWarning() << "Failed to create image_urls table:" << query.lastError().text();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建索引
|
|
|
|
|
query.exec("CREATE INDEX IF NOT EXISTS idx_image_user ON image_urls(user_id)");
|
|
|
|
|
query.exec("CREATE INDEX IF NOT EXISTS idx_image_server ON image_urls(server_id)");
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ImageUrlDatabase::addUser(const QString &username)
|
|
|
|
|
bool ImageManager::addDomain(const QString &domain)
|
|
|
|
|
{
|
|
|
|
|
QSqlQuery query;
|
|
|
|
|
QSqlQuery query(m_db);
|
|
|
|
|
query.prepare("INSERT OR IGNORE INTO domains (domain) VALUES (?)");
|
|
|
|
|
query.addBindValue(domain);
|
|
|
|
|
return query.exec();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ImageManager::addUser(const QString &user)
|
|
|
|
|
{
|
|
|
|
|
QSqlQuery query(m_db);
|
|
|
|
|
query.prepare("INSERT OR IGNORE INTO users (username) VALUES (?)");
|
|
|
|
|
query.addBindValue(username);
|
|
|
|
|
|
|
|
|
|
if (!query.exec()) {
|
|
|
|
|
qWarning() << "Failed to add user:" << query.lastError().text();
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取用户ID
|
|
|
|
|
return getUserId(username);
|
|
|
|
|
query.addBindValue(user);
|
|
|
|
|
return query.exec();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ImageUrlDatabase::getUserId(const QString &username)
|
|
|
|
|
int ImageManager::getDomainId(const QString &domain)
|
|
|
|
|
{
|
|
|
|
|
QSqlQuery query;
|
|
|
|
|
QSqlQuery query(m_db);
|
|
|
|
|
query.prepare("SELECT domain_id FROM domains WHERE domain = ?");
|
|
|
|
|
query.addBindValue(domain);
|
|
|
|
|
if (query.exec() && query.next()) {
|
|
|
|
|
return query.value(0).toInt();
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ImageManager::getUserId(const QString &user)
|
|
|
|
|
{
|
|
|
|
|
QSqlQuery query(m_db);
|
|
|
|
|
query.prepare("SELECT user_id FROM users WHERE username = ?");
|
|
|
|
|
query.addBindValue(username);
|
|
|
|
|
|
|
|
|
|
if (!query.exec() || !query.next()) {
|
|
|
|
|
return -1;
|
|
|
|
|
query.addBindValue(user);
|
|
|
|
|
if (query.exec() && query.next()) {
|
|
|
|
|
return query.value(0).toInt();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return query.value(0).toInt();
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ImageUrlDatabase::addServer(const QString &domain)
|
|
|
|
|
bool ImageManager::addDomainUser(const QString &domain, const QString &user)
|
|
|
|
|
{
|
|
|
|
|
QSqlQuery query;
|
|
|
|
|
query.prepare("INSERT OR IGNORE INTO servers (domain) VALUES (?)");
|
|
|
|
|
query.addBindValue(domain);
|
|
|
|
|
|
|
|
|
|
if (!query.exec()) {
|
|
|
|
|
qWarning() << "Failed to add server:" << query.lastError().text();
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取服务器ID
|
|
|
|
|
return getServerId(domain);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ImageUrlDatabase::getServerId(const QString &domain)
|
|
|
|
|
{
|
|
|
|
|
QSqlQuery query;
|
|
|
|
|
query.prepare("SELECT server_id FROM servers WHERE domain = ?");
|
|
|
|
|
query.addBindValue(domain);
|
|
|
|
|
|
|
|
|
|
if (!query.exec() || !query.next()) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return query.value(0).toInt();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ImageUrlDatabase::addImageUrl(const QString &username, const QString &domain, const QString &imageUrl)
|
|
|
|
|
{
|
|
|
|
|
int userId = addUser(username);
|
|
|
|
|
if (userId == -1) return false;
|
|
|
|
|
|
|
|
|
|
int serverId = addServer(domain);
|
|
|
|
|
if (serverId == -1) return false;
|
|
|
|
|
|
|
|
|
|
QSqlQuery query;
|
|
|
|
|
query.prepare("INSERT OR REPLACE INTO image_urls (user_id, server_id, image_url, last_accessed) "
|
|
|
|
|
"VALUES (?, ?, ?, datetime('now'))");
|
|
|
|
|
query.addBindValue(userId);
|
|
|
|
|
query.addBindValue(serverId);
|
|
|
|
|
query.addBindValue(imageUrl);
|
|
|
|
|
|
|
|
|
|
if (!query.exec()) {
|
|
|
|
|
qWarning() << "Failed to add image URL:" << query.lastError().text();
|
|
|
|
|
if (!addDomain(domain) || !addUser(user)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
int domainId = getDomainId(domain);
|
|
|
|
|
int userId = getUserId(user);
|
|
|
|
|
|
|
|
|
|
QList<QString> ImageUrlDatabase::getUserImages(const QString &username)
|
|
|
|
|
{
|
|
|
|
|
QList<QString> result;
|
|
|
|
|
int userId = getUserId(username);
|
|
|
|
|
|
|
|
|
|
if (userId == -1) return result;
|
|
|
|
|
|
|
|
|
|
QSqlQuery query;
|
|
|
|
|
query.prepare("SELECT i.image_url FROM image_urls i "
|
|
|
|
|
"WHERE i.user_id = ? "
|
|
|
|
|
"ORDER BY i.last_accessed DESC");
|
|
|
|
|
query.addBindValue(userId);
|
|
|
|
|
|
|
|
|
|
if (!query.exec()) {
|
|
|
|
|
qWarning() << "Failed to get user images:" << query.lastError().text();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (query.next()) {
|
|
|
|
|
result.append(query.value(0).toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QList<QString> ImageUrlDatabase::getServerImages(const QString &domain)
|
|
|
|
|
{
|
|
|
|
|
QList<QString> result;
|
|
|
|
|
int serverId = getServerId(domain);
|
|
|
|
|
|
|
|
|
|
if (serverId == -1) return result;
|
|
|
|
|
|
|
|
|
|
QSqlQuery query;
|
|
|
|
|
query.prepare("SELECT i.image_url FROM image_urls i "
|
|
|
|
|
"WHERE i.server_id = ? "
|
|
|
|
|
"ORDER BY i.last_accessed DESC");
|
|
|
|
|
query.addBindValue(serverId);
|
|
|
|
|
|
|
|
|
|
if (!query.exec()) {
|
|
|
|
|
qWarning() << "Failed to get server images:" << query.lastError().text();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (query.next()) {
|
|
|
|
|
result.append(query.value(0).toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ImageUrlDatabase::updateLastAccessed(const QString &username, const QString &domain, const QString &imageUrl)
|
|
|
|
|
{
|
|
|
|
|
int userId = getUserId(username);
|
|
|
|
|
if (userId == -1) return false;
|
|
|
|
|
|
|
|
|
|
int serverId = getServerId(domain);
|
|
|
|
|
if (serverId == -1) return false;
|
|
|
|
|
|
|
|
|
|
QSqlQuery query;
|
|
|
|
|
query.prepare("UPDATE image_urls SET last_accessed = datetime('now') "
|
|
|
|
|
"WHERE user_id = ? AND server_id = ? AND image_url = ?");
|
|
|
|
|
query.addBindValue(userId);
|
|
|
|
|
query.addBindValue(serverId);
|
|
|
|
|
query.addBindValue(imageUrl);
|
|
|
|
|
|
|
|
|
|
if (!query.exec()) {
|
|
|
|
|
qWarning() << "Failed to update last accessed time:" << query.lastError().text();
|
|
|
|
|
if (domainId == -1 || userId == -1) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return query.numRowsAffected() > 0;
|
|
|
|
|
QSqlQuery query(m_db);
|
|
|
|
|
query.prepare("INSERT OR IGNORE INTO domain_users (domain_id, user_id) VALUES (?, ?)");
|
|
|
|
|
query.addBindValue(domainId);
|
|
|
|
|
query.addBindValue(userId);
|
|
|
|
|
return query.exec();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ImageManager::addImageUrl(const QString &domain, const QString &user, const QUrl &imageUrl)
|
|
|
|
|
{
|
|
|
|
|
int domainId = getDomainId(domain);
|
|
|
|
|
int userId = getUserId(user);
|
|
|
|
|
|
|
|
|
|
if (domainId == -1 || userId == -1) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QSqlQuery query(m_db);
|
|
|
|
|
query.prepare("SELECT id FROM domain_users WHERE domain_id = ? AND user_id = ?");
|
|
|
|
|
query.addBindValue(domainId);
|
|
|
|
|
query.addBindValue(userId);
|
|
|
|
|
|
|
|
|
|
if (!query.exec() || !query.next()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int domainUserId = query.value(0).toInt();
|
|
|
|
|
|
|
|
|
|
query.prepare("INSERT OR IGNORE INTO image_urls (domain_user_id, image_url) VALUES (?, ?)");
|
|
|
|
|
query.addBindValue(domainUserId);
|
|
|
|
|
query.addBindValue(imageUrl.toString());
|
|
|
|
|
return query.exec();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QList<QUrl> ImageManager::getImageUrls(const QString &domain, const QString &user)
|
|
|
|
|
{
|
|
|
|
|
QList<QUrl> urls;
|
|
|
|
|
|
|
|
|
|
int domainId = getDomainId(domain);
|
|
|
|
|
int userId = getUserId(user);
|
|
|
|
|
|
|
|
|
|
if (domainId == -1 || userId == -1) {
|
|
|
|
|
return urls;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QSqlQuery query(m_db);
|
|
|
|
|
query.prepare("SELECT i.image_url FROM image_urls i "
|
|
|
|
|
"JOIN domain_users du ON i.domain_user_id = du.id "
|
|
|
|
|
"WHERE du.domain_id = ? AND du.user_id = ? "
|
|
|
|
|
"ORDER BY i.upload_time DESC");
|
|
|
|
|
query.addBindValue(domainId);
|
|
|
|
|
query.addBindValue(userId);
|
|
|
|
|
|
|
|
|
|
if (query.exec()) {
|
|
|
|
|
while (query.next()) {
|
|
|
|
|
urls.append(QUrl(query.value(0).toString()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return urls;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ImageManager::containsDomainUser(const QString &domain, const QString &user)
|
|
|
|
|
{
|
|
|
|
|
int domainId = getDomainId(domain);
|
|
|
|
|
int userId = getUserId(user);
|
|
|
|
|
|
|
|
|
|
if (domainId == -1 || userId == -1) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QSqlQuery query(m_db);
|
|
|
|
|
query.prepare("SELECT 1 FROM domain_users WHERE domain_id = ? AND user_id = ?");
|
|
|
|
|
query.addBindValue(domainId);
|
|
|
|
|
query.addBindValue(userId);
|
|
|
|
|
|
|
|
|
|
return query.exec() && query.next();
|
|
|
|
|
}
|
|
|
|
|
|