Compare commits
7 Commits
v1.0.0
...
d42f2c358e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d42f2c358e | ||
|
|
34fdb18919 | ||
|
|
879598585b | ||
|
|
cb09f2caac | ||
|
|
093924ea99 | ||
|
|
82653c1a6c | ||
|
|
b915e69222 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1 +1,3 @@
|
||||
/target
|
||||
/albums/
|
||||
.idea
|
||||
3417
Cargo.lock
generated
Normal file
3417
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -13,10 +13,12 @@ sqlx = { version = "0.7", features = ["sqlite", "runtime-tokio", "migrate"] }
|
||||
chrono = { version = "0.4", features = ["serde"] }
|
||||
uuid = { version = "1.0", features = ["v4", "serde"] }
|
||||
image = { version = "0.24", default-features = false, features = ["jpeg", "png", "gif", "webp"] }
|
||||
reqwest = { version = "0.11", default-features = false, features = ["blocking","rustls-tls"] }
|
||||
reqwest = { version = "0.11", default-features = false, features = ["rustls-tls", "json"] }
|
||||
config = "0.13"
|
||||
rand = "0.8"
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = "0.3"
|
||||
thiserror = "1.0"
|
||||
mimalloc = "0.1.48"
|
||||
mimalloc = "0.1.48"
|
||||
regex = "1"
|
||||
lazy_static = "1.4"
|
||||
@@ -18,6 +18,7 @@
|
||||
<div class="album-info" id="album-info">
|
||||
<i class="fas fa-images"></i> 正在加载...
|
||||
</div>
|
||||
<div class="album-meta-tags" id="album-meta-tags"></div>
|
||||
</div>
|
||||
|
||||
<div class="images-grid" id="images-container">
|
||||
@@ -78,6 +79,6 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="/assets/js/album.js"></script>
|
||||
<script src="/assets/js/album.js?v=4"></script>
|
||||
</body>
|
||||
</html>
|
||||
1001
assets/css/album.css
1001
assets/css/album.css
File diff suppressed because it is too large
Load Diff
1893
assets/css/style.css
1893
assets/css/style.css
File diff suppressed because it is too large
Load Diff
@@ -8,39 +8,150 @@
|
||||
<link rel="stylesheet" href="/assets/css/style.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="header">
|
||||
<h1><i class="fas fa-images"></i> <span>流云图库</span></h1>
|
||||
<p>发现精彩瞬间,浏览精美图片</p>
|
||||
</div>
|
||||
|
||||
<div class="search-container">
|
||||
<div class="search-box">
|
||||
<label for="search-input"></label><input type="text" id="search-input" placeholder="搜索图片标题...">
|
||||
<button onclick="searchAlbums()"><i class="fas fa-search"></i></button>
|
||||
<div class="app-layout">
|
||||
|
||||
<!-- ============ 左侧边栏(可折叠) ============ -->
|
||||
<aside class="sidebar" id="sidebar">
|
||||
<!-- Logo 区域 -->
|
||||
<div class="sidebar-brand">
|
||||
<div class="brand-icon"><i class="fas fa-images"></i></div>
|
||||
<div class="brand-text">流云图库</div>
|
||||
</div>
|
||||
|
||||
<div class="action-buttons">
|
||||
<button class="random-button" onclick="openRandomAlbum()">
|
||||
<i class="fas fa-random"></i> 随机相册
|
||||
</button>
|
||||
<button class="favorites-button" onclick="showFavorites()">
|
||||
<i class="fas fa-heart"></i> 收藏夹
|
||||
|
||||
<!-- 导航菜单 -->
|
||||
<nav class="sidebar-nav">
|
||||
<div class="nav-item active" data-view="home" onclick="switchView('home')">
|
||||
<i class="fas fa-home"></i>
|
||||
<span>首页</span>
|
||||
</div>
|
||||
<div class="nav-item" data-view="favorites" onclick="showFavorites()">
|
||||
<i class="fas fa-heart"></i>
|
||||
<span>收藏夹</span>
|
||||
</div>
|
||||
<div class="nav-item" onclick="openRandomAlbum()">
|
||||
<i class="fas fa-random"></i>
|
||||
<span>随机相册</span>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<!-- 分隔线 -->
|
||||
<div class="sidebar-divider"></div>
|
||||
|
||||
<!-- 热门标签 -->
|
||||
<div class="sidebar-section">
|
||||
<div class="sidebar-section-title">
|
||||
<i class="fas fa-tags"></i>
|
||||
<span>热门标签</span>
|
||||
</div>
|
||||
<div class="sidebar-tags" id="sidebar-tags">
|
||||
<!-- 动态填充 -->
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 底部折叠按钮 -->
|
||||
<div class="sidebar-footer">
|
||||
<button class="collapse-btn" onclick="toggleSidebar()" id="collapse-btn">
|
||||
<i class="fas fa-chevron-left"></i>
|
||||
<span>收起侧边栏</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="stats" id="stats">
|
||||
正在加载相册数据...
|
||||
</div>
|
||||
|
||||
<div class="albums-grid" id="albums-container">
|
||||
<div class="loading">
|
||||
<i class="fas fa-spinner fa-spin"></i><br>加载中...
|
||||
</aside>
|
||||
|
||||
<!-- ============ 主内容区 ============ -->
|
||||
<main class="main-content">
|
||||
|
||||
<!-- 顶部工具栏 -->
|
||||
<header class="topbar">
|
||||
<!-- 移动端菜单按钮 -->
|
||||
<button class="mobile-menu-btn" onclick="toggleMobileSidebar()">
|
||||
<i class="fas fa-bars"></i>
|
||||
</button>
|
||||
|
||||
<!-- 搜索框 -->
|
||||
<div class="search-bar">
|
||||
<i class="fas fa-search search-icon"></i>
|
||||
<label for="search-input"></label>
|
||||
<input type="text" id="search-input" placeholder="搜索相册标题、人物、标签..." onkeydown="if(event.key==='Enter') searchAlbums()">
|
||||
<button class="search-clear" id="search-clear" onclick="clearSearch()" style="display:none;">
|
||||
<i class="fas fa-times"></i>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<!-- 右侧快捷按钮 -->
|
||||
<div class="topbar-actions">
|
||||
<button class="action-btn" onclick="openRandomAlbum()" title="随机相册">
|
||||
<i class="fas fa-random"></i>
|
||||
</button>
|
||||
<button class="action-btn" onclick="showFavorites()" title="收藏夹">
|
||||
<i class="fas fa-heart"></i>
|
||||
</button>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<!-- 筛选状态条 -->
|
||||
<div class="filter-bar" id="filter-bar" style="display:none;">
|
||||
<span class="filter-indicator">
|
||||
<i class="fas fa-filter"></i>
|
||||
当前筛选:
|
||||
</span>
|
||||
<span class="filter-pill" id="current-filter-pill"></span>
|
||||
<button class="filter-remove" onclick="clearFilter()">
|
||||
<i class="fas fa-times"></i>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 内容区域 -->
|
||||
<div class="content-area">
|
||||
|
||||
<!-- 统计条 -->
|
||||
<div class="stats-bar" id="stats-bar">
|
||||
<div class="stat-item">
|
||||
<span class="stat-icon"><i class="fas fa-folder-open"></i></span>
|
||||
<span class="stat-value" id="stat-albums">0</span>
|
||||
<span class="stat-label">个相册</span>
|
||||
</div>
|
||||
<div class="stat-divider"></div>
|
||||
<div class="stat-item">
|
||||
<span class="stat-icon"><i class="fas fa-image"></i></span>
|
||||
<span class="stat-value" id="stat-images">0</span>
|
||||
<span class="stat-label">张图片</span>
|
||||
</div>
|
||||
<div class="stat-divider"></div>
|
||||
<div class="stat-item">
|
||||
<span class="stat-icon"><i class="fas fa-user"></i></span>
|
||||
<span class="stat-value" id="stat-people">0</span>
|
||||
<span class="stat-label">位人物</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 热门人物横向滚动条 -->
|
||||
<div class="people-strip" id="people-strip" style="display:none;">
|
||||
<div class="people-strip-header">
|
||||
<i class="fas fa-users"></i>
|
||||
<span>热门人物</span>
|
||||
</div>
|
||||
<div class="people-scroll" id="people-scroll">
|
||||
<!-- 动态填充 -->
|
||||
</div>
|
||||
<div class="people-scroll-hint">
|
||||
<i class="fas fa-chevron-right"></i>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 相册网格 -->
|
||||
<div class="albums-grid" id="albums-container">
|
||||
<div class="loading">
|
||||
<i class="fas fa-spinner fa-spin"></i><br>加载中...
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
|
||||
<script src="/assets/js/app.js"></script>
|
||||
|
||||
<!-- 移动端遮罩 -->
|
||||
<div class="mobile-overlay" id="mobile-overlay" onclick="toggleMobileSidebar()"></div>
|
||||
|
||||
<script src="/assets/js/app.js?v=5"></script>
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
||||
|
||||
@@ -261,11 +261,146 @@ async function loadAlbum(albumId) {
|
||||
// 更新相册信息
|
||||
function updateAlbumInfo() {
|
||||
document.getElementById('album-title').textContent = currentAlbum.title;
|
||||
document.getElementById('album-info').innerHTML =
|
||||
`<i class="fas fa-images"></i> 共 ${images.length} 张图片`;
|
||||
|
||||
let infoHtml = `<i class="fas fa-images"></i> 共 ${images.length} 张图片`;
|
||||
document.getElementById('album-info').innerHTML = infoHtml;
|
||||
|
||||
// 显示相册元数据(人名 + 标签)
|
||||
updateAlbumMeta();
|
||||
|
||||
document.title = `${currentAlbum.title} - 图片展示服务`;
|
||||
}
|
||||
|
||||
// 标签分类映射(与后端 tag_extractor.rs 保持一致)
|
||||
const TAG_CATEGORIES = {
|
||||
style: ['日系', '韩系', '森系', '复古', '甜美', '性感', '清新', '冷艳',
|
||||
'优雅', '知性', '可爱', '俏皮', '温柔', '妩媚', '野性', '清纯',
|
||||
'时尚', '潮流', '街头', '学院', '文艺', '古典', '民族', '异域',
|
||||
'japanese', 'korean', 'retro', 'vintage', 'cute', 'sexy',
|
||||
'elegant', 'fresh', 'sweet', 'wild', 'pure', 'fashion'],
|
||||
outfit: ['比基尼', '泳装', '泳衣', '旗袍', '制服', '丝袜', '内衣', '文胸',
|
||||
'包臀裙', '紧身裙', '超短裙', '连衣裙', '半身裙', '短裙', '长裙',
|
||||
'牛仔裤', '热裤', '短裤', '休闲裤', '西装裤',
|
||||
'T恤', '衬衫', '毛衣', '针织衫', '外套', '风衣', '大衣', '西装',
|
||||
'吊带', '抹胸', '露背', '透视', '蕾丝', '镂空', '开叉',
|
||||
'高跟鞋', '运动鞋', '凉鞋', '拖鞋', '靴子',
|
||||
'bikini', 'swimsuit', 'stockings', 'lingerie', 'dress',
|
||||
'skirt', 'jeans', 'shirt', 'sweater', 'coat', 'heels'],
|
||||
scene: ['室内', '户外', '街拍', '浴室', '浴缸', '床上', '床照', '沙发',
|
||||
'厨房', '阳台', '窗边', '楼梯', '走廊', '电梯', '车内', '驾驶座',
|
||||
'海边', '沙滩', '泳池', '温泉', '森林', '草地', '花园', '公园',
|
||||
'街道', '巷弄', '建筑', '天台', '屋顶', '咖啡馆', '餐厅', '酒店',
|
||||
'indoor', 'outdoor', 'street', 'beach', 'pool', 'bathroom',
|
||||
'bedroom', 'kitchen', 'balcony', 'car', 'cafe', 'hotel'],
|
||||
body: ['美腿', '美胸', '翘臀', '玉足', '美背', '香肩', '蛮腰', '细腰',
|
||||
'长腿', '酥胸', '丰臀', '白皙', '肌肤', '纹身',
|
||||
'legs', 'breasts', 'ass', 'feet', 'back', 'shoulders', 'waist',
|
||||
'skin', 'tattoo'],
|
||||
mood: ['清纯', '诱惑', '唯美', '清新', '冷艳', '妩媚', '娇羞', '俏皮',
|
||||
'温柔', '野性', '冷傲', '热情', '慵懒', '迷离', '甜美', '知性',
|
||||
'pure', 'seductive', 'beautiful', 'fresh', 'cool', 'charming',
|
||||
'shy', 'cute', 'gentle', 'wild', 'lazy', 'sweet'],
|
||||
topic: ['写真', '私房', '街拍', '旅拍', '婚纱照', '艺术照', '生活照',
|
||||
'自拍', '跟拍', '约拍', '商拍',
|
||||
'photoshoot', 'portrait', 'selfie', 'travel', 'wedding'],
|
||||
};
|
||||
|
||||
// 获取标签的分类
|
||||
function getTagCategory(tag) {
|
||||
const tagLower = tag.toLowerCase();
|
||||
for (const [category, tags] of Object.entries(TAG_CATEGORIES)) {
|
||||
if (tags.some(t => t.toLowerCase() === tagLower)) {
|
||||
return category;
|
||||
}
|
||||
}
|
||||
return 'default';
|
||||
}
|
||||
|
||||
// 更新相册元数据(人名 + 标签)点击可搜索
|
||||
function updateAlbumMeta() {
|
||||
const metaContainer = document.getElementById('album-meta-tags');
|
||||
if (!metaContainer) return;
|
||||
|
||||
let chips = [];
|
||||
|
||||
// 添加人名标签
|
||||
if (currentAlbum.person_name_cleaned) {
|
||||
chips.push({
|
||||
text: currentAlbum.person_name_cleaned,
|
||||
type: 'person',
|
||||
category: 'person',
|
||||
query: currentAlbum.person_name_cleaned
|
||||
});
|
||||
}
|
||||
|
||||
// 添加标签(支持多种格式)
|
||||
if (currentAlbum.tags) {
|
||||
let tagList = [];
|
||||
try {
|
||||
// 格式1: JSON数组 [["标签名", "分类"], ...]
|
||||
const parsed = JSON.parse(currentAlbum.tags);
|
||||
if (Array.isArray(parsed)) {
|
||||
parsed.forEach(item => {
|
||||
if (Array.isArray(item)) {
|
||||
tagList.push({ text: item[0], category: item[1] || 'default' });
|
||||
} else if (typeof item === 'string') {
|
||||
tagList.push({ text: item, category: getTagCategory(item) });
|
||||
} else if (item.tag || item.name) {
|
||||
const t = item.tag || item.name;
|
||||
const c = item.category || item.type || getTagCategory(t);
|
||||
tagList.push({ text: t, category: c });
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
// 格式2: 逗号分隔字符串
|
||||
currentAlbum.tags.split(',').forEach(tag => {
|
||||
const t = tag.trim();
|
||||
if (t) tagList.push({ text: t, category: getTagCategory(t) });
|
||||
});
|
||||
}
|
||||
|
||||
tagList.forEach(t => {
|
||||
chips.push({
|
||||
text: t.text,
|
||||
type: 'tag',
|
||||
category: t.category,
|
||||
query: t.text
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
if (chips.length === 0) {
|
||||
metaContainer.style.display = 'none';
|
||||
return;
|
||||
}
|
||||
|
||||
metaContainer.style.display = 'flex';
|
||||
metaContainer.innerHTML = chips.map(chip => {
|
||||
const icon = chip.type === 'person' ? 'fa-user' : 'fa-tag';
|
||||
// 生成 CSS 类名:人物用 person,标签用 tag-{category}
|
||||
const cssClass = chip.type === 'person' ? 'meta-chip-person' : `meta-chip-tag-${chip.category || 'default'}`;
|
||||
return `<span class="meta-chip ${cssClass}" data-meta-type="${chip.type}" data-meta-query="${encodeURIComponent(chip.query)}">
|
||||
<i class="fas ${icon}"></i> ${chip.text}
|
||||
</span>`;
|
||||
}).join('');
|
||||
}
|
||||
|
||||
// 事件委托:点击元数据标签
|
||||
document.addEventListener('click', function(e) {
|
||||
const chip = e.target.closest('.meta-chip');
|
||||
if (!chip) return;
|
||||
const query = chip.dataset.metaQuery;
|
||||
if (query) {
|
||||
searchByMeta(query);
|
||||
}
|
||||
});
|
||||
|
||||
// 点击元数据标签搜索
|
||||
function searchByMeta(encodedQuery) {
|
||||
window.location.href = `/?search=${encodedQuery}`;
|
||||
}
|
||||
|
||||
// 智能判断图片方向 - 更精确的判断逻辑
|
||||
function getImageOrientation(width, height) {
|
||||
if (!width || !height) return 'square';
|
||||
@@ -708,8 +843,12 @@ function toggleFavorite(event, imageIndex) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 找到按钮元素(event.target 可能是 i 图标)
|
||||
const btn = event.target.closest('.favorite-btn');
|
||||
if (!btn) return;
|
||||
|
||||
// 检查当前收藏状态
|
||||
const isFavorited = event.target.classList.contains('favorited');
|
||||
const isFavorited = btn.classList.contains('favorited');
|
||||
|
||||
if (isFavorited) {
|
||||
// 取消收藏
|
||||
@@ -723,7 +862,7 @@ function toggleFavorite(event, imageIndex) {
|
||||
return response.json();
|
||||
})
|
||||
.then(() => {
|
||||
event.target.classList.remove('favorited');
|
||||
btn.classList.remove('favorited');
|
||||
showFavoriteToast('已取消收藏', false);
|
||||
})
|
||||
.catch(error => {
|
||||
@@ -750,7 +889,7 @@ function toggleFavorite(event, imageIndex) {
|
||||
return response.json();
|
||||
})
|
||||
.then(() => {
|
||||
event.target.classList.add('favorited');
|
||||
btn.classList.add('favorited');
|
||||
showFavoriteToast('已添加到收藏夹', true);
|
||||
})
|
||||
.catch(error => {
|
||||
@@ -835,4 +974,5 @@ window.openLightbox = openLightbox;
|
||||
window.closeLightbox = closeLightbox;
|
||||
window.prevImage = prevImage;
|
||||
window.nextImage = nextImage;
|
||||
window.toggleFavorite = toggleFavorite;
|
||||
window.toggleFavorite = toggleFavorite;
|
||||
window.addToFavoritesFromLightbox = addToFavoritesFromLightbox;
|
||||
405
assets/js/app.js
405
assets/js/app.js
@@ -10,6 +10,13 @@ let lazyLoadObserver = null;
|
||||
let isAllAlbumsLoaded = false; // 标记是否已加载所有相册数据
|
||||
let isInFavoritesPage = false; // 标记当前是否在收藏页
|
||||
|
||||
// 标签和人物筛选
|
||||
let currentFilter = null; // { type: 'tag'|'person', value: string }
|
||||
let collapsedSections = { people: false, tags: false };
|
||||
|
||||
// 侧边栏状态(持久化)
|
||||
let sidebarCollapsed = localStorage.getItem('sidebarCollapsed') === 'true';
|
||||
|
||||
// API基础URL
|
||||
const API_BASE = '/api';
|
||||
|
||||
@@ -99,10 +106,21 @@ document.addEventListener('DOMContentLoaded', function () {
|
||||
document.body.classList.add('touch-device');
|
||||
}
|
||||
|
||||
loadAlbums();
|
||||
initSearch();
|
||||
initInfiniteScroll();
|
||||
initLazyLoading(); // 初始化图片懒加载
|
||||
|
||||
// 检查URL中是否有search参数
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
const searchQuery = urlParams.get('search');
|
||||
if (searchQuery) {
|
||||
const searchInput = document.getElementById('search-input');
|
||||
searchInput.value = searchQuery;
|
||||
// 直接搜索,不先加载全部相册
|
||||
searchAlbums();
|
||||
} else {
|
||||
loadAlbums();
|
||||
}
|
||||
});
|
||||
|
||||
// 初始化搜索功能
|
||||
@@ -355,7 +373,7 @@ function appendAlbums(albums) {
|
||||
const createdDate = new Date(album.created_at).toLocaleDateString('zh-CN');
|
||||
|
||||
return `
|
||||
<div class="album-card" data-album-id="${album.id}">
|
||||
<div class="album-card" data-album-id="${album.id}" onclick="openAlbum('${album.id}')">
|
||||
<div class="album-cover">
|
||||
<img
|
||||
src="${coverImageUrl}"
|
||||
@@ -371,9 +389,6 @@ function appendAlbums(albums) {
|
||||
<span><i class="fas fa-images"></i> ${album.image_count} 张图片</span>
|
||||
<span><i class="fas fa-calendar"></i> ${createdDate}</span>
|
||||
</div>
|
||||
<a href="/album?id=${album.id}" class="view-button">
|
||||
<i class="fas fa-eye"></i> 查看相册
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
@@ -429,6 +444,9 @@ async function loadAlbums(reset = false) {
|
||||
// 预加载下一批图片
|
||||
preloadNextBatchImages();
|
||||
|
||||
// 加载热门标签和人物
|
||||
loadDiscoverData();
|
||||
|
||||
} catch (error) {
|
||||
console.error('加载相册失败:', error);
|
||||
showError('加载相册数据失败,请刷新页面重试');
|
||||
@@ -555,7 +573,7 @@ function displayAlbums(albums) {
|
||||
const createdDate = new Date(album.created_at).toLocaleDateString('zh-CN');
|
||||
|
||||
return `
|
||||
<div class="album-card" data-album-id="${album.id}">
|
||||
<div class="album-card" data-album-id="${album.id}" onclick="openAlbum('${album.id}')">
|
||||
<div class="album-cover">
|
||||
<img
|
||||
src="${coverImageUrl}"
|
||||
@@ -571,9 +589,6 @@ function displayAlbums(albums) {
|
||||
<span><i class="fas fa-images"></i> ${album.image_count} 张图片</span>
|
||||
<span><i class="fas fa-calendar"></i> ${createdDate}</span>
|
||||
</div>
|
||||
<a href="/album?id=${album.id}" class="view-button">
|
||||
<i class="fas fa-eye"></i> 查看相册
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
@@ -610,14 +625,32 @@ function updateStats(albums) {
|
||||
albums = allAlbums;
|
||||
}
|
||||
const totalImages = albums.reduce((sum, album) => sum + album.image_count, 0);
|
||||
document.getElementById('stats').innerHTML = `共发现 ${albums.length} 个相册,包含 ${totalImages} 张图片`;
|
||||
|
||||
// 更新统计条
|
||||
const statAlbums = document.getElementById('stat-albums');
|
||||
const statImages = document.getElementById('stat-images');
|
||||
if (statAlbums) statAlbums.textContent = albums.length.toLocaleString();
|
||||
if (statImages) statImages.textContent = totalImages.toLocaleString();
|
||||
|
||||
// 兼容旧版 stats
|
||||
const oldStats = document.getElementById('stats');
|
||||
if (oldStats) oldStats.innerHTML = `共发现 ${albums.length} 个相册,包含 ${totalImages} 张图片`;
|
||||
|
||||
// 获取人物数量
|
||||
fetch(`${API_BASE}/recommend/popular-persons?limit=1`).then(r => r.ok && r.json()).then(data => {
|
||||
if (data && data.length > 0 && data[0].total) {
|
||||
const statPeople = document.getElementById('stat-people');
|
||||
if (statPeople) statPeople.textContent = data[0].total.toLocaleString();
|
||||
}
|
||||
}).catch(() => {});
|
||||
}
|
||||
|
||||
// 显示错误信息
|
||||
function showError(message) {
|
||||
const container = document.getElementById('albums-container');
|
||||
container.innerHTML = `<div class="no-results"><i class="fas fa-exclamation-triangle"></i><br>${message}</div>`;
|
||||
document.getElementById('stats').innerHTML = '';
|
||||
const oldStats = document.getElementById('stats');
|
||||
if (oldStats) oldStats.innerHTML = '';
|
||||
}
|
||||
|
||||
// 搜索相册函数
|
||||
@@ -759,6 +792,11 @@ preloadCriticalResources();
|
||||
// 优化滚动性能
|
||||
optimizeScrollPerformance();
|
||||
|
||||
// 打开相册详情
|
||||
function openAlbum(albumId) {
|
||||
window.location.href = `/album?id=${albumId}`;
|
||||
}
|
||||
|
||||
// 随机打开相册
|
||||
function openRandomAlbum() {
|
||||
// 添加随机动画效果
|
||||
@@ -818,12 +856,7 @@ function showFavorites() {
|
||||
return response.json();
|
||||
})
|
||||
.then(favoriteImages => {
|
||||
if (favoriteImages.length === 0) {
|
||||
alert('收藏夹为空,快去收藏一些喜欢的图片吧!');
|
||||
return;
|
||||
}
|
||||
|
||||
// 创建收藏夹页面或跳转到收藏夹视图
|
||||
// 始终显示收藏夹视图(包括空状态)
|
||||
displayFavorites(favoriteImages);
|
||||
})
|
||||
.catch(error => {
|
||||
@@ -1194,7 +1227,343 @@ function openFavoriteImage(favoriteId) {
|
||||
window.location.href = `/album?id=${favoriteId}`;
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// 标签和人物发现功能
|
||||
// ============================================================
|
||||
|
||||
// 加载热门标签和人物数据
|
||||
async function loadDiscoverData() {
|
||||
try {
|
||||
// 并行加载
|
||||
const [tagsResp, peopleResp] = await Promise.all([
|
||||
fetch(`${API_BASE}/tags/popular?limit=40`),
|
||||
fetch(`${API_BASE}/recommend/popular-persons?limit=30`)
|
||||
]);
|
||||
|
||||
if (tagsResp.ok) {
|
||||
const tags = await tagsResp.json();
|
||||
if (tags && tags.length > 0) {
|
||||
renderTagCloud(tags);
|
||||
}
|
||||
}
|
||||
|
||||
if (peopleResp.ok) {
|
||||
const people = await peopleResp.json();
|
||||
if (people && people.length > 0) {
|
||||
renderPeopleCloud(people);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('加载发现数据失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// 渲染标签(侧边栏列表)
|
||||
function renderTagCloud(tags) {
|
||||
// 新版:侧边栏标签列表
|
||||
const sidebarTags = document.getElementById('sidebar-tags');
|
||||
if (sidebarTags && tags && tags.length) {
|
||||
sidebarTags.innerHTML = tags.slice(0, 25).map(tag => {
|
||||
const count = tag.album_cnt || tag.count || 1;
|
||||
const name = tag.tag || tag.name || '';
|
||||
const isActive = currentFilter && currentFilter.type === 'tag' && currentFilter.value === name;
|
||||
return `<div class="sidebar-tag ${isActive ? 'active' : ''}" onclick="filterByTag('${name}')">
|
||||
<span class="sidebar-tag-name">${name}</span>
|
||||
<span class="sidebar-tag-count">${count}</span>
|
||||
</div>`;
|
||||
}).join('');
|
||||
}
|
||||
|
||||
// 兼容旧版:标签云区域
|
||||
const section = document.getElementById('tags-section');
|
||||
const cloud = document.getElementById('tag-cloud');
|
||||
if (section && cloud && tags && tags.length) {
|
||||
section.style.display = 'block';
|
||||
const maxCount = Math.max(...tags.map(t => t.album_cnt || t.count || 1));
|
||||
const minCount = Math.min(...tags.map(t => t.album_cnt || t.count || 1));
|
||||
cloud.innerHTML = tags.map(tag => {
|
||||
const count = tag.album_cnt || tag.count || 1;
|
||||
const name = tag.tag || tag.name || '';
|
||||
const ratio = maxCount === minCount ? 0.5 : (count - minCount) / (maxCount - minCount);
|
||||
const fontSize = 12 + ratio * 10;
|
||||
const hue = 210 - ratio * 50;
|
||||
const opacity = 0.6 + ratio * 0.4;
|
||||
return `<span class="tag-chip"
|
||||
style="font-size:${fontSize}px; color:hsl(${hue}, 70%, 45%); opacity:${opacity};"
|
||||
onclick="filterByTag('${name}')"
|
||||
title="${count} 个相册">
|
||||
${name} <small>(${count})</small>
|
||||
</span>`;
|
||||
}).join('');
|
||||
}
|
||||
}
|
||||
|
||||
// 渲染热门人物(横向滚动条)
|
||||
function renderPeopleCloud(people) {
|
||||
// 新版:横向滚动条
|
||||
const strip = document.getElementById('people-strip');
|
||||
const scroll = document.getElementById('people-scroll');
|
||||
if (strip && scroll && people && people.length) {
|
||||
strip.style.display = 'flex';
|
||||
scroll.innerHTML = people.slice(0, 20).map(p => {
|
||||
const name = p.name || p.display_name || '';
|
||||
const albumCount = p.total_albums || 0;
|
||||
const isActive = currentFilter && currentFilter.type === 'person' && currentFilter.value === name;
|
||||
return `<div class="person-chip-scroll ${isActive ? 'active' : ''}" onclick="filterByPerson('${name}')" title="${albumCount} 个相册">
|
||||
<div class="person-chip-avatar">${name.charAt(0)}</div>
|
||||
<div class="person-chip-info">
|
||||
<div class="person-chip-name">${name}</div>
|
||||
<div class="person-chip-count">${albumCount} 相册</div>
|
||||
</div>
|
||||
</div>`;
|
||||
}).join('');
|
||||
}
|
||||
|
||||
// 兼容旧版:人物卡片区域
|
||||
const section = document.getElementById('people-section');
|
||||
const cloud = document.getElementById('people-cloud');
|
||||
if (section && cloud && people && people.length) {
|
||||
section.style.display = 'block';
|
||||
cloud.innerHTML = people.map(p => {
|
||||
const name = p.name || p.display_name || '';
|
||||
const albumCount = p.total_albums || 0;
|
||||
const imageCount = p.total_images || 0;
|
||||
return `<div class="person-chip" onclick="filterByPerson('${name}')" title="${albumCount} 个相册, ${imageCount} 张图片">
|
||||
<div class="person-avatar">${name.charAt(0)}</div>
|
||||
<div class="person-info">
|
||||
<div class="person-name">${name}</div>
|
||||
<div class="person-meta">${albumCount} 相册 · ${imageCount} 图</div>
|
||||
</div>
|
||||
</div>`;
|
||||
}).join('');
|
||||
}
|
||||
}
|
||||
|
||||
// 按标签筛选
|
||||
async function filterByTag(tag) {
|
||||
currentFilter = { type: 'tag', value: tag };
|
||||
showFilterStatus(`标签: ${tag}`);
|
||||
renderTagCloud(await fetchTags().catch(() => []));
|
||||
|
||||
try {
|
||||
showSkeleton(6);
|
||||
const response = await fetch(`${API_BASE}/tags/search?tag=${encodeURIComponent(tag)}&limit=200`);
|
||||
if (!response.ok) throw new Error('搜索失败');
|
||||
const results = await response.json();
|
||||
|
||||
displayAlbums(results);
|
||||
updateStats(results);
|
||||
initLazyLoading();
|
||||
} catch (error) {
|
||||
console.error('标签筛选失败:', error);
|
||||
showError('标签筛选失败');
|
||||
}
|
||||
}
|
||||
|
||||
// 获取标签(辅助函数)
|
||||
async function fetchTags() {
|
||||
const resp = await fetch(`${API_BASE}/tags/popular?limit=25`);
|
||||
if (!resp.ok) return [];
|
||||
return resp.json();
|
||||
}
|
||||
|
||||
// 按人物筛选
|
||||
async function filterByPerson(personName) {
|
||||
currentFilter = { type: 'person', value: personName };
|
||||
showFilterStatus(`人物: ${personName}`);
|
||||
|
||||
try {
|
||||
showSkeleton(6);
|
||||
const response = await fetch(`${API_BASE}/people/search?q=${encodeURIComponent(personName)}`);
|
||||
if (!response.ok) throw new Error('搜索失败');
|
||||
const persons = await response.json();
|
||||
|
||||
if (persons && persons.length > 0) {
|
||||
try {
|
||||
const detailResp = await fetch(`${API_BASE}/people/${encodeURIComponent(personName)}`);
|
||||
if (detailResp.ok) {
|
||||
const detail = await detailResp.json();
|
||||
if (detail && detail.albums) {
|
||||
displayAlbums(detail.albums);
|
||||
updateStats(detail.albums);
|
||||
initLazyLoading();
|
||||
return;
|
||||
}
|
||||
}
|
||||
} catch (e) { /* 回退 */ }
|
||||
}
|
||||
await performServerSearch(personName);
|
||||
} catch (error) {
|
||||
console.error('人物筛选失败:', error);
|
||||
await performServerSearch(personName);
|
||||
}
|
||||
}
|
||||
|
||||
// 显示筛选状态
|
||||
function showFilterStatus(text) {
|
||||
// 新版筛选条
|
||||
const bar = document.getElementById('filter-bar');
|
||||
const pill = document.getElementById('current-filter-pill');
|
||||
if (bar && pill) {
|
||||
pill.textContent = text;
|
||||
bar.style.display = 'flex';
|
||||
}
|
||||
|
||||
// 兼容旧版
|
||||
const status = document.getElementById('filter-status');
|
||||
const label = document.getElementById('current-filter');
|
||||
if (status && label) {
|
||||
label.textContent = text;
|
||||
status.style.display = 'flex';
|
||||
}
|
||||
}
|
||||
|
||||
// 清除筛选
|
||||
function clearFilter() {
|
||||
currentFilter = null;
|
||||
|
||||
// 新版筛选条
|
||||
const bar = document.getElementById('filter-bar');
|
||||
if (bar) bar.style.display = 'none';
|
||||
|
||||
// 兼容旧版
|
||||
const status = document.getElementById('filter-status');
|
||||
if (status) status.style.display = 'none';
|
||||
|
||||
// 清空搜索框
|
||||
const searchInput = document.getElementById('search-input');
|
||||
if (searchInput) {
|
||||
searchInput.value = '';
|
||||
updateSearchClear();
|
||||
}
|
||||
|
||||
// 重新渲染标签和人物(清除高亮)
|
||||
loadDiscoverData();
|
||||
|
||||
// 重新加载所有相册
|
||||
loadAlbums(true);
|
||||
}
|
||||
|
||||
// ============ 侧边栏交互 ============
|
||||
|
||||
// 初始化侧边栏状态
|
||||
function initSidebar() {
|
||||
const sidebar = document.getElementById('sidebar');
|
||||
if (sidebar && sidebarCollapsed) {
|
||||
sidebar.classList.add('collapsed');
|
||||
}
|
||||
}
|
||||
|
||||
// 切换侧边栏折叠
|
||||
function toggleSidebar() {
|
||||
const sidebar = document.getElementById('sidebar');
|
||||
if (!sidebar) return;
|
||||
sidebarCollapsed = !sidebarCollapsed;
|
||||
sidebar.classList.toggle('collapsed', sidebarCollapsed);
|
||||
localStorage.setItem('sidebarCollapsed', sidebarCollapsed);
|
||||
}
|
||||
|
||||
// 移动端切换侧边栏
|
||||
function toggleMobileSidebar() {
|
||||
const sidebar = document.getElementById('sidebar');
|
||||
const overlay = document.getElementById('mobile-overlay');
|
||||
if (!sidebar || !overlay) return;
|
||||
sidebar.classList.toggle('mobile-open');
|
||||
overlay.classList.toggle('active');
|
||||
}
|
||||
|
||||
// 切换视图(首页/收藏夹)
|
||||
function switchView(view) {
|
||||
// 更新导航高亮
|
||||
document.querySelectorAll('.nav-item').forEach(item => {
|
||||
item.classList.toggle('active', item.dataset.view === view);
|
||||
});
|
||||
|
||||
// 移动端关闭侧边栏
|
||||
const sidebar = document.getElementById('sidebar');
|
||||
const overlay = document.getElementById('mobile-overlay');
|
||||
if (sidebar && sidebar.classList.contains('mobile-open')) {
|
||||
sidebar.classList.remove('mobile-open');
|
||||
overlay.classList.remove('active');
|
||||
}
|
||||
|
||||
if (view === 'home') {
|
||||
backToHome();
|
||||
}
|
||||
}
|
||||
|
||||
// 搜索框清除按钮
|
||||
function updateSearchClear() {
|
||||
const input = document.getElementById('search-input');
|
||||
const clear = document.getElementById('search-clear');
|
||||
if (input && clear) {
|
||||
clear.style.display = input.value.length > 0 ? 'flex' : 'none';
|
||||
}
|
||||
}
|
||||
|
||||
// 清除搜索
|
||||
function clearSearch() {
|
||||
const input = document.getElementById('search-input');
|
||||
if (input) {
|
||||
input.value = '';
|
||||
updateSearchClear();
|
||||
if (!currentFilter) {
|
||||
loadAlbums(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 折叠/展开区域(兼容旧版)
|
||||
function toggleSection(name) {
|
||||
collapsedSections[name] = !collapsedSections[name];
|
||||
const cloudId = name === 'people' ? 'people-cloud' : 'tag-cloud';
|
||||
const cloud = document.getElementById(cloudId);
|
||||
const btn = document.querySelector(`#${name}-section .section-toggle i`);
|
||||
if (cloud) {
|
||||
if (collapsedSections[name]) {
|
||||
cloud.style.display = 'none';
|
||||
btn.className = 'fas fa-chevron-down';
|
||||
} else {
|
||||
cloud.style.display = '';
|
||||
btn.className = 'fas fa-chevron-up';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ============ 初始化 ============
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
initSidebar();
|
||||
|
||||
// 搜索框输入事件
|
||||
const searchInput = document.getElementById('search-input');
|
||||
if (searchInput) {
|
||||
searchInput.addEventListener('input', updateSearchClear);
|
||||
}
|
||||
|
||||
// 键盘快捷键:ESC 关闭筛选/搜索
|
||||
document.addEventListener('keydown', function(e) {
|
||||
if (e.key === 'Escape') {
|
||||
if (currentFilter) clearFilter();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// 导出函数供全局使用
|
||||
window.searchAlbums = searchAlbums;
|
||||
window.openRandomAlbum = openRandomAlbum;
|
||||
window.showFavorites = showFavorites;
|
||||
window.showFavorites = showFavorites;
|
||||
window.openAlbum = openAlbum;
|
||||
window.backToHome = backToHome;
|
||||
window.loadAlbums = loadAlbums;
|
||||
window.removeFavorite = removeFavorite;
|
||||
window.previewFavorite = previewFavorite;
|
||||
window.openFavoriteImage = openFavoriteImage;
|
||||
window.removeFavoriteFromPreview = removeFavoriteFromPreview;
|
||||
window.filterByTag = filterByTag;
|
||||
window.filterByPerson = filterByPerson;
|
||||
window.clearFilter = clearFilter;
|
||||
window.toggleSection = toggleSection;
|
||||
window.toggleSidebar = toggleSidebar;
|
||||
window.toggleMobileSidebar = toggleMobileSidebar;
|
||||
window.switchView = switchView;
|
||||
window.clearSearch = clearSearch;
|
||||
52
migrations/002_metadata.sql
Normal file
52
migrations/002_metadata.sql
Normal file
@@ -0,0 +1,52 @@
|
||||
-- 相册元数据扩展字段
|
||||
ALTER TABLE albums ADD COLUMN person_name_original TEXT;
|
||||
ALTER TABLE albums ADD COLUMN person_name_cleaned TEXT;
|
||||
ALTER TABLE albums ADD COLUMN tags TEXT; -- JSON: [tag, ...]
|
||||
ALTER TABLE albums ADD COLUMN all_words TEXT; -- JSON: [[word, pos], ...]
|
||||
ALTER TABLE albums ADD COLUMN raw_title TEXT;
|
||||
ALTER TABLE albums ADD COLUMN processed_at TEXT;
|
||||
|
||||
-- 标签索引表(倒排索引)
|
||||
CREATE TABLE IF NOT EXISTS album_tags (
|
||||
album_id TEXT NOT NULL,
|
||||
tag TEXT NOT NULL,
|
||||
PRIMARY KEY (album_id, tag),
|
||||
FOREIGN KEY (album_id) REFERENCES albums(id) ON DELETE CASCADE
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_album_tags_tag ON album_tags(tag);
|
||||
|
||||
-- 人物表
|
||||
CREATE TABLE IF NOT EXISTS persons (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
display_name TEXT,
|
||||
total_albums INTEGER DEFAULT 0,
|
||||
total_images INTEGER DEFAULT 0,
|
||||
original_names TEXT, -- JSON: [raw_name, ...]
|
||||
created_at TEXT DEFAULT (datetime('now', 'localtime')),
|
||||
updated_at TEXT DEFAULT (datetime('now', 'localtime'))
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_persons_name ON persons(name);
|
||||
|
||||
-- 别名表
|
||||
CREATE TABLE IF NOT EXISTS person_aliases (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
person_id INTEGER NOT NULL,
|
||||
alias_type TEXT NOT NULL, -- prefix / english / nickname / chinese
|
||||
alias_name TEXT NOT NULL,
|
||||
created_at TEXT DEFAULT (datetime('now', 'localtime')),
|
||||
FOREIGN KEY (person_id) REFERENCES persons(id) ON DELETE CASCADE
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_aliases_name ON person_aliases(alias_name);
|
||||
CREATE INDEX IF NOT EXISTS idx_aliases_person ON person_aliases(person_id);
|
||||
|
||||
-- 人物-相册关联表
|
||||
CREATE TABLE IF NOT EXISTS person_albums (
|
||||
person_id INTEGER NOT NULL,
|
||||
album_id TEXT NOT NULL,
|
||||
PRIMARY KEY (person_id, album_id),
|
||||
FOREIGN KEY (person_id) REFERENCES persons(id),
|
||||
FOREIGN KEY (album_id) REFERENCES albums(id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_albums_cleaned ON albums(person_name_cleaned);
|
||||
284
scripts/import_test_data.py
Normal file
284
scripts/import_test_data.py
Normal file
@@ -0,0 +1,284 @@
|
||||
"""
|
||||
从 192.168.1.100:4500 API 拉取指定数量相册作为测试数据(多线程并行版)
|
||||
导入到 Rust imager 项目的 SQLite 数据库和 albums 目录
|
||||
"""
|
||||
import requests
|
||||
import sqlite3
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import uuid
|
||||
import threading
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
from datetime import datetime
|
||||
|
||||
API_BASE = "http://192.168.1.100:4500"
|
||||
DB_PATH = os.path.join(os.path.dirname(os.path.dirname(__file__)), "data", "imager.db")
|
||||
ALBUMS_DIR = os.path.join(os.path.dirname(os.path.dirname(__file__)), "albums")
|
||||
|
||||
# 全局 DB 写锁
|
||||
db_lock = threading.Lock()
|
||||
# 进度计数器
|
||||
progress_lock = threading.Lock()
|
||||
completed_count = [0]
|
||||
total_count = [0]
|
||||
|
||||
# 线程本地 Session(复用连接)
|
||||
thread_local = threading.local()
|
||||
|
||||
|
||||
def get_session():
|
||||
if not hasattr(thread_local, "session"):
|
||||
thread_local.session = requests.Session()
|
||||
thread_local.session.timeout = 60
|
||||
return thread_local.session
|
||||
|
||||
|
||||
def fetch_albums(limit=100):
|
||||
"""获取相册列表"""
|
||||
url = f"{API_BASE}/api/albums"
|
||||
print(f"[1/4] 获取相册列表: {url}")
|
||||
resp = requests.get(url, timeout=30)
|
||||
data = resp.json()
|
||||
|
||||
if isinstance(data, dict) and "albums" in data:
|
||||
albums = data["albums"]
|
||||
elif isinstance(data, list):
|
||||
albums = data
|
||||
else:
|
||||
print(f"未知响应格式: {type(data)}")
|
||||
return []
|
||||
|
||||
albums = albums[:limit]
|
||||
print(f" 获取到 {len(albums)} 个相册")
|
||||
return albums
|
||||
|
||||
|
||||
def fetch_album_images(album_id):
|
||||
"""获取相册的图片列表"""
|
||||
url = f"{API_BASE}/api/album/{album_id}"
|
||||
try:
|
||||
resp = requests.get(url, timeout=30)
|
||||
data = resp.json()
|
||||
if isinstance(data, dict) and "images" in data:
|
||||
return data["images"]
|
||||
elif isinstance(data, list):
|
||||
return data
|
||||
except Exception as e:
|
||||
print(f" 获取图片列表失败 {album_id}: {e}")
|
||||
return []
|
||||
|
||||
|
||||
def download_single_image(img):
|
||||
"""下载单张图片(线程安全)"""
|
||||
img_id = img.get("id", str(uuid.uuid4()))
|
||||
mime = img.get("mime_type", "image/jpeg")
|
||||
ext = "jpg"
|
||||
if mime and "/" in mime:
|
||||
ext = mime.split("/")[-1]
|
||||
|
||||
album_id = str(img.get("_album_id", ""))
|
||||
filename = f"{img_id}.{ext}"
|
||||
db_filename = f"{album_id}/{filename}" # 数据库路径:{album_id}/{img_id}.ext
|
||||
save_path = os.path.join(ALBUMS_DIR, album_id, filename)
|
||||
|
||||
# 跳过已下载
|
||||
if os.path.exists(save_path) and os.path.getsize(save_path) > 0:
|
||||
file_size = os.path.getsize(save_path)
|
||||
width, height = get_image_dimensions(save_path)
|
||||
return (img_id, db_filename, file_size, width, height, mime)
|
||||
|
||||
url = f"{API_BASE}/api/image/{img_id}"
|
||||
try:
|
||||
session = get_session()
|
||||
resp = session.get(url, timeout=60)
|
||||
if resp.status_code == 200:
|
||||
os.makedirs(os.path.dirname(save_path), exist_ok=True)
|
||||
with open(save_path, "wb") as f:
|
||||
f.write(resp.content)
|
||||
file_size = len(resp.content)
|
||||
width, height = get_image_dimensions(save_path)
|
||||
return (img_id, db_filename, file_size, width, height, resp.headers.get("content-type", mime))
|
||||
except Exception as e:
|
||||
pass
|
||||
return None
|
||||
|
||||
|
||||
def get_image_dimensions(path):
|
||||
"""获取图片尺寸(二进制头解析)"""
|
||||
try:
|
||||
with open(path, "rb") as f:
|
||||
header = f.read(32)
|
||||
if header[:2] == b"\xff\xd8":
|
||||
f.seek(2)
|
||||
while True:
|
||||
chunk = f.read(4)
|
||||
if len(chunk) < 4:
|
||||
break
|
||||
if chunk[0] != 0xFF:
|
||||
break
|
||||
seg_type = chunk[1]
|
||||
if seg_type == 0xDA:
|
||||
break
|
||||
seg_len = int.from_bytes(chunk[2:4], "big")
|
||||
if seg_type in (0xC0, 0xC1, 0xC2):
|
||||
data = f.read(seg_len - 2)
|
||||
if len(data) >= 5:
|
||||
return int.from_bytes(data[1:3], "big"), int.from_bytes(data[3:5], "big")
|
||||
f.seek(seg_len - 2, 1)
|
||||
return 0, 0
|
||||
elif header[:8] == b"\x89PNG\r\n\x1a\n":
|
||||
return int.from_bytes(header[16:20], "big"), int.from_bytes(header[20:24], "big")
|
||||
elif header[:4] == b"RIFF" and header[8:12] == b"WEBP":
|
||||
return int.from_bytes(header[24:26], "little"), int.from_bytes(header[26:28], "little")
|
||||
elif header[:6] in (b"GIF87a", b"GIF89a"):
|
||||
return int.from_bytes(header[6:8], "little"), int.from_bytes(header[8:10], "little")
|
||||
return 0, 0
|
||||
except Exception:
|
||||
return 0, 0
|
||||
|
||||
|
||||
def sync_album_to_db(conn, album, images_info):
|
||||
"""将相册和图片写入数据库(调用方持有 db_lock)"""
|
||||
cursor = conn.cursor()
|
||||
now = datetime.utcnow().isoformat() + "Z"
|
||||
album_id = album["id"]
|
||||
title = album.get("title", "Untitled")
|
||||
|
||||
cursor.execute(
|
||||
"""INSERT OR REPLACE INTO albums
|
||||
(id, title, document, description, cover_image_id, image_count, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
|
||||
(
|
||||
album_id, title, album_id,
|
||||
album.get("description"),
|
||||
images_info[0][0] if images_info else None,
|
||||
len(images_info),
|
||||
album.get("created_at", now),
|
||||
now,
|
||||
),
|
||||
)
|
||||
|
||||
for img_id, filename, file_size, width, height, mime_type in images_info:
|
||||
cursor.execute(
|
||||
"""INSERT OR REPLACE INTO images
|
||||
(id, album_id, filename, original_url, title, description, file_size, width, height, mime_type, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
|
||||
(img_id, album_id, filename, f"{API_BASE}/api/image/{img_id}",
|
||||
f"图片 {img_id[:8]}", None, file_size, width, height, mime_type or "image/jpeg", now),
|
||||
)
|
||||
|
||||
conn.commit()
|
||||
|
||||
|
||||
def process_album(album, conn, dl_workers):
|
||||
"""处理单个相册:下载所有图片并写入数据库"""
|
||||
aid = album["id"]
|
||||
title = album.get("title", "Untitled")[:60]
|
||||
|
||||
# 获取图片列表
|
||||
images = fetch_album_images(aid)
|
||||
if not images:
|
||||
with progress_lock:
|
||||
completed_count[0] += 1
|
||||
print(f"[{completed_count[0]}/{total_count[0]}] {title} — 无图片,跳过")
|
||||
return 0
|
||||
|
||||
# 为每个图片打上 album_id 标记
|
||||
for img in images:
|
||||
img["_album_id"] = aid
|
||||
|
||||
# 并行下载
|
||||
downloaded = []
|
||||
with ThreadPoolExecutor(max_workers=dl_workers) as pool:
|
||||
futures = {pool.submit(download_single_image, img): img for img in images}
|
||||
for future in as_completed(futures):
|
||||
result = future.result()
|
||||
if result:
|
||||
downloaded.append(result)
|
||||
|
||||
if not downloaded:
|
||||
with progress_lock:
|
||||
completed_count[0] += 1
|
||||
print(f"[{completed_count[0]}/{total_count[0]}] {title} — 0 张下载成功")
|
||||
return 0
|
||||
|
||||
# 写入数据库
|
||||
with db_lock:
|
||||
sync_album_to_db(conn, album, downloaded)
|
||||
|
||||
n = len(downloaded)
|
||||
with progress_lock:
|
||||
completed_count[0] += 1
|
||||
print(f"[{completed_count[0]}/{total_count[0]}] {title} — {n} 张图片")
|
||||
|
||||
return n
|
||||
|
||||
|
||||
def main():
|
||||
total = int(sys.argv[1]) if len(sys.argv) > 1 else 100
|
||||
dl_workers = int(sys.argv[2]) if len(sys.argv) > 2 else 8
|
||||
album_workers = int(sys.argv[3]) if len(sys.argv) > 3 else 4
|
||||
|
||||
print(f"=== 从 API 导入 {total} 个相册(并行下载: {dl_workers}线程/相册, 并行相册: {album_workers})===\n")
|
||||
|
||||
os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
|
||||
os.makedirs(ALBUMS_DIR, exist_ok=True)
|
||||
|
||||
conn = sqlite3.connect(DB_PATH, check_same_thread=False)
|
||||
conn.execute("PRAGMA journal_mode=WAL")
|
||||
conn.execute("PRAGMA foreign_keys=ON")
|
||||
conn.execute("PRAGMA synchronous=NORMAL")
|
||||
|
||||
# 获取已有相册
|
||||
existing = set()
|
||||
for row in conn.execute("SELECT id FROM albums"):
|
||||
existing.add(row[0])
|
||||
print(f" 数据库中已有 {len(existing)} 个相册")
|
||||
|
||||
# 获取相册列表
|
||||
albums = fetch_albums(total)
|
||||
if not albums:
|
||||
print("未获取到相册,退出")
|
||||
return
|
||||
|
||||
new_albums = [a for a in albums if a["id"] not in existing]
|
||||
skipped = len(albums) - len(new_albums)
|
||||
if skipped > 0:
|
||||
print(f" 跳过 {skipped} 个已存在的相册")
|
||||
print(f" 需要导入 {len(new_albums)} 个新相册\n")
|
||||
|
||||
if not new_albums:
|
||||
print("没有新相册需要导入")
|
||||
conn.close()
|
||||
return
|
||||
|
||||
total_count[0] = len(new_albums)
|
||||
total_images = 0
|
||||
t0 = time.time()
|
||||
|
||||
# 并行处理相册
|
||||
with ThreadPoolExecutor(max_workers=album_workers) as pool:
|
||||
futures = {pool.submit(process_album, album, conn, dl_workers): album for album in new_albums}
|
||||
for future in as_completed(futures):
|
||||
n = future.result()
|
||||
total_images += n
|
||||
|
||||
elapsed = time.time() - t0
|
||||
|
||||
# 重建 image_count
|
||||
print(f"\n[4/4] 更新相册图片计数...")
|
||||
conn.execute("""UPDATE albums SET image_count = (SELECT COUNT(*) FROM images WHERE images.album_id = albums.id)""")
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
print(f"\n=== 导入完成 ===")
|
||||
print(f" 成功导入: {len(new_albums)} 个相册")
|
||||
print(f" 总图片数: {total_images} 张")
|
||||
print(f" 耗时: {elapsed:.1f} 秒")
|
||||
print(f" 数据库: {DB_PATH}")
|
||||
print(f" 图片目录: {ALBUMS_DIR}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
132
src/cache/mod.rs
vendored
Normal file
132
src/cache/mod.rs
vendored
Normal file
@@ -0,0 +1,132 @@
|
||||
//! 缓存层 — 内存缓存管理
|
||||
|
||||
use crate::models::*;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::RwLock;
|
||||
use tracing::info;
|
||||
|
||||
/// 相册缓存
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AlbumsCache {
|
||||
pub albums: Vec<AlbumInfo>,
|
||||
pub search_index: HashMap<String, Vec<AlbumInfo>>,
|
||||
pub last_updated: chrono::DateTime<chrono::Utc>,
|
||||
}
|
||||
|
||||
impl AlbumsCache {
|
||||
pub fn new() -> Self {
|
||||
Self { albums: Vec::new(), search_index: HashMap::new(), last_updated: chrono::Utc::now() }
|
||||
}
|
||||
|
||||
pub fn is_expired(&self) -> bool {
|
||||
(chrono::Utc::now() - self.last_updated).num_minutes() >= 30
|
||||
}
|
||||
|
||||
pub fn update(&mut self, albums: Vec<AlbumInfo>) {
|
||||
self.search_index.clear();
|
||||
for album in &albums {
|
||||
let title_lower = album.title.to_lowercase();
|
||||
for word in title_lower.split_whitespace() {
|
||||
if word.len() >= 2 {
|
||||
self.search_index.entry(word.to_string()).or_default().push(album.clone());
|
||||
}
|
||||
}
|
||||
self.search_index.entry(title_lower).or_default().push(album.clone());
|
||||
}
|
||||
self.albums = albums;
|
||||
self.last_updated = chrono::Utc::now();
|
||||
info!("相册缓存已更新,共 {} 个相册,{} 个搜索索引项", self.albums.len(), self.search_index.len());
|
||||
}
|
||||
|
||||
pub fn search(&self, query: &str) -> Vec<AlbumInfo> {
|
||||
let q = query.to_lowercase().trim().to_string();
|
||||
if q.is_empty() { return Vec::new(); }
|
||||
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
let mut results = Vec::new();
|
||||
|
||||
// 1. 完整匹配
|
||||
if let Some(matches) = self.search_index.get(&q) {
|
||||
for album in matches {
|
||||
if seen.insert(album.id) { results.push(album.clone()); }
|
||||
}
|
||||
}
|
||||
// 2. 单词匹配
|
||||
for word in q.split_whitespace() {
|
||||
if word.len() >= 2 {
|
||||
if let Some(matches) = self.search_index.get(word) {
|
||||
for album in matches {
|
||||
if seen.insert(album.id) { results.push(album.clone()); }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 3. 模糊搜索
|
||||
if results.is_empty() {
|
||||
for album in &self.albums {
|
||||
let tl = album.title.to_lowercase();
|
||||
if tl.contains(&q) {
|
||||
if seen.insert(album.id) { results.push(album.clone()); }
|
||||
}
|
||||
}
|
||||
}
|
||||
// 4. 部分匹配
|
||||
if results.is_empty() && q.len() >= 2 {
|
||||
for album in &self.albums {
|
||||
let tl = album.title.to_lowercase();
|
||||
if q.split_whitespace().any(|w| w.len() >= 2 && tl.contains(w)) {
|
||||
if seen.insert(album.id) { results.push(album.clone()); }
|
||||
}
|
||||
}
|
||||
}
|
||||
results
|
||||
}
|
||||
}
|
||||
|
||||
/// 标签缓存
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct TagCache {
|
||||
pub popular_tags: Vec<TagStat>,
|
||||
pub last_updated: chrono::DateTime<chrono::Utc>,
|
||||
}
|
||||
|
||||
impl TagCache {
|
||||
pub fn update(&mut self, tags: Vec<TagStat>) {
|
||||
self.popular_tags = tags;
|
||||
self.last_updated = chrono::Utc::now();
|
||||
info!("标签缓存已更新,共 {} 个热门标签", self.popular_tags.len());
|
||||
}
|
||||
}
|
||||
|
||||
/// 人物缓存
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct PersonCache {
|
||||
pub persons: Vec<PersonWithCounts>,
|
||||
pub last_updated: chrono::DateTime<chrono::Utc>,
|
||||
}
|
||||
|
||||
impl PersonCache {
|
||||
pub fn update(&mut self, persons: Vec<PersonWithCounts>) {
|
||||
self.persons = persons;
|
||||
self.last_updated = chrono::Utc::now();
|
||||
info!("人物缓存已更新,共 {} 人", self.persons.len());
|
||||
}
|
||||
}
|
||||
|
||||
/// 共享缓存
|
||||
pub struct AppCache {
|
||||
pub albums: Arc<RwLock<AlbumsCache>>,
|
||||
pub tags: Arc<RwLock<TagCache>>,
|
||||
pub persons: Arc<RwLock<PersonCache>>,
|
||||
}
|
||||
|
||||
impl AppCache {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
albums: Arc::new(RwLock::new(AlbumsCache::new())),
|
||||
tags: Arc::new(RwLock::new(TagCache::default())),
|
||||
persons: Arc::new(RwLock::new(PersonCache::default())),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,9 @@ pub struct Config {
|
||||
pub server: ServerConfig,
|
||||
pub storage: StorageConfig,
|
||||
pub queue: QueueConfig,
|
||||
pub api_server: Option<ApiServerConfig>,
|
||||
#[serde(default)]
|
||||
pub old_db: Option<OldDbConfig>,
|
||||
}
|
||||
|
||||
|
||||
@@ -35,15 +38,37 @@ pub struct QueueConfig {
|
||||
pub idle_request_rate_threshold: f64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct ApiServerConfig {
|
||||
pub url: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct OldDbConfig {
|
||||
pub path: String,
|
||||
#[serde(default = "default_auto_migrate")]
|
||||
pub auto_migrate: bool,
|
||||
#[serde(default = "default_auto_parse")]
|
||||
pub auto_parse: bool,
|
||||
}
|
||||
|
||||
fn default_auto_migrate() -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn default_auto_parse() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn load() -> Result<Self, config::ConfigError> {
|
||||
let config = config::Config::builder()
|
||||
.add_source(config::File::with_name("config"))
|
||||
.build()?;
|
||||
|
||||
|
||||
config.try_deserialize()
|
||||
}
|
||||
|
||||
|
||||
pub fn server_address(&self) -> String {
|
||||
format!("{}:{}", self.server.host, self.server.port)
|
||||
}
|
||||
|
||||
846
src/database.rs
846
src/database.rs
@@ -1,846 +0,0 @@
|
||||
use sqlx::{SqlitePool, sqlite::SqlitePoolOptions, Row};
|
||||
use crate::config::Config;
|
||||
use crate::models::*;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub struct Database {
|
||||
pub pool: SqlitePool,
|
||||
config: Config,
|
||||
}
|
||||
|
||||
impl Database {
|
||||
pub async fn new(config: &Config) -> Result<Self, sqlx::Error> {
|
||||
// 从URL中提取文件路径
|
||||
let url = &config.database.url;
|
||||
let file_path = if url.starts_with("sqlite:") {
|
||||
&url[7..] // 移除 "sqlite:" 前缀
|
||||
} else {
|
||||
url
|
||||
};
|
||||
|
||||
// 确保数据目录存在
|
||||
if let Some(parent) = std::path::Path::new(file_path).parent() {
|
||||
if !parent.exists() {
|
||||
std::fs::create_dir_all(parent)?;
|
||||
}
|
||||
}
|
||||
|
||||
// 如果数据库文件不存在,创建一个空文件
|
||||
if !std::path::Path::new(file_path).exists() {
|
||||
std::fs::File::create(file_path)?;
|
||||
println!("创建新的数据库文件: {}", file_path);
|
||||
}
|
||||
|
||||
let pool = SqlitePoolOptions::new()
|
||||
.max_connections(10) // 优化:减少最大连接数,避免资源浪费
|
||||
.min_connections(2) // 优化:减少最小连接数
|
||||
.acquire_timeout(std::time::Duration::from_secs(5)) // 优化:减少获取连接超时时间
|
||||
.idle_timeout(std::time::Duration::from_secs(300)) // 优化:减少空闲超时时间
|
||||
.max_lifetime(std::time::Duration::from_secs(1800)) // 优化:减少最大生命周期
|
||||
.connect(url)
|
||||
.await?;
|
||||
|
||||
// 在迁移之前设置SQLite性能优化参数
|
||||
// 这些PRAGMA语句必须在事务外执行
|
||||
sqlx::query("PRAGMA journal_mode=WAL;").execute(&pool).await?;
|
||||
sqlx::query("PRAGMA wal_autocheckpoint=4000;").execute(&pool).await?;
|
||||
sqlx::query("PRAGMA synchronous=NORMAL;").execute(&pool).await?;
|
||||
sqlx::query("PRAGMA cache_size=-64000;").execute(&pool).await?;
|
||||
sqlx::query("PRAGMA mmap_size=268435456;").execute(&pool).await?;
|
||||
sqlx::query("PRAGMA temp_store=MEMORY;").execute(&pool).await?;
|
||||
sqlx::query("PRAGMA foreign_keys=ON;").execute(&pool).await?;
|
||||
|
||||
// 创建表
|
||||
sqlx::migrate!("./migrations").run(&pool).await?;
|
||||
|
||||
// 确保image_count字段存在并正确初始化
|
||||
let db = Database {
|
||||
pool,
|
||||
config: config.clone(), // 保存配置副本
|
||||
};
|
||||
db.ensure_image_count_field().await?;
|
||||
|
||||
Ok(db)
|
||||
}
|
||||
|
||||
// pub async fn get_albums(&self, page: Option<usize>, limit: Option<usize>) -> Result<Vec<AlbumInfo>, sqlx::Error> {
|
||||
// let page = page.unwrap_or(1);
|
||||
// let limit = limit.unwrap_or(50); // 减少默认加载数量,提高响应速度
|
||||
// let offset = (page - 1) * limit;
|
||||
//
|
||||
// let albums = sqlx::query(
|
||||
// r#"
|
||||
// SELECT
|
||||
// id,
|
||||
// title,
|
||||
// document,
|
||||
// description,
|
||||
// cover_image_id,
|
||||
// image_count,
|
||||
// created_at
|
||||
// FROM albums
|
||||
// ORDER BY created_at DESC
|
||||
// LIMIT ? OFFSET ?
|
||||
// "#
|
||||
// )
|
||||
// .bind(limit as i64)
|
||||
// .bind(offset as i64)
|
||||
// .fetch_all(&self.pool)
|
||||
// .await?;
|
||||
//
|
||||
// let mut result = Vec::new();
|
||||
// for row in albums {
|
||||
// let id_str = row.get::<String, _>("id");
|
||||
// let id = match Uuid::parse_str(&id_str) {
|
||||
// Ok(uuid) => uuid,
|
||||
// Err(e) => {
|
||||
// eprintln!("无效的UUID格式: {} - 错误: {}", id_str, e);
|
||||
// continue; // 跳过无效的记录
|
||||
// }
|
||||
// };
|
||||
//
|
||||
// result.push(AlbumInfo {
|
||||
// id,
|
||||
// title: row.get("title"),
|
||||
// document: row.get("document"),
|
||||
// description: row.get("description"),
|
||||
// cover_image_id: row.get("cover_image_id"),
|
||||
// image_count: row.get("image_count"),
|
||||
// created_at: row.get::<String, _>("created_at"),
|
||||
// });
|
||||
// }
|
||||
//
|
||||
// Ok(result)
|
||||
// }
|
||||
// 相册操作
|
||||
pub async fn get_all_albums(&self) -> Result<Vec<AlbumInfo>, sqlx::Error> {
|
||||
|
||||
let albums = sqlx::query(
|
||||
r#"
|
||||
SELECT
|
||||
id,
|
||||
title,
|
||||
document,
|
||||
description,
|
||||
cover_image_id,
|
||||
image_count,
|
||||
created_at
|
||||
FROM albums
|
||||
ORDER BY created_at DESC
|
||||
"#
|
||||
)
|
||||
.fetch_all(&self.pool)
|
||||
.await?;
|
||||
|
||||
let mut result = Vec::new();
|
||||
for row in albums {
|
||||
let id_str = row.get::<String, _>("id");
|
||||
let id = match Uuid::parse_str(&id_str) {
|
||||
Ok(uuid) => uuid,
|
||||
Err(e) => {
|
||||
eprintln!("无效的UUID格式: {} - 错误: {}", id_str, e);
|
||||
continue; // 跳过无效的记录
|
||||
}
|
||||
};
|
||||
|
||||
result.push(AlbumInfo {
|
||||
id,
|
||||
title: row.get("title"),
|
||||
document: row.get("document"),
|
||||
description: row.get("description"),
|
||||
cover_image_id: row.get("cover_image_id"),
|
||||
image_count: row.get("image_count"),
|
||||
created_at: row.get::<String, _>("created_at"),
|
||||
});
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
// pub async fn search_albums(&self, query: &str, page: Option<usize>, limit: Option<usize>) -> Result<Vec<AlbumInfo>, sqlx::Error> {
|
||||
// let search_pattern = format!("%{}%", query);
|
||||
// let page = page.unwrap_or(1);
|
||||
// let limit = limit.unwrap_or(50); // 减少默认加载数量,提高响应速度
|
||||
// let offset = (page - 1) * limit;
|
||||
//
|
||||
// let albums = sqlx::query(
|
||||
// r#"
|
||||
// SELECT
|
||||
// id,
|
||||
// title,
|
||||
// document,
|
||||
// description,
|
||||
// cover_image_id,
|
||||
// image_count,
|
||||
// created_at
|
||||
// FROM albums
|
||||
// WHERE title LIKE ?
|
||||
// ORDER BY created_at DESC
|
||||
// LIMIT ? OFFSET ?
|
||||
// "#
|
||||
// )
|
||||
// .bind(&search_pattern)
|
||||
// .bind(limit as i64)
|
||||
// .bind(offset as i64)
|
||||
// .fetch_all(&self.pool)
|
||||
// .await?;
|
||||
//
|
||||
// let mut result = Vec::new();
|
||||
// for row in albums {
|
||||
// let id_str = row.get::<String, _>("id");
|
||||
// let id = match Uuid::parse_str(&id_str) {
|
||||
// Ok(uuid) => uuid,
|
||||
// Err(e) => {
|
||||
// eprintln!("无效的UUID格式: {} - 错误: {}", id_str, e);
|
||||
// continue; // 跳过无效的记录
|
||||
// }
|
||||
// };
|
||||
//
|
||||
// result.push(AlbumInfo {
|
||||
// id,
|
||||
// title: row.get("title"),
|
||||
// document: row.get("document"),
|
||||
// description: row.get("description"),
|
||||
// cover_image_id: row.get("cover_image_id"),
|
||||
// image_count: row.get("image_count"),
|
||||
// created_at: row.get::<String, _>("created_at"),
|
||||
// });
|
||||
// }
|
||||
//
|
||||
// Ok(result)
|
||||
// }
|
||||
|
||||
pub async fn get_album_with_images(&self, album_id: Uuid) -> Result<Option<AlbumWithImages>, sqlx::Error> {
|
||||
// 获取相册基本信息
|
||||
let album_row = sqlx::query(
|
||||
"SELECT * FROM albums WHERE id = ?"
|
||||
)
|
||||
.bind(album_id.to_string())
|
||||
.fetch_optional(&self.pool)
|
||||
.await?;
|
||||
|
||||
if let Some(row) = album_row {
|
||||
let id_str = row.get::<String, _>("id");
|
||||
let id = match Uuid::parse_str(&id_str) {
|
||||
Ok(uuid) => uuid,
|
||||
Err(e) => {
|
||||
eprintln!("无效的相册UUID格式: {} - 错误: {}", id_str, e);
|
||||
return Ok(None);
|
||||
}
|
||||
};
|
||||
|
||||
let cover_image_id = match row.get::<Option<String>, _>("cover_image_id") {
|
||||
Some(s) => match Uuid::parse_str(&s) {
|
||||
Ok(uuid) => Some(uuid),
|
||||
Err(e) => {
|
||||
eprintln!("无效的封面图片UUID格式: {} - 错误: {}", s, e);
|
||||
None
|
||||
}
|
||||
},
|
||||
None => None
|
||||
};
|
||||
|
||||
let album = Album {
|
||||
id,
|
||||
title: row.get("title"),
|
||||
description: row.get("description"),
|
||||
cover_image_id,
|
||||
image_count: row.get("image_count"),
|
||||
created_at: row.get::<String, _>("created_at"),
|
||||
updated_at: row.get::<String, _>("updated_at"),
|
||||
};
|
||||
|
||||
// 获取相册中的图片
|
||||
let image_rows = sqlx::query(
|
||||
"SELECT * FROM images WHERE album_id = ?"
|
||||
)
|
||||
.bind(album_id.to_string())
|
||||
.fetch_all(&self.pool)
|
||||
.await?;
|
||||
|
||||
let mut images = Vec::new();
|
||||
for row in image_rows {
|
||||
let image_id_str = row.get::<String, _>("id");
|
||||
let image_id = match Uuid::parse_str(&image_id_str) {
|
||||
Ok(uuid) => uuid,
|
||||
Err(e) => {
|
||||
eprintln!("无效的图片UUID格式: {} - 错误: {}", image_id_str, e);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let album_id_str = row.get::<String, _>("album_id");
|
||||
let album_id = match Uuid::parse_str(&album_id_str) {
|
||||
Ok(uuid) => uuid,
|
||||
Err(e) => {
|
||||
eprintln!("无效的相册UUID格式: {} - 错误: {}", album_id_str, e);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
images.push(Image {
|
||||
id: image_id,
|
||||
album_id,
|
||||
filename: row.get("filename"),
|
||||
original_url: row.get("original_url"),
|
||||
title: row.get("title"),
|
||||
description: row.get("description"),
|
||||
file_size: row.get("file_size"),
|
||||
width: row.get("width"),
|
||||
height: row.get("height"),
|
||||
mime_type: row.get("mime_type"),
|
||||
created_at: row.get::<String, _>("created_at"),
|
||||
});
|
||||
}
|
||||
|
||||
let image_count = images.len() as i64;
|
||||
|
||||
Ok(Some(AlbumWithImages {
|
||||
album,
|
||||
images,
|
||||
image_count,
|
||||
}))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_album(&self, album: CreateAlbum) -> Result<Uuid, sqlx::Error> {
|
||||
let id = Uuid::new_v4();
|
||||
let now = chrono::Utc::now().to_rfc3339();
|
||||
|
||||
sqlx::query(
|
||||
"INSERT INTO albums (id, title, document, description, image_count, created_at, updated_at) VALUES (?, ?, ?, ?, 0, ?, ?)"
|
||||
)
|
||||
.bind(id.to_string())
|
||||
.bind(album.title)
|
||||
.bind(album.document)
|
||||
.bind(album.description)
|
||||
.bind(&now)
|
||||
.bind(&now)
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
// 图片操作
|
||||
// pub async fn add_image(&self, image: CreateImage) -> Result<Uuid, sqlx::Error> {
|
||||
// let id = Uuid::new_v4();
|
||||
// let now = chrono::Utc::now().to_rfc3339();
|
||||
//
|
||||
// sqlx::query(
|
||||
// "INSERT INTO images (id, album_id, filename, original_url, title, description, file_size, width, height, mime_type, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
|
||||
// )
|
||||
// .bind(id.to_string())
|
||||
// .bind(image.album_id.to_string())
|
||||
// .bind(image.filename)
|
||||
// .bind(image.original_url)
|
||||
// .bind(image.title)
|
||||
// .bind(image.description)
|
||||
// .bind(image.file_size)
|
||||
// .bind(image.width)
|
||||
// .bind(image.height)
|
||||
// .bind(image.mime_type)
|
||||
// .bind(&now)
|
||||
// .execute(&self.pool)
|
||||
// .await?;
|
||||
//
|
||||
// // 更新相册的图片数量
|
||||
// self.update_album_image_count(image.album_id).await?;
|
||||
//
|
||||
// // 如果这是相册的第一张图片,设置为封面
|
||||
// let image_count = sqlx::query_scalar::<_, i64>(
|
||||
// "SELECT COUNT(*) FROM images WHERE album_id = ?"
|
||||
// )
|
||||
// .bind(image.album_id.to_string())
|
||||
// .fetch_one(&self.pool)
|
||||
// .await?;
|
||||
//
|
||||
// if image_count == 1 {
|
||||
// sqlx::query(
|
||||
// "UPDATE albums SET cover_image_id = ? WHERE id = ?"
|
||||
// )
|
||||
// .bind(id.to_string())
|
||||
// .bind(image.album_id.to_string())
|
||||
// .execute(&self.pool)
|
||||
// .await?;
|
||||
// }
|
||||
//
|
||||
// Ok(id)
|
||||
// }
|
||||
|
||||
/// 更新相册的图片数量
|
||||
pub async fn update_album_image_count(&self, album_id: Uuid) -> Result<(), sqlx::Error> {
|
||||
sqlx::query(
|
||||
r#"
|
||||
UPDATE albums
|
||||
SET image_count = (
|
||||
SELECT COUNT(*) FROM images WHERE album_id = ?
|
||||
),
|
||||
updated_at = datetime('now')
|
||||
WHERE id = ?
|
||||
"#
|
||||
)
|
||||
.bind(album_id.to_string())
|
||||
.bind(album_id.to_string())
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn add_images(&self, images: Vec<CreateImage>) -> Result<Vec<Uuid>, sqlx::Error> {
|
||||
if images.is_empty() {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
|
||||
// 由于生命周期问题,简化实现,不使用重试机制
|
||||
let now = chrono::Utc::now().to_rfc3339();
|
||||
let mut ids = Vec::new();
|
||||
|
||||
// 保存第一个相册ID用于封面设置
|
||||
let first_album_id = images.first().map(|img| img.album_id);
|
||||
|
||||
// 使用事务确保批量插入的原子性
|
||||
let mut tx = self.pool.begin().await?;
|
||||
|
||||
for image in &images {
|
||||
let id = Uuid::new_v4();
|
||||
ids.push(id);
|
||||
|
||||
sqlx::query(
|
||||
"INSERT INTO images (id, album_id, filename, original_url, title, description, file_size, width, height, mime_type, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
|
||||
)
|
||||
.bind(id.to_string())
|
||||
.bind(image.album_id.to_string())
|
||||
.bind(&image.filename)
|
||||
.bind(&image.original_url)
|
||||
.bind(&image.title)
|
||||
.bind(&image.description)
|
||||
.bind(image.file_size)
|
||||
.bind(image.width)
|
||||
.bind(image.height)
|
||||
.bind(&image.mime_type)
|
||||
.bind(&now)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
}
|
||||
|
||||
// 提交事务
|
||||
tx.commit().await?;
|
||||
|
||||
// 批量设置封面图片(只对每个相册的第一批图片设置)
|
||||
if let Some(album_id) = first_album_id {
|
||||
// 检查相册是否已有图片
|
||||
let existing_count = sqlx::query_scalar::<_, i64>(
|
||||
"SELECT COUNT(*) FROM images WHERE album_id = ?"
|
||||
)
|
||||
.bind(album_id.to_string())
|
||||
.fetch_one(&self.pool)
|
||||
.await?;
|
||||
|
||||
// 如果这是相册的第一批图片,设置第一张为封面
|
||||
if existing_count == images.len() as i64 {
|
||||
if let Some(first_id) = ids.first() {
|
||||
sqlx::query(
|
||||
"UPDATE albums SET cover_image_id = ? WHERE id = ?"
|
||||
)
|
||||
.bind(first_id.to_string())
|
||||
.bind(album_id.to_string())
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
|
||||
// 更新相册的图片数量
|
||||
self.update_album_image_count(album_id).await?;
|
||||
}
|
||||
|
||||
Ok(ids)
|
||||
}
|
||||
|
||||
/// 获取相册中的图片数量(直接从image_count字段读取)
|
||||
pub async fn get_album_image_count(&self, album_id: Uuid) -> Result<i64, sqlx::Error> {
|
||||
let count = sqlx::query_scalar::<_, i64>(
|
||||
"SELECT COUNT(*) FROM images WHERE album_id = ?"
|
||||
)
|
||||
.bind(album_id.to_string())
|
||||
.fetch_one(&self.pool)
|
||||
.await?;
|
||||
|
||||
Ok(count)
|
||||
}
|
||||
|
||||
/// 确保image_count字段存在并正确初始化
|
||||
pub async fn ensure_image_count_field(&self) -> Result<(), sqlx::Error> {
|
||||
// 检查字段是否存在
|
||||
let exists: i64 = sqlx::query_scalar(
|
||||
"SELECT COUNT(*) FROM pragma_table_info('albums') WHERE name = 'image_count'"
|
||||
)
|
||||
.fetch_one(&self.pool)
|
||||
.await?;
|
||||
|
||||
if exists == 0 {
|
||||
// 字段不存在,添加字段
|
||||
sqlx::query("ALTER TABLE albums ADD COLUMN image_count INTEGER NOT NULL DEFAULT 0")
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
|
||||
// 更新现有数据的image_count
|
||||
sqlx::query(
|
||||
"UPDATE albums SET image_count = (
|
||||
SELECT COUNT(*) FROM images WHERE images.album_id = albums.id
|
||||
)"
|
||||
)
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
|
||||
println!("✅ image_count字段已成功添加并初始化");
|
||||
} else {
|
||||
println!("ℹ️ image_count字段已存在,跳过初始化");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 删除相册及其关联的图片
|
||||
pub async fn delete_album(&self, album_id: Uuid) -> Result<(), sqlx::Error> {
|
||||
let mut tx = self.pool.begin().await?;
|
||||
|
||||
// 删除相册关联的图片
|
||||
sqlx::query("DELETE FROM images WHERE album_id = ?")
|
||||
.bind(album_id.to_string())
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
|
||||
// 删除相册
|
||||
sqlx::query("DELETE FROM albums WHERE id = ?")
|
||||
.bind(album_id.to_string())
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
|
||||
tx.commit().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// pub async fn delete_image(&self, image_id: Uuid) -> Result<bool, sqlx::Error> {
|
||||
// // 先获取图片所属的相册ID
|
||||
// let album_id_result = sqlx::query_scalar::<_, String>(
|
||||
// r#"
|
||||
// SELECT album_id FROM images WHERE id = ?
|
||||
// "#
|
||||
// )
|
||||
// .bind(image_id.to_string())
|
||||
// .fetch_optional(&self.pool)
|
||||
// .await?;
|
||||
//
|
||||
// let result = sqlx::query(
|
||||
// r#"
|
||||
// DELETE FROM images
|
||||
// WHERE id = ?
|
||||
// "#
|
||||
// )
|
||||
// .bind(image_id.to_string())
|
||||
// .execute(&self.pool)
|
||||
// .await?;
|
||||
//
|
||||
// // 如果删除成功且找到了相册ID,更新相册图片数量
|
||||
// if result.rows_affected() > 0 {
|
||||
// if let Some(album_id_str) = album_id_result {
|
||||
// if let Ok(album_id) = Uuid::parse_str(&album_id_str) {
|
||||
// self.update_album_image_count(album_id).await?;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// Ok(result.rows_affected() > 0)
|
||||
// }
|
||||
|
||||
pub async fn update_album_cover(&self, album_id: Uuid, cover_image_id: Option<Uuid>) -> Result<(), sqlx::Error> {
|
||||
let cover_id_str = cover_image_id.map(|id| id.to_string());
|
||||
|
||||
sqlx::query(
|
||||
"UPDATE albums SET cover_image_id = ?, updated_at = datetime('now') WHERE id = ?"
|
||||
)
|
||||
.bind(cover_id_str)
|
||||
.bind(album_id.to_string())
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get_image(&self, image_id: Uuid) -> Result<Option<Image>, sqlx::Error> {
|
||||
let row = sqlx::query(
|
||||
"SELECT * FROM images WHERE id = ?"
|
||||
)
|
||||
.bind(image_id.to_string())
|
||||
.fetch_optional(&self.pool)
|
||||
.await?;
|
||||
|
||||
if let Some(row) = row {
|
||||
let image_id_str = row.get::<String, _>("id");
|
||||
let image_id = match Uuid::parse_str(&image_id_str) {
|
||||
Ok(uuid) => uuid,
|
||||
Err(e) => {
|
||||
eprintln!("无效的图片UUID格式: {} - 错误: {}", image_id_str, e);
|
||||
return Ok(None);
|
||||
}
|
||||
};
|
||||
|
||||
let album_id_str = row.get::<String, _>("album_id");
|
||||
let album_id = match Uuid::parse_str(&album_id_str) {
|
||||
Ok(uuid) => uuid,
|
||||
Err(e) => {
|
||||
eprintln!("无效的相册UUID格式: {} - 错误: {}", album_id_str, e);
|
||||
return Ok(None);
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Some(Image {
|
||||
id: image_id,
|
||||
album_id,
|
||||
filename: row.get("filename"),
|
||||
original_url: row.get("original_url"),
|
||||
title: row.get("title"),
|
||||
description: row.get("description"),
|
||||
file_size: row.get("file_size"),
|
||||
width: row.get("width"),
|
||||
height: row.get("height"),
|
||||
mime_type: row.get("mime_type"),
|
||||
created_at: row.get::<String, _>("created_at"),
|
||||
}))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
// 收藏功能数据库操作
|
||||
|
||||
// 获取用户的收藏列表
|
||||
pub async fn get_user_favorites(&self, user_id: &str) -> Result<Vec<FavoriteWithImage>, sqlx::Error> {
|
||||
let rows = sqlx::query(
|
||||
r#"
|
||||
SELECT
|
||||
f.id as favorite_id,
|
||||
f.user_id,
|
||||
f.image_id,
|
||||
f.album_id,
|
||||
f.created_at as favorite_created_at,
|
||||
i.id as image_id,
|
||||
i.album_id,
|
||||
i.filename,
|
||||
i.original_url,
|
||||
i.title,
|
||||
i.description,
|
||||
i.file_size,
|
||||
i.width,
|
||||
i.height,
|
||||
i.mime_type,
|
||||
i.created_at as image_created_at,
|
||||
a.title as album_title
|
||||
FROM favorites f
|
||||
JOIN images i ON f.image_id = i.id
|
||||
JOIN albums a ON f.album_id = a.id
|
||||
WHERE f.user_id = ?
|
||||
ORDER BY f.created_at DESC
|
||||
"#
|
||||
)
|
||||
.bind(user_id)
|
||||
.fetch_all(&self.pool)
|
||||
.await?;
|
||||
|
||||
let mut favorites = Vec::new();
|
||||
for row in rows {
|
||||
let favorite_id_str = row.get::<String, _>("favorite_id");
|
||||
let favorite_id = match Uuid::parse_str(&favorite_id_str) {
|
||||
Ok(uuid) => uuid,
|
||||
Err(e) => {
|
||||
eprintln!("无效的收藏UUID格式: {} - 错误: {}", favorite_id_str, e);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let image_id_str = row.get::<String, _>("image_id");
|
||||
let image_id = match Uuid::parse_str(&image_id_str) {
|
||||
Ok(uuid) => uuid,
|
||||
Err(e) => {
|
||||
eprintln!("无效的图片UUID格式: {} - 错误: {}", image_id_str, e);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let album_id_str = row.get::<String, _>("album_id");
|
||||
let album_id = match Uuid::parse_str(&album_id_str) {
|
||||
Ok(uuid) => uuid,
|
||||
Err(e) => {
|
||||
eprintln!("无效的相册UUID格式: {} - 错误: {}", album_id_str, e);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let favorite = Favorite {
|
||||
id: favorite_id,
|
||||
user_id: row.get("user_id"),
|
||||
image_id,
|
||||
album_id,
|
||||
created_at: row.get::<String, _>("favorite_created_at"),
|
||||
};
|
||||
|
||||
let image = Image {
|
||||
id: image_id,
|
||||
album_id,
|
||||
filename: row.get("filename"),
|
||||
original_url: row.get("original_url"),
|
||||
title: row.get("title"),
|
||||
description: row.get("description"),
|
||||
file_size: row.get("file_size"),
|
||||
width: row.get("width"),
|
||||
height: row.get("height"),
|
||||
mime_type: row.get("mime_type"),
|
||||
created_at: row.get::<String, _>("image_created_at"),
|
||||
};
|
||||
|
||||
favorites.push(FavoriteWithImage {
|
||||
favorite,
|
||||
image,
|
||||
album_title: row.get("album_title"),
|
||||
});
|
||||
}
|
||||
|
||||
Ok(favorites)
|
||||
}
|
||||
|
||||
// 检查图片是否被用户收藏
|
||||
pub async fn get_favorite_status(&self, user_id: &str, image_id: Uuid) -> Result<FavoriteStatus, sqlx::Error> {
|
||||
let row = sqlx::query(
|
||||
"SELECT id FROM favorites WHERE user_id = ? AND image_id = ?"
|
||||
)
|
||||
.bind(user_id)
|
||||
.bind(image_id.to_string())
|
||||
.fetch_optional(&self.pool)
|
||||
.await?;
|
||||
|
||||
if let Some(row) = row {
|
||||
let favorite_id_str = row.get::<String, _>("id");
|
||||
let favorite_id = match Uuid::parse_str(&favorite_id_str) {
|
||||
Ok(uuid) => Some(uuid),
|
||||
Err(e) => {
|
||||
eprintln!("无效的收藏UUID格式: {} - 错误: {}", favorite_id_str, e);
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
Ok(FavoriteStatus {
|
||||
is_favorited: true,
|
||||
favorite_id,
|
||||
})
|
||||
} else {
|
||||
Ok(FavoriteStatus {
|
||||
is_favorited: false,
|
||||
favorite_id: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 添加收藏
|
||||
pub async fn add_favorite(&self, user_id: &str, favorite_data: CreateFavorite) -> Result<Uuid, sqlx::Error> {
|
||||
let favorite_id = Uuid::new_v4();
|
||||
let now = chrono::Utc::now().to_rfc3339();
|
||||
|
||||
sqlx::query(
|
||||
"INSERT INTO favorites (id, user_id, image_id, album_id, created_at) VALUES (?, ?, ?, ?, ?)"
|
||||
)
|
||||
.bind(favorite_id.to_string())
|
||||
.bind(user_id)
|
||||
.bind(favorite_data.image_id.to_string())
|
||||
.bind(favorite_data.album_id.to_string())
|
||||
.bind(&now)
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
|
||||
Ok(favorite_id)
|
||||
}
|
||||
|
||||
// 移除收藏(通过收藏ID)
|
||||
pub async fn remove_favorite(&self, favorite_id: Uuid) -> Result<(), sqlx::Error> {
|
||||
sqlx::query(
|
||||
"DELETE FROM favorites WHERE id = ?"
|
||||
)
|
||||
.bind(favorite_id.to_string())
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// 移除收藏(通过用户ID和图片ID)
|
||||
pub async fn remove_favorite_by_image(&self, user_id: &str, image_id: Uuid) -> Result<(), sqlx::Error> {
|
||||
sqlx::query(
|
||||
"DELETE FROM favorites WHERE user_id = ? AND image_id = ?"
|
||||
)
|
||||
.bind(user_id)
|
||||
.bind(image_id.to_string())
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 获取配置
|
||||
pub fn get_config(&self) -> &Config {
|
||||
&self.config
|
||||
}
|
||||
|
||||
/// 获取未处理的图片(即缩略图可能不存在的图片)
|
||||
pub async fn get_unprocessed_images(&self, limit: i64) -> Result<Vec<Image>, sqlx::Error> {
|
||||
let rows = sqlx::query(
|
||||
r#"
|
||||
SELECT * FROM images
|
||||
ORDER BY created_at DESC
|
||||
LIMIT ?
|
||||
"#
|
||||
)
|
||||
.bind(limit)
|
||||
.fetch_all(&self.pool)
|
||||
.await?;
|
||||
|
||||
let mut images = Vec::new();
|
||||
for row in rows {
|
||||
let image_id_str = row.get::<String, _>("id");
|
||||
let image_id = match Uuid::parse_str(&image_id_str) {
|
||||
Ok(uuid) => uuid,
|
||||
Err(e) => {
|
||||
eprintln!("无效的图片UUID格式: {} - 错误: {}", image_id_str, e);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let album_id_str = row.get::<String, _>("album_id");
|
||||
let album_id = match Uuid::parse_str(&album_id_str) {
|
||||
Ok(uuid) => uuid,
|
||||
Err(e) => {
|
||||
eprintln!("无效的相册UUID格式: {} - 错误: {}", album_id_str, e);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
images.push(Image {
|
||||
id: image_id,
|
||||
album_id,
|
||||
filename: row.get("filename"),
|
||||
original_url: row.get("original_url"),
|
||||
title: row.get("title"),
|
||||
description: row.get("description"),
|
||||
file_size: row.get("file_size"),
|
||||
width: row.get("width"),
|
||||
height: row.get("height"),
|
||||
mime_type: row.get("mime_type"),
|
||||
created_at: row.get::<String, _>("created_at"),
|
||||
});
|
||||
}
|
||||
|
||||
Ok(images)
|
||||
}
|
||||
}
|
||||
132
src/db/album.rs
Normal file
132
src/db/album.rs
Normal file
@@ -0,0 +1,132 @@
|
||||
use super::Database;
|
||||
use crate::models::*;
|
||||
use sqlx::Row;
|
||||
use uuid::Uuid;
|
||||
|
||||
impl Database {
|
||||
// ── 查询 ──
|
||||
|
||||
pub async fn get_all_albums(&self) -> Result<Vec<AlbumInfo>, sqlx::Error> {
|
||||
let rows = sqlx::query(
|
||||
r#"SELECT id, title, document, description, cover_image_id, image_count, created_at
|
||||
FROM albums ORDER BY created_at DESC"#
|
||||
).fetch_all(&self.pool).await?;
|
||||
|
||||
Ok(rows.iter().map(|r| {
|
||||
let id_str = r.get::<String, _>("id");
|
||||
AlbumInfo {
|
||||
id: Uuid::parse_str(&id_str).unwrap_or_else(|_| Uuid::nil()),
|
||||
title: r.get("title"),
|
||||
document: r.get("document"),
|
||||
description: r.get("description"),
|
||||
cover_image_id: r.get("cover_image_id"),
|
||||
image_count: r.get("image_count"),
|
||||
created_at: r.get("created_at"),
|
||||
}
|
||||
}).collect())
|
||||
}
|
||||
|
||||
pub async fn get_album_with_images(&self, album_id: Uuid) -> Result<Option<AlbumWithImages>, sqlx::Error> {
|
||||
let row = sqlx::query("SELECT * FROM albums WHERE id = ?")
|
||||
.bind(album_id.to_string())
|
||||
.fetch_optional(&self.pool).await?;
|
||||
|
||||
let Some(row) = row else { return Ok(None) };
|
||||
|
||||
let id_str = row.get::<String, _>("id");
|
||||
let id = Uuid::parse_str(&id_str).unwrap_or_else(|_| Uuid::nil());
|
||||
let cover = row.get::<Option<String>, _>("cover_image_id")
|
||||
.and_then(|s| Uuid::parse_str(&s).ok());
|
||||
|
||||
let album = Album {
|
||||
id,
|
||||
title: row.get("title"),
|
||||
description: row.get("description"),
|
||||
cover_image_id: cover,
|
||||
image_count: row.get("image_count"),
|
||||
created_at: row.get("created_at"),
|
||||
updated_at: row.get("updated_at"),
|
||||
person_name_cleaned: row.get("person_name_cleaned"),
|
||||
tags: row.get("tags"),
|
||||
};
|
||||
|
||||
let img_rows = sqlx::query("SELECT * FROM images WHERE album_id = ?")
|
||||
.bind(album_id.to_string()).fetch_all(&self.pool).await?;
|
||||
|
||||
let mut images = Vec::new();
|
||||
for r in &img_rows {
|
||||
let img_id = Uuid::parse_str(&r.get::<String, _>("id")).unwrap_or_else(|_| Uuid::nil());
|
||||
let aid = Uuid::parse_str(&r.get::<String, _>("album_id")).unwrap_or_else(|_| Uuid::nil());
|
||||
images.push(super::image::Image {
|
||||
id: img_id, album_id: aid,
|
||||
filename: r.get("filename"),
|
||||
original_url: r.get("original_url"),
|
||||
title: r.get("title"),
|
||||
description: r.get("description"),
|
||||
file_size: r.get("file_size"),
|
||||
width: r.get("width"),
|
||||
height: r.get("height"),
|
||||
mime_type: r.get("mime_type"),
|
||||
created_at: r.get("created_at"),
|
||||
});
|
||||
}
|
||||
|
||||
Ok(Some(AlbumWithImages { album, images: images.clone(), image_count: images.len() as i64 }))
|
||||
}
|
||||
|
||||
pub async fn get_all_albums_full(&self) -> Vec<AlbumMeta> {
|
||||
let has_col = sqlx::query_scalar::<_, i64>(
|
||||
"SELECT COUNT(*) FROM pragma_table_info('albums') WHERE name = 'person_name_original'"
|
||||
).fetch_one(&self.pool).await.unwrap_or(0);
|
||||
if has_col == 0 { return Vec::new(); }
|
||||
|
||||
let rows = sqlx::query(
|
||||
r#"SELECT id, title, person_name_original, person_name_cleaned, tags, all_words, raw_title, processed_at, image_count
|
||||
FROM albums ORDER BY created_at DESC"#
|
||||
).fetch_all(&self.pool).await.unwrap_or_default();
|
||||
|
||||
rows.iter().map(|r| AlbumMeta {
|
||||
id: r.get("id"), title: r.get("title"),
|
||||
person_name_original: r.get("person_name_original"),
|
||||
person_name_cleaned: r.get("person_name_cleaned"),
|
||||
tags: r.get("tags"), all_words: r.get("all_words"),
|
||||
raw_title: r.get("raw_title"), processed_at: r.get("processed_at"),
|
||||
image_count: r.get("image_count"),
|
||||
}).collect()
|
||||
}
|
||||
|
||||
// ── 写入 ──
|
||||
|
||||
pub async fn create_album(&self, album: CreateAlbum) -> Result<Uuid, sqlx::Error> {
|
||||
let id = Uuid::new_v4();
|
||||
let now = chrono::Utc::now().to_rfc3339();
|
||||
sqlx::query(
|
||||
"INSERT INTO albums (id, title, document, description, image_count, created_at, updated_at) VALUES (?, ?, ?, ?, 0, ?, ?)"
|
||||
).bind(id.to_string()).bind(album.title).bind(album.document).bind(album.description).bind(&now).bind(&now)
|
||||
.execute(&self.pool).await?;
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
pub async fn delete_album(&self, album_id: Uuid) -> Result<(), sqlx::Error> {
|
||||
sqlx::query("DELETE FROM albums WHERE id = ?").bind(album_id.to_string()).execute(&self.pool).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn update_album_cover(&self, album_id: Uuid, image_id: Uuid) -> Result<(), sqlx::Error> {
|
||||
sqlx::query("UPDATE albums SET cover_image_id = ? WHERE id = ?")
|
||||
.bind(image_id.to_string()).bind(album_id.to_string()).execute(&self.pool).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn update_album_image_count(&self, album_id: Uuid) -> Result<(), sqlx::Error> {
|
||||
sqlx::query(
|
||||
r#"UPDATE albums SET image_count = (SELECT COUNT(*) FROM images WHERE album_id = ?), updated_at = datetime('now') WHERE id = ?"#
|
||||
).bind(album_id.to_string()).bind(album_id.to_string()).execute(&self.pool).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get_album_image_count(&self, album_id: Uuid) -> Result<i64, sqlx::Error> {
|
||||
sqlx::query_scalar::<_, i64>("SELECT COUNT(*) FROM images WHERE album_id = ?")
|
||||
.bind(album_id.to_string()).fetch_one(&self.pool).await
|
||||
}
|
||||
}
|
||||
63
src/db/connection.rs
Normal file
63
src/db/connection.rs
Normal file
@@ -0,0 +1,63 @@
|
||||
use sqlx::{SqlitePool, sqlite::SqlitePoolOptions};
|
||||
use crate::config::Config;
|
||||
|
||||
pub struct Database {
|
||||
pub pool: SqlitePool,
|
||||
pub config: Config,
|
||||
}
|
||||
|
||||
impl Database {
|
||||
pub async fn new(config: &Config) -> Result<Self, sqlx::Error> {
|
||||
let url = &config.database.url;
|
||||
let file_path = if url.starts_with("sqlite:") { &url[7..] } else { url };
|
||||
|
||||
if let Some(parent) = std::path::Path::new(file_path).parent() {
|
||||
if !parent.exists() {
|
||||
std::fs::create_dir_all(parent)?;
|
||||
}
|
||||
}
|
||||
if !std::path::Path::new(file_path).exists() {
|
||||
std::fs::File::create(file_path)?;
|
||||
println!("创建新的数据库文件: {}", file_path);
|
||||
}
|
||||
|
||||
let pool = SqlitePoolOptions::new()
|
||||
.max_connections(10)
|
||||
.min_connections(2)
|
||||
.acquire_timeout(std::time::Duration::from_secs(5))
|
||||
.idle_timeout(std::time::Duration::from_secs(300))
|
||||
.max_lifetime(std::time::Duration::from_secs(1800))
|
||||
.connect(url)
|
||||
.await?;
|
||||
|
||||
// SQLite 性能优化
|
||||
sqlx::query("PRAGMA journal_mode=WAL;").execute(&pool).await?;
|
||||
sqlx::query("PRAGMA wal_autocheckpoint=4000;").execute(&pool).await?;
|
||||
sqlx::query("PRAGMA synchronous=NORMAL;").execute(&pool).await?;
|
||||
sqlx::query("PRAGMA cache_size=-64000;").execute(&pool).await?;
|
||||
sqlx::query("PRAGMA mmap_size=268435456;").execute(&pool).await?;
|
||||
sqlx::query("PRAGMA temp_store=MEMORY;").execute(&pool).await?;
|
||||
sqlx::query("PRAGMA foreign_keys=ON;").execute(&pool).await?;
|
||||
|
||||
sqlx::migrate!("./migrations").run(&pool).await?;
|
||||
|
||||
let db = Database { pool, config: config.clone() };
|
||||
db.ensure_image_count_field().await?;
|
||||
Ok(db)
|
||||
}
|
||||
|
||||
/// 确保 image_count 字段存在
|
||||
async fn ensure_image_count_field(&self) -> Result<(), sqlx::Error> {
|
||||
let has_col = sqlx::query_scalar::<_, i64>(
|
||||
"SELECT COUNT(*) FROM pragma_table_info('albums') WHERE name = 'image_count'"
|
||||
).fetch_one(&self.pool).await.unwrap_or(0);
|
||||
|
||||
if has_col == 0 {
|
||||
sqlx::query("ALTER TABLE albums ADD COLUMN image_count INTEGER DEFAULT 0").execute(&self.pool).await?;
|
||||
println!("✅ image_count 字段已添加");
|
||||
} else {
|
||||
println!("ℹ️ image_count字段已存在,跳过初始化");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
67
src/db/favorite.rs
Normal file
67
src/db/favorite.rs
Normal file
@@ -0,0 +1,67 @@
|
||||
use super::Database;
|
||||
use crate::models::*;
|
||||
use sqlx::Row;
|
||||
use uuid::Uuid;
|
||||
|
||||
impl Database {
|
||||
pub async fn get_user_favorites(&self, user_id: &str) -> Result<Vec<FavoriteWithImage>, sqlx::Error> {
|
||||
let rows = sqlx::query(
|
||||
r#"SELECT f.id, f.user_id, f.image_id, f.album_id, f.created_at,
|
||||
i.album_id as img_album_id, i.filename, i.original_url, i.title, i.description,
|
||||
i.file_size, i.width, i.height, i.mime_type, i.created_at as img_created_at,
|
||||
a.title as album_title
|
||||
FROM favorites f
|
||||
JOIN images i ON f.image_id = i.id
|
||||
JOIN albums a ON f.album_id = a.id
|
||||
WHERE f.user_id = ? ORDER BY f.created_at DESC"#
|
||||
).bind(user_id).fetch_all(&self.pool).await?;
|
||||
|
||||
Ok(rows.iter().map(|r| {
|
||||
let fav_id = Uuid::parse_str(&r.get::<String, _>("id")).unwrap_or_else(|_| Uuid::nil());
|
||||
let img_id = Uuid::parse_str(&r.get::<String, _>("image_id")).unwrap_or_else(|_| Uuid::nil());
|
||||
let aid = Uuid::parse_str(&r.get::<String, _>("album_id")).unwrap_or_else(|_| Uuid::nil());
|
||||
let img_aid = Uuid::parse_str(&r.get::<String, _>("img_album_id")).unwrap_or_else(|_| Uuid::nil());
|
||||
FavoriteWithImage {
|
||||
favorite: Favorite { id: fav_id, user_id: r.get("user_id"), image_id: img_id, album_id: aid, created_at: r.get("created_at") },
|
||||
image: crate::models::image::Image {
|
||||
id: img_id, album_id: img_aid,
|
||||
filename: r.get("filename"), original_url: r.get("original_url"),
|
||||
title: r.get("title"), description: r.get("description"),
|
||||
file_size: r.get("file_size"), width: r.get("width"),
|
||||
height: r.get("height"), mime_type: r.get("mime_type"),
|
||||
created_at: r.get("img_created_at"),
|
||||
},
|
||||
album_title: r.get("album_title"),
|
||||
}
|
||||
}).collect())
|
||||
}
|
||||
|
||||
pub async fn get_favorite_status(&self, user_id: &str, image_id: Uuid) -> Result<FavoriteStatus, sqlx::Error> {
|
||||
let row = sqlx::query("SELECT id FROM favorites WHERE user_id = ? AND image_id = ?")
|
||||
.bind(user_id).bind(image_id.to_string()).fetch_optional(&self.pool).await?;
|
||||
Ok(FavoriteStatus {
|
||||
is_favorited: row.is_some(),
|
||||
favorite_id: row.map(|r| Uuid::parse_str(&r.get::<String, _>("id")).unwrap_or_else(|_| Uuid::nil())),
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn add_favorite(&self, user_id: &str, fav: CreateFavorite) -> Result<Uuid, sqlx::Error> {
|
||||
let id = Uuid::new_v4();
|
||||
let now = chrono::Utc::now().to_rfc3339();
|
||||
sqlx::query("INSERT INTO favorites (id, user_id, image_id, album_id, created_at) VALUES (?, ?, ?, ?, ?)")
|
||||
.bind(id.to_string()).bind(user_id).bind(fav.image_id.to_string()).bind(fav.album_id.to_string()).bind(&now)
|
||||
.execute(&self.pool).await?;
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
pub async fn remove_favorite(&self, favorite_id: Uuid) -> Result<(), sqlx::Error> {
|
||||
sqlx::query("DELETE FROM favorites WHERE id = ?").bind(favorite_id.to_string()).execute(&self.pool).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn remove_favorite_by_image(&self, user_id: &str, image_id: Uuid) -> Result<(), sqlx::Error> {
|
||||
sqlx::query("DELETE FROM favorites WHERE user_id = ? AND image_id = ?")
|
||||
.bind(user_id).bind(image_id.to_string()).execute(&self.pool).await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
74
src/db/image.rs
Normal file
74
src/db/image.rs
Normal file
@@ -0,0 +1,74 @@
|
||||
use super::Database;
|
||||
use crate::models::*;
|
||||
use sqlx::Row;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// 重新导出用于 cross-module access
|
||||
pub use crate::models::image::Image;
|
||||
|
||||
impl Database {
|
||||
pub async fn add_images(&self, images: Vec<CreateImage>) -> Result<Vec<Uuid>, sqlx::Error> {
|
||||
if images.is_empty() { return Ok(Vec::new()); }
|
||||
let now = chrono::Utc::now().to_rfc3339();
|
||||
let mut ids = Vec::new();
|
||||
let first_album_id = images.first().map(|img| img.album_id);
|
||||
|
||||
for img in &images {
|
||||
let id = Uuid::new_v4();
|
||||
sqlx::query(
|
||||
"INSERT INTO images (id, album_id, filename, original_url, title, description, file_size, width, height, mime_type, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
|
||||
).bind(id.to_string()).bind(img.album_id.to_string()).bind(&img.filename)
|
||||
.bind(&img.original_url).bind(&img.title).bind(&img.description)
|
||||
.bind(img.file_size).bind(img.width).bind(img.height).bind(&img.mime_type).bind(&now)
|
||||
.execute(&self.pool).await?;
|
||||
ids.push(id);
|
||||
}
|
||||
|
||||
if let Some(aid) = first_album_id {
|
||||
self.update_album_image_count(aid).await?;
|
||||
let count = sqlx::query_scalar::<_, i64>("SELECT COUNT(*) FROM images WHERE album_id = ?")
|
||||
.bind(aid.to_string()).fetch_one(&self.pool).await?;
|
||||
if count == images.len() as i64 {
|
||||
self.update_album_cover(aid, ids[0]).await?;
|
||||
}
|
||||
}
|
||||
Ok(ids)
|
||||
}
|
||||
|
||||
pub async fn get_image(&self, image_id: Uuid) -> Result<Option<Image>, sqlx::Error> {
|
||||
let row = sqlx::query("SELECT * FROM images WHERE id = ?")
|
||||
.bind(image_id.to_string()).fetch_optional(&self.pool).await?;
|
||||
let Some(r) = row else { return Ok(None) };
|
||||
Ok(Some(Image {
|
||||
id: image_id,
|
||||
album_id: Uuid::parse_str(&r.get::<String, _>("album_id")).unwrap_or_else(|_| Uuid::nil()),
|
||||
filename: r.get("filename"),
|
||||
original_url: r.get("original_url"),
|
||||
title: r.get("title"),
|
||||
description: r.get("description"),
|
||||
file_size: r.get("file_size"),
|
||||
width: r.get("width"),
|
||||
height: r.get("height"),
|
||||
mime_type: r.get("mime_type"),
|
||||
created_at: r.get("created_at"),
|
||||
}))
|
||||
}
|
||||
|
||||
pub async fn get_unprocessed_images(&self, limit: i64) -> Result<Vec<Image>, sqlx::Error> {
|
||||
let rows = sqlx::query("SELECT * FROM images WHERE file_size > 0 LIMIT ?")
|
||||
.bind(limit).fetch_all(&self.pool).await?;
|
||||
Ok(rows.iter().map(|r| Image {
|
||||
id: Uuid::parse_str(&r.get::<String, _>("id")).unwrap_or_else(|_| Uuid::nil()),
|
||||
album_id: Uuid::parse_str(&r.get::<String, _>("album_id")).unwrap_or_else(|_| Uuid::nil()),
|
||||
filename: r.get("filename"),
|
||||
original_url: r.get("original_url"),
|
||||
title: r.get("title"),
|
||||
description: r.get("description"),
|
||||
file_size: r.get("file_size"),
|
||||
width: r.get("width"),
|
||||
height: r.get("height"),
|
||||
mime_type: r.get("mime_type"),
|
||||
created_at: r.get("created_at"),
|
||||
}).collect())
|
||||
}
|
||||
}
|
||||
239
src/db/metadata.rs
Normal file
239
src/db/metadata.rs
Normal file
@@ -0,0 +1,239 @@
|
||||
use super::Database;
|
||||
use crate::models::*;
|
||||
use sqlx::Row;
|
||||
use uuid::Uuid;
|
||||
|
||||
impl Database {
|
||||
// ── 人员查询 ──
|
||||
|
||||
pub async fn get_all_person_names(&self) -> Vec<(String, i64)> {
|
||||
sqlx::query("SELECT p.name, p.total_albums FROM persons p ORDER BY p.total_albums DESC")
|
||||
.fetch_all(&self.pool).await.unwrap_or_default()
|
||||
.iter().map(|r| (r.get::<String, _>("name"), r.get::<i64, _>("total_albums"))).collect()
|
||||
}
|
||||
|
||||
pub async fn get_all_aliases(&self) -> Vec<(String, String)> {
|
||||
sqlx::query("SELECT a.alias_name, p.name as person_name FROM person_aliases a JOIN persons p ON a.person_id = p.id")
|
||||
.fetch_all(&self.pool).await.unwrap_or_default()
|
||||
.iter().map(|r| (r.get::<String, _>("alias_name"), r.get::<String, _>("person_name"))).collect()
|
||||
}
|
||||
|
||||
pub async fn get_all_persons_with_counts(&self) -> Vec<(i64, String, i64, i64)> {
|
||||
sqlx::query("SELECT id, name, total_albums, total_images FROM persons ORDER BY total_albums DESC")
|
||||
.fetch_all(&self.pool).await.unwrap_or_default()
|
||||
.iter().map(|r| (r.get::<i64, _>("id"), r.get::<String, _>("name"), r.get::<i64, _>("total_albums"), r.get::<i64, _>("total_images"))).collect()
|
||||
}
|
||||
|
||||
pub async fn get_person_by_name(&self, name: &str) -> Option<Person> {
|
||||
let r = sqlx::query("SELECT * FROM persons WHERE name = ?").bind(name).fetch_optional(&self.pool).await.ok()??;
|
||||
Some(Person {
|
||||
id: r.get("id"), name: r.get("name"), display_name: r.get("display_name"),
|
||||
total_albums: r.get("total_albums"), total_images: r.get("total_images"),
|
||||
original_names: r.get("original_names"), created_at: r.get("created_at"), updated_at: r.get("updated_at"),
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn get_person_albums(&self, person_id: i64) -> Vec<AlbumInfo> {
|
||||
sqlx::query(
|
||||
r#"SELECT a.id, a.title, a.document, a.description, a.cover_image_id, a.image_count, a.created_at
|
||||
FROM albums a JOIN person_albums pa ON a.id = pa.album_id WHERE pa.person_id = ? ORDER BY a.created_at DESC"#
|
||||
).bind(person_id).fetch_all(&self.pool).await.unwrap_or_default()
|
||||
.iter().map(|r| AlbumInfo {
|
||||
id: Uuid::parse_str(&r.get::<String, _>("id")).unwrap_or_else(|_| Uuid::nil()),
|
||||
title: r.get("title"), document: r.get("document"),
|
||||
description: r.get("description"), cover_image_id: r.get("cover_image_id"),
|
||||
image_count: r.get("image_count"), created_at: r.get("created_at"),
|
||||
}).collect()
|
||||
}
|
||||
|
||||
pub async fn get_person_aliases(&self, person_id: i64) -> Vec<PersonAlias> {
|
||||
sqlx::query("SELECT * FROM person_aliases WHERE person_id = ?").bind(person_id)
|
||||
.fetch_all(&self.pool).await.unwrap_or_default()
|
||||
.iter().map(|r| PersonAlias {
|
||||
id: r.get("id"), person_id: r.get("person_id"),
|
||||
alias_type: r.get("alias_type"), alias_name: r.get("alias_name"),
|
||||
created_at: r.get("created_at"),
|
||||
}).collect()
|
||||
}
|
||||
|
||||
pub async fn search_persons(&self, query: &str) -> Vec<PersonWithCounts> {
|
||||
let pattern = format!("%{}%", query);
|
||||
let rows = sqlx::query(
|
||||
r#"SELECT DISTINCT p.id, p.name, p.display_name, p.total_albums, p.total_images
|
||||
FROM persons p LEFT JOIN person_aliases a ON p.id = a.person_id
|
||||
WHERE p.name LIKE ? OR a.alias_name LIKE ? ORDER BY p.total_albums DESC"#
|
||||
).bind(&pattern).bind(&pattern).fetch_all(&self.pool).await.unwrap_or_default();
|
||||
|
||||
let mut results = Vec::new();
|
||||
for r in &rows {
|
||||
let pid = r.get::<i64, _>("id");
|
||||
let aliases = self.get_person_aliases(pid).await;
|
||||
results.push(PersonWithCounts {
|
||||
id: pid, name: r.get("name"), display_name: r.get("display_name"),
|
||||
total_albums: r.get("total_albums"), total_images: r.get("total_images"),
|
||||
aliases: aliases.into_iter().map(|a| a.alias_name).collect(),
|
||||
});
|
||||
}
|
||||
results
|
||||
}
|
||||
|
||||
pub async fn get_all_persons_with_aliases(&self) -> Vec<PersonWithCounts> {
|
||||
let rows = sqlx::query("SELECT id, name, display_name, total_albums, total_images FROM persons ORDER BY total_albums DESC")
|
||||
.fetch_all(&self.pool).await.unwrap_or_default();
|
||||
let mut results = Vec::new();
|
||||
for r in &rows {
|
||||
let pid = r.get::<i64, _>("id");
|
||||
let aliases = self.get_person_aliases(pid).await;
|
||||
results.push(PersonWithCounts {
|
||||
id: pid, name: r.get("name"), display_name: r.get("display_name"),
|
||||
total_albums: r.get("total_albums"), total_images: r.get("total_images"),
|
||||
aliases: aliases.into_iter().map(|a| a.alias_name).collect(),
|
||||
});
|
||||
}
|
||||
results
|
||||
}
|
||||
|
||||
// ── 标签查询 ──
|
||||
|
||||
pub async fn get_popular_tags(&self, limit: i64) -> Vec<TagStat> {
|
||||
sqlx::query(
|
||||
r#"SELECT tag, COUNT(*) as cnt, COUNT(DISTINCT album_id) as album_cnt
|
||||
FROM album_tags GROUP BY tag ORDER BY cnt DESC LIMIT ?"#
|
||||
).bind(limit).fetch_all(&self.pool).await.unwrap_or_default()
|
||||
.iter().map(|r| TagStat {
|
||||
tag: r.get("tag"), count: r.get("cnt"), album_count: r.get("album_cnt"),
|
||||
}).collect()
|
||||
}
|
||||
|
||||
pub async fn get_tag_co_occurrences(&self, tag: &str, limit: i64) -> Vec<TagCoOccurrence> {
|
||||
let rows = sqlx::query(
|
||||
r#"SELECT t2.tag as co_tag, COUNT(*) as cnt
|
||||
FROM album_tags t1 JOIN album_tags t2 ON t1.album_id = t2.album_id
|
||||
WHERE t1.tag = ? AND t2.tag != ? GROUP BY t2.tag ORDER BY cnt DESC LIMIT ?"#
|
||||
).bind(tag).bind(tag).bind(limit).fetch_all(&self.pool).await.unwrap_or_default();
|
||||
|
||||
let total = sqlx::query_scalar::<_, i64>("SELECT COUNT(*) FROM album_tags WHERE tag = ?")
|
||||
.bind(tag).fetch_one(&self.pool).await.unwrap_or(1);
|
||||
rows.iter().map(|r| {
|
||||
let cnt: i64 = r.get("cnt");
|
||||
TagCoOccurrence {
|
||||
tag: tag.to_string(), co_tag: r.get("co_tag"),
|
||||
count: cnt, score: cnt as f64 / total as f64,
|
||||
}
|
||||
}).collect()
|
||||
}
|
||||
|
||||
pub async fn search_albums_by_tag(&self, tag: &str) -> Vec<AlbumInfo> {
|
||||
sqlx::query(
|
||||
r#"SELECT a.id, a.title, a.document, a.description, a.cover_image_id, a.image_count, a.created_at
|
||||
FROM albums a JOIN album_tags t ON a.id = t.album_id WHERE t.tag = ? ORDER BY a.created_at DESC"#
|
||||
).bind(tag).fetch_all(&self.pool).await.unwrap_or_default()
|
||||
.iter().map(|r| AlbumInfo {
|
||||
id: Uuid::parse_str(&r.get::<String, _>("id")).unwrap_or_else(|_| Uuid::nil()),
|
||||
title: r.get("title"), document: r.get("document"),
|
||||
description: r.get("description"), cover_image_id: r.get("cover_image_id"),
|
||||
image_count: r.get("image_count"), created_at: r.get("created_at"),
|
||||
}).collect()
|
||||
}
|
||||
|
||||
// ── 统计 ──
|
||||
|
||||
pub async fn get_metadata_stats(&self) -> MetadataStats {
|
||||
MetadataStats {
|
||||
total_persons: sqlx::query_scalar("SELECT COUNT(*) FROM persons").fetch_one(&self.pool).await.unwrap_or(0),
|
||||
total_aliases: sqlx::query_scalar("SELECT COUNT(*) FROM person_aliases").fetch_one(&self.pool).await.unwrap_or(0),
|
||||
total_albums_with_person: sqlx::query_scalar("SELECT COUNT(*) FROM albums WHERE person_name_original IS NOT NULL AND person_name_original != ''").fetch_one(&self.pool).await.unwrap_or(0),
|
||||
total_albums_without_person: sqlx::query_scalar("SELECT COUNT(*) FROM albums WHERE person_name_original IS NULL OR person_name_original = ''").fetch_one(&self.pool).await.unwrap_or(0),
|
||||
total_tags: sqlx::query_scalar("SELECT COUNT(*) FROM album_tags").fetch_one(&self.pool).await.unwrap_or(0),
|
||||
}
|
||||
}
|
||||
|
||||
// ── 写入 ──
|
||||
|
||||
pub async fn insert_person(&self, name: &str, display_name: Option<&str>) -> i64 {
|
||||
if let Some(p) = self.get_person_by_name(name).await { return p.id; }
|
||||
sqlx::query("INSERT OR IGNORE INTO persons (name, display_name) VALUES (?, ?)")
|
||||
.bind(name).bind(display_name).execute(&self.pool).await.ok();
|
||||
self.get_person_by_name(name).await.map(|p| p.id).unwrap_or(0)
|
||||
}
|
||||
|
||||
pub async fn insert_person_alias(&self, person_id: i64, alias_type: &str, alias_name: &str) {
|
||||
sqlx::query("INSERT OR IGNORE INTO person_aliases (person_id, alias_type, alias_name) VALUES (?, ?, ?)")
|
||||
.bind(person_id).bind(alias_type).bind(alias_name).execute(&self.pool).await.ok();
|
||||
}
|
||||
|
||||
pub async fn insert_person_album(&self, person_id: i64, album_id: &str) {
|
||||
sqlx::query("INSERT OR IGNORE INTO person_albums (person_id, album_id) VALUES (?, ?)")
|
||||
.bind(person_id).bind(album_id).execute(&self.pool).await.ok();
|
||||
}
|
||||
|
||||
pub async fn delete_person_albums(&self, person_id: i64) {
|
||||
sqlx::query("DELETE FROM person_albums WHERE person_id = ?").bind(person_id).execute(&self.pool).await.ok();
|
||||
}
|
||||
|
||||
/// 把 src_id 的所有相册关联、别名移动到 dst_id,然后删除 src_id
|
||||
pub async fn merge_person_into(&self, src_id: i64, dst_id: i64) {
|
||||
// 1. 移动相册关联
|
||||
sqlx::query("INSERT OR IGNORE INTO person_albums (person_id, album_id) SELECT ?, album_id FROM person_albums WHERE person_id = ?")
|
||||
.bind(dst_id).bind(src_id).execute(&self.pool).await.ok();
|
||||
|
||||
// 2. 移动别名
|
||||
sqlx::query("INSERT OR IGNORE INTO person_aliases (person_id, alias_type, alias_name) SELECT ?, alias_type, alias_name FROM person_aliases WHERE person_id = ?")
|
||||
.bind(dst_id).bind(src_id).execute(&self.pool).await.ok();
|
||||
|
||||
// 3. 把 src 的原名也作为别名加入 dst
|
||||
if let Some(src_person) = self.get_person_by_id(src_id).await {
|
||||
// 原始名作为别名
|
||||
self.insert_person_alias(dst_id, "merged_original", &src_person.name).await;
|
||||
// 如果有 display_name 也作为别名
|
||||
if let Some(dn) = &src_person.display_name {
|
||||
self.insert_person_alias(dst_id, "merged_display", dn).await;
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 删除 src 的关联和别名
|
||||
self.delete_person_albums(src_id).await;
|
||||
sqlx::query("DELETE FROM person_aliases WHERE person_id = ?").bind(src_id).execute(&self.pool).await.ok();
|
||||
|
||||
// 5. 删除 src 人物
|
||||
sqlx::query("DELETE FROM persons WHERE id = ?").bind(src_id).execute(&self.pool).await.ok();
|
||||
|
||||
// 6. 更新 dst 的统计数据
|
||||
self.update_person_counts(dst_id).await;
|
||||
}
|
||||
|
||||
pub async fn get_person_by_id(&self, id: i64) -> Option<Person> {
|
||||
sqlx::query_as::<_, Person>("SELECT * FROM persons WHERE id = ?")
|
||||
.bind(id).fetch_optional(&self.pool).await.ok().flatten()
|
||||
}
|
||||
|
||||
pub async fn update_album_person(&self, album_id: &str, person_name_original: &str, person_name_cleaned: &str) {
|
||||
sqlx::query("UPDATE albums SET person_name_original = ?, person_name_cleaned = ?, processed_at = datetime('now') WHERE id = ?")
|
||||
.bind(person_name_original).bind(person_name_cleaned).bind(album_id).execute(&self.pool).await.ok();
|
||||
}
|
||||
|
||||
pub async fn update_album_tags(&self, album_id: &str, tags_json: &str, all_words_json: &str) {
|
||||
sqlx::query("UPDATE albums SET tags = ?, all_words = ?, processed_at = datetime('now') WHERE id = ?")
|
||||
.bind(tags_json).bind(all_words_json).bind(album_id).execute(&self.pool).await.ok();
|
||||
sqlx::query("DELETE FROM album_tags WHERE album_id = ?").bind(album_id).execute(&self.pool).await.ok();
|
||||
if let Ok(tags) = serde_json::from_str::<Vec<String>>(tags_json) {
|
||||
for tag in &tags {
|
||||
sqlx::query("INSERT OR IGNORE INTO album_tags (album_id, tag) VALUES (?, ?)")
|
||||
.bind(album_id).bind(tag).execute(&self.pool).await.ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn update_person_counts(&self, person_id: i64) {
|
||||
sqlx::query(
|
||||
r#"UPDATE persons SET total_albums = (SELECT COUNT(*) FROM person_albums WHERE person_id = ?),
|
||||
total_images = (SELECT COALESCE(SUM(a.image_count), 0) FROM albums a JOIN person_albums pa ON a.id = pa.album_id WHERE pa.person_id = ?),
|
||||
updated_at = datetime('now') WHERE id = ?"#
|
||||
).bind(person_id).bind(person_id).bind(person_id).execute(&self.pool).await.ok();
|
||||
}
|
||||
|
||||
/// 获取所有相册 ID 集合(用于迁移)
|
||||
pub async fn get_all_album_ids(&self) -> Vec<String> {
|
||||
sqlx::query_scalar::<_, String>("SELECT id FROM albums").fetch_all(&self.pool).await.unwrap_or_default()
|
||||
}
|
||||
}
|
||||
161
src/db/migration.rs
Normal file
161
src/db/migration.rs
Normal file
@@ -0,0 +1,161 @@
|
||||
//! 从老数据库(Python 项目 persons.db)迁移元数据到新数据库
|
||||
//!
|
||||
//! 迁移策略:
|
||||
//! 1. 读取老 DB 的 persons/aliases/albums 数据
|
||||
//! 2. 按 album_id 对齐新 DB 中的相册
|
||||
//! 3. 批量插入人员、别名、标签、关联
|
||||
|
||||
use super::Database;
|
||||
use sqlx::sqlite::SqlitePoolOptions;
|
||||
use sqlx::Row;
|
||||
|
||||
/// 迁移结果统计
|
||||
#[derive(Debug, Default)]
|
||||
pub struct MigrationStats {
|
||||
pub persons_imported: usize,
|
||||
pub persons_skipped: usize,
|
||||
pub aliases_imported: usize,
|
||||
pub albums_updated: usize,
|
||||
pub albums_missing: usize,
|
||||
pub tags_imported: usize,
|
||||
pub person_albums_created: usize,
|
||||
}
|
||||
|
||||
impl Database {
|
||||
/// 从老数据库迁移元数据
|
||||
pub async fn migrate_from_old_db(&self, old_db_path: &str) -> Result<MigrationStats, String> {
|
||||
if !std::path::Path::new(old_db_path).exists() {
|
||||
return Err(format!("老数据库不存在: {}", old_db_path));
|
||||
}
|
||||
|
||||
println!("[迁移] 打开老数据库: {}", old_db_path);
|
||||
let old_pool = SqlitePoolOptions::new()
|
||||
.max_connections(1)
|
||||
.connect(&format!("sqlite:{}", old_db_path))
|
||||
.await
|
||||
.map_err(|e| format!("连接老数据库失败: {}", e))?;
|
||||
|
||||
// 先获取新 DB 中所有相册 ID
|
||||
let new_album_ids: std::collections::HashSet<String> = self.get_all_album_ids().await.into_iter().collect();
|
||||
println!("[迁移] 新数据库中有 {} 个相册", new_album_ids.len());
|
||||
|
||||
let mut stats = MigrationStats::default();
|
||||
|
||||
// ── 1. 迁移人员 ──
|
||||
println!("[迁移] 步骤 1/4: 迁移人员...");
|
||||
let old_persons: Vec<(i64, String, Option<String>, i64, i64, Option<String>)> = sqlx::query(
|
||||
"SELECT id, name, display_name, total_albums, total_images, original_names FROM persons"
|
||||
).fetch_all(&old_pool).await.map_err(|e| format!("读取老人员失败: {}", e))?
|
||||
.iter().map(|r| (
|
||||
r.get("id"), r.get("name"), r.get("display_name"),
|
||||
r.get("total_albums"), r.get("total_images"), r.get("original_names"),
|
||||
)).collect();
|
||||
|
||||
let mut old_to_new_person: std::collections::HashMap<i64, i64> = std::collections::HashMap::new();
|
||||
|
||||
for (old_pid, name, display_name, _albums, _images, original_names) in &old_persons {
|
||||
let new_pid = self.insert_person(name, display_name.as_deref()).await;
|
||||
if new_pid > 0 {
|
||||
old_to_new_person.insert(*old_pid, new_pid);
|
||||
// 更新 original_names
|
||||
if let Some(on) = original_names {
|
||||
sqlx::query("UPDATE persons SET original_names = ? WHERE id = ?")
|
||||
.bind(on).bind(new_pid).execute(&self.pool).await.ok();
|
||||
}
|
||||
stats.persons_imported += 1;
|
||||
} else {
|
||||
stats.persons_skipped += 1;
|
||||
}
|
||||
}
|
||||
println!("[迁移] 导入 {} 人,跳过 {} 人", stats.persons_imported, stats.persons_skipped);
|
||||
|
||||
// ── 2. 迁移别名 ──
|
||||
println!("[迁移] 步骤 2/4: 迁移别名...");
|
||||
let old_aliases: Vec<(i64, String, String)> = sqlx::query(
|
||||
"SELECT person_id, alias_type, alias_name FROM person_aliases"
|
||||
).fetch_all(&old_pool).await.unwrap_or_default()
|
||||
.iter().map(|r| (r.get("person_id"), r.get("alias_type"), r.get("alias_name"))).collect();
|
||||
|
||||
for (old_pid, alias_type, alias_name) in &old_aliases {
|
||||
if let Some(&new_pid) = old_to_new_person.get(old_pid) {
|
||||
self.insert_person_alias(new_pid, alias_type, alias_name).await;
|
||||
stats.aliases_imported += 1;
|
||||
}
|
||||
}
|
||||
println!("[迁移] 导入 {} 个别名", stats.aliases_imported);
|
||||
|
||||
// ── 3. 迁移相册人员标签 ──
|
||||
println!("[迁移] 步骤 3/4: 迁移相册人员+标签...");
|
||||
let old_albums: Vec<(String, Option<String>, Option<String>, Option<String>, Option<String>, Option<String>)> = sqlx::query(
|
||||
"SELECT id, title, person_name_original, person_name_cleaned, tags, all_words FROM albums"
|
||||
).fetch_all(&old_pool).await.unwrap_or_default()
|
||||
.iter().map(|r| (
|
||||
r.get("id"), r.get("title"), r.get("person_name_original"),
|
||||
r.get("person_name_cleaned"), r.get("tags"), r.get("all_words"),
|
||||
)).collect();
|
||||
|
||||
for (album_id, _title, person_orig, person_cleaned, tags, all_words) in &old_albums {
|
||||
if !new_album_ids.contains(album_id) {
|
||||
stats.albums_missing += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
// 更新人员信息
|
||||
if let (Some(po), Some(pc)) = (person_orig, person_cleaned) {
|
||||
if !po.is_empty() {
|
||||
self.update_album_person(album_id, po, pc).await;
|
||||
stats.albums_updated += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// 更新标签
|
||||
if let Some(t) = tags {
|
||||
if t != "[]" && !t.is_empty() {
|
||||
let aw = all_words.clone().unwrap_or_else(|| "[]".to_string());
|
||||
self.update_album_tags(album_id, t, &aw).await;
|
||||
stats.tags_imported += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
println!("[迁移] 更新 {} 个相册,{} 个不在新库中,{} 个标签导入",
|
||||
stats.albums_updated, stats.albums_missing, stats.tags_imported);
|
||||
|
||||
// ── 4. 迁移人物-相册关联 ──
|
||||
println!("[迁移] 步骤 4/4: 迁移人物-相册关联...");
|
||||
let old_person_albums: Vec<(i64, String)> = sqlx::query(
|
||||
"SELECT person_id, album_id FROM person_albums"
|
||||
).fetch_all(&old_pool).await.unwrap_or_default()
|
||||
.iter().map(|r| (r.get("person_id"), r.get("album_id"))).collect();
|
||||
|
||||
for (old_pid, album_id) in &old_person_albums {
|
||||
if let Some(&new_pid) = old_to_new_person.get(old_pid) {
|
||||
if new_album_ids.contains(album_id) {
|
||||
self.insert_person_album(new_pid, album_id).await;
|
||||
stats.person_albums_created += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
println!("[迁移] 创建 {} 个关联", stats.person_albums_created);
|
||||
|
||||
// 更新所有人员的计数
|
||||
println!("[迁移] 更新人员统计...");
|
||||
for &new_pid in old_to_new_person.values() {
|
||||
self.update_person_counts(new_pid).await;
|
||||
}
|
||||
|
||||
println!("[迁移] ✅ 完成!");
|
||||
Ok(stats)
|
||||
}
|
||||
|
||||
/// 获取迁移统计摘要
|
||||
pub fn migration_summary(&self, stats: &MigrationStats) -> String {
|
||||
format!(
|
||||
"迁移完成:\n - 人员: {} 导入, {} 跳过\n - 别名: {} 导入\n - 相册: {} 更新, {} 缺失\n - 标签: {} 导入\n - 关联: {} 创建",
|
||||
stats.persons_imported, stats.persons_skipped,
|
||||
stats.aliases_imported,
|
||||
stats.albums_updated, stats.albums_missing,
|
||||
stats.tags_imported,
|
||||
stats.person_albums_created,
|
||||
)
|
||||
}
|
||||
}
|
||||
15
src/db/mod.rs
Normal file
15
src/db/mod.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
//! 数据库操作层 — 按领域拆分
|
||||
|
||||
mod connection;
|
||||
pub mod album;
|
||||
pub mod image;
|
||||
pub mod favorite;
|
||||
pub mod metadata;
|
||||
pub mod migration;
|
||||
|
||||
pub use connection::Database;
|
||||
#[allow(unused)] pub use album::*;
|
||||
#[allow(unused)] pub use image::*;
|
||||
#[allow(unused)] pub use favorite::*;
|
||||
#[allow(unused)] pub use metadata::*;
|
||||
pub use migration::MigrationStats;
|
||||
176
src/engine/crawler.rs
Normal file
176
src/engine/crawler.rs
Normal file
@@ -0,0 +1,176 @@
|
||||
//! API 数据获取
|
||||
//! 从 http://192.168.1.100:4500 获取相册和图片数据
|
||||
//! 对应 Python 的 fetch_from_api.py
|
||||
|
||||
use serde::Deserialize;
|
||||
use std::time::Duration;
|
||||
|
||||
/// 从 API 获取的相册信息
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct ApiAlbum {
|
||||
pub id: String,
|
||||
pub title: String,
|
||||
#[serde(default)]
|
||||
pub image_count: i64,
|
||||
#[serde(default)]
|
||||
pub description: Option<String>,
|
||||
#[serde(default)]
|
||||
pub created_at: Option<String>,
|
||||
}
|
||||
|
||||
/// 从 API 获取的图片详情
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct ApiImage {
|
||||
pub id: String,
|
||||
#[serde(default)]
|
||||
pub filename: Option<String>,
|
||||
#[serde(default)]
|
||||
pub original_url: Option<String>,
|
||||
#[serde(default)]
|
||||
pub url: Option<String>,
|
||||
#[serde(default)]
|
||||
pub width: Option<i32>,
|
||||
#[serde(default)]
|
||||
pub height: Option<i32>,
|
||||
#[serde(default)]
|
||||
pub file_size: Option<i64>,
|
||||
#[serde(default)]
|
||||
pub mime_type: Option<String>,
|
||||
}
|
||||
|
||||
/// 相册详情(含图片列表)
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct ApiAlbumDetail {
|
||||
#[serde(default)]
|
||||
pub images: Vec<ApiImage>,
|
||||
#[serde(default)]
|
||||
pub id: Option<String>,
|
||||
#[serde(default)]
|
||||
pub title: Option<String>,
|
||||
}
|
||||
|
||||
/// API 数据获取器
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Crawler {
|
||||
api_base: String,
|
||||
client: reqwest::Client,
|
||||
}
|
||||
|
||||
impl Crawler {
|
||||
/// 创建 Crawler 实例
|
||||
pub fn new(api_base: &str) -> Self {
|
||||
let client = reqwest::Client::builder()
|
||||
.timeout(Duration::from_secs(30))
|
||||
.user_agent("imager-rs/1.0")
|
||||
.build()
|
||||
.expect("创建 HTTP 客户端失败");
|
||||
Self {
|
||||
api_base: api_base.trim_end_matches('/').to_string(),
|
||||
client,
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取所有相册列表
|
||||
/// GET /api/albums
|
||||
pub async fn get_albums(&self) -> Result<Vec<ApiAlbum>, Box<dyn std::error::Error>> {
|
||||
let url = format!("{}/api/albums", self.api_base);
|
||||
let resp = self.client.get(&url).send().await?;
|
||||
let data: serde_json::Value = resp.json().await?;
|
||||
|
||||
let albums: Vec<ApiAlbum> = if let Some(_arr) = data.as_array() {
|
||||
serde_json::from_value(data)?
|
||||
} else if let Some(arr) = data.get("albums").and_then(|v: &serde_json::Value| v.as_array()) {
|
||||
serde_json::from_value(serde_json::Value::Array(arr.clone()))?
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
|
||||
Ok(albums)
|
||||
}
|
||||
|
||||
/// 获取相册详情(含图片列表)
|
||||
/// GET /api/album/{id}
|
||||
pub async fn get_album_detail(&self, album_id: &str) -> Result<ApiAlbumDetail, Box<dyn std::error::Error>> {
|
||||
let url = format!("{}/api/album/{}", self.api_base, album_id);
|
||||
let resp = self.client.get(&url).send().await?;
|
||||
let data: serde_json::Value = resp.json().await?;
|
||||
|
||||
let detail = if let Some(images) = data.get("images").and_then(|v: &serde_json::Value| v.as_array()) {
|
||||
ApiAlbumDetail {
|
||||
images: serde_json::from_value(serde_json::Value::Array(images.clone()))?,
|
||||
id: data.get("id").and_then(|v: &serde_json::Value| v.as_str()).map(|s| s.to_string()),
|
||||
title: data.get("title").and_then(|v: &serde_json::Value| v.as_str()).map(|s| s.to_string()),
|
||||
}
|
||||
} else if let Some(_arr) = data.as_array() {
|
||||
ApiAlbumDetail {
|
||||
images: serde_json::from_value(data)?,
|
||||
id: None,
|
||||
title: None,
|
||||
}
|
||||
} else {
|
||||
ApiAlbumDetail {
|
||||
images: Vec::new(),
|
||||
id: None,
|
||||
title: None,
|
||||
}
|
||||
};
|
||||
|
||||
Ok(detail)
|
||||
}
|
||||
|
||||
/// 获取图片二进制数据
|
||||
/// GET /api/image/{id}
|
||||
pub async fn get_image_data(&self, image_id: &str) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
|
||||
let url = format!("{}/api/image/{}", self.api_base, image_id);
|
||||
let resp = self.client.get(&url).send().await?;
|
||||
let bytes = resp.bytes().await?;
|
||||
Ok(bytes.to_vec())
|
||||
}
|
||||
|
||||
/// 获取图片的下载 URL(不下载,只返回 URL 字符串)
|
||||
pub fn get_image_url(&self, image_id: &str) -> String {
|
||||
format!("{}/api/image/{}", self.api_base, image_id)
|
||||
}
|
||||
|
||||
/// 获取多个相册的详情(批量)
|
||||
pub async fn get_album_details_batch(
|
||||
&self,
|
||||
album_ids: &[String],
|
||||
) -> Result<Vec<(String, ApiAlbumDetail)>, Box<dyn std::error::Error>> {
|
||||
let mut results = Vec::new();
|
||||
for aid in album_ids {
|
||||
match self.get_album_detail(aid).await {
|
||||
Ok(detail) => results.push((aid.clone(), detail)),
|
||||
Err(e) => {
|
||||
eprintln!("[Crawler] 获取相册 {} 详情失败: {}", aid, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(results)
|
||||
}
|
||||
|
||||
/// 获取所有相册详情(含图片)
|
||||
pub async fn fetch_all_albums_with_images(
|
||||
&self,
|
||||
) -> Result<Vec<(ApiAlbum, ApiAlbumDetail)>, Box<dyn std::error::Error>> {
|
||||
let albums = self.get_albums().await?;
|
||||
let mut results = Vec::new();
|
||||
|
||||
for album in &albums {
|
||||
match self.get_album_detail(&album.id).await {
|
||||
Ok(detail) => results.push((album.clone(), detail)),
|
||||
Err(e) => {
|
||||
eprintln!("[Crawler] 获取相册 {} 详情失败: {}", album.id, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(results)
|
||||
}
|
||||
|
||||
/// 获取指定相册的图片列表(仅返回 id 列表)
|
||||
pub async fn get_album_image_ids(&self, album_id: &str) -> Result<Vec<String>, Box<dyn std::error::Error>> {
|
||||
let detail = self.get_album_detail(album_id).await?;
|
||||
Ok(detail.images.into_iter().map(|img| img.id).collect())
|
||||
}
|
||||
}
|
||||
15
src/engine/mod.rs
Normal file
15
src/engine/mod.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
//! 核心引擎层 — 标题解析、人名匹配、标签提取、推荐计算
|
||||
|
||||
pub mod title_parser;
|
||||
pub mod person_merger;
|
||||
pub mod tag_extractor;
|
||||
pub mod recommend;
|
||||
pub mod pipeline;
|
||||
pub mod crawler;
|
||||
|
||||
#[allow(unused)] pub use title_parser::*;
|
||||
#[allow(unused)] pub use person_merger::*;
|
||||
#[allow(unused)] pub use tag_extractor::*;
|
||||
pub use recommend::*;
|
||||
#[allow(unused)] pub use pipeline::*;
|
||||
#[allow(unused)] pub use crawler::*;
|
||||
422
src/engine/person_merger.rs
Normal file
422
src/engine/person_merger.rs
Normal file
@@ -0,0 +1,422 @@
|
||||
//! 人名匹配与合并
|
||||
//! 对应 Python 的 match_person_from_title.py + merge_persons_by_core.py
|
||||
//! 所有函数均为 async,因为底层 Database 操作是异步的
|
||||
|
||||
use crate::db::Database;
|
||||
use crate::engine::title_parser::{extract_cn, is_excluded_name};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
/// 匹配模式
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Pattern {
|
||||
pub text: String,
|
||||
pub person_name: String,
|
||||
pub source: String, // "name" | "alias"
|
||||
pub length: usize,
|
||||
}
|
||||
|
||||
/// 匹配结果
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MatchResult {
|
||||
pub matched: bool,
|
||||
pub method: String,
|
||||
pub person: Option<String>,
|
||||
pub matched_text: Option<String>,
|
||||
pub all_persons: Vec<String>,
|
||||
pub multi_person: bool,
|
||||
}
|
||||
|
||||
/// 深度匹配结果
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DeepMatchResult {
|
||||
pub matched: bool,
|
||||
pub method: String, // "jieba_extract" | "exact_name_match" | "alias_fuzzy_match"
|
||||
pub person: Option<String>,
|
||||
pub matched_text: Option<String>,
|
||||
pub all_persons: Vec<String>,
|
||||
pub multi_person: bool,
|
||||
}
|
||||
|
||||
// ── Pass1: 收集模式 ──
|
||||
|
||||
/// 收集所有人名和别名,构建匹配模式列表
|
||||
/// 按长度降序排列,确保长名优先匹配
|
||||
pub async fn collect_person_patterns(db: &Database) -> Vec<Pattern> {
|
||||
let mut patterns = Vec::new();
|
||||
|
||||
// 1. 收集所有人名
|
||||
let rows = db.get_all_person_names().await;
|
||||
for (name, _albums) in &rows {
|
||||
if is_excluded_name(name) {
|
||||
continue;
|
||||
}
|
||||
patterns.push(Pattern {
|
||||
text: name.clone(),
|
||||
person_name: name.clone(),
|
||||
source: "name".to_string(),
|
||||
length: name.len(),
|
||||
});
|
||||
}
|
||||
|
||||
// 2. 收集所有别名
|
||||
let aliases = db.get_all_aliases().await;
|
||||
for (alias_name, person_name) in &aliases {
|
||||
if is_excluded_name(person_name) || is_excluded_name(alias_name) {
|
||||
continue;
|
||||
}
|
||||
if alias_name != person_name {
|
||||
patterns.push(Pattern {
|
||||
text: alias_name.clone(),
|
||||
person_name: person_name.clone(),
|
||||
source: "alias".to_string(),
|
||||
length: alias_name.len(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 去重
|
||||
let mut best_map: HashMap<String, Pattern> = HashMap::new();
|
||||
for p in patterns {
|
||||
let key = p.text.to_lowercase();
|
||||
best_map.entry(key).or_insert(p);
|
||||
}
|
||||
let mut patterns: Vec<Pattern> = best_map.into_values().collect();
|
||||
|
||||
// 4. 按长度降序排列
|
||||
patterns.sort_by(|a, b| b.length.cmp(&a.length));
|
||||
|
||||
patterns
|
||||
}
|
||||
|
||||
/// Pass1: 简单子串匹配
|
||||
pub fn match_title(title: &str, patterns: &[Pattern]) -> Option<(String, String, String)> {
|
||||
if title.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
for p in patterns {
|
||||
if p.text.len() < 2 {
|
||||
continue;
|
||||
}
|
||||
if title.contains(&p.text) {
|
||||
return Some((p.text.clone(), p.person_name.clone(), p.source.clone()));
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
/// Pass2: 深度匹配(多重策略)
|
||||
pub fn deep_match_title(
|
||||
title: &str,
|
||||
known_names: &HashSet<String>,
|
||||
known_aliases: &HashSet<String>,
|
||||
) -> DeepMatchResult {
|
||||
let mut result = DeepMatchResult {
|
||||
matched: false,
|
||||
method: String::new(),
|
||||
person: None,
|
||||
matched_text: None,
|
||||
all_persons: Vec::new(),
|
||||
multi_person: false,
|
||||
};
|
||||
|
||||
if title.is_empty() {
|
||||
return result;
|
||||
}
|
||||
|
||||
let title_clean = title.trim();
|
||||
|
||||
// 策略1: 用正则提取人名
|
||||
if let Some(extracted) = crate::engine::title_parser::extract_person_name_from_title(title_clean) {
|
||||
if known_names.contains(&extracted) {
|
||||
result.matched = true;
|
||||
result.method = "jieba_extract".to_string();
|
||||
result.person = Some(extracted.clone());
|
||||
result.matched_text = Some(extracted);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
// 策略2: 标题中精确子串匹配已知人名(长名优先)
|
||||
let mut found: Vec<String> = Vec::new();
|
||||
let mut sorted_names: Vec<&String> = known_names.iter().collect();
|
||||
sorted_names.sort_by(|a, b| b.len().cmp(&a.len()));
|
||||
|
||||
for name in &sorted_names {
|
||||
if name.len() < 2 {
|
||||
continue;
|
||||
}
|
||||
if title_clean.contains(name.as_str()) {
|
||||
found.push(name.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
if !found.is_empty() {
|
||||
found.sort_by(|a, b| b.len().cmp(&a.len()));
|
||||
let best = found[0].clone();
|
||||
result.matched = true;
|
||||
result.method = "exact_name_match".to_string();
|
||||
result.person = Some(best.clone());
|
||||
result.matched_text = Some(best);
|
||||
result.all_persons = found.clone();
|
||||
result.multi_person = found.len() > 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
// 策略3: 别名模糊匹配
|
||||
let title_lower = title_clean.to_lowercase();
|
||||
let mut sorted_aliases: Vec<&String> = known_aliases.iter().collect();
|
||||
sorted_aliases.sort_by(|a, b| b.len().cmp(&a.len()));
|
||||
|
||||
for alias in &sorted_aliases {
|
||||
if alias.len() < 2 {
|
||||
continue;
|
||||
}
|
||||
if title_lower.contains(alias.to_lowercase().as_str()) {
|
||||
result.matched = true;
|
||||
result.method = "alias_fuzzy_match".to_string();
|
||||
result.person = Some(alias.to_string());
|
||||
result.matched_text = Some(alias.to_string());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
/// 收集所有已知人名和别名
|
||||
pub async fn collect_known_names(db: &Database) -> (HashSet<String>, HashSet<String>) {
|
||||
let mut known_names = HashSet::new();
|
||||
let mut known_aliases = HashSet::new();
|
||||
|
||||
for (name, _) in db.get_all_person_names().await {
|
||||
known_names.insert(name);
|
||||
}
|
||||
for (alias_name, _) in db.get_all_aliases().await {
|
||||
known_aliases.insert(alias_name);
|
||||
}
|
||||
|
||||
(known_names, known_aliases)
|
||||
}
|
||||
|
||||
/// 检测含多个人名的相册
|
||||
pub async fn detect_multi_person_titles(
|
||||
db: &Database,
|
||||
known_names: Option<&HashSet<String>>,
|
||||
) -> Vec<(String, String, Vec<String>)> {
|
||||
let names = match known_names {
|
||||
Some(n) => n.clone(),
|
||||
None => collect_known_names(db).await.0,
|
||||
};
|
||||
|
||||
let albums = db.get_all_albums_full().await;
|
||||
let mut multi_person = Vec::new();
|
||||
|
||||
for album in &albums {
|
||||
let title = album.title.as_deref().unwrap_or("");
|
||||
if title.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let mut found: Vec<String> = Vec::new();
|
||||
let mut sorted_names: Vec<&String> = names.iter().collect();
|
||||
sorted_names.sort_by(|a, b| b.len().cmp(&a.len()));
|
||||
|
||||
for name in &sorted_names {
|
||||
if name.len() < 2 {
|
||||
continue;
|
||||
}
|
||||
if title.contains(name.as_str()) {
|
||||
found.push(name.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
if found.len() > 1 {
|
||||
multi_person.push((
|
||||
album.id.clone(),
|
||||
title.to_string(),
|
||||
found,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
multi_person
|
||||
}
|
||||
|
||||
// ── 中文核心合并分析 ──
|
||||
|
||||
/// 分析所有人名的中文核心分组
|
||||
/// 返回: {core: [(id, name, albums, images), ...]}
|
||||
pub async fn analyze_groups(db: &Database) -> HashMap<String, Vec<(i64, String, i64, i64)>> {
|
||||
let persons = db.get_all_persons_with_counts().await;
|
||||
let mut groups: HashMap<String, Vec<(i64, String, i64, i64)>> = HashMap::new();
|
||||
|
||||
for (pid, name, albums, images) in persons {
|
||||
if let Some(cn) = extract_cn(&name) {
|
||||
if cn.len() >= 2 {
|
||||
groups.entry(cn).or_default().push((pid, name, albums, images));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for items in groups.values_mut() {
|
||||
items.sort_by(|a, b| b.2.cmp(&a.2));
|
||||
}
|
||||
|
||||
groups.retain(|_, v| v.len() >= 2);
|
||||
|
||||
groups
|
||||
}
|
||||
|
||||
/// 合并结果统计
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct MergeResult {
|
||||
pub groups_analyzed: usize,
|
||||
pub persons_merged: usize,
|
||||
pub aliases_moved: usize,
|
||||
}
|
||||
|
||||
/// 执行合并:扫描所有可合并的人物组,合并到相册数最多的那个
|
||||
/// 返回合并统计
|
||||
pub async fn execute_merge(db: &Database) -> MergeResult {
|
||||
let groups = analyze_groups(db).await;
|
||||
let mut result = MergeResult::default();
|
||||
result.groups_analyzed = groups.len();
|
||||
|
||||
for (_core, items) in &groups {
|
||||
if items.len() < 2 {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 第一个(相册数最多)作为目标
|
||||
let dst_id = items[0].0;
|
||||
let dst_name = &items[0].1;
|
||||
|
||||
// 其余的合并到目标
|
||||
for (src_id, src_name, _, _) in &items[1..] {
|
||||
println!("[合并] {} (id={}) → {} (id={})", src_name, src_id, dst_name, dst_id);
|
||||
db.merge_person_into(*src_id, dst_id).await;
|
||||
result.persons_merged += 1;
|
||||
}
|
||||
}
|
||||
|
||||
if result.persons_merged > 0 {
|
||||
println!("[合并] 完成:分析 {} 组,合并 {} 个人物", result.groups_analyzed, result.persons_merged);
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
/// 统计匹配分析
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MatchStats {
|
||||
pub total_albums: usize,
|
||||
pub matched: usize,
|
||||
pub unmatched: usize,
|
||||
pub already_set: usize,
|
||||
pub changed: usize,
|
||||
pub by_source: HashMap<String, usize>,
|
||||
pub name_frequency: HashMap<String, usize>,
|
||||
}
|
||||
|
||||
/// 执行 Pass1 匹配分析
|
||||
pub async fn analyze_matches(db: &Database, patterns: &[Pattern]) -> MatchStats {
|
||||
let albums = db.get_all_albums_full().await;
|
||||
let mut stats = MatchStats {
|
||||
total_albums: albums.len(),
|
||||
matched: 0,
|
||||
unmatched: 0,
|
||||
already_set: 0,
|
||||
changed: 0,
|
||||
by_source: HashMap::new(),
|
||||
name_frequency: HashMap::new(),
|
||||
};
|
||||
|
||||
for album in &albums {
|
||||
let title = album.title.as_deref().unwrap_or("");
|
||||
let current = album.person_name_original.as_deref().unwrap_or("");
|
||||
|
||||
if let Some((_text, person, source)) = match_title(title, patterns) {
|
||||
*stats.by_source.entry(source).or_insert(0) += 1;
|
||||
*stats.name_frequency.entry(person.clone()).or_insert(0) += 1;
|
||||
|
||||
if current.is_empty() {
|
||||
stats.matched += 1;
|
||||
} else if current != person {
|
||||
stats.changed += 1;
|
||||
} else {
|
||||
stats.already_set += 1;
|
||||
}
|
||||
} else {
|
||||
stats.unmatched += 1;
|
||||
}
|
||||
}
|
||||
|
||||
stats
|
||||
}
|
||||
|
||||
/// 深度匹配分析统计
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DeepMatchStats {
|
||||
pub total_albums: usize,
|
||||
pub pass1_matched: usize,
|
||||
pub pass1_unmatched: usize,
|
||||
pub pass2_jieba_match: usize,
|
||||
pub pass2_exact_match: usize,
|
||||
pub pass2_alias_match: usize,
|
||||
pub pass2_still_unmatched: usize,
|
||||
pub pass2_multi_person: usize,
|
||||
}
|
||||
|
||||
/// 执行 Pass2 深度匹配分析
|
||||
pub async fn analyze_deep_matches(db: &Database) -> DeepMatchStats {
|
||||
let (known_names, known_aliases) = collect_known_names(db).await;
|
||||
let albums = db.get_all_albums_full().await;
|
||||
|
||||
let mut stats = DeepMatchStats {
|
||||
total_albums: albums.len(),
|
||||
pass1_matched: 0,
|
||||
pass1_unmatched: 0,
|
||||
pass2_jieba_match: 0,
|
||||
pass2_exact_match: 0,
|
||||
pass2_alias_match: 0,
|
||||
pass2_still_unmatched: 0,
|
||||
pass2_multi_person: 0,
|
||||
};
|
||||
|
||||
for album in &albums {
|
||||
let title = album.title.as_deref().unwrap_or("");
|
||||
let current = album.person_name_original.as_deref().unwrap_or("");
|
||||
|
||||
if title.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
if !current.is_empty() && known_names.contains(current) {
|
||||
stats.pass1_matched += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
stats.pass1_unmatched += 1;
|
||||
|
||||
let result = deep_match_title(title, &known_names, &known_aliases);
|
||||
if !result.matched {
|
||||
stats.pass2_still_unmatched += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
match result.method.as_str() {
|
||||
"jieba_extract" => stats.pass2_jieba_match += 1,
|
||||
"exact_name_match" => stats.pass2_exact_match += 1,
|
||||
"alias_fuzzy_match" => stats.pass2_alias_match += 1,
|
||||
_ => {}
|
||||
}
|
||||
|
||||
if result.multi_person {
|
||||
stats.pass2_multi_person += 1;
|
||||
}
|
||||
}
|
||||
|
||||
stats
|
||||
}
|
||||
90
src/engine/pipeline.rs
Normal file
90
src/engine/pipeline.rs
Normal file
@@ -0,0 +1,90 @@
|
||||
//! 处理管线 — 批量处理相册元数据
|
||||
|
||||
use crate::db::Database;
|
||||
use crate::engine::title_parser;
|
||||
use crate::engine::tag_extractor;
|
||||
use crate::engine::person_merger;
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// 处理所有相册的元数据:提取人名 + 标签
|
||||
/// 两阶段:先用正则提取人名入库,再用匹配精炼,最后提取标签
|
||||
pub async fn process_all_albums(db: &Database) -> (usize, usize) {
|
||||
let albums = db.get_all_albums_full().await;
|
||||
let mut person_count = 0;
|
||||
let mut tag_count = 0;
|
||||
|
||||
// Phase 1: 先用正则从标题直接提取人名(冷启动,不需要已有 persons)
|
||||
let patterns = person_merger::collect_person_patterns(db).await;
|
||||
let use_patterns = !patterns.is_empty();
|
||||
|
||||
for album in &albums {
|
||||
let title = album.title.as_deref().unwrap_or("");
|
||||
if title.is_empty() { continue; }
|
||||
|
||||
// 提取人名
|
||||
if album.person_name_original.is_none() || album.person_name_original.as_deref() == Some("") {
|
||||
let person_name = if use_patterns {
|
||||
// 已有人物库,用匹配
|
||||
person_merger::match_title(title, &patterns).map(|(_, name, _)| name)
|
||||
} else {
|
||||
// 冷启动,直接用正则提取
|
||||
title_parser::extract_person_name_from_title(title)
|
||||
};
|
||||
|
||||
if let Some(person_name) = person_name {
|
||||
// 拆分中文主名 + 别名
|
||||
let (primary_name, aliases) = title_parser::split_person_name_with_aliases(&person_name);
|
||||
let final_name = if primary_name.is_empty() { person_name.clone() } else { primary_name };
|
||||
db.update_album_person(&album.id, &person_name, &final_name).await;
|
||||
|
||||
// 创建人员(先查是否已有同名)
|
||||
let pid = db.insert_person(&final_name, None).await;
|
||||
db.insert_person_album(pid, &album.id).await;
|
||||
|
||||
// 保存所有别名
|
||||
for (alias_type, alias_name) in &aliases {
|
||||
if !alias_name.is_empty() && alias_name != &final_name {
|
||||
db.insert_person_alias(pid, alias_type, alias_name).await;
|
||||
}
|
||||
}
|
||||
|
||||
db.update_person_counts(pid).await;
|
||||
person_count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Phase 2: 提取标签(使用已填充的人名来排除)
|
||||
// 重新获取 albums(因为 person_name_cleaned 已更新)
|
||||
let albums_refreshed = db.get_all_albums_full().await;
|
||||
let mut seen_albums: HashMap<String, String> = HashMap::new();
|
||||
for album in &albums {
|
||||
if let Some(pn) = &album.person_name_cleaned {
|
||||
seen_albums.insert(album.id.clone(), pn.clone());
|
||||
}
|
||||
}
|
||||
|
||||
for album in &albums_refreshed {
|
||||
let title = album.title.as_deref().unwrap_or("");
|
||||
if title.is_empty() { continue; }
|
||||
|
||||
let existing_tags = album.tags.as_deref().unwrap_or("");
|
||||
if existing_tags.is_empty() || existing_tags == "[]" {
|
||||
let person = album.person_name_cleaned.as_deref()
|
||||
.or_else(|| seen_albums.get(&album.id).map(|s| s.as_str()));
|
||||
let tags = tag_extractor::extract_tags_from_title(title, person);
|
||||
let unique = tag_extractor::unique_tags(tags);
|
||||
if !unique.is_empty() {
|
||||
let tag_names: Vec<String> = unique.iter().map(|(t, _)| t.clone()).collect();
|
||||
let tags_json = serde_json::to_string(&tag_names).unwrap_or_else(|_| "[]".to_string());
|
||||
let all_words: Vec<[String; 2]> = unique.iter().map(|(t, tp)| [t.clone(), tp.clone()]).collect();
|
||||
let words_json = serde_json::to_string(&all_words).unwrap_or_else(|_| "[]".to_string());
|
||||
db.update_album_tags(&album.id, &tags_json, &words_json).await;
|
||||
tag_count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!("[管线] 处理完成:{} 个人名,{} 个标签", person_count, tag_count);
|
||||
(person_count, tag_count)
|
||||
}
|
||||
203
src/engine/recommend.rs
Normal file
203
src/engine/recommend.rs
Normal file
@@ -0,0 +1,203 @@
|
||||
//! 推荐引擎 — 标签推荐、人物推荐、关联推荐
|
||||
//!
|
||||
//! 推荐策略:
|
||||
//! 1. 标签推荐:基于输入前缀匹配 + 热门度排序
|
||||
//! 2. 人物推荐:基于名称匹配 + 相册数排序
|
||||
//! 3. 关联推荐:基于标签共现关系 + 人物共现关系
|
||||
|
||||
use crate::db::Database;
|
||||
use crate::models::*;
|
||||
use sqlx::Row;
|
||||
|
||||
/// 推荐引擎
|
||||
pub struct RecommendEngine;
|
||||
|
||||
impl RecommendEngine {
|
||||
/// 标签自动补全推荐
|
||||
pub async fn recommend_tags(
|
||||
db: &Database,
|
||||
query: &str,
|
||||
limit: usize,
|
||||
) -> RecommendResponse {
|
||||
let limit = limit.min(20);
|
||||
let q = query.trim().to_lowercase();
|
||||
|
||||
let tags = db.get_popular_tags(200).await;
|
||||
let mut items: Vec<RecommendItem> = Vec::new();
|
||||
|
||||
for t in &tags {
|
||||
let tag_lower = t.tag.to_lowercase();
|
||||
let score = if q.is_empty() {
|
||||
// 无查询时按热门度
|
||||
t.count as f64
|
||||
} else if tag_lower == q {
|
||||
// 精确匹配
|
||||
1000.0 + t.count as f64
|
||||
} else if tag_lower.starts_with(&q) {
|
||||
// 前缀匹配
|
||||
500.0 + t.count as f64
|
||||
} else if tag_lower.contains(&q) {
|
||||
// 包含匹配
|
||||
100.0 + t.count as f64
|
||||
} else {
|
||||
continue;
|
||||
};
|
||||
|
||||
items.push(RecommendItem {
|
||||
value: t.tag.clone(),
|
||||
label: format!("{} ({})", t.tag, t.count),
|
||||
score,
|
||||
count: Some(t.count),
|
||||
source: "tag".to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
items.sort_by(|a, b| b.score.partial_cmp(&a.score).unwrap_or(std::cmp::Ordering::Equal));
|
||||
let total = items.len();
|
||||
items.truncate(limit);
|
||||
|
||||
RecommendResponse { items, total, category: "tags".to_string() }
|
||||
}
|
||||
|
||||
/// 人物推荐
|
||||
pub async fn recommend_persons(
|
||||
db: &Database,
|
||||
query: &str,
|
||||
limit: usize,
|
||||
) -> RecommendResponse {
|
||||
let limit = limit.min(20);
|
||||
let q = query.trim().to_lowercase();
|
||||
|
||||
let persons = db.get_all_persons_with_aliases().await;
|
||||
let mut items: Vec<RecommendItem> = Vec::new();
|
||||
|
||||
for p in &persons {
|
||||
let name_lower = p.name.to_lowercase();
|
||||
let score = if q.is_empty() {
|
||||
p.total_albums as f64
|
||||
} else if name_lower == q {
|
||||
1000.0 + p.total_albums as f64
|
||||
} else if name_lower.starts_with(&q) {
|
||||
500.0 + p.total_albums as f64
|
||||
} else if name_lower.contains(&q) {
|
||||
100.0 + p.total_albums as f64
|
||||
} else {
|
||||
// 检查别名
|
||||
let alias_match = p.aliases.iter().any(|a| a.to_lowercase().contains(&q));
|
||||
if alias_match { 200.0 + p.total_albums as f64 } else { continue }
|
||||
};
|
||||
|
||||
let label = if p.aliases.is_empty() {
|
||||
format!("{} ({}相册)", p.name, p.total_albums)
|
||||
} else {
|
||||
format!("{} [{}] ({}相册)", p.name, p.aliases.join(", "), p.total_albums)
|
||||
};
|
||||
|
||||
items.push(RecommendItem {
|
||||
value: p.name.clone(),
|
||||
label,
|
||||
score,
|
||||
count: Some(p.total_albums),
|
||||
source: "person".to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
items.sort_by(|a, b| b.score.partial_cmp(&a.score).unwrap_or(std::cmp::Ordering::Equal));
|
||||
let total = items.len();
|
||||
items.truncate(limit);
|
||||
|
||||
RecommendResponse { items, total, category: "persons".to_string() }
|
||||
}
|
||||
|
||||
/// 关联推荐(共现标签 + 共现人物)
|
||||
pub async fn recommend_related(
|
||||
db: &Database,
|
||||
context: &str, // 标签或人名
|
||||
limit: usize,
|
||||
) -> TagRecommendation {
|
||||
let limit = limit.min(15);
|
||||
|
||||
// 获取共现标签
|
||||
let co_tags = db.get_tag_co_occurrences(context, limit as i64).await;
|
||||
let related_tags: Vec<RecommendItem> = co_tags.iter().map(|c| {
|
||||
RecommendItem {
|
||||
value: c.co_tag.clone(),
|
||||
label: format!("{} (共现{}次)", c.co_tag, c.count),
|
||||
score: c.score,
|
||||
count: Some(c.count),
|
||||
source: "co-occurrence".to_string(),
|
||||
}
|
||||
}).collect();
|
||||
|
||||
// 获取相关人物(通过标签)
|
||||
let albums = db.search_albums_by_tag(context).await;
|
||||
let mut person_scores: std::collections::HashMap<String, (i64, f64)> = std::collections::HashMap::new();
|
||||
let total = albums.len() as f64;
|
||||
for album in &albums {
|
||||
let meta = db.get_all_albums_full().await;
|
||||
for m in &meta {
|
||||
if m.id == album.id.to_string() {
|
||||
if let Some(ref name) = m.person_name_cleaned {
|
||||
let entry = person_scores.entry(name.clone()).or_insert((0, 0.0));
|
||||
entry.0 += 1;
|
||||
entry.1 = entry.0 as f64 / total;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut similar_persons: Vec<RecommendItem> = person_scores.into_iter()
|
||||
.filter(|(_, (cnt, _))| *cnt >= 1)
|
||||
.map(|(name, (cnt, score))| RecommendItem {
|
||||
value: name.clone(),
|
||||
label: format!("{} ({}次)", name, cnt),
|
||||
score,
|
||||
count: Some(cnt),
|
||||
source: "co-occurrence".to_string(),
|
||||
}).collect();
|
||||
similar_persons.sort_by(|a, b| b.score.partial_cmp(&a.score).unwrap_or(std::cmp::Ordering::Equal));
|
||||
similar_persons.truncate(limit);
|
||||
|
||||
TagRecommendation {
|
||||
tag: context.to_string(),
|
||||
related_tags,
|
||||
similar_persons,
|
||||
}
|
||||
}
|
||||
|
||||
/// 热门人物
|
||||
pub async fn popular_persons(db: &Database, limit: usize) -> Vec<PopularPerson> {
|
||||
let persons = db.get_all_persons_with_aliases().await;
|
||||
let mut results: Vec<PopularPerson> = persons.iter().take(limit).map(|p| {
|
||||
PopularPerson {
|
||||
name: p.name.clone(),
|
||||
display_name: p.display_name.clone(),
|
||||
total_albums: p.total_albums,
|
||||
total_images: p.total_images,
|
||||
top_tags: Vec::new(),
|
||||
aliases: p.aliases.clone(),
|
||||
}
|
||||
}).collect();
|
||||
|
||||
// 为热门人物获取 top tags
|
||||
// (简化:从 person_albums 查标签)
|
||||
for rp in &mut results {
|
||||
if let Some(person) = db.get_person_by_name(&rp.name).await {
|
||||
let albums = db.get_person_albums(person.id).await;
|
||||
let mut tag_counts: std::collections::HashMap<String, i64> = std::collections::HashMap::new();
|
||||
for album in &albums {
|
||||
let aid = album.id.to_string();
|
||||
let rows = sqlx::query("SELECT tag FROM album_tags WHERE album_id = ?")
|
||||
.bind(&aid).fetch_all(&db.pool).await.unwrap_or_default();
|
||||
for row in &rows {
|
||||
*tag_counts.entry(row.get::<String, _>("tag")).or_insert(0) += 1;
|
||||
}
|
||||
}
|
||||
let mut tags: Vec<(String, i64)> = tag_counts.into_iter().collect();
|
||||
tags.sort_by(|a, b| b.1.cmp(&a.1));
|
||||
rp.top_tags = tags.into_iter().take(5).map(|(t, _)| t).collect();
|
||||
}
|
||||
}
|
||||
results
|
||||
}
|
||||
}
|
||||
281
src/engine/tag_extractor.rs
Normal file
281
src/engine/tag_extractor.rs
Normal file
@@ -0,0 +1,281 @@
|
||||
//! 标签提取器 — 从标题中提取分类明确、格式美观的标签
|
||||
//!
|
||||
//! 标签分类体系:
|
||||
//! - style: 风格标签(日系/韩系/森系/复古/甜美/性感等)
|
||||
//! - outfit: 服装标签(比基尼/泳装/旗袍/制服/丝袜/内衣等)
|
||||
//! - scene: 场景标签(室内/户外/街拍/浴室/床上/车内等)
|
||||
//! - body: 身体标签(美腿/美胸/翘臀/玉足等)
|
||||
//! - mood: 情绪标签(清纯/诱惑/唯美/清新/冷艳等)
|
||||
//! - topic: 主题标签(写真/私房/街拍/旅拍等)
|
||||
//! - other: 其他关键词
|
||||
|
||||
use regex::Regex;
|
||||
use lazy_static::lazy_static;
|
||||
use std::collections::HashSet;
|
||||
|
||||
lazy_static! {
|
||||
static ref WORD_RE: Regex = Regex::new(r"[a-zA-Z0-9\u{4e00}-\u{9fff}]+").unwrap();
|
||||
static ref HAS_CHINESE: Regex = Regex::new(r"[\u{4e00}-\u{9fff}]").unwrap();
|
||||
}
|
||||
|
||||
// ── 标签词库(按分类) ──
|
||||
|
||||
/// 风格标签
|
||||
const STYLE_TAGS: &[&str] = &[
|
||||
"日系", "韩系", "森系", "复古", "甜美", "性感", "清新", "冷艳",
|
||||
"优雅", "知性", "可爱", "俏皮", "温柔", "妩媚", "野性", "清纯",
|
||||
"时尚", "潮流", "街头", "学院", "文艺", "古典", "民族", "异域",
|
||||
"japanese", "korean", "retro", "vintage", "cute", "sexy",
|
||||
"elegant", "fresh", "sweet", "wild", "pure", "fashion",
|
||||
];
|
||||
|
||||
/// 服装标签
|
||||
const OUTFIT_TAGS: &[&str] = &[
|
||||
"比基尼", "泳装", "泳衣", "旗袍", "制服", "丝袜", "内衣", "文胸",
|
||||
"包臀裙", "紧身裙", "超短裙", "连衣裙", "半身裙", "短裙", "长裙",
|
||||
"牛仔裤", "热裤", "短裤", "休闲裤", "西装裤",
|
||||
"T恤", "衬衫", "毛衣", "针织衫", "外套", "风衣", "大衣", "西装",
|
||||
"吊带", "抹胸", "露背", "透视", "蕾丝", "镂空", "开叉",
|
||||
"高跟鞋", "运动鞋", "凉鞋", "拖鞋", "靴子",
|
||||
"bikini", "swimsuit", "stockings", "lingerie", "dress",
|
||||
"skirt", "jeans", "shirt", "sweater", "coat", "heels",
|
||||
];
|
||||
|
||||
/// 场景标签
|
||||
const SCENE_TAGS: &[&str] = &[
|
||||
"室内", "户外", "街拍", "浴室", "浴缸", "床上", "床照", "沙发",
|
||||
"厨房", "阳台", "窗边", "楼梯", "走廊", "电梯", "车内", "驾驶座",
|
||||
"海边", "沙滩", "泳池", "温泉", "森林", "草地", "花园", "公园",
|
||||
"街道", "巷弄", "建筑", "天台", "屋顶", "咖啡馆", "餐厅", "酒店",
|
||||
"indoor", "outdoor", "street", "beach", "pool", "bathroom",
|
||||
"bedroom", "kitchen", "balcony", "car", "cafe", "hotel",
|
||||
];
|
||||
|
||||
/// 身体标签
|
||||
const BODY_TAGS: &[&str] = &[
|
||||
"美腿", "美胸", "翘臀", "玉足", "美背", "香肩", "蛮腰", "细腰",
|
||||
"长腿", "酥胸", "丰臀", "白皙", "肌肤", "纹身",
|
||||
"legs", "breasts", "ass", "feet", "back", "shoulders", "waist",
|
||||
"skin", "tattoo",
|
||||
];
|
||||
|
||||
/// 情绪标签
|
||||
const MOOD_TAGS: &[&str] = &[
|
||||
"清纯", "诱惑", "唯美", "清新", "冷艳", "妩媚", "娇羞", "俏皮",
|
||||
"温柔", "野性", "冷傲", "热情", "慵懒", "迷离", "甜美", "知性",
|
||||
"pure", "seductive", "beautiful", "fresh", "cool", "charming",
|
||||
"shy", "cute", "gentle", "wild", "lazy", "sweet",
|
||||
];
|
||||
|
||||
/// 主题标签
|
||||
const TOPIC_TAGS: &[&str] = &[
|
||||
"写真", "私房", "街拍", "旅拍", "婚纱照", "艺术照", "生活照",
|
||||
"自拍", "跟拍", "约拍", "商拍",
|
||||
"photoshoot", "portrait", "selfie", "travel", "wedding",
|
||||
];
|
||||
|
||||
/// 停用词(完全排除,不做标签)
|
||||
const STOP_WORDS: &[&str] = &[
|
||||
"photos", "photo", "pictures", "picture", "images", "image", "video", "videos",
|
||||
"cosplay", "cos", "and", "the", "of", "in", "a", "an", "is", "to", "for",
|
||||
"part", "vol", "collection", "special", "extra", "bonus",
|
||||
"new", "old", "selfie", "official", "original", "preview", "patreon",
|
||||
"with", "without", "set", "gallery", "album", "page", "issue",
|
||||
"model", "idol", "star", "actor", "actress", "singer",
|
||||
"秀人", "网", "图", "套", "合集", "系列", "作品", "美女", "模特",
|
||||
"女神", "小姐姐", "性感", "高清", "原版", "无水印", "番号", "编号",
|
||||
"site", "web", "online", "digital", "exclusive", "premium",
|
||||
"classic", "photography", "shoot", "shot",
|
||||
];
|
||||
|
||||
/// 全局停用词(标签提取时也排除)
|
||||
fn get_stop_words_set() -> HashSet<&'static str> {
|
||||
let mut s = HashSet::new();
|
||||
for w in STOP_WORDS { s.insert(*w); }
|
||||
s
|
||||
}
|
||||
|
||||
/// 所有分类词库(用于快速匹配分类)
|
||||
fn get_tag_categories() -> Vec<(&'static [&'static str], &'static str)> {
|
||||
vec![
|
||||
(STYLE_TAGS, "style"),
|
||||
(OUTFIT_TAGS, "outfit"),
|
||||
(SCENE_TAGS, "scene"),
|
||||
(BODY_TAGS, "body"),
|
||||
(MOOD_TAGS, "mood"),
|
||||
(TOPIC_TAGS, "topic"),
|
||||
]
|
||||
}
|
||||
|
||||
/// 判断一个词属于哪个分类
|
||||
fn classify_tag(word: &str) -> &'static str {
|
||||
let word_lower = word.to_lowercase();
|
||||
for (tags, category) in get_tag_categories() {
|
||||
for tag in tags {
|
||||
if tag.to_lowercase() == word_lower {
|
||||
return category;
|
||||
}
|
||||
}
|
||||
}
|
||||
"other"
|
||||
}
|
||||
|
||||
// ── 核心函数 ──
|
||||
|
||||
/// 从标题中提取分类标签
|
||||
pub fn extract_tags_from_title(
|
||||
title: &str,
|
||||
person_name: Option<&str>,
|
||||
) -> Vec<(String, String)> {
|
||||
let mut tags = Vec::new();
|
||||
let mut title_lower = title.to_lowercase();
|
||||
|
||||
// 移除人名
|
||||
if let Some(name) = person_name {
|
||||
title_lower = title_lower.replace(&name.to_lowercase(), " ");
|
||||
// 也移除英文名部分
|
||||
for part in name.split_whitespace() {
|
||||
title_lower = title_lower.replace(&part.to_lowercase(), " ");
|
||||
}
|
||||
}
|
||||
|
||||
let stop_words = get_stop_words_set();
|
||||
let person_name_lower = person_name.map(|n| n.to_lowercase());
|
||||
let mut seen = HashSet::new();
|
||||
|
||||
// 第一步:优先匹配分类词库中的词(保证高质量标签)
|
||||
for (tag_list, category) in get_tag_categories() {
|
||||
for tag in tag_list {
|
||||
let tag_lower = tag.to_lowercase();
|
||||
if title_lower.contains(&tag_lower) && !seen.contains(&tag_lower) {
|
||||
seen.insert(tag_lower);
|
||||
tags.push((tag.to_string(), category.to_string()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 第二步:分词提取其他关键词
|
||||
for cap in WORD_RE.find_iter(&title_lower) {
|
||||
let word = cap.as_str().trim();
|
||||
let word_lower = word.to_lowercase();
|
||||
|
||||
// 基础过滤
|
||||
if word.len() < 2 { continue; }
|
||||
if word.chars().all(|c| c.is_ascii_digit()) { continue; }
|
||||
if stop_words.contains(word_lower.as_str()) { continue; }
|
||||
if let Some(ref pn) = person_name_lower {
|
||||
if word_lower.contains(pn.as_str()) { continue; }
|
||||
}
|
||||
if seen.contains(&word_lower) { continue; }
|
||||
|
||||
// 已经在分类词库中匹配过的跳过
|
||||
let mut already_matched = false;
|
||||
for (tag_list, _) in get_tag_categories() {
|
||||
for tag in tag_list {
|
||||
if tag.to_lowercase() == word_lower {
|
||||
already_matched = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if already_matched { break; }
|
||||
}
|
||||
if already_matched { continue; }
|
||||
|
||||
seen.insert(word_lower.clone());
|
||||
|
||||
let tag_type = if HAS_CHINESE.is_match(word) { "chinese" } else { "english" };
|
||||
tags.push((word.to_string(), tag_type.to_string()));
|
||||
}
|
||||
|
||||
tags
|
||||
}
|
||||
|
||||
/// 标签去重并排序(分类标签优先)
|
||||
pub fn unique_tags(tags: Vec<(String, String)>) -> Vec<(String, String)> {
|
||||
let mut seen = HashSet::new();
|
||||
let mut result = Vec::new();
|
||||
|
||||
// 分类排序权重
|
||||
let category_order: Vec<&str> = vec![
|
||||
"style", "outfit", "scene", "body", "mood", "topic", "chinese", "english", "other"
|
||||
];
|
||||
|
||||
let mut sorted_tags = tags;
|
||||
sorted_tags.sort_by(|a, b| {
|
||||
let idx_a = category_order.iter().position(|&c| c == a.1).unwrap_or(999);
|
||||
let idx_b = category_order.iter().position(|&c| c == b.1).unwrap_or(999);
|
||||
idx_a.cmp(&idx_b)
|
||||
});
|
||||
|
||||
for (tag, tag_type) in sorted_tags {
|
||||
let key = tag.to_lowercase();
|
||||
if seen.insert(key) {
|
||||
result.push((tag, tag_type));
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
/// 获取标签的中文分类名(用于前端展示)
|
||||
pub fn get_category_label(category: &str) -> &'static str {
|
||||
match category {
|
||||
"style" => "风格",
|
||||
"outfit" => "服装",
|
||||
"scene" => "场景",
|
||||
"body" => "身体",
|
||||
"mood" => "情绪",
|
||||
"topic" => "主题",
|
||||
"chinese" => "中文",
|
||||
"english" => "英文",
|
||||
_ => "其他",
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取标签分类的颜色(用于前端标签着色)
|
||||
pub fn get_category_color(category: &str) -> (&'static str, &'static str) {
|
||||
match category {
|
||||
"style" => ("#f0f9ff", "#0284c7"), // 浅蓝底 + 深蓝字
|
||||
"outfit" => ("#fdf2f8", "#db2777"), // 浅粉底 + 深粉字
|
||||
"scene" => ("#f0fdf4", "#15803d"), // 浅绿底 + 深绿字
|
||||
"body" => ("#fef3c7", "#b45309"), // 浅黄底 + 深黄字
|
||||
"mood" => ("#faf5ff", "#7c3aed"), // 浅紫底 + 深紫字
|
||||
"topic" => ("#f0f9ff", "#0369a1"), // 浅蓝底 + 蓝字
|
||||
"chinese" => ("#f1f5f9", "#475569"), // 浅灰底 + 深灰字
|
||||
"english" => ("#f1f5f9", "#64748b"), // 浅灰底 + 灰字
|
||||
_ => ("#f1f5f9", "#64748b"), // 默认
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_extract_tags() {
|
||||
let tags = extract_tags_from_title(
|
||||
"XiuRen秀人网 – 杨晨晨Yome – Night Dress '81 photos'",
|
||||
Some("杨晨晨Yome"),
|
||||
);
|
||||
let names: Vec<&str> = tags.iter().map(|(t, _)| t.as_str()).collect();
|
||||
assert!(!names.contains(&"photos"));
|
||||
assert!(!names.contains(&"杨晨晨"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_classify_tag() {
|
||||
assert_eq!(classify_tag("日系"), "style");
|
||||
assert_eq!(classify_tag("比基尼"), "outfit");
|
||||
assert_eq!(classify_tag("浴室"), "scene");
|
||||
assert_eq!(classify_tag("美腿"), "body");
|
||||
assert_eq!(classify_tag("清纯"), "mood");
|
||||
assert_eq!(classify_tag("写真"), "topic");
|
||||
assert_eq!(classify_tag("unknown"), "other");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_category_colors() {
|
||||
let (bg, fg) = get_category_color("style");
|
||||
assert_eq!(bg, "#f0f9ff");
|
||||
assert_eq!(fg, "#0284c7");
|
||||
}
|
||||
}
|
||||
435
src/engine/title_parser.rs
Normal file
435
src/engine/title_parser.rs
Normal file
@@ -0,0 +1,435 @@
|
||||
//! 标题解析、人名提取、标签提取
|
||||
//! 对应 Python 的 tag_index.py + clean_person_names.py 的核心逻辑
|
||||
|
||||
use regex::Regex;
|
||||
use std::collections::HashSet;
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
// ── 常量 ──
|
||||
|
||||
/// 模特称呼前缀(移除用)
|
||||
const MODEL_TITLE_PREFIX: &[&str] = &[
|
||||
"美女模特", "性感女神", "清纯美女", "气质美女", "可爱女孩",
|
||||
"甜美少女", "时尚达人", "女神", "美女", "模特", "小姐姐",
|
||||
"小仙女", "御姐", "萝莉", "辣妹", "潮人",
|
||||
];
|
||||
|
||||
/// 停用词(标签过滤用)
|
||||
const STOP_WORDS: &[&str] = &[
|
||||
"写真", "套图", "图片", "照片", "摄影", "拍摄", "风格",
|
||||
"系列", "作品", "专辑", "主题", "人像", "艺术", "唯美",
|
||||
"诱惑", "性感", "迷人", "魅力", "时尚", "潮流", "高清",
|
||||
"原版", "无水印", "私房", "内衣", "比基尼", "泳装",
|
||||
"旗袍", "制服", "丝袜", "美腿", "美胸", "翘臀",
|
||||
"cosplay", "cos", "角色扮演", "动漫", "游戏",
|
||||
"classic", "exclusive", "premium", "collection",
|
||||
"photo", "photos", "video", "videos", "set", "vol",
|
||||
"no", "番号", "编号", "期号",
|
||||
];
|
||||
|
||||
/// 保留词性(仅用于 jieba 分词场景,这里做占位符)
|
||||
const _KEEP_POS: &[&str] = &["n", "nr", "ns", "nz", "v", "vn", "a", "an", "eng", "x"];
|
||||
|
||||
/// 工作室/平台排除名
|
||||
const EXCLUDED_NAMES: &[&str] = &[
|
||||
"Pure Media", "XiuRen秀人网", "尤蜜", "魅妍社", "花漾",
|
||||
"瑞丝馆", "撸图", "情欲萝莉", "蜜桃社", "tgod",
|
||||
"MiStar", "BLUECAKE", "HuaYang", "YouMi", "秀人",
|
||||
];
|
||||
|
||||
lazy_static! {
|
||||
/// 编译后的正则
|
||||
static ref RE_COSPLAY: Regex = Regex::new(
|
||||
r"(?i)^(.+?)\s+cos(play)?\s+(.+?)(?:\s*[–—\-]\s*(.+))?$"
|
||||
).unwrap();
|
||||
static ref RE_COSPLAY_BY: Regex = Regex::new(
|
||||
r"(?i)^(.+?)\s+cos(play)?\s+by\s+(.+?)(?:\s*[–—\-]\s*(.+))?$"
|
||||
).unwrap();
|
||||
static ref RE_BRACKET_PREFIX: Regex = Regex::new(r"^\[([^\]]+)\]\s*").unwrap();
|
||||
static ref RE_NUMBER_SUFFIX: Regex = Regex::new(r"[\s#]*(?:no\.?\s*|vol\.?\s*|#)?\d+[\s]*$").unwrap();
|
||||
static ref RE_PAREN_CONTENT: Regex = Regex::new(r"[((]([^))]*)[))]").unwrap();
|
||||
static ref RE_SPLIT_DASH: Regex = Regex::new(r"\s*[–—\-]\s*").unwrap();
|
||||
static ref RE_PHOTOS_SUFFIX: Regex = Regex::new(r"(?i)\s*\d+\s*photos?\s*(?:and\s*\d+\s*videos?)?\s*$").unwrap();
|
||||
static ref RE_CHINESE: Regex = Regex::new(r"[\u{4e00}-\u{9fff}]").unwrap();
|
||||
static ref RE_ENGLISH_SUFFIX: Regex = Regex::new(r"^([\u{4e00}-\u{9fff}]+)([A-Za-z].*)$").unwrap();
|
||||
static ref RE_TRAILING_PUNCT: Regex = Regex::new(r"[\s.,;:,。;:\-–—]+$").unwrap();
|
||||
/// 人名模式:2-5个中文字符 + 可选英文后缀
|
||||
static ref RE_CN_NAME: Regex = Regex::new(
|
||||
r"([\u{4e00}-\u{9fff}]{2,5}(?:[A-Za-z][A-Za-z0-9_]*)?)"
|
||||
).unwrap();
|
||||
/// 常见人名后缀(表明前面是人名)
|
||||
static ref RE_NAME_CONTEXT: Regex = Regex::new(
|
||||
r"(?i)(私房|写真|内衣|比基尼|泳装|旗袍|制服|街拍|室内|户外|浴室|浴室|浴缸|床上|床照|内衣|透视|包臀裙|紧身裙|超短裙|丝袜|美腿|美臀|美胸|翘臀|性感|清纯|甜美|气质|可爱|女神|嫩模|模特)"
|
||||
).unwrap();
|
||||
|
||||
/// 停用词集合
|
||||
static ref STOP_WORDS_SET: HashSet<&'static str> = {
|
||||
let mut s = HashSet::new();
|
||||
for w in STOP_WORDS { s.insert(*w); }
|
||||
s
|
||||
};
|
||||
|
||||
/// 排除名集合(小写)
|
||||
static ref EXCLUDED_LOWER: HashSet<String> = {
|
||||
let mut s = HashSet::new();
|
||||
for n in EXCLUDED_NAMES { s.insert(n.to_lowercase()); }
|
||||
s.extend(["youmi", "huayang", "bluecake", "mistar", "xiuren", "秀人"].map(|s| s.to_string()));
|
||||
s
|
||||
};
|
||||
}
|
||||
|
||||
// ── 人名提取 ──
|
||||
|
||||
/// 从标题中提取模特/人物名称
|
||||
/// 核心策略:
|
||||
/// 1. 清理标题(移除[工作室]、编号、N photos等)
|
||||
/// 2. 尝试用分隔符(–—-)切分,第一部分常含人名
|
||||
/// 3. 扫描匹配"2-5中文+可选英文"模式的词
|
||||
/// 4. 结合上下文(后面是否跟"写真/私房/性感"等关键词)
|
||||
pub fn extract_person_name_from_title(title: &str) -> Option<String> {
|
||||
let title = title.trim();
|
||||
if title.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
// 步骤1: 基础清理
|
||||
let mut t = RE_PHOTOS_SUFFIX.replace(title, "").trim().to_string();
|
||||
|
||||
// 步骤2: cosplay 格式优先匹配
|
||||
if let Some(caps) = RE_COSPLAY_BY.captures(&t) {
|
||||
return Some(clean_name(caps[3].trim()));
|
||||
}
|
||||
if let Some(caps) = RE_COSPLAY.captures(&t) {
|
||||
return Some(clean_name(caps[1].trim()));
|
||||
}
|
||||
|
||||
// 步骤3: 移除 [工作室] 前缀
|
||||
t = RE_BRACKET_PREFIX.replace(&t, "").trim().to_string();
|
||||
|
||||
// 步骤4: 移除模特称呼前缀
|
||||
for prefix in MODEL_TITLE_PREFIX {
|
||||
if t.starts_with(prefix) {
|
||||
t = t[prefix.len()..].trim().to_string();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 步骤5: 移除编号后缀
|
||||
t = RE_NUMBER_SUFFIX.replace(&t, "").trim().to_string();
|
||||
|
||||
// 步骤6: 用分隔符切分,检查每一部分
|
||||
let parts: Vec<&str> = RE_SPLIT_DASH.split(&t).collect();
|
||||
|
||||
// 策略A: 检查第一部分(分隔符前的内容最可能包含人名)
|
||||
if let Some(first) = parts.first() {
|
||||
if let Some(name) = extract_name_from_segment(first) {
|
||||
if !is_excluded_name(&name) {
|
||||
return Some(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 策略B: 如果第一部分没找到,扫描所有部分
|
||||
// 优先选择后面跟有关键词的部分
|
||||
for i in 0..parts.len() {
|
||||
let part = parts[i];
|
||||
// 检查这一部分后面是否跟有"写真/私房"等关键词
|
||||
let has_context_after = if i + 1 < parts.len() {
|
||||
RE_NAME_CONTEXT.is_match(parts[i + 1])
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
if let Some(name) = extract_name_from_segment(part) {
|
||||
if !is_excluded_name(&name) {
|
||||
if has_context_after {
|
||||
return Some(name); // 有关键词上下文,优先返回
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 策略C: 直接在整个标题中搜索人名模式
|
||||
for cap in RE_CN_NAME.captures_iter(&t) {
|
||||
let name = clean_name(&cap[1]);
|
||||
if !is_excluded_name(&name) {
|
||||
return Some(name);
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
/// 从一个文本片段中提取人名
|
||||
fn extract_name_from_segment(segment: &str) -> Option<String> {
|
||||
let seg = segment.trim();
|
||||
if seg.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
// 尝试直接匹配人名模式
|
||||
if let Some(cap) = RE_CN_NAME.captures(seg) {
|
||||
let name = clean_name(&cap[1]);
|
||||
if name.len() >= 2 {
|
||||
return Some(name);
|
||||
}
|
||||
}
|
||||
|
||||
// 如果整个片段是纯中文(2-5字),也认为是人名
|
||||
let is_pure_cn = seg.chars().all(|c| RE_CHINESE.is_match(&c.to_string()));
|
||||
if is_pure_cn && seg.len() >= 2 && seg.len() <= 6 {
|
||||
return Some(clean_name(seg));
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
/// 清理人名:移除前后空白和标点
|
||||
fn clean_name(name: &str) -> String {
|
||||
let name = name.trim();
|
||||
// 移除末尾标点
|
||||
let name = RE_TRAILING_PUNCT.replace(name, "").trim().to_string();
|
||||
name
|
||||
}
|
||||
|
||||
/// 提取中文核心(开头的连续中文字符)
|
||||
/// 对应 Python merge_persons_by_core.extract_cn
|
||||
pub fn extract_cn(name: &str) -> Option<String> {
|
||||
let re = Regex::new(r"^([\u{4e00}-\u{9fff}]+)").unwrap();
|
||||
re.captures(name).map(|c| c[1].to_string())
|
||||
}
|
||||
|
||||
/// 判断后缀是否有效(不含中文=有效英文后缀)
|
||||
/// 对应 Python merge_persons_by_core.is_valid_suffix
|
||||
pub fn is_valid_suffix(suffix: &str) -> bool {
|
||||
if suffix.is_empty() {
|
||||
return true;
|
||||
}
|
||||
!RE_CHINESE.is_match(suffix)
|
||||
}
|
||||
|
||||
/// 判断是否为工作室/平台名(需排除)
|
||||
pub fn is_excluded_name(name: &str) -> bool {
|
||||
EXCLUDED_LOWER.contains(&name.to_lowercase())
|
||||
}
|
||||
|
||||
// ── 人名清洗 ──
|
||||
|
||||
/// 清洗人名:移除前缀、括号内容、末尾标点
|
||||
/// 返回 (cleaned_name, aliases)
|
||||
/// 对应 Python clean_person_names.clean_person_name
|
||||
pub fn clean_person_name(raw_name: &str) -> (String, Vec<(String, String)>) {
|
||||
let mut aliases = Vec::new();
|
||||
let mut name = raw_name.to_string();
|
||||
|
||||
// 1. 移除前缀
|
||||
for prefix in MODEL_TITLE_PREFIX {
|
||||
if name.starts_with(prefix) {
|
||||
aliases.push(("prefix".to_string(), prefix.to_string()));
|
||||
name = name[prefix.len()..].trim().to_string();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 移除末尾括号内容
|
||||
let mut cleaned = String::new();
|
||||
let mut last_end = 0;
|
||||
for cap in RE_PAREN_CONTENT.captures_iter(&name) {
|
||||
let m = cap.get(0).unwrap();
|
||||
let content = cap.get(1).map_or("", |c| c.as_str());
|
||||
cleaned.push_str(&name[last_end..m.start()]);
|
||||
last_end = m.end();
|
||||
|
||||
if !content.is_empty() {
|
||||
if RE_CHINESE.is_match(content) {
|
||||
aliases.push(("nickname".to_string(), content.to_string()));
|
||||
} else {
|
||||
aliases.push(("english".to_string(), content.to_string()));
|
||||
}
|
||||
}
|
||||
}
|
||||
cleaned.push_str(&name[last_end..]);
|
||||
let name = cleaned.trim().to_string();
|
||||
|
||||
// 3. 清理末尾标点
|
||||
let name = RE_TRAILING_PUNCT.replace(&name, "").trim().to_string();
|
||||
|
||||
// 4. 从清洗后名字提取英文名别名
|
||||
if let Some(caps) = RE_ENGLISH_SUFFIX.captures(&name) {
|
||||
let chinese_part = caps[1].to_string();
|
||||
let english_part = caps[2].to_string();
|
||||
if !english_part.is_empty() {
|
||||
aliases.push(("chinese".to_string(), chinese_part));
|
||||
aliases.push(("english".to_string(), english_part));
|
||||
}
|
||||
}
|
||||
|
||||
if name.is_empty() {
|
||||
(raw_name.to_string(), aliases)
|
||||
} else {
|
||||
(name, aliases)
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取归并键(小写化+移除空格和标点)
|
||||
pub fn get_person_key(name: &str) -> String {
|
||||
name.to_lowercase()
|
||||
.chars()
|
||||
.filter(|c| !c.is_whitespace() && !c.is_ascii_punctuation())
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// 拆分人名为「中文主名 + 别名列表」
|
||||
/// 规则:
|
||||
/// - 如果有中文部分:主名 = 中文部分,别名 = 英文后缀 + 原始全名 + 括号内容
|
||||
/// - 如果只有英文:主名 = 英文全名,别名 = 原始全名
|
||||
///
|
||||
/// 示例:
|
||||
/// - 杨晨晨Yome -> 主名"杨晨晨", 别名["Yome", "杨晨晨Yome"]
|
||||
/// - 杨晨晨Sugar -> 主名"杨晨晨", 别名["Sugar", "杨晨晨Sugar"]
|
||||
/// - Yome -> 主名"Yome", 别名["Yome"]
|
||||
pub fn split_person_name_with_aliases(raw_name: &str) -> (String, Vec<(String, String)>) {
|
||||
let (cleaned, mut aliases) = clean_person_name(raw_name);
|
||||
let mut primary_name = cleaned.clone();
|
||||
|
||||
// 尝试提取中文主名
|
||||
if let Some(cn_part) = extract_cn(&cleaned) {
|
||||
if !cn_part.is_empty() && cn_part != cleaned {
|
||||
primary_name = cn_part.clone();
|
||||
|
||||
// 提取英文后缀作为别名
|
||||
if let Some(rest) = cleaned.strip_prefix(&cn_part) {
|
||||
let english_suffix = rest.trim().to_string();
|
||||
if !english_suffix.is_empty() {
|
||||
// 避免重复
|
||||
let already_has = aliases.iter().any(|(_, a)| a == &english_suffix);
|
||||
if !already_has {
|
||||
aliases.push(("english".to_string(), english_suffix));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 原始完整名也作为别名(用于搜索匹配)
|
||||
if !raw_name.trim().is_empty() && raw_name.trim() != primary_name {
|
||||
let already_has = aliases.iter().any(|(_, a)| a == raw_name.trim());
|
||||
if !already_has {
|
||||
aliases.push(("original".to_string(), raw_name.trim().to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
// cleaned 名也作为别名(如果和主名不同)
|
||||
if cleaned != primary_name {
|
||||
let already_has = aliases.iter().any(|(_, a)| a == &cleaned);
|
||||
if !already_has {
|
||||
aliases.push(("cleaned".to_string(), cleaned));
|
||||
}
|
||||
}
|
||||
|
||||
(primary_name, aliases)
|
||||
}
|
||||
|
||||
// ── 标签提取 ──
|
||||
|
||||
/// 从标题中提取标签(排除人名部分)
|
||||
/// 返回 Vec<(tag, pos)>
|
||||
/// 对应 Python tag_index.extract_tags_from_title
|
||||
#[allow(dead_code)]
|
||||
pub fn extract_tags_from_title_legacy(title: &str, person_name: Option<&str>) -> Vec<(String, String)> {
|
||||
let mut title = title.to_string();
|
||||
|
||||
// 清理末尾的 "N photos" 等
|
||||
title = RE_PHOTOS_SUFFIX.replace(&title, "").to_string();
|
||||
|
||||
// 移除人名
|
||||
if let Some(name) = person_name {
|
||||
title = title.replace(name, " ");
|
||||
for part in name.split_whitespace() {
|
||||
title = title.replace(part, " ");
|
||||
}
|
||||
}
|
||||
|
||||
// 移除 [工作室] 前缀
|
||||
title = RE_BRACKET_PREFIX.replace(&title, "").to_string();
|
||||
|
||||
// 按空白分割,过滤停用词和短词
|
||||
let mut tags = Vec::new();
|
||||
let mut seen = HashSet::new();
|
||||
|
||||
for word in title.split_whitespace() {
|
||||
let word = word.trim().to_lowercase();
|
||||
if word.len() < 2 {
|
||||
continue;
|
||||
}
|
||||
if STOP_WORDS_SET.contains(word.as_str()) {
|
||||
continue;
|
||||
}
|
||||
if word.chars().all(|c| c.is_ascii_digit() || c == '.') {
|
||||
continue;
|
||||
}
|
||||
if seen.contains(&word) {
|
||||
continue;
|
||||
}
|
||||
seen.insert(word.clone());
|
||||
tags.push((word, "x".to_string()));
|
||||
}
|
||||
|
||||
tags
|
||||
}
|
||||
|
||||
/// 标签归一化
|
||||
pub fn normalize_tag(tag: &str) -> String {
|
||||
tag.trim().to_lowercase()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_extract_person_name() {
|
||||
assert_eq!(extract_person_name_from_title("杨晨晨Yome 私房写真").unwrap(), "杨晨晨Yome");
|
||||
assert_eq!(extract_person_name_from_title("王馨瑶yanni 比基尼").unwrap(), "王馨瑶yanni");
|
||||
assert_eq!(extract_person_name_from_title("[XiuRen] 杨晨晨 性感").unwrap(), "杨晨晨");
|
||||
assert_eq!(extract_person_name_from_title("美女模特 杨晨晨 私房").unwrap(), "杨晨晨");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_extract_cn() {
|
||||
assert_eq!(extract_cn("杨晨晨Yome").unwrap(), "杨晨晨");
|
||||
assert_eq!(extract_cn("王馨瑶yanni").unwrap(), "王馨瑶");
|
||||
assert!(extract_cn("Yome").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_clean_person_name() {
|
||||
let (cleaned, aliases) = clean_person_name("美女模特 杨晨晨Yome");
|
||||
assert_eq!(cleaned, "美女模特 杨晨晨Yome");
|
||||
assert!(!aliases.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_person_name_with_aliases() {
|
||||
// 中文 + 英文后缀
|
||||
let (name, aliases) = split_person_name_with_aliases("杨晨晨Yome");
|
||||
assert_eq!(name, "杨晨晨");
|
||||
let alias_names: Vec<&str> = aliases.iter().map(|(_, a)| a.as_str()).collect();
|
||||
assert!(alias_names.contains(&"Yome"));
|
||||
assert!(alias_names.contains(&"杨晨晨Yome"));
|
||||
|
||||
// 同一人,不同英文名
|
||||
let (name2, aliases2) = split_person_name_with_aliases("杨晨晨Sugar");
|
||||
assert_eq!(name2, "杨晨晨");
|
||||
let alias_names2: Vec<&str> = aliases2.iter().map(|(_, a)| a.as_str()).collect();
|
||||
assert!(alias_names2.contains(&"Sugar"));
|
||||
|
||||
// 只有中文
|
||||
let (name3, _) = split_person_name_with_aliases("杨晨晨");
|
||||
assert_eq!(name3, "杨晨晨");
|
||||
|
||||
// 只有英文
|
||||
let (name4, _) = split_person_name_with_aliases("Yome");
|
||||
assert_eq!(name4, "Yome");
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::config::StorageConfig;
|
||||
use image::{GenericImageView, ImageFormat};
|
||||
use image::GenericImageView;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use thiserror::Error;
|
||||
|
||||
192
src/main.rs
192
src/main.rs
@@ -1,23 +1,26 @@
|
||||
mod config;
|
||||
mod database;
|
||||
mod image_processor;
|
||||
mod import;
|
||||
mod models;
|
||||
mod queue;
|
||||
mod db;
|
||||
mod engine;
|
||||
mod cache;
|
||||
mod routes;
|
||||
mod services;
|
||||
mod image_processor;
|
||||
mod queue;
|
||||
|
||||
use crate::models::AlbumsCache;
|
||||
use crate::cache::AlbumsCache;
|
||||
use actix_files as afs;
|
||||
use actix_web::{App, HttpResponse, HttpServer, middleware, web};
|
||||
use std::fs;
|
||||
use std::sync::Arc;
|
||||
use tokio::time::{Duration, interval};
|
||||
use tracing::{error, info};
|
||||
|
||||
#[global_allocator]
|
||||
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
// 初始化结构化日志
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
// 加载配置
|
||||
@@ -29,11 +32,11 @@ async fn main() -> std::io::Result<()> {
|
||||
}
|
||||
};
|
||||
|
||||
info!("启动图片展示服务...");
|
||||
info!("服务地址: http://localhost:{} http://{}", config.server.port, config.server_address());
|
||||
info!("启动图片展示服务 v2.0...");
|
||||
info!("服务地址: http://{}", config.server_address());
|
||||
|
||||
// 初始化数据库
|
||||
let db = match database::Database::new(&config).await {
|
||||
let db = match db::Database::new(&config).await {
|
||||
Ok(db) => {
|
||||
info!("数据库连接成功");
|
||||
Arc::new(db)
|
||||
@@ -46,21 +49,45 @@ async fn main() -> std::io::Result<()> {
|
||||
|
||||
let image_processor = Arc::new(image_processor::ImageProcessor::new(config.storage.clone()));
|
||||
|
||||
// 在后台线程中自动导入新相册(不阻塞HTTP服务启动)
|
||||
// 后台任务1: 扫描导入新相册
|
||||
let config_clone = config.clone();
|
||||
let db_clone = Arc::clone(&db);
|
||||
let db_import = Arc::clone(&db);
|
||||
tokio::spawn(async move {
|
||||
info!("[后台任务] 开始扫描图片目录并导入新相册...");
|
||||
match import::import_existing_data(&config_clone, db_clone, None).await {
|
||||
match services::import::import_existing_data(&config_clone, db_import, None).await {
|
||||
Ok(_) => info!("[后台任务] 相册导入完成"),
|
||||
Err(e) => error!("[后台任务] 相册导入失败: {}", e),
|
||||
}
|
||||
});
|
||||
|
||||
// 初始化相册内存缓存
|
||||
// 后台任务2: 自动解析已有相册的 title(增量,针对老版本 db)
|
||||
let db_parse = Arc::clone(&db);
|
||||
tokio::spawn(async move {
|
||||
// 等待 3 秒让导入任务先开始(导入后也会触发解析,避免重复)
|
||||
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
|
||||
info!("[后台任务] 检查是否有未解析的相册(针对老版本 db)...");
|
||||
let (persons, tags) = engine::pipeline::process_all_albums(&db_parse).await;
|
||||
if persons > 0 || tags > 0 {
|
||||
info!("[后台任务] Title 解析完成:{} 个人名,{} 个标签", persons, tags);
|
||||
} else {
|
||||
info!("[后台任务] 所有相册已解析,无需处理");
|
||||
}
|
||||
|
||||
// 解析完成后,执行一次人物合并
|
||||
tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
|
||||
info!("[后台任务] 执行人物合并分析...");
|
||||
let result = engine::person_merger::execute_merge(&db_parse).await;
|
||||
if result.persons_merged > 0 {
|
||||
info!("[后台任务] 人物合并完成:分析 {} 组,合并 {} 人", result.groups_analyzed, result.persons_merged);
|
||||
} else {
|
||||
info!("[后台任务] 无需要合并的人物");
|
||||
}
|
||||
});
|
||||
|
||||
// 相册内存缓存
|
||||
let albums_cache = Arc::new(tokio::sync::RwLock::new(AlbumsCache::new()));
|
||||
|
||||
// 初始加载相册数据到缓存
|
||||
// 初始加载缓存
|
||||
let initial_cache = Arc::clone(&albums_cache);
|
||||
let initial_db = Arc::clone(&db);
|
||||
tokio::spawn(async move {
|
||||
@@ -76,15 +103,22 @@ async fn main() -> std::io::Result<()> {
|
||||
}
|
||||
});
|
||||
|
||||
// 设置定时刷新缓存任务(每30分钟)
|
||||
// 定时任务(每30分钟):刷新缓存 + 人物合并
|
||||
let refresh_cache = Arc::clone(&albums_cache);
|
||||
let refresh_db = Arc::clone(&db);
|
||||
tokio::spawn(async move {
|
||||
let mut interval = interval(Duration::from_secs(30 * 60)); // 30分钟
|
||||
|
||||
let mut interval = interval(Duration::from_secs(30 * 60));
|
||||
loop {
|
||||
interval.tick().await;
|
||||
|
||||
// 1. 执行人物合并
|
||||
info!("[定时任务] 执行人物合并分析...");
|
||||
let result = engine::person_merger::execute_merge(&refresh_db).await;
|
||||
if result.persons_merged > 0 {
|
||||
info!("[定时任务] 人物合并完成:分析 {} 组,合并 {} 人", result.groups_analyzed, result.persons_merged);
|
||||
}
|
||||
|
||||
// 2. 刷新缓存
|
||||
match refresh_db.get_all_albums().await {
|
||||
Ok(albums) => {
|
||||
let mut cache = refresh_cache.write().await;
|
||||
@@ -98,61 +132,88 @@ async fn main() -> std::io::Result<()> {
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// 任务队列
|
||||
let task_queue = Arc::new(queue::TaskQueue::new(
|
||||
config.queue.max_concurrent_tasks,
|
||||
config.queue.queue_size_limit,
|
||||
config.queue.high_request_rate_threshold,
|
||||
config.queue.idle_request_rate_threshold,
|
||||
));
|
||||
|
||||
println!("API接口:");
|
||||
println!(" - GET /api/albums - 获取所有相册");
|
||||
println!(" - GET /api/search?q=关键词 - 搜索相册");
|
||||
println!(" - GET /api/album/random - 随机相册");
|
||||
println!(" - GET /api/album/:id - 获取相册详情");
|
||||
println!(" - POST /api/albums - 创建新相册");
|
||||
println!(" - GET /api/image/:id - 获取图片(已移除缩略图功能)");
|
||||
println!(" - GET /api/favorites - 获取收藏列表");
|
||||
println!(" - GET /api/favorites/status/:id - 检查收藏状态");
|
||||
println!(" - GET /api/image/:id - 获取图片");
|
||||
println!(" - GET /api/favorites - 收藏列表");
|
||||
println!(" - POST /api/favorites - 添加收藏");
|
||||
println!(" - DELETE /api/favorites/:id - 取消收藏");
|
||||
println!(" - DELETE /api/favorites/image/:id - 通过图片ID取消收藏");
|
||||
println!("元数据接口:");
|
||||
println!(" - GET /api/people - 人员列表");
|
||||
println!(" - GET /api/people/search?q=关键词 - 搜索人员");
|
||||
println!(" - GET /api/people/{{name}} - 人员详情");
|
||||
println!(" - GET /api/people/{{id}}/aliases - 人员别名");
|
||||
println!(" - GET /api/metadata/stats - 元数据统计");
|
||||
println!(" - GET /api/tags/search?tag=标签 - 按标签搜索");
|
||||
println!(" - GET /api/tags/popular?limit=50 - 热门标签");
|
||||
println!("推荐接口:");
|
||||
println!(" - GET /api/recommend/tags?q=xxx - 标签推荐");
|
||||
println!(" - GET /api/recommend/persons?q=xxx - 人物推荐");
|
||||
println!(" - GET /api/recommend/related?context=xxx - 关联推荐");
|
||||
println!(" - GET /api/recommend/popular-persons - 热门人物");
|
||||
println!("迁移接口:");
|
||||
println!(" - GET /api/migration/stats - 迁移状态");
|
||||
println!(" - POST /api/migration/run?path=persons.db - 执行迁移");
|
||||
println!(" - GET /api/queue/stats - 队列状态");
|
||||
println!("按 Ctrl+C 停止服务");
|
||||
|
||||
// 创建数据目录
|
||||
fs::create_dir_all(&config.storage.images_dir).unwrap_or(());
|
||||
|
||||
// 在闭包外提取需要的路径
|
||||
let server_address = config.server_address();
|
||||
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
// 启用压缩中间件
|
||||
.wrap(middleware::Compress::default())
|
||||
// 启用日志中间件
|
||||
.wrap(middleware::Logger::default())
|
||||
.app_data(web::Data::new(Arc::clone(&db)))
|
||||
.app_data(web::Data::new(Arc::clone(&image_processor)))
|
||||
.app_data(web::Data::new(Arc::clone(&albums_cache)))
|
||||
// API路由
|
||||
.app_data(web::Data::new(Arc::clone(&task_queue)))
|
||||
// 相册 API
|
||||
.route("/api/albums", web::get().to(routes::get_albums))
|
||||
.route("/api/search", web::get().to(routes::search_albums))
|
||||
.route("/api/album/random", web::get().to(routes::get_random_album))
|
||||
.route("/api/album/{id}", web::get().to(routes::get_album_detail))
|
||||
.route("/api/albums", web::post().to(routes::create_album))
|
||||
.route("/api/image/{id}", web::get().to(routes::get_image))
|
||||
// 收藏功能API路由
|
||||
// 收藏 API
|
||||
.route("/api/favorites", web::get().to(routes::get_favorites))
|
||||
.route(
|
||||
"/api/favorites/status/{id}",
|
||||
web::get().to(routes::check_favorite_status),
|
||||
)
|
||||
.route("/api/favorites/status/{id}", web::get().to(routes::check_favorite_status))
|
||||
.route("/api/favorites", web::post().to(routes::add_favorite))
|
||||
.route(
|
||||
"/api/favorites/{id}",
|
||||
web::delete().to(routes::remove_favorite),
|
||||
)
|
||||
.route(
|
||||
"/api/favorites/image/{id}",
|
||||
web::delete().to(routes::remove_favorite_by_image),
|
||||
)
|
||||
// 队列状态API路由
|
||||
.route("/api/favorites/{id}", web::delete().to(routes::remove_favorite))
|
||||
.route("/api/favorites/image/{id}", web::delete().to(routes::remove_favorite_by_image))
|
||||
// 人员 API
|
||||
.route("/api/people", web::get().to(routes::get_people))
|
||||
.route("/api/people/search", web::get().to(routes::search_people))
|
||||
.route("/api/people/{name}", web::get().to(routes::get_person_detail))
|
||||
.route("/api/people/{id}/aliases", web::get().to(routes::get_person_aliases))
|
||||
// 标签 API
|
||||
.route("/api/metadata/stats", web::get().to(routes::get_metadata_stats))
|
||||
.route("/api/tags/search", web::get().to(routes::search_by_tag))
|
||||
.route("/api/tags/popular", web::get().to(routes::get_popular_tags))
|
||||
// 推荐 API
|
||||
.route("/api/recommend/tags", web::get().to(routes::recommend_tags))
|
||||
.route("/api/recommend/persons", web::get().to(routes::recommend_persons))
|
||||
.route("/api/recommend/related", web::get().to(routes::recommend_related))
|
||||
.route("/api/recommend/popular-persons", web::get().to(routes::popular_persons))
|
||||
// 迁移 API
|
||||
.route("/api/migration/stats", web::get().to(routes::migration_stats))
|
||||
.route("/api/migration/run", web::post().to(routes::migration_run))
|
||||
// 管线 API
|
||||
.route("/api/pipeline/run", web::post().to(routes::pipeline_run))
|
||||
// 队列 API
|
||||
.route("/api/queue/stats", web::get().to(routes::get_queue_stats))
|
||||
// 静态文件服务 - 启用缓存
|
||||
// 静态文件
|
||||
.service(
|
||||
afs::Files::new("/assets", "./assets")
|
||||
.show_files_listing()
|
||||
@@ -160,33 +221,24 @@ async fn main() -> std::io::Result<()> {
|
||||
.use_last_modified(true)
|
||||
.use_etag(true),
|
||||
)
|
||||
// HTML页面路由
|
||||
.route(
|
||||
"/",
|
||||
web::get().to(|| async {
|
||||
match fs::read_to_string("./assets/index.html") {
|
||||
Ok(content) => HttpResponse::Ok()
|
||||
.content_type("text/html; charset=utf-8")
|
||||
.body(content),
|
||||
Err(_) => HttpResponse::NotFound().body("首页文件不存在"),
|
||||
}
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/album",
|
||||
web::get().to(|| async {
|
||||
match fs::read_to_string("./assets/album.html") {
|
||||
Ok(content) => HttpResponse::Ok()
|
||||
.content_type("text/html; charset=utf-8")
|
||||
.body(content),
|
||||
Err(_) => HttpResponse::NotFound().body("相册页面文件不存在"),
|
||||
}
|
||||
}),
|
||||
)
|
||||
.route("/", web::get().to(|| async {
|
||||
match fs::read_to_string("./assets/index.html") {
|
||||
Ok(content) => HttpResponse::Ok()
|
||||
.content_type("text/html; charset=utf-8")
|
||||
.body(content),
|
||||
Err(_) => HttpResponse::NotFound().body("首页文件不存在"),
|
||||
}
|
||||
}))
|
||||
.route("/album", web::get().to(|| async {
|
||||
match fs::read_to_string("./assets/album.html") {
|
||||
Ok(content) => HttpResponse::Ok()
|
||||
.content_type("text/html; charset=utf-8")
|
||||
.body(content),
|
||||
Err(_) => HttpResponse::NotFound().body("相册页面不存在"),
|
||||
}
|
||||
}))
|
||||
})
|
||||
.bind(server_address)?
|
||||
// 限制worker数量为2个,减少系统资源占用
|
||||
.workers(3)
|
||||
.bind(config.server_address())?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
230
src/models.rs
230
src/models.rs
@@ -1,230 +0,0 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use tracing::info;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Album {
|
||||
pub id: Uuid,
|
||||
pub title: String,
|
||||
pub description: Option<String>,
|
||||
pub cover_image_id: Option<Uuid>,
|
||||
pub image_count: i64, // 新增图片数量字段
|
||||
pub created_at: String,
|
||||
pub updated_at: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct Image {
|
||||
pub id: Uuid,
|
||||
pub album_id: Uuid,
|
||||
pub filename: String,
|
||||
pub original_url: Option<String>,
|
||||
pub title: Option<String>,
|
||||
pub description: Option<String>,
|
||||
pub file_size: i64,
|
||||
pub width: i32,
|
||||
pub height: i32,
|
||||
pub mime_type: String,
|
||||
pub created_at: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct AlbumWithImages {
|
||||
pub album: Album,
|
||||
pub images: Vec<Image>,
|
||||
pub image_count: i64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct AlbumInfo {
|
||||
pub id: Uuid,
|
||||
pub title: String,
|
||||
pub document: String,
|
||||
pub description: Option<String>,
|
||||
pub cover_image_id: Option<String>,
|
||||
pub image_count: i64,
|
||||
pub created_at: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct CreateAlbum {
|
||||
pub title: String,
|
||||
pub document: String,
|
||||
pub description: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct CreateImage {
|
||||
pub album_id: Uuid,
|
||||
pub filename: String,
|
||||
pub original_url: Option<String>,
|
||||
pub title: Option<String>,
|
||||
pub description: Option<String>,
|
||||
pub file_size: i64,
|
||||
pub width: i32,
|
||||
pub height: i32,
|
||||
pub mime_type: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Favorite {
|
||||
pub id: Uuid,
|
||||
pub user_id: String,
|
||||
pub image_id: Uuid,
|
||||
pub album_id: Uuid,
|
||||
pub created_at: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct CreateFavorite {
|
||||
pub image_id: Uuid,
|
||||
pub album_id: Uuid,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct FavoriteWithImage {
|
||||
pub favorite: Favorite,
|
||||
pub image: Image,
|
||||
pub album_title: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct FavoriteStatus {
|
||||
pub is_favorited: bool,
|
||||
pub favorite_id: Option<Uuid>,
|
||||
}
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct AlbumIndex {
|
||||
pub title: String,
|
||||
pub images: Vec<String>,
|
||||
}
|
||||
// 内存缓存结构
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AlbumsCache {
|
||||
pub albums: Vec<AlbumInfo>,
|
||||
pub search_index: HashMap<String, Vec<AlbumInfo>>,
|
||||
pub last_updated: chrono::DateTime<chrono::Utc>,
|
||||
}
|
||||
|
||||
impl AlbumsCache {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
albums: Vec::new(),
|
||||
search_index: HashMap::new(),
|
||||
last_updated: chrono::Utc::now(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_expired(&self) -> bool {
|
||||
let now = chrono::Utc::now();
|
||||
let duration = now - self.last_updated;
|
||||
duration.num_minutes() >= 30 // 30分钟过期
|
||||
}
|
||||
|
||||
pub fn update(&mut self, albums: Vec<AlbumInfo>) {
|
||||
self.albums = albums; // 不再克隆整个向量,而是移动所有权
|
||||
self.last_updated = chrono::Utc::now();
|
||||
|
||||
// 构建搜索索引
|
||||
self.search_index.clear();
|
||||
for album in &self.albums {
|
||||
let title_lower = album.title.to_lowercase();
|
||||
|
||||
// 为每个单词创建索引
|
||||
for word in title_lower.split_whitespace() {
|
||||
if word.len() >= 2 {
|
||||
// 只索引长度>=2的单词
|
||||
self.search_index
|
||||
.entry(word.to_string())
|
||||
.or_insert_with(Vec::new)
|
||||
.push(album.clone()); // 这里仍需要克隆单个元素
|
||||
}
|
||||
}
|
||||
|
||||
// 为完整标题创建索引
|
||||
self.search_index
|
||||
.entry(title_lower)
|
||||
.or_insert_with(Vec::new)
|
||||
.push(album.clone()); // 这里仍需要克隆单个元素
|
||||
}
|
||||
|
||||
info!(
|
||||
"相册缓存已更新,共 {} 个相册,{} 个搜索索引项",
|
||||
self.albums.len(),
|
||||
self.search_index.len()
|
||||
);
|
||||
}
|
||||
|
||||
pub fn search(&self, query: &str) -> Vec<AlbumInfo> {
|
||||
let query_lower = query.to_lowercase().trim().to_string();
|
||||
|
||||
if query_lower.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
let mut results = Vec::new();
|
||||
let mut seen_ids = std::collections::HashSet::new();
|
||||
|
||||
// 1. 搜索完整匹配(最高优先级)
|
||||
if let Some(matches) = self.search_index.get(&query_lower) {
|
||||
for album in matches {
|
||||
if seen_ids.insert(album.id) {
|
||||
results.push(album.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 搜索单词匹配
|
||||
for word in query_lower.split_whitespace() {
|
||||
if word.len() >= 2 {
|
||||
// 只搜索长度>=2的单词
|
||||
if let Some(matches) = self.search_index.get(word) {
|
||||
for album in matches {
|
||||
if seen_ids.insert(album.id) {
|
||||
results.push(album.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 模糊搜索:在相册标题和描述中搜索包含查询词的结果
|
||||
if results.is_empty() {
|
||||
for album in &self.albums {
|
||||
let title_lower = album.title.to_lowercase();
|
||||
let description_lower = album
|
||||
.description
|
||||
.as_ref()
|
||||
.map(|d| d.to_lowercase())
|
||||
.unwrap_or_default();
|
||||
|
||||
// 检查标题或描述是否包含搜索词
|
||||
if title_lower.contains(&query_lower) || description_lower.contains(&query_lower) {
|
||||
if seen_ids.insert(album.id) {
|
||||
results.push(album.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 如果还没有结果,尝试部分匹配(更宽松的搜索)
|
||||
if results.is_empty() && query_lower.len() >= 2 {
|
||||
for album in &self.albums {
|
||||
let title_lower = album.title.to_lowercase();
|
||||
|
||||
// 检查标题是否包含搜索词的任何部分
|
||||
if query_lower
|
||||
.split_whitespace()
|
||||
.any(|word| word.len() >= 2 && title_lower.contains(word))
|
||||
{
|
||||
if seen_ids.insert(album.id) {
|
||||
results.push(album.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
results
|
||||
}
|
||||
}
|
||||
62
src/models/album.rs
Normal file
62
src/models/album.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct Album {
|
||||
pub id: Uuid,
|
||||
pub title: String,
|
||||
pub description: Option<String>,
|
||||
pub cover_image_id: Option<Uuid>,
|
||||
pub image_count: i64,
|
||||
pub created_at: String,
|
||||
pub updated_at: String,
|
||||
#[serde(default)]
|
||||
pub person_name_cleaned: Option<String>,
|
||||
#[serde(default)]
|
||||
pub tags: Option<String>, // JSON: [tag, ...]
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct AlbumInfo {
|
||||
pub id: Uuid,
|
||||
pub title: String,
|
||||
pub document: String,
|
||||
pub description: Option<String>,
|
||||
pub cover_image_id: Option<String>,
|
||||
pub image_count: i64,
|
||||
pub created_at: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct AlbumWithImages {
|
||||
pub album: Album,
|
||||
pub images: Vec<super::image::Image>,
|
||||
pub image_count: i64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct CreateAlbum {
|
||||
pub title: String,
|
||||
pub document: String,
|
||||
pub description: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct AlbumIndex {
|
||||
pub title: String,
|
||||
pub images: Vec<String>,
|
||||
}
|
||||
|
||||
/// 相册完整元数据(含扩展字段)
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct AlbumMeta {
|
||||
pub id: String,
|
||||
pub title: Option<String>,
|
||||
pub person_name_original: Option<String>,
|
||||
pub person_name_cleaned: Option<String>,
|
||||
pub tags: Option<String>,
|
||||
pub all_words: Option<String>,
|
||||
pub raw_title: Option<String>,
|
||||
pub processed_at: Option<String>,
|
||||
pub image_count: Option<i64>,
|
||||
}
|
||||
30
src/models/favorite.rs
Normal file
30
src/models/favorite.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Favorite {
|
||||
pub id: Uuid,
|
||||
pub user_id: String,
|
||||
pub image_id: Uuid,
|
||||
pub album_id: Uuid,
|
||||
pub created_at: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct CreateFavorite {
|
||||
pub image_id: Uuid,
|
||||
pub album_id: Uuid,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct FavoriteWithImage {
|
||||
pub favorite: Favorite,
|
||||
pub image: super::image::Image,
|
||||
pub album_title: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct FavoriteStatus {
|
||||
pub is_favorited: bool,
|
||||
pub favorite_id: Option<Uuid>,
|
||||
}
|
||||
30
src/models/image.rs
Normal file
30
src/models/image.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct Image {
|
||||
pub id: Uuid,
|
||||
pub album_id: Uuid,
|
||||
pub filename: String,
|
||||
pub original_url: Option<String>,
|
||||
pub title: Option<String>,
|
||||
pub description: Option<String>,
|
||||
pub file_size: i64,
|
||||
pub width: i32,
|
||||
pub height: i32,
|
||||
pub mime_type: String,
|
||||
pub created_at: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct CreateImage {
|
||||
pub album_id: Uuid,
|
||||
pub filename: String,
|
||||
pub original_url: Option<String>,
|
||||
pub title: Option<String>,
|
||||
pub description: Option<String>,
|
||||
pub file_size: i64,
|
||||
pub width: i32,
|
||||
pub height: i32,
|
||||
pub mime_type: String,
|
||||
}
|
||||
13
src/models/mod.rs
Normal file
13
src/models/mod.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
pub mod album;
|
||||
pub mod image;
|
||||
pub mod person;
|
||||
pub mod tag;
|
||||
pub mod favorite;
|
||||
pub mod recommend;
|
||||
|
||||
pub use album::*;
|
||||
pub use image::*;
|
||||
pub use person::*;
|
||||
pub use tag::*;
|
||||
pub use favorite::*;
|
||||
pub use recommend::*;
|
||||
53
src/models/person.rs
Normal file
53
src/models/person.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use super::album::AlbumInfo;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct Person {
|
||||
pub id: i64,
|
||||
pub name: String,
|
||||
pub display_name: Option<String>,
|
||||
pub total_albums: i64,
|
||||
pub total_images: i64,
|
||||
pub original_names: Option<String>,
|
||||
pub created_at: String,
|
||||
pub updated_at: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct PersonAlias {
|
||||
pub id: i64,
|
||||
pub person_id: i64,
|
||||
pub alias_type: String,
|
||||
pub alias_name: String,
|
||||
pub created_at: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct PersonWithAlbums {
|
||||
pub person: Person,
|
||||
pub albums: Vec<AlbumInfo>,
|
||||
pub album_count: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct PersonWithCounts {
|
||||
pub id: i64,
|
||||
pub name: String,
|
||||
pub display_name: Option<String>,
|
||||
pub total_albums: i64,
|
||||
pub total_images: i64,
|
||||
pub aliases: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct PersonListResponse {
|
||||
pub persons: Vec<PersonWithCounts>,
|
||||
pub total: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct SearchPersonResponse {
|
||||
pub person: Option<PersonWithCounts>,
|
||||
pub albums: Vec<AlbumInfo>,
|
||||
pub total_albums: usize,
|
||||
}
|
||||
46
src/models/recommend.rs
Normal file
46
src/models/recommend.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// 推荐请求
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct RecommendQuery {
|
||||
pub q: Option<String>,
|
||||
pub limit: Option<usize>,
|
||||
pub context: Option<String>, // 上下文标签(用于共现推荐)
|
||||
}
|
||||
|
||||
/// 推荐条目(通用)
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct RecommendItem {
|
||||
pub value: String,
|
||||
pub label: String,
|
||||
pub score: f64,
|
||||
pub count: Option<i64>,
|
||||
pub source: String, // "tag" | "person" | "co-occurrence" | "popular"
|
||||
}
|
||||
|
||||
/// 推荐响应
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct RecommendResponse {
|
||||
pub items: Vec<RecommendItem>,
|
||||
pub total: usize,
|
||||
pub category: String, // "tags" | "persons" | "related"
|
||||
}
|
||||
|
||||
/// 标签推荐(带上下文)
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct TagRecommendation {
|
||||
pub tag: String,
|
||||
pub related_tags: Vec<RecommendItem>,
|
||||
pub similar_persons: Vec<RecommendItem>,
|
||||
}
|
||||
|
||||
/// 热门人物
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct PopularPerson {
|
||||
pub name: String,
|
||||
pub display_name: Option<String>,
|
||||
pub total_albums: i64,
|
||||
pub total_images: i64,
|
||||
pub top_tags: Vec<String>,
|
||||
pub aliases: Vec<String>,
|
||||
}
|
||||
35
src/models/tag.rs
Normal file
35
src/models/tag.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// 标签统计
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct TagStat {
|
||||
pub tag: String,
|
||||
pub count: i64,
|
||||
pub album_count: i64,
|
||||
}
|
||||
|
||||
/// 标签列表响应
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct TagListResponse {
|
||||
pub tags: Vec<TagStat>,
|
||||
pub total: usize,
|
||||
}
|
||||
|
||||
/// 元数据统计
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct MetadataStats {
|
||||
pub total_persons: i64,
|
||||
pub total_aliases: i64,
|
||||
pub total_albums_with_person: i64,
|
||||
pub total_albums_without_person: i64,
|
||||
pub total_tags: i64,
|
||||
}
|
||||
|
||||
/// 标签共现关系
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct TagCoOccurrence {
|
||||
pub tag: String,
|
||||
pub co_tag: String,
|
||||
pub count: i64,
|
||||
pub score: f64,
|
||||
}
|
||||
@@ -3,7 +3,7 @@ use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicU64, AtomicBool};
|
||||
use std::time::{Duration, Instant};
|
||||
use tokio::sync::Notify;
|
||||
use tracing::{info, debug};
|
||||
use tracing::info;
|
||||
|
||||
/// 队列统计信息
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
@@ -34,7 +34,7 @@ impl TaskQueue {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_with_db(_max_concurrent_tasks: usize, _queue_size_limit: usize, _high_request_rate_threshold: f64, _idle_request_rate_threshold: f64, _db: Arc<crate::database::Database>) -> Self {
|
||||
pub fn new_with_db(_max_concurrent_tasks: usize, _queue_size_limit: usize, _high_request_rate_threshold: f64, _idle_request_rate_threshold: f64, _db: Arc<crate::db::Database>) -> Self {
|
||||
Self {
|
||||
// HTTP请求频率监控
|
||||
request_count: AtomicU64::new(0),
|
||||
|
||||
365
src/routes.rs
365
src/routes.rs
@@ -1,365 +0,0 @@
|
||||
use crate::database::Database;
|
||||
use crate::image_processor::ImageProcessor;
|
||||
use crate::models::*;
|
||||
use actix_web::{HttpResponse, Responder, web};
|
||||
use std::fs;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::RwLock;
|
||||
use tracing::error;
|
||||
use uuid::Uuid;
|
||||
use crate::queue;
|
||||
|
||||
|
||||
|
||||
// 收藏功能API路由
|
||||
|
||||
// 获取用户的收藏列表
|
||||
pub async fn get_favorites(db: web::Data<Arc<Database>>) -> impl Responder {
|
||||
// 这里使用固定用户ID,实际项目中应该从认证信息中获取
|
||||
let user_id = "default_user".to_string();
|
||||
|
||||
let favorites = match db.get_user_favorites(&user_id).await {
|
||||
Ok(value) => value,
|
||||
Err(e) => {
|
||||
error!("获取收藏列表失败: {}", e);
|
||||
return HttpResponse::InternalServerError().json(serde_json::json!({
|
||||
"error": "获取收藏列表失败"
|
||||
}));
|
||||
}
|
||||
};
|
||||
|
||||
HttpResponse::Ok().json(favorites)
|
||||
}
|
||||
|
||||
// 图片质量检查功能已移除,直接返回原始图片
|
||||
|
||||
// 检查图片是否被收藏
|
||||
pub async fn check_favorite_status(
|
||||
db: web::Data<Arc<Database>>,
|
||||
path: web::Path<String>,
|
||||
) -> impl Responder {
|
||||
let image_id = match Uuid::parse_str(&path.into_inner()) {
|
||||
Ok(id) => id,
|
||||
Err(_) => {
|
||||
return HttpResponse::BadRequest().json(serde_json::json!({
|
||||
"error": "无效的图片ID"
|
||||
}));
|
||||
}
|
||||
};
|
||||
|
||||
// 这里使用固定用户ID,实际项目中应该从认证信息中获取
|
||||
let user_id = "default_user".to_string();
|
||||
|
||||
let status = match db.get_favorite_status(&user_id, image_id).await {
|
||||
Ok(value) => value,
|
||||
Err(e) => {
|
||||
error!("检查收藏状态失败: {}", e);
|
||||
return HttpResponse::InternalServerError().json(serde_json::json!({
|
||||
"error": "检查收藏状态失败"
|
||||
}));
|
||||
}
|
||||
};
|
||||
|
||||
HttpResponse::Ok().json(status)
|
||||
}
|
||||
|
||||
// 添加收藏
|
||||
pub async fn add_favorite(
|
||||
db: web::Data<Arc<Database>>,
|
||||
favorite_data: web::Json<CreateFavorite>,
|
||||
) -> impl Responder {
|
||||
// 这里使用固定用户ID,实际项目中应该从认证信息中获取
|
||||
let user_id = "default_user".to_string();
|
||||
|
||||
match db.add_favorite(&user_id, favorite_data.into_inner()).await {
|
||||
Ok(favorite_id) => HttpResponse::Created().json(serde_json::json!({
|
||||
"id": favorite_id,
|
||||
"message": "收藏成功"
|
||||
})),
|
||||
Err(e) => {
|
||||
eprintln!("添加收藏失败: {}", e);
|
||||
HttpResponse::InternalServerError().json(serde_json::json!({
|
||||
"error": "添加收藏失败"
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 取消收藏
|
||||
pub async fn remove_favorite(
|
||||
db: web::Data<Arc<Database>>,
|
||||
path: web::Path<String>,
|
||||
) -> impl Responder {
|
||||
let favorite_id = match Uuid::parse_str(&path.into_inner()) {
|
||||
Ok(id) => id,
|
||||
Err(_) => {
|
||||
return HttpResponse::BadRequest().json(serde_json::json!({
|
||||
"error": "无效的收藏ID"
|
||||
}));
|
||||
}
|
||||
};
|
||||
|
||||
match db.remove_favorite(favorite_id).await {
|
||||
Ok(_) => HttpResponse::Ok().json(serde_json::json!({
|
||||
"message": "取消收藏成功"
|
||||
})),
|
||||
Err(e) => {
|
||||
eprintln!("取消收藏失败: {}", e);
|
||||
HttpResponse::InternalServerError().json(serde_json::json!({
|
||||
"error": "取消收藏失败"
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取任务队列状态
|
||||
pub async fn get_queue_stats(
|
||||
task_queue: web::Data<Arc<queue::TaskQueue>>,
|
||||
) -> impl Responder {
|
||||
// 记录HTTP请求频率
|
||||
task_queue.record_http_request().await;
|
||||
|
||||
let stats = task_queue.get_stats().await;
|
||||
HttpResponse::Ok().json(stats)
|
||||
}
|
||||
|
||||
// 通过图片ID取消收藏
|
||||
pub async fn remove_favorite_by_image(
|
||||
db: web::Data<Arc<Database>>,
|
||||
path: web::Path<String>,
|
||||
) -> impl Responder {
|
||||
let image_id = match Uuid::parse_str(&path.into_inner()) {
|
||||
Ok(id) => id,
|
||||
Err(_) => {
|
||||
return HttpResponse::BadRequest().json(serde_json::json!({
|
||||
"error": "无效的图片ID"
|
||||
}));
|
||||
}
|
||||
};
|
||||
|
||||
// 这里使用固定用户ID,实际项目中应该从认证信息中获取
|
||||
let user_id = "default_user".to_string();
|
||||
|
||||
match db.remove_favorite_by_image(&user_id, image_id).await {
|
||||
Ok(_) => HttpResponse::Ok().json(serde_json::json!({
|
||||
"message": "取消收藏成功"
|
||||
})),
|
||||
Err(e) => {
|
||||
eprintln!("取消收藏失败: {}", e);
|
||||
HttpResponse::InternalServerError().json(serde_json::json!({
|
||||
"error": "取消收藏失败"
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
pub async fn get_albums(albums_cache: web::Data<Arc<RwLock<AlbumsCache>>>) -> impl Responder {
|
||||
// 从内存缓存获取数据
|
||||
let cache = albums_cache.read().await;
|
||||
let total_albums = cache.albums.len();
|
||||
|
||||
// 应用分页
|
||||
let start = 0;
|
||||
let end = total_albums;
|
||||
let paginated_albums = if start < end {
|
||||
cache.albums[start..end].to_vec()
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
|
||||
drop(cache); // 释放读锁
|
||||
|
||||
HttpResponse::Ok().json(paginated_albums)
|
||||
}
|
||||
|
||||
pub async fn search_albums(
|
||||
albums_cache: web::Data<Arc<RwLock<AlbumsCache>>>,
|
||||
query: web::Query<std::collections::HashMap<String, String>>,
|
||||
) -> impl Responder {
|
||||
let q = query.get("q").map(|s| s.as_str()).unwrap_or("");
|
||||
|
||||
// 输入验证
|
||||
if q.len() > 100 {
|
||||
return HttpResponse::BadRequest().json(serde_json::json!({
|
||||
"error": "搜索查询过长,最多100个字符"
|
||||
}));
|
||||
}
|
||||
|
||||
// 允许Unicode字符(包括中文)、ASCII图形字符和空白字符
|
||||
// 只阻止控制字符和其他潜在危险字符
|
||||
if q.chars().any(|c| c.is_control() || c == '\0') {
|
||||
return HttpResponse::BadRequest().json(serde_json::json!({
|
||||
"error": "搜索查询包含无效字符"
|
||||
}));
|
||||
}
|
||||
|
||||
let page = query
|
||||
.get("page")
|
||||
.and_then(|s| s.parse::<usize>().ok())
|
||||
.unwrap_or(1);
|
||||
let limit = query
|
||||
.get("limit")
|
||||
.and_then(|s| s.parse::<usize>().ok())
|
||||
.unwrap_or(50);
|
||||
let offset = (page - 1) * limit;
|
||||
|
||||
if q.is_empty() {
|
||||
// 如果没有搜索词,返回所有相册(带分页)
|
||||
let cache = albums_cache.read().await;
|
||||
let total_albums = cache.albums.len();
|
||||
|
||||
// 应用分页
|
||||
let start = offset.min(total_albums);
|
||||
let end = (offset + limit).min(total_albums);
|
||||
let paginated_albums = if start < end {
|
||||
cache.albums[start..end].to_vec()
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
|
||||
drop(cache);
|
||||
return HttpResponse::Ok().json(paginated_albums);
|
||||
}
|
||||
|
||||
// 从内存缓存搜索
|
||||
let cache = albums_cache.read().await;
|
||||
let mut search_results = cache.search(q);
|
||||
|
||||
// 应用分页到搜索结果
|
||||
let total_results = search_results.len();
|
||||
let start = offset.min(total_results);
|
||||
let end = (offset + limit).min(total_results);
|
||||
let paginated_results = if start < end {
|
||||
search_results.drain(start..end).collect()
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
|
||||
drop(cache);
|
||||
|
||||
HttpResponse::Ok().json(paginated_results)
|
||||
}
|
||||
|
||||
pub async fn get_random_album(
|
||||
albums_cache: web::Data<Arc<RwLock<AlbumsCache>>>,
|
||||
) -> impl Responder {
|
||||
// 从内存缓存获取数据
|
||||
let cache = albums_cache.read().await;
|
||||
|
||||
if cache.albums.is_empty() {
|
||||
drop(cache);
|
||||
return HttpResponse::NotFound().json(serde_json::json!({
|
||||
"error": "没有可用的相册"
|
||||
}));
|
||||
}
|
||||
|
||||
// 使用随机数生成器选择一个随机相册
|
||||
use rand::Rng;
|
||||
let mut rng = rand::thread_rng();
|
||||
let random_index = rng.gen_range(0..cache.albums.len());
|
||||
let random_album = cache.albums[random_index].clone();
|
||||
|
||||
drop(cache);
|
||||
|
||||
HttpResponse::Ok().json(random_album)
|
||||
}
|
||||
|
||||
pub async fn get_album_detail(
|
||||
db: web::Data<Arc<Database>>,
|
||||
path: web::Path<String>,
|
||||
) -> impl Responder {
|
||||
let album_id = match Uuid::parse_str(&path.into_inner()) {
|
||||
Ok(id) => id,
|
||||
Err(_) => {
|
||||
return HttpResponse::BadRequest().json(serde_json::json!({
|
||||
"error": "无效的相册ID"
|
||||
}));
|
||||
}
|
||||
};
|
||||
|
||||
match db.get_album_with_images(album_id).await {
|
||||
Ok(Some(album)) => HttpResponse::Ok().json(album),
|
||||
Ok(None) => HttpResponse::NotFound().json(serde_json::json!({
|
||||
"error": "相册不存在"
|
||||
})),
|
||||
Err(e) => {
|
||||
eprintln!("获取相册详情失败: {}", e);
|
||||
HttpResponse::InternalServerError().json(serde_json::json!({
|
||||
"error": "获取相册详情失败"
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_album(
|
||||
db: web::Data<Arc<Database>>,
|
||||
album_data: web::Json<CreateAlbum>,
|
||||
) -> impl Responder {
|
||||
match db.create_album(album_data.into_inner()).await {
|
||||
Ok(album_id) => HttpResponse::Created().json(serde_json::json!({
|
||||
"id": album_id,
|
||||
"message": "相册创建成功"
|
||||
})),
|
||||
Err(e) => {
|
||||
eprintln!("创建相册失败: {}", e);
|
||||
HttpResponse::InternalServerError().json(serde_json::json!({
|
||||
"error": "创建相册失败"
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取原始图片
|
||||
pub async fn get_image(
|
||||
db: web::Data<Arc<Database>>,
|
||||
image_processor: web::Data<Arc<ImageProcessor>>,
|
||||
path: web::Path<String>,
|
||||
) -> impl Responder {
|
||||
let image_id = match Uuid::parse_str(&path.into_inner()) {
|
||||
Ok(id) => id,
|
||||
Err(_) => {
|
||||
return HttpResponse::BadRequest().json(serde_json::json!({
|
||||
"error": "无效的图片ID"
|
||||
}));
|
||||
}
|
||||
};
|
||||
|
||||
match db.get_image(image_id).await {
|
||||
Ok(Some(image)) => {
|
||||
let original_path = image_processor.config.images_dir.join(&image.filename);
|
||||
|
||||
if original_path.exists() {
|
||||
match fs::read(&original_path) {
|
||||
Ok(data) => {
|
||||
HttpResponse::Ok()
|
||||
.content_type(image.mime_type.clone())
|
||||
.insert_header(("Cache-Control", "public, max-age=31536000"))
|
||||
.insert_header(("ETag", format!("\"{}\"", image_id)))
|
||||
.body(data)
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("读取原始图片失败: {}", e);
|
||||
HttpResponse::InternalServerError().json(serde_json::json!({
|
||||
"error": "读取图片失败"
|
||||
}))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
HttpResponse::NotFound().json(serde_json::json!({
|
||||
"error": "图片文件不存在"
|
||||
}))
|
||||
}
|
||||
}
|
||||
Ok(None) => HttpResponse::NotFound().json(serde_json::json!({
|
||||
"error": "图片不存在"
|
||||
})),
|
||||
Err(e) => {
|
||||
eprintln!("获取图片信息失败: {}", e);
|
||||
HttpResponse::InternalServerError().json(serde_json::json!({
|
||||
"error": "获取图片信息失败"
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
65
src/routes/album.rs
Normal file
65
src/routes/album.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
use crate::cache::AlbumsCache;
|
||||
use crate::db::Database;
|
||||
use crate::models::*;
|
||||
use actix_web::{HttpResponse, Responder, web};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::RwLock;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub async fn get_albums(cache: web::Data<Arc<RwLock<AlbumsCache>>>) -> impl Responder {
|
||||
let c = cache.read().await;
|
||||
HttpResponse::Ok().json(&c.albums)
|
||||
}
|
||||
|
||||
pub async fn search_albums(
|
||||
cache: web::Data<Arc<RwLock<AlbumsCache>>>,
|
||||
query: web::Query<HashMap<String, String>>,
|
||||
) -> impl Responder {
|
||||
let q = query.get("q").map(|s| s.as_str()).unwrap_or("");
|
||||
if q.len() > 100 { return HttpResponse::BadRequest().json(serde_json::json!({"error": "搜索查询过长"})); }
|
||||
if q.chars().any(|c| c.is_control() || c == '\0') { return HttpResponse::BadRequest().json(serde_json::json!({"error": "无效字符"})); }
|
||||
|
||||
let page: usize = query.get("page").and_then(|s| s.parse().ok()).unwrap_or(1);
|
||||
let limit: usize = query.get("limit").and_then(|s| s.parse().ok()).unwrap_or(50).min(200);
|
||||
let offset = (page - 1) * limit;
|
||||
|
||||
let c = cache.read().await;
|
||||
let results = if q.is_empty() {
|
||||
let total = c.albums.len();
|
||||
let start = offset.min(total);
|
||||
let end = (offset + limit).min(total);
|
||||
c.albums[start..end].to_vec()
|
||||
} else {
|
||||
let mut results = c.search(q);
|
||||
let total = results.len();
|
||||
let start = offset.min(total);
|
||||
let end = (offset + limit).min(total);
|
||||
if start < end { results.drain(start..end).collect() } else { Vec::new() }
|
||||
};
|
||||
|
||||
HttpResponse::Ok().json(results)
|
||||
}
|
||||
|
||||
pub async fn get_random_album(cache: web::Data<Arc<RwLock<AlbumsCache>>>) -> impl Responder {
|
||||
let c = cache.read().await;
|
||||
if c.albums.is_empty() { return HttpResponse::NotFound().json(serde_json::json!({"error": "没有相册"})); }
|
||||
let idx = rand::random::<usize>() % c.albums.len();
|
||||
HttpResponse::Ok().json(&c.albums[idx])
|
||||
}
|
||||
|
||||
pub async fn get_album_detail(db: web::Data<Arc<Database>>, path: web::Path<String>) -> impl Responder {
|
||||
let id = match Uuid::parse_str(&path.into_inner()) { Ok(id) => id, Err(_) => return HttpResponse::BadRequest().json(serde_json::json!({"error": "无效ID"})) };
|
||||
match db.get_album_with_images(id).await {
|
||||
Ok(Some(album)) => HttpResponse::Ok().json(album),
|
||||
Ok(None) => HttpResponse::NotFound().json(serde_json::json!({"error": "相册不存在"})),
|
||||
Err(e) => HttpResponse::InternalServerError().json(serde_json::json!({"error": format!("{}", e)})),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_album(db: web::Data<Arc<Database>>, body: web::Json<CreateAlbum>) -> impl Responder {
|
||||
match db.create_album(body.into_inner()).await {
|
||||
Ok(id) => HttpResponse::Created().json(serde_json::json!({"id": id})),
|
||||
Err(e) => HttpResponse::InternalServerError().json(serde_json::json!({"error": format!("{}", e)})),
|
||||
}
|
||||
}
|
||||
45
src/routes/favorite.rs
Normal file
45
src/routes/favorite.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use crate::db::Database;
|
||||
use crate::models::*;
|
||||
use actix_web::{HttpResponse, Responder, web};
|
||||
use std::sync::Arc;
|
||||
use uuid::Uuid;
|
||||
|
||||
static USER_ID: &str = "default_user";
|
||||
|
||||
pub async fn get_favorites(db: web::Data<Arc<Database>>) -> impl Responder {
|
||||
match db.get_user_favorites(USER_ID).await {
|
||||
Ok(favs) => HttpResponse::Ok().json(favs),
|
||||
Err(e) => HttpResponse::InternalServerError().json(serde_json::json!({"error": format!("{}", e)})),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn check_favorite_status(db: web::Data<Arc<Database>>, path: web::Path<String>) -> impl Responder {
|
||||
let id = match Uuid::parse_str(&path.into_inner()) { Ok(id) => id, Err(_) => return HttpResponse::BadRequest().json(serde_json::json!({"error": "无效ID"})) };
|
||||
match db.get_favorite_status(USER_ID, id).await {
|
||||
Ok(s) => HttpResponse::Ok().json(s),
|
||||
Err(e) => HttpResponse::InternalServerError().json(serde_json::json!({"error": format!("{}", e)})),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn add_favorite(db: web::Data<Arc<Database>>, body: web::Json<CreateFavorite>) -> impl Responder {
|
||||
match db.add_favorite(USER_ID, body.into_inner()).await {
|
||||
Ok(id) => HttpResponse::Created().json(serde_json::json!({"id": id})),
|
||||
Err(e) => HttpResponse::InternalServerError().json(serde_json::json!({"error": format!("{}", e)})),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn remove_favorite(db: web::Data<Arc<Database>>, path: web::Path<String>) -> impl Responder {
|
||||
let id = match Uuid::parse_str(&path.into_inner()) { Ok(id) => id, Err(_) => return HttpResponse::BadRequest().json(serde_json::json!({"error": "无效ID"})) };
|
||||
match db.remove_favorite(id).await {
|
||||
Ok(_) => HttpResponse::Ok().json(serde_json::json!({"message": "已取消"})),
|
||||
Err(e) => HttpResponse::InternalServerError().json(serde_json::json!({"error": format!("{}", e)})),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn remove_favorite_by_image(db: web::Data<Arc<Database>>, path: web::Path<String>) -> impl Responder {
|
||||
let id = match Uuid::parse_str(&path.into_inner()) { Ok(id) => id, Err(_) => return HttpResponse::BadRequest().json(serde_json::json!({"error": "无效ID"})) };
|
||||
match db.remove_favorite_by_image(USER_ID, id).await {
|
||||
Ok(_) => HttpResponse::Ok().json(serde_json::json!({"message": "已取消"})),
|
||||
Err(e) => HttpResponse::InternalServerError().json(serde_json::json!({"error": format!("{}", e)})),
|
||||
}
|
||||
}
|
||||
29
src/routes/image.rs
Normal file
29
src/routes/image.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use crate::db::Database;
|
||||
use crate::image_processor::ImageProcessor;
|
||||
use actix_web::{HttpResponse, Responder, web};
|
||||
use std::fs;
|
||||
use std::sync::Arc;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub async fn get_image(
|
||||
db: web::Data<Arc<Database>>,
|
||||
ip: web::Data<Arc<ImageProcessor>>,
|
||||
path: web::Path<String>,
|
||||
) -> impl Responder {
|
||||
let id = match Uuid::parse_str(&path.into_inner()) { Ok(id) => id, Err(_) => return HttpResponse::BadRequest().json(serde_json::json!({"error": "无效ID"})) };
|
||||
match db.get_image(id).await {
|
||||
Ok(Some(img)) => {
|
||||
let fp = ip.config.images_dir.join(&img.filename);
|
||||
if fp.exists() {
|
||||
match fs::read(&fp) {
|
||||
Ok(data) => HttpResponse::Ok().content_type(img.mime_type)
|
||||
.insert_header(("Cache-Control", "public, max-age=31536000"))
|
||||
.insert_header(("ETag", format!("\"{}\"", id))).body(data),
|
||||
Err(_) => HttpResponse::InternalServerError().json(serde_json::json!({"error": "读取失败"})),
|
||||
}
|
||||
} else { HttpResponse::NotFound().json(serde_json::json!({"error": "文件不存在"})) }
|
||||
}
|
||||
Ok(None) => HttpResponse::NotFound().json(serde_json::json!({"error": "图片不存在"})),
|
||||
Err(_) => HttpResponse::InternalServerError().json(serde_json::json!({"error": "查询失败"})),
|
||||
}
|
||||
}
|
||||
48
src/routes/migration.rs
Normal file
48
src/routes/migration.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use crate::db::Database;
|
||||
#[allow(unused)] use crate::db::MigrationStats;
|
||||
use actix_web::{HttpResponse, Responder, web};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// GET /api/migration/stats — 查看迁移状态
|
||||
pub async fn migration_stats(db: web::Data<Arc<Database>>) -> impl Responder {
|
||||
let persons = db.get_all_persons_with_counts().await.len();
|
||||
let aliases = db.get_all_aliases().await.len();
|
||||
let albums = db.get_all_albums().await.unwrap_or_default().len();
|
||||
|
||||
HttpResponse::Ok().json(serde_json::json!({
|
||||
"persons": persons,
|
||||
"aliases": aliases,
|
||||
"albums": albums,
|
||||
"message": if persons == 0 { "数据库为空,建议执行迁移" } else { "数据已就绪" }
|
||||
}))
|
||||
}
|
||||
|
||||
/// POST /api/migration/run — 执行从老数据库迁移
|
||||
pub async fn migration_run(db: web::Data<Arc<Database>>, query: web::Query<HashMap<String, String>>) -> impl Responder {
|
||||
let old_db_path = match query.get("path") {
|
||||
Some(p) if !p.is_empty() => p.clone(),
|
||||
_ => return HttpResponse::BadRequest().json(serde_json::json!({
|
||||
"error": "请提供老数据库路径: ?path=persons.db"
|
||||
})),
|
||||
};
|
||||
|
||||
match db.migrate_from_old_db(&old_db_path).await {
|
||||
Ok(stats) => HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": true,
|
||||
"summary": db.migration_summary(&stats),
|
||||
"stats": {
|
||||
"persons_imported": stats.persons_imported,
|
||||
"persons_skipped": stats.persons_skipped,
|
||||
"aliases_imported": stats.aliases_imported,
|
||||
"albums_updated": stats.albums_updated,
|
||||
"albums_missing": stats.albums_missing,
|
||||
"tags_imported": stats.tags_imported,
|
||||
"person_albums_created": stats.person_albums_created,
|
||||
}
|
||||
})),
|
||||
Err(e) => HttpResponse::InternalServerError().json(serde_json::json!({
|
||||
"error": format!("迁移失败: {}", e)
|
||||
})),
|
||||
}
|
||||
}
|
||||
21
src/routes/mod.rs
Normal file
21
src/routes/mod.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
//! API 路由层 — 按领域拆分
|
||||
|
||||
pub mod album;
|
||||
pub mod image;
|
||||
pub mod favorite;
|
||||
pub mod person;
|
||||
pub mod tag;
|
||||
pub mod recommend;
|
||||
pub mod queue;
|
||||
pub mod migration;
|
||||
pub mod pipeline;
|
||||
|
||||
pub use album::*;
|
||||
pub use image::*;
|
||||
pub use favorite::*;
|
||||
pub use person::*;
|
||||
pub use tag::*;
|
||||
pub use recommend::*;
|
||||
pub use queue::*;
|
||||
pub use migration::*;
|
||||
pub use pipeline::*;
|
||||
36
src/routes/person.rs
Normal file
36
src/routes/person.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
use crate::db::Database;
|
||||
use crate::models::*;
|
||||
use actix_web::{HttpResponse, Responder, web};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub async fn get_people(db: web::Data<Arc<Database>>) -> impl Responder {
|
||||
let persons = db.get_all_persons_with_aliases().await;
|
||||
HttpResponse::Ok().json(PersonListResponse { total: persons.len(), persons })
|
||||
}
|
||||
|
||||
pub async fn search_people(db: web::Data<Arc<Database>>, query: web::Query<HashMap<String, String>>) -> impl Responder {
|
||||
let q = query.get("q").map(|s| s.as_str()).unwrap_or("");
|
||||
if q.is_empty() {
|
||||
let persons = db.get_all_persons_with_aliases().await;
|
||||
return HttpResponse::Ok().json(PersonListResponse { total: persons.len(), persons });
|
||||
}
|
||||
let persons = db.search_persons(q).await;
|
||||
HttpResponse::Ok().json(PersonListResponse { total: persons.len(), persons })
|
||||
}
|
||||
|
||||
pub async fn get_person_detail(db: web::Data<Arc<Database>>, path: web::Path<String>) -> impl Responder {
|
||||
let name = path.into_inner();
|
||||
match db.get_person_by_name(&name).await {
|
||||
Some(p) => {
|
||||
let albums = db.get_person_albums(p.id).await;
|
||||
HttpResponse::Ok().json(PersonWithAlbums { album_count: albums.len(), person: p, albums })
|
||||
}
|
||||
None => HttpResponse::NotFound().json(serde_json::json!({"error": format!("人员 '{}' 不存在", name)})),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_person_aliases(db: web::Data<Arc<Database>>, path: web::Path<i64>) -> impl Responder {
|
||||
let aliases = db.get_person_aliases(path.into_inner()).await;
|
||||
HttpResponse::Ok().json(aliases)
|
||||
}
|
||||
15
src/routes/pipeline.rs
Normal file
15
src/routes/pipeline.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
use crate::db::Database;
|
||||
use crate::engine::pipeline;
|
||||
use actix_web::{HttpResponse, Responder, web};
|
||||
use std::sync::Arc;
|
||||
|
||||
/// POST /api/pipeline/run — 处理所有相册元数据(提取人名+标签)
|
||||
pub async fn pipeline_run(db: web::Data<Arc<Database>>) -> impl Responder {
|
||||
let (persons, tags) = pipeline::process_all_albums(db.get_ref()).await;
|
||||
HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": true,
|
||||
"persons_updated": persons,
|
||||
"tags_updated": tags,
|
||||
"message": format!("处理完成:{} 个人名,{} 个标签", persons, tags)
|
||||
}))
|
||||
}
|
||||
8
src/routes/queue.rs
Normal file
8
src/routes/queue.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
use crate::queue::TaskQueue;
|
||||
use actix_web::{HttpResponse, Responder, web};
|
||||
use std::sync::Arc;
|
||||
|
||||
pub async fn get_queue_stats(q: web::Data<Arc<TaskQueue>>) -> impl Responder {
|
||||
q.record_http_request().await;
|
||||
HttpResponse::Ok().json(q.get_stats().await)
|
||||
}
|
||||
36
src/routes/recommend.rs
Normal file
36
src/routes/recommend.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
use crate::db::Database;
|
||||
use crate::engine::RecommendEngine;
|
||||
#[allow(unused)] use crate::models::*;
|
||||
use actix_web::{HttpResponse, Responder, web};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// 标签推荐(自动补全)
|
||||
pub async fn recommend_tags(db: web::Data<Arc<Database>>, query: web::Query<HashMap<String, String>>) -> impl Responder {
|
||||
let q = query.get("q").map(|s| s.as_str()).unwrap_or("");
|
||||
let limit: usize = query.get("limit").and_then(|s| s.parse().ok()).unwrap_or(10);
|
||||
HttpResponse::Ok().json(RecommendEngine::recommend_tags(&db, q, limit).await)
|
||||
}
|
||||
|
||||
/// 人物推荐(自动补全)
|
||||
pub async fn recommend_persons(db: web::Data<Arc<Database>>, query: web::Query<HashMap<String, String>>) -> impl Responder {
|
||||
let q = query.get("q").map(|s| s.as_str()).unwrap_or("");
|
||||
let limit: usize = query.get("limit").and_then(|s| s.parse().ok()).unwrap_or(10);
|
||||
HttpResponse::Ok().json(RecommendEngine::recommend_persons(&db, q, limit).await)
|
||||
}
|
||||
|
||||
/// 关联推荐(标签共现 + 共现人物)
|
||||
pub async fn recommend_related(db: web::Data<Arc<Database>>, query: web::Query<HashMap<String, String>>) -> impl Responder {
|
||||
let context = match query.get("context") {
|
||||
Some(c) if !c.is_empty() => c,
|
||||
_ => return HttpResponse::BadRequest().json(serde_json::json!({"error": "请提供context参数"})),
|
||||
};
|
||||
let limit: usize = query.get("limit").and_then(|s| s.parse().ok()).unwrap_or(15);
|
||||
HttpResponse::Ok().json(RecommendEngine::recommend_related(&db, context, limit).await)
|
||||
}
|
||||
|
||||
/// 热门人物
|
||||
pub async fn popular_persons(db: web::Data<Arc<Database>>, query: web::Query<HashMap<String, String>>) -> impl Responder {
|
||||
let limit: usize = query.get("limit").and_then(|s| s.parse().ok()).unwrap_or(20).min(50);
|
||||
HttpResponse::Ok().json(RecommendEngine::popular_persons(&db, limit).await)
|
||||
}
|
||||
24
src/routes/tag.rs
Normal file
24
src/routes/tag.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
use crate::db::Database;
|
||||
use crate::models::*;
|
||||
use actix_web::{HttpResponse, Responder, web};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub async fn get_metadata_stats(db: web::Data<Arc<Database>>) -> impl Responder {
|
||||
HttpResponse::Ok().json(db.get_metadata_stats().await)
|
||||
}
|
||||
|
||||
pub async fn search_by_tag(db: web::Data<Arc<Database>>, query: web::Query<HashMap<String, String>>) -> impl Responder {
|
||||
let tag = match query.get("tag") {
|
||||
Some(t) if !t.is_empty() => t,
|
||||
_ => return HttpResponse::BadRequest().json(serde_json::json!({"error": "请提供tag参数"})),
|
||||
};
|
||||
let albums = db.search_albums_by_tag(tag).await;
|
||||
HttpResponse::Ok().json(albums)
|
||||
}
|
||||
|
||||
pub async fn get_popular_tags(db: web::Data<Arc<Database>>, query: web::Query<HashMap<String, String>>) -> impl Responder {
|
||||
let limit: i64 = query.get("limit").and_then(|s| s.parse().ok()).unwrap_or(50);
|
||||
let tags = db.get_popular_tags(limit).await;
|
||||
HttpResponse::Ok().json(TagListResponse { total: tags.len(), tags })
|
||||
}
|
||||
@@ -1,46 +1,40 @@
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
use image::GenericImageView;
|
||||
use serde::Deserialize;
|
||||
use ::image::GenericImageView;
|
||||
use tokio::task;
|
||||
use uuid::Uuid;
|
||||
use crate::config::Config;
|
||||
use crate::database::Database;
|
||||
use crate::db::Database;
|
||||
use crate::models::*;
|
||||
|
||||
/// 批量插入图片并设置封面缩略图的辅助函数
|
||||
/// 批量插入图片并设置封面的辅助函数
|
||||
async fn batch_insert_images_and_set_cover(
|
||||
db: &Database,
|
||||
images_to_insert: Vec<CreateImage>,
|
||||
album_id: Uuid,
|
||||
album_path: &Path,
|
||||
folder_name: &str,
|
||||
_album_path: &Path,
|
||||
_folder_name: &str,
|
||||
) -> Result<usize, Box<dyn std::error::Error>> {
|
||||
if images_to_insert.is_empty() {
|
||||
return Ok(0);
|
||||
}
|
||||
|
||||
|
||||
println!(" 批量插入 {} 张图片...", images_to_insert.len());
|
||||
match db.add_images(images_to_insert).await {
|
||||
Ok(ids) => {
|
||||
println!(" 批量插入成功,共插入 {} 张图片", ids.len());
|
||||
|
||||
// 设置第一张图片为封面图片并生成缩略图
|
||||
|
||||
if let Some(first_image_id) = ids.first() {
|
||||
println!(" 设置第一张图片为封面图片 (ID: {})...", first_image_id);
|
||||
|
||||
// 更新相册的封面图片ID
|
||||
if let Err(e) = db.update_album_cover(album_id, Some(*first_image_id)).await {
|
||||
if let Err(e) = db.update_album_cover(album_id, *first_image_id).await {
|
||||
eprintln!(" 更新相册封面失败: {}", e);
|
||||
}
|
||||
|
||||
// 获取第一张图片的信息
|
||||
if let Ok(Some(first_image)) = db.get_image(*first_image_id).await {
|
||||
println!(" 封面图片已设置: {}", first_image.filename);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Ok(ids.len())
|
||||
}
|
||||
Err(e) => {
|
||||
@@ -50,36 +44,30 @@ async fn batch_insert_images_and_set_cover(
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub async fn import_existing_data(config: &Config, db: Arc<Database>, _task_queue: Option<Arc<crate::queue::TaskQueue>>) -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("开始扫描图片目录并导入新相册...");
|
||||
|
||||
|
||||
let albums_dir = Path::new(&config.storage.images_dir);
|
||||
|
||||
|
||||
if !albums_dir.exists() {
|
||||
println!("albums目录不存在,跳过导入");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// 获取数据库中现有的相册列表
|
||||
|
||||
let existing_albums = db.get_all_albums().await?;
|
||||
let existing_titles: Vec<String> = existing_albums.iter()
|
||||
.map(|album| album.document.to_lowercase())
|
||||
.collect();
|
||||
|
||||
|
||||
println!("数据库中现有相册数量: {}", existing_albums.len());
|
||||
|
||||
// 第一阶段:检查并重新导入失败的相册
|
||||
println!("🔍 检查需要重新导入的相册...");
|
||||
|
||||
let mut reimported_count = 0;
|
||||
let mut deleted_count = 0;
|
||||
|
||||
|
||||
for album in &existing_albums {
|
||||
let album_folder_path = albums_dir.join(&album.document);
|
||||
|
||||
// 检查相册文件夹是否存在
|
||||
|
||||
if !album_folder_path.exists() {
|
||||
// 文件夹不存在,删除数据库中的相册记录
|
||||
println!("🗑️ 相册文件夹不存在,删除相册: {}", album.document);
|
||||
if let Err(e) = db.delete_album(album.id).await {
|
||||
eprintln!("❌ 删除相册失败: {}", e);
|
||||
@@ -88,11 +76,9 @@ pub async fn import_existing_data(config: &Config, db: Arc<Database>, _task_queu
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// 检查相册是否有图片
|
||||
|
||||
let image_count = db.get_album_image_count(album.id).await.unwrap_or(0);
|
||||
if image_count == 0 {
|
||||
// 相册存在但没有图片,尝试重新导入
|
||||
println!("🔄 重新导入相册: {} (图片数量: 0)", album.document);
|
||||
match reimport_single_album(&db, &album.document, &album_folder_path, album.id).await {
|
||||
Ok(count) => {
|
||||
@@ -109,20 +95,18 @@ pub async fn import_existing_data(config: &Config, db: Arc<Database>, _task_queu
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if reimported_count > 0 || deleted_count > 0 {
|
||||
println!("🎯 重新导入完成: 重新导入 {} 个相册,删除 {} 个相册", reimported_count, deleted_count);
|
||||
}
|
||||
|
||||
// 第二阶段:快速扫描所有相册文件夹,导入新相册
|
||||
|
||||
let entries: Vec<_> = fs::read_dir(albums_dir)?.collect::<Result<Vec<_>, _>>()?;
|
||||
let mut album_folders = Vec::new();
|
||||
|
||||
|
||||
for entry in entries {
|
||||
let path = entry.path();
|
||||
if path.is_dir() {
|
||||
if let Some(folder_name) = path.file_name().and_then(|s| s.to_str()) {
|
||||
// 检查是否已存在同名相册
|
||||
if existing_titles.contains(&folder_name.to_lowercase()) {
|
||||
continue;
|
||||
}
|
||||
@@ -130,23 +114,22 @@ pub async fn import_existing_data(config: &Config, db: Arc<Database>, _task_queu
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
println!("发现 {} 个新相册需要导入", album_folders.len());
|
||||
|
||||
|
||||
if album_folders.is_empty() {
|
||||
println!("没有新相册需要导入");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// 使用简单的串行处理方式导入相册
|
||||
|
||||
println!("📁 开始导入新相册...");
|
||||
|
||||
|
||||
let mut total_imported = 0;
|
||||
let mut successful_albums = 0;
|
||||
|
||||
|
||||
for (folder_name, path) in album_folders {
|
||||
println!("📁 开始导入相册: {}", folder_name);
|
||||
|
||||
|
||||
match import_single_album(&db, &folder_name, &path).await {
|
||||
Ok(count) => {
|
||||
if count > 0 {
|
||||
@@ -159,21 +142,18 @@ pub async fn import_existing_data(config: &Config, db: Arc<Database>, _task_queu
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("❌ 相册 {} 导入失败: {}", folder_name, e);
|
||||
// 继续处理下一个相册,不中断整个导入过程
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
println!("🎉 导入完成!成功导入 {} 个相册,共 {} 张图片", successful_albums, total_imported);
|
||||
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 导入单个相册的辅助函数
|
||||
async fn import_single_album(db: &Database, folder_name: &str, path: &Path) -> Result<usize, Box<dyn std::error::Error>> {
|
||||
println!("📁 开始导入相册: {}", folder_name);
|
||||
|
||||
// 读取index.json文件
|
||||
|
||||
let index_file_path = path.join("index.json");
|
||||
let album_title = if index_file_path.exists() {
|
||||
match fs::read_to_string(&index_file_path) {
|
||||
@@ -198,18 +178,16 @@ async fn import_single_album(db: &Database, folder_name: &str, path: &Path) -> R
|
||||
println!(" index.json不存在,使用文件夹名作为标题");
|
||||
folder_name.to_string()
|
||||
};
|
||||
|
||||
// 创建新相册
|
||||
|
||||
let new_album = CreateAlbum {
|
||||
title: album_title.clone(),
|
||||
document: folder_name.to_string(),
|
||||
description: Some(format!("从文件夹 {} 自动导入的相册", folder_name)),
|
||||
};
|
||||
|
||||
|
||||
let album_id = db.create_album(new_album).await?;
|
||||
println!(" 创建相册: {} (ID: {})", album_title, album_id);
|
||||
|
||||
// 读取图片URL列表(如果有index.json)
|
||||
|
||||
let image_urls = if index_file_path.exists() {
|
||||
match fs::read_to_string(&index_file_path) {
|
||||
Ok(content) => {
|
||||
@@ -226,20 +204,18 @@ async fn import_single_album(db: &Database, folder_name: &str, path: &Path) -> R
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
// 快速扫描图片文件
|
||||
|
||||
let image_files = scan_image_files(path, folder_name)?;
|
||||
|
||||
|
||||
if image_files.is_empty() {
|
||||
println!(" 相册 {} 中没有找到图片文件", folder_name);
|
||||
return Ok(0);
|
||||
}
|
||||
|
||||
|
||||
println!(" 发现 {} 张图片,开始处理...", image_files.len());
|
||||
|
||||
// 并行处理图片信息获取
|
||||
|
||||
let mut image_tasks = Vec::new();
|
||||
|
||||
|
||||
for (index, (filename, image_path)) in image_files.iter().enumerate() {
|
||||
let image_url = image_urls.as_ref().and_then(|urls| {
|
||||
if urls.len() > index {
|
||||
@@ -248,14 +224,13 @@ async fn import_single_album(db: &Database, folder_name: &str, path: &Path) -> R
|
||||
None
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
let filename_clone = filename.clone();
|
||||
let image_path_clone = image_path.clone();
|
||||
let album_id_clone = album_id;
|
||||
let folder_name_clone = folder_name.to_string();
|
||||
|
||||
|
||||
let task = task::spawn_blocking(move || {
|
||||
// 在阻塞线程中处理图片信息
|
||||
let (file_size, width, height) = match get_image_info(&image_path_clone) {
|
||||
Ok(info) => info,
|
||||
Err(e) => {
|
||||
@@ -263,7 +238,7 @@ async fn import_single_album(db: &Database, folder_name: &str, path: &Path) -> R
|
||||
(0, 0, 0)
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
CreateImage {
|
||||
album_id: album_id_clone,
|
||||
filename: format!("{}/{}", folder_name_clone, filename_clone),
|
||||
@@ -283,11 +258,10 @@ async fn import_single_album(db: &Database, folder_name: &str, path: &Path) -> R
|
||||
},
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
image_tasks.push(task);
|
||||
}
|
||||
|
||||
// 收集所有图片信息
|
||||
|
||||
let mut images_to_insert = Vec::new();
|
||||
for task in image_tasks {
|
||||
match task.await {
|
||||
@@ -295,70 +269,62 @@ async fn import_single_album(db: &Database, folder_name: &str, path: &Path) -> R
|
||||
Err(e) => eprintln!("图片处理任务失败: {}", e),
|
||||
}
|
||||
}
|
||||
|
||||
// 使用辅助函数批量插入图片并设置封面
|
||||
|
||||
batch_insert_images_and_set_cover(&db, images_to_insert, album_id, path, folder_name).await
|
||||
}
|
||||
|
||||
/// 快速扫描图片文件
|
||||
pub(crate) fn scan_image_files(path: &Path, _folder_name: &str) -> Result<Vec<(String, std::path::PathBuf)>, Box<dyn std::error::Error>> {
|
||||
let mut image_files = Vec::new();
|
||||
|
||||
|
||||
for entry in fs::read_dir(path)? {
|
||||
let entry = entry?;
|
||||
let image_path = entry.path();
|
||||
|
||||
|
||||
if image_path.is_file() {
|
||||
if let Some(extension) = image_path.extension().and_then(|s| s.to_str()) {
|
||||
let valid_extensions = ["jpg", "jpeg", "png", "gif", "webp", "bmp"];
|
||||
|
||||
|
||||
if valid_extensions.contains(&extension.to_lowercase().as_str()) {
|
||||
let filename = image_path.file_name()
|
||||
.and_then(|s| s.to_str())
|
||||
.unwrap_or("unknown.jpg")
|
||||
.to_string();
|
||||
|
||||
|
||||
image_files.push((filename, image_path));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 按数字顺序排序文件名
|
||||
|
||||
image_files.sort_by(|a, b| {
|
||||
// 提取文件名中的数字部分进行排序
|
||||
let num_a = a.0.chars()
|
||||
.take_while(|c| c.is_ascii_digit())
|
||||
.collect::<String>()
|
||||
.parse::<u32>()
|
||||
.unwrap_or(0);
|
||||
|
||||
|
||||
let num_b = b.0.chars()
|
||||
.take_while(|c| c.is_ascii_digit())
|
||||
.collect::<String>()
|
||||
.parse::<u32>()
|
||||
.unwrap_or(0);
|
||||
|
||||
|
||||
num_a.cmp(&num_b)
|
||||
});
|
||||
|
||||
|
||||
Ok(image_files)
|
||||
}
|
||||
|
||||
/// 获取图片的实际信息(文件大小、宽度、高度)
|
||||
fn get_image_info(image_path: &Path) -> Result<(i64, i32, i32), Box<dyn std::error::Error>> {
|
||||
// 获取文件大小
|
||||
let metadata = fs::metadata(image_path)?;
|
||||
let file_size = metadata.len() as i64;
|
||||
|
||||
// 使用image库获取图片尺寸
|
||||
let img = image::open(image_path)?;
|
||||
|
||||
let img = ::image::open(image_path)?;
|
||||
let (width, height) = img.dimensions();
|
||||
|
||||
|
||||
Ok((file_size, width as i32, height as i32))
|
||||
}
|
||||
|
||||
/// 重新导入单个相册(使用现有的相册ID)
|
||||
async fn reimport_single_album(
|
||||
db: &Database,
|
||||
folder_name: &str,
|
||||
@@ -366,8 +332,7 @@ async fn reimport_single_album(
|
||||
album_id: Uuid,
|
||||
) -> Result<usize, Box<dyn std::error::Error>> {
|
||||
println!("📁 重新导入相册: {} (ID: {})", folder_name, album_id);
|
||||
|
||||
// 读取index.json文件
|
||||
|
||||
let index_file_path = path.join("index.json");
|
||||
let album_title = if index_file_path.exists() {
|
||||
match fs::read_to_string(&index_file_path) {
|
||||
@@ -382,27 +347,24 @@ async fn reimport_single_album(
|
||||
} else {
|
||||
folder_name.to_string()
|
||||
};
|
||||
|
||||
|
||||
println!(" 相册标题: {}", album_title);
|
||||
|
||||
// 扫描图片文件
|
||||
|
||||
let mut images_to_insert = Vec::new();
|
||||
let image_extensions = ["jpg", "jpeg", "png", "gif", "webp", "bmp"];
|
||||
|
||||
|
||||
let entries: Vec<_> = fs::read_dir(path)?.collect::<Result<Vec<_>, _>>()?;
|
||||
|
||||
|
||||
for entry in entries {
|
||||
let file_path = entry.path();
|
||||
if file_path.is_file() {
|
||||
if let Some(extension) = file_path.extension().and_then(|ext| ext.to_str()) {
|
||||
if image_extensions.contains(&extension.to_lowercase().as_str()) {
|
||||
if let Some(filename) = file_path.file_name().and_then(|s| s.to_str()) {
|
||||
// 跳过index.json文件
|
||||
if filename == "index.json" {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 获取图片信息
|
||||
|
||||
let (file_size, width, height) = match get_image_info(&file_path) {
|
||||
Ok(info) => info,
|
||||
Err(e) => {
|
||||
@@ -410,7 +372,7 @@ async fn reimport_single_album(
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
let image = CreateImage {
|
||||
album_id,
|
||||
filename: format!("{}/{}", folder_name, filename),
|
||||
@@ -429,18 +391,15 @@ async fn reimport_single_album(
|
||||
_ => "application/octet-stream".to_string(),
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
images_to_insert.push(image);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!(" 发现 {} 张图片需要导入", images_to_insert.len());
|
||||
|
||||
// 使用辅助函数批量插入图片并设置封面
|
||||
batch_insert_images_and_set_cover(&db, images_to_insert, album_id, path, folder_name).await
|
||||
}
|
||||
|
||||
// 缩略图生成功能已移除
|
||||
println!(" 发现 {} 张图片需要导入", images_to_insert.len());
|
||||
|
||||
batch_insert_images_and_set_cover(&db, images_to_insert, album_id, path, folder_name).await
|
||||
}
|
||||
5
src/services/mod.rs
Normal file
5
src/services/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
//! 服务层 — 导入、处理等业务逻辑
|
||||
|
||||
pub mod import;
|
||||
|
||||
#[allow(unused)] pub use import::*;
|
||||
Reference in New Issue
Block a user