mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 20:42:30 +08:00
go through upload, create kb, add doc to kb (#11)
* add field progress msg into docinfo; add file processing procedure * go through upload, create kb, add doc to kb
This commit is contained in:
@ -1,15 +1,20 @@
|
||||
use std::collections::HashMap;
|
||||
use actix_multipart::Multipart;
|
||||
use std::io::Write;
|
||||
use std::slice::Chunks;
|
||||
//use actix_multipart::{Multipart, MultipartError, Field};
|
||||
use actix_multipart_extract::{File, Multipart, MultipartForm};
|
||||
use actix_web::{get, HttpResponse, post, web};
|
||||
use actix_web::web::Bytes;
|
||||
use chrono::Local;
|
||||
use futures_util::StreamExt;
|
||||
use serde::Deserialize;
|
||||
use std::io::Write;
|
||||
use sea_orm::DbConn;
|
||||
use crate::api::JsonResponse;
|
||||
use crate::AppState;
|
||||
use crate::entity::doc_info::Model;
|
||||
use crate::errors::AppError;
|
||||
use crate::service::doc_info::{Mutation, Query};
|
||||
use serde::Deserialize;
|
||||
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Params {
|
||||
@ -53,41 +58,54 @@ async fn list(params: web::Json<Params>, data: web::Data<AppState>) -> Result<Ht
|
||||
.body(serde_json::to_string(&json_response)?))
|
||||
}
|
||||
|
||||
#[derive(Deserialize, MultipartForm, Debug)]
|
||||
pub struct UploadForm {
|
||||
#[multipart(max_size = 512MB)]
|
||||
file_field: File,
|
||||
uid: i64,
|
||||
did: i64
|
||||
}
|
||||
|
||||
#[post("/v1.0/upload")]
|
||||
async fn upload(mut payload: Multipart, filename: web::Data<String>, did: web::Data<i64>, uid: web::Data<i64>, data: web::Data<AppState>) -> Result<HttpResponse, AppError> {
|
||||
let mut size = 0;
|
||||
|
||||
while let Some(item) = payload.next().await {
|
||||
let mut field = item.unwrap();
|
||||
|
||||
let filepath = format!("./uploads/{}", filename.as_str());
|
||||
|
||||
let mut file = web::block(|| std::fs::File::create(filepath))
|
||||
.await
|
||||
.unwrap()?;
|
||||
|
||||
while let Some(chunk) = field.next().await {
|
||||
let data = chunk.unwrap();
|
||||
size += data.len() as u64;
|
||||
file = web::block(move || file.write_all(&data).map(|_| file))
|
||||
.await
|
||||
.unwrap()?;
|
||||
async fn upload(payload: Multipart<UploadForm>, data: web::Data<AppState>) -> Result<HttpResponse, AppError> {
|
||||
let uid = payload.uid;
|
||||
async fn add_number_to_filename(file_name: String, conn:&DbConn, uid:i64) -> String {
|
||||
let mut i = 0;
|
||||
let mut new_file_name = file_name.to_string();
|
||||
let arr: Vec<&str> = file_name.split(".").collect();
|
||||
let suffix = String::from(arr[arr.len()-1]);
|
||||
let preffix = arr[..arr.len()-1].join(".");
|
||||
let mut docs = Query::find_doc_infos_by_name(conn, uid, new_file_name.clone()).await.unwrap();
|
||||
while docs.len()>0 {
|
||||
i += 1;
|
||||
new_file_name = format!("{}_{}.{}", preffix, i, suffix);
|
||||
docs = Query::find_doc_infos_by_name(conn, uid, new_file_name.clone()).await.unwrap();
|
||||
}
|
||||
new_file_name
|
||||
}
|
||||
let fnm = add_number_to_filename(payload.file_field.name.clone(), &data.conn, uid).await;
|
||||
|
||||
let _ = Mutation::create_doc_info(&data.conn, Model {
|
||||
did: *did.into_inner(),
|
||||
uid: *uid.into_inner(),
|
||||
doc_name: filename.to_string(),
|
||||
size,
|
||||
std::fs::create_dir_all(format!("./upload/{}/", uid));
|
||||
let filepath = format!("./upload/{}/{}-{}", payload.uid, payload.did, fnm.clone());
|
||||
let mut f =std::fs::File::create(&filepath)?;
|
||||
f.write(&payload.file_field.bytes)?;
|
||||
|
||||
let doc = Mutation::create_doc_info(&data.conn, Model {
|
||||
did:Default::default(),
|
||||
uid: uid,
|
||||
doc_name: fnm,
|
||||
size: payload.file_field.bytes.len() as i64,
|
||||
kb_infos: Vec::new(),
|
||||
kb_progress: 0.0,
|
||||
location: "".to_string(),
|
||||
r#type: "".to_string(),
|
||||
kb_progress_msg: "".to_string(),
|
||||
location: filepath,
|
||||
r#type: "doc".to_string(),
|
||||
created_at: Local::now().date_naive(),
|
||||
updated_at: Local::now().date_naive(),
|
||||
}).await?;
|
||||
|
||||
let _ = Mutation::place_doc(&data.conn, payload.did, doc.did.unwrap()).await?;
|
||||
|
||||
Ok(HttpResponse::Ok().body("File uploaded successfully"))
|
||||
}
|
||||
|
||||
@ -121,4 +139,4 @@ async fn mv(params: web::Json<MvParams>, data: web::Data<AppState>) -> Result<Ht
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("application/json")
|
||||
.body(serde_json::to_string(&json_response)?))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user