fix: update bytes import and fix data_path field references
Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) <aider@aider.chat>
This commit is contained in:
@@ -2,7 +2,6 @@ use crate::services::error::CoreError;
|
||||
use crate::services::item_service::ItemService;
|
||||
use crate::services::types::{ItemWithContent, ItemWithMeta};
|
||||
use crate::common::is_binary::is_binary;
|
||||
use bytes::Bytes;
|
||||
use rusqlite::Connection;
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
@@ -10,6 +9,7 @@ use std::sync::Arc;
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::io::{AsyncReadExt, AsyncSeekExt};
|
||||
use tokio_util::io::ReaderStream;
|
||||
use tokio_util::bytes::Bytes;
|
||||
|
||||
/// An asynchronous wrapper around the `ItemService` for use in async contexts like the web server.
|
||||
/// It uses `tokio::task::spawn_blocking` to run synchronous database and filesystem operations
|
||||
@@ -22,17 +22,17 @@ pub struct AsyncItemService {
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl AsyncItemService {
|
||||
pub fn new(data_path: PathBuf, db: Arc<Mutex<Connection>>) -> Self {
|
||||
Self { data_path, db }
|
||||
pub fn new(data_dir: PathBuf, db: Arc<Mutex<Connection>>) -> Self {
|
||||
Self { data_dir, db }
|
||||
}
|
||||
|
||||
pub async fn get_item(&self, id: i64) -> Result<ItemWithMeta, CoreError> {
|
||||
let data_path = self.data_path.clone();
|
||||
let data_dir = self.data_dir.clone();
|
||||
let db = self.db.clone();
|
||||
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let conn = db.blocking_lock();
|
||||
let item_service = ItemService::new(data_path);
|
||||
let item_service = ItemService::new(data_dir);
|
||||
item_service.get_item(&conn, id)
|
||||
})
|
||||
.await
|
||||
@@ -40,12 +40,12 @@ impl AsyncItemService {
|
||||
}
|
||||
|
||||
pub async fn get_item_content(&self, id: i64) -> Result<ItemWithContent, CoreError> {
|
||||
let data_path = self.data_path.clone();
|
||||
let data_dir = self.data_dir.clone();
|
||||
let db = self.db.clone();
|
||||
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let conn = db.blocking_lock();
|
||||
let item_service = ItemService::new(data_path);
|
||||
let item_service = ItemService::new(data_dir);
|
||||
item_service.get_item_content(&conn, id)
|
||||
})
|
||||
.await
|
||||
@@ -58,7 +58,7 @@ impl AsyncItemService {
|
||||
allow_binary: bool,
|
||||
offset: u64,
|
||||
length: u64,
|
||||
) -> Result<(impl tokio_stream::Stream<Item = Result<bytes::Bytes, std::io::Error>>, String), CoreError> {
|
||||
) -> Result<(impl tokio_stream::Stream<Item = Result<tokio_util::bytes::Bytes, std::io::Error>>, String), CoreError> {
|
||||
let item_with_content = self.get_item_content(item_id).await?;
|
||||
let metadata = item_with_content.item_with_meta.meta_as_map();
|
||||
|
||||
@@ -82,7 +82,7 @@ impl AsyncItemService {
|
||||
.unwrap_or_else(|| "application/octet-stream".to_string());
|
||||
|
||||
// Open the file for streaming
|
||||
let file_path = self.data_path.join(format!("{}.dat", item_id));
|
||||
let file_path = self.data_dir.join(format!("{}.dat", item_id));
|
||||
let file = tokio::fs::File::open(&file_path).await?;
|
||||
let mut buffered_file = tokio::io::BufReader::new(file);
|
||||
|
||||
@@ -110,12 +110,12 @@ impl AsyncItemService {
|
||||
tags: Vec<String>,
|
||||
meta: HashMap<String, String>,
|
||||
) -> Result<ItemWithMeta, CoreError> {
|
||||
let data_path = self.data_path.clone();
|
||||
let data_dir = self.data_dir.clone();
|
||||
let db = self.db.clone();
|
||||
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let conn = db.blocking_lock();
|
||||
let item_service = ItemService::new(data_path);
|
||||
let item_service = ItemService::new(data_dir);
|
||||
item_service.find_item(&conn, &ids, &tags, &meta)
|
||||
})
|
||||
.await
|
||||
@@ -127,12 +127,12 @@ impl AsyncItemService {
|
||||
tags: Vec<String>,
|
||||
meta: HashMap<String, String>,
|
||||
) -> Result<Vec<ItemWithMeta>, CoreError> {
|
||||
let data_path = self.data_path.clone();
|
||||
let data_dir = self.data_dir.clone();
|
||||
let db = self.db.clone();
|
||||
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let conn = db.blocking_lock();
|
||||
let item_service = ItemService::new(data_path);
|
||||
let item_service = ItemService::new(data_dir);
|
||||
item_service.list_items(&conn, &tags, &meta)
|
||||
})
|
||||
.await
|
||||
@@ -140,12 +140,12 @@ impl AsyncItemService {
|
||||
}
|
||||
|
||||
pub async fn delete_item(&self, id: i64) -> Result<(), CoreError> {
|
||||
let data_path = self.data_path.clone();
|
||||
let data_dir = self.data_dir.clone();
|
||||
let db = self.db.clone();
|
||||
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let mut conn = db.blocking_lock();
|
||||
let item_service = ItemService::new(data_path);
|
||||
let item_service = ItemService::new(data_dir);
|
||||
item_service.delete_item(&mut conn, id)
|
||||
})
|
||||
.await
|
||||
@@ -158,12 +158,12 @@ impl AsyncItemService {
|
||||
tags: Vec<String>,
|
||||
metadata: HashMap<String, String>,
|
||||
) -> Result<ItemWithMeta, CoreError> {
|
||||
let data_path = self.data_path.clone();
|
||||
let data_dir = self.data_dir.clone();
|
||||
let db = self.db.clone();
|
||||
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let mut conn = db.blocking_lock();
|
||||
let item_service = ItemService::new(data_path);
|
||||
let item_service = ItemService::new(data_dir);
|
||||
item_service.save_item_from_mcp(&content, &tags, &metadata, &mut conn)
|
||||
})
|
||||
.await
|
||||
|
||||
Reference in New Issue
Block a user