Skip to content

Commit a3ed72a

Browse files
author
Taois
committed
init: 初始化文件索引服务
1 parent 94eefbe commit a3ed72a

File tree

6 files changed

+2755
-0
lines changed

6 files changed

+2755
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,5 @@ dist
137137
# Vite logs files
138138
vite.config.js.timestamp-*
139139
vite.config.ts.timestamp-*
140+
/.idea/
141+
index.db

README.md

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
# 文件索引服务 (File Index Service)
2+
3+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
4+
[![Node.js](https://img.shields.io/badge/Node.js-18+-green.svg)](https://nodejs.org/)
5+
[![Fastify](https://img.shields.io/badge/Fastify-4+-blue.svg)](https://www.fastify.io/)
6+
7+
一个高性能的文件索引和搜索服务,基于 Node.js + Fastify + SQLite 构建,提供快速的文件搜索和统计功能。
8+
9+
## ✨ 特性
10+
11+
- 🚀 **高性能**: 基于 Fastify 框架,提供极快的响应速度
12+
- 🔍 **全文搜索**: 支持文件名和路径的模糊搜索
13+
- 📊 **统计信息**: 提供详细的文件类型和大小统计
14+
- 🌐 **Web界面**: 内置美观的Web管理界面
15+
- 🔄 **实时重建**: 支持手动重建文件索引
16+
- 📦 **跨平台**: 支持 Windows、Linux、macOS
17+
- 🎯 **轻量级**: 单文件部署,无需复杂配置
18+
- 🔒 **安全**: 精细化的CORS控制,保护敏感操作
19+
- 🛠️ **灵活配置**: 支持自定义端口和扫描路径
20+
21+
## 🚀 快速开始
22+
23+
### 方式一:直接运行源码
24+
25+
1. **克隆项目**
26+
```bash
27+
git clone https://github.com/hjdhnx/file-index.git
28+
cd file-index
29+
```
30+
31+
2. **安装依赖**
32+
```bash
33+
npm install
34+
# 或者使用 pnpm
35+
pnpm install
36+
```
37+
38+
3. **启动服务**
39+
```bash
40+
# 使用默认配置启动
41+
npm start
42+
43+
# 或指定端口和扫描路径
44+
node main.cjs --port 8080 --path /your/target/directory
45+
```
46+
47+
### 方式二:使用预编译可执行文件
48+
49+
下载对应平台的可执行文件,直接运行:
50+
51+
```bash
52+
# Windows
53+
./file-index-win-x64.exe --port 8080
54+
55+
# Linux
56+
./file-index-linux-x64 --port 8080
57+
58+
# macOS
59+
./file-index-macos-x64 --port 8080
60+
```
61+
62+
## 📖 使用说明
63+
64+
### 命令行参数
65+
66+
```bash
67+
node main.cjs [选项]
68+
69+
选项:
70+
-port, --port <端口号> 指定服务器端口 (默认: 3002)
71+
-path, --path <路径> 指定要扫描的根目录 (默认: 当前目录)
72+
-h, --help 显示帮助信息
73+
74+
示例:
75+
node main.cjs --port 8080
76+
node main.cjs --port 3000 --path /home/user/documents
77+
node main.cjs --path ./my-folder
78+
node main.cjs --path C:\Users\Documents
79+
```
80+
81+
### Web界面
82+
83+
启动服务后,在浏览器中访问 `http://localhost:端口号` 即可使用Web界面进行文件搜索和管理。
84+
85+
## 🔌 API接口
86+
87+
### 搜索文件
88+
```http
89+
GET /api/search?q=关键词&type=文件类型&limit=100&offset=0
90+
```
91+
92+
**参数说明:**
93+
- `q`: 搜索关键词(可选)
94+
- `type`: 文件类型过滤(可选)
95+
- `limit`: 返回结果数量限制(默认100)
96+
- `offset`: 分页偏移量(默认0)
97+
98+
**响应示例:**
99+
```json
100+
{
101+
"files": [
102+
{
103+
"id": 1,
104+
"file_path": "/path/to/file.txt",
105+
"file_name": "file.txt",
106+
"file_size": 1024,
107+
"file_type": "txt",
108+
"created_at": "2024-01-01T00:00:00.000Z",
109+
"modified_at": "2024-01-01T00:00:00.000Z"
110+
}
111+
],
112+
"total": 1,
113+
"limit": 100,
114+
"offset": 0
115+
}
116+
```
117+
118+
### 获取统计信息
119+
```http
120+
GET /api/stats
121+
```
122+
123+
**响应示例:**
124+
```json
125+
{
126+
"totalFiles": 1000,
127+
"totalSize": 1073741824,
128+
"fileTypes": {
129+
"txt": 100,
130+
"jpg": 200,
131+
"pdf": 50
132+
},
133+
"lastUpdated": "2024-01-01T00:00:00.000Z"
134+
}
135+
```
136+
137+
### 重建索引
138+
```http
139+
POST /api/rebuild
140+
```
141+
142+
**响应示例:**
143+
```json
144+
{
145+
"success": true,
146+
"message": "索引重建完成",
147+
"filesProcessed": 1000,
148+
"timeTaken": "2.5s"
149+
}
150+
```
151+
152+
## 🔒 安全特性
153+
154+
- **CORS控制**: 搜索和统计接口支持跨域访问,重建接口仅限同源访问
155+
- **路径验证**: 严格验证扫描路径的有效性
156+
- **错误处理**: 完善的错误处理和日志记录
157+
158+
## 🛠️ 开发
159+
160+
### 开发环境启动
161+
```bash
162+
npm run dev
163+
```
164+
165+
### 构建可执行文件
166+
167+
```bash
168+
# 构建所有平台
169+
npm run build:all
170+
171+
# 构建特定平台
172+
npm run build:win # Windows
173+
npm run build:linux # Linux
174+
npm run build:macos # macOS Intel
175+
npm run build:macos-arm # macOS Apple Silicon
176+
177+
# 构建优化版本(更小体积)
178+
npm run build:lite
179+
npm run build:mini
180+
```
181+
182+
### 项目结构
183+
184+
```
185+
file-index/
186+
├── main.cjs # 主程序文件
187+
├── package.json # 项目配置
188+
├── build.js # 构建脚本
189+
├── index.db # SQLite数据库文件
190+
├── dist/ # 构建输出目录
191+
└── README.md # 项目说明
192+
```
193+
194+
## 📋 系统要求
195+
196+
- **Node.js**: 18.0.0 或更高版本
197+
- **内存**: 最少 512MB RAM
198+
- **存储**: 根据索引文件数量而定
199+
- **操作系统**: Windows 10+, Linux, macOS 10.15+
200+
201+
## 🤝 贡献
202+
203+
欢迎提交 Issue 和 Pull Request!
204+
205+
1. Fork 本项目
206+
2. 创建特性分支 (`git checkout -b feature/AmazingFeature`)
207+
3. 提交更改 (`git commit -m 'Add some AmazingFeature'`)
208+
4. 推送到分支 (`git push origin feature/AmazingFeature`)
209+
5. 开启 Pull Request
210+
211+
## 📄 许可证
212+
213+
本项目采用 MIT 许可证 - 查看 [LICENSE](LICENSE) 文件了解详情。
214+
215+
## 🙏 致谢
216+
217+
- [Fastify](https://www.fastify.io/) - 高性能Web框架
218+
- [SQLite](https://www.sqlite.org/) - 轻量级数据库
219+
- [pkg](https://github.com/vercel/pkg) - Node.js打包工具
220+
221+
## 📞 联系
222+
223+
- 项目地址: [https://github.com/hjdhnx/file-index](https://github.com/hjdhnx/file-index)
224+
- 问题反馈: [Issues](https://github.com/hjdhnx/file-index/issues)
225+
226+
---
227+
228+
⭐ 如果这个项目对你有帮助,请给它一个星标!

build.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { execSync } from 'child_process';
2+
import fs from 'fs';
3+
import path from 'path';
4+
5+
const distDir = 'dist';
6+
7+
// 确保dist目录存在
8+
if (!fs.existsSync(distDir)) {
9+
fs.mkdirSync(distDir, { recursive: true });
10+
}
11+
12+
console.log('🚀 开始构建多平台二进制文件...\n');
13+
14+
const builds = [
15+
{
16+
name: 'Windows x64',
17+
target: 'node18-win-x64',
18+
output: 'dist/file-index-win-x64.exe',
19+
compress: 'GZip'
20+
},
21+
{
22+
name: 'Linux x64',
23+
target: 'node18-linux-x64',
24+
output: 'dist/file-index-linux-x64',
25+
compress: 'GZip'
26+
},
27+
{
28+
name: 'macOS x64',
29+
target: 'node18-macos-x64',
30+
output: 'dist/file-index-macos-x64',
31+
compress: 'GZip'
32+
},
33+
{
34+
name: 'macOS ARM64',
35+
target: 'node18-macos-arm64',
36+
output: 'dist/file-index-macos-arm64',
37+
compress: 'GZip'
38+
}
39+
];
40+
41+
const startTime = Date.now();
42+
43+
for (const build of builds) {
44+
console.log(`📦 正在构建 ${build.name}...`);
45+
46+
try {
47+
const buildStartTime = Date.now();
48+
const command = `pkg server.cjs --targets ${build.target} --compress ${build.compress} --output ${build.output}`;
49+
50+
execSync(command, {
51+
stdio: 'inherit',
52+
cwd: process.cwd()
53+
});
54+
55+
const buildTime = Date.now() - buildStartTime;
56+
57+
// 获取文件大小
58+
if (fs.existsSync(build.output)) {
59+
const stats = fs.statSync(build.output);
60+
const fileSizeMB = (stats.size / (1024 * 1024)).toFixed(2);
61+
console.log(`✅ ${build.name} 构建完成! 大小: ${fileSizeMB}MB, 耗时: ${buildTime}ms\n`);
62+
}
63+
} catch (error) {
64+
console.error(`❌ ${build.name} 构建失败:`, error.message);
65+
}
66+
}
67+
68+
const totalTime = Date.now() - startTime;
69+
console.log(`🎉 所有构建完成! 总耗时: ${totalTime}ms`);
70+
71+
// 显示构建结果
72+
console.log('\n📋 构建结果:');
73+
builds.forEach(build => {
74+
if (fs.existsSync(build.output)) {
75+
const stats = fs.statSync(build.output);
76+
const fileSizeMB = (stats.size / (1024 * 1024)).toFixed(2);
77+
console.log(` ✅ ${build.output} (${fileSizeMB}MB)`);
78+
} else {
79+
console.log(` ❌ ${build.output} (构建失败)`);
80+
}
81+
});

0 commit comments

Comments
 (0)