Stowr Core

stowr-core 是一个用 Rust 编写的文件管理核心库,提供文件压缩、存储和索引功能。它可以独立使用,也可以集成到其他应用程序中。
功能特性
- 文件压缩存储: 使用 gzip 压缩算法减少存储空间
- 双重索引系统: 支持 JSON 和 SQLite 两种索引模式,自动选择最优方案
- 批量文件操作: 支持通配符模式批量处理文件
- 多线程支持: 并行处理大量文件,提升性能
- 灵活配置: 可配置压缩级别、存储路径、索引模式等
- 模块化设计: 易于集成到不同类型的应用程序中
快速开始
将以下依赖添加到您的 Cargo.toml:
[dependencies]
stowr-core = "0.2.2"
基本使用
use stowr_core::{Config, StorageManager, create_index};
use std::path::Path;
fn main() -> anyhow::Result<()> {
let config = Config::default();
let index = create_index(&config)?;
let mut storage = StorageManager::new(config, index);
storage.store_file(Path::new("example.txt"), false)?;
let files = storage.list_files()?;
for file in files {
println!("File: {} ({} bytes)",
file.original_path.display(),
file.file_size);
}
let results = storage.search_files("*.txt")?;
println!("Found {} text files", results.len());
storage.owe_file(Path::new("example.txt"))?;
Ok(())
}
配置选项
use stowr_core::{Config, IndexMode};
use std::path::PathBuf;
let mut config = Config::default();
config.storage_path = PathBuf::from("./my_storage");
config.index_mode = IndexMode::Sqlite; config.compression_level = 9; config.multithread = 4;
高级功能
批量操作
storage.store_files_from_list(Path::new("file_list.txt"), false)?;
storage.owe_files_from_list(Path::new("extract_list.txt"))?;
storage.owe_all_files()?;
文件管理
storage.rename_file(Path::new("old_name.txt"), Path::new("new_name.txt"))?;
storage.move_file(Path::new("file.txt"), Path::new("new/location/"))?;
storage.delete_file(Path::new("unwanted.txt"))?;
与其他框架集成
Tauri 集成
use stowr_core::{Config, StorageManager, create_index};
use tauri::State;
use std::sync::Mutex;
type StorageState = Mutex<StorageManager>;
#[tauri::command]
async fn store_file(
state: State<'_, StorageState>,
file_path: String,
) -> Result<String, String> {
let mut storage = state.lock().unwrap();
storage.store_file(Path::new(&file_path), false)
.map_err(|e| e.to_string())?;
Ok("File stored successfully".to_string())
}
Web 服务集成
use stowr_core::{Config, StorageManager, create_index};
pub struct FileService {
storage: StorageManager,
}
impl FileService {
pub fn new() -> anyhow::Result<Self> {
let config = Config::default();
let index = create_index(&config)?;
let storage = StorageManager::new(config, index);
Ok(Self { storage })
}
pub fn store_upload(&mut self, file_path: &Path) -> anyhow::Result<()> {
self.storage.store_file(file_path, false)
}
}
索引模式
- Auto: 根据文件数量自动选择(< 1000 文件使用 JSON,>= 1000 使用 SQLite)
- Json: 使用 JSON 文件存储索引,适合小规模使用
- Sqlite: 使用 SQLite 数据库存储索引,适合大规模使用
性能考虑
- 默认压缩级别为 6,在压缩率和速度之间取得平衡
- 多线程处理在文件数量 > 1 且线程数 > 1 时自动启用
- SQLite 索引在大量文件时性能更好
- 内存使用量与并发线程数成正比
许可证
本项目采用 GPL-3.0-or-later 许可证。详见 LICENSE 文件。
贡献
欢迎提交 Issue 和 Pull Request。请确保在提交前运行测试:
cargo test
相关项目
- stowr - 基于 stowr-core 的命令行工具