Add task for adding data file from FS. Improve home page

This commit is contained in:
2024-11-04 05:15:42 +01:00
parent feeaecac85
commit 918d7ea327
10 changed files with 222 additions and 12 deletions

View File

@@ -1,3 +1,4 @@
use core::task;
use std::path::Path;
use async_trait::async_trait;
@@ -72,6 +73,7 @@ impl Hooks for App {
tasks.register(tasks::create_user::CreateUserData);
tasks.register(tasks::create_job::CreateJobData);
tasks.register(tasks::create_skill::CreateSkillData);
tasks.register(tasks::add_data_file::AddDataFile);
}
async fn truncate(db: &DatabaseConnection) -> Result<()> {

View File

@@ -33,7 +33,7 @@ pub async fn upload_data(
Err(_) => return unauthorized("Unauthorized"),
}
services::data::add(&auth, &ctx, payload).await?;
services::data::add(&auth, &ctx, payload, true).await?;
format::html("<h1>File uploaded successfully</h1>")
}

View File

@@ -5,9 +5,13 @@ use crate::models::users::users;
use axum::extract::Multipart;
use axum_extra::headers::Range;
use axum_extra::TypedHeader;
use bytes::Bytes;
use loco_rs::prelude::*;
use tokio::fs::File;
use std::fs::{self};
use axum_range::KnownSize;
use axum_range::Ranged;
@@ -51,7 +55,49 @@ pub async fn serve_data_file(
}
}
pub async fn add(auth: &auth::JWT, ctx: &AppContext, mut payload: Multipart) -> ModelResult<Model> {
pub async fn add_data_file_from_path(
ctx: &AppContext,
file_path: &str,
file_name: &str,
protected: bool,
uuid_name: bool,
) -> ModelResult<Model> {
let ext = String::from(file_name.split('.').last().unwrap_or("txt"));
let file_name = if uuid_name {
let temp_file_name = uuid::Uuid::new_v4().to_string();
format!("{}.{}", temp_file_name, ext)
} else {
file_name.to_string()
};
let path = PathBuf::from(file_path);
let content = fs::read(&path).map_err(|_| ModelError::Any("Failed to read file".into()))?;
let content = Bytes::from(content);
let mut item = ActiveModel {
..Default::default()
};
item.protected = Set(protected);
item.file_name = Set(file_name.to_string());
item.file_url = Set(format!("uploads/{}", file_name));
let item = item.insert(&ctx.db).await?;
match ctx.storage.as_ref().upload(&path.as_path(), &content).await {
Ok(_) => {}
Err(_) => return Err(ModelError::Any("Failed to save file to storage".into())),
}
Ok(item)
}
pub async fn add(
auth: &auth::JWT,
ctx: &AppContext,
mut payload: Multipart,
uuid_name: bool,
) -> ModelResult<Model> {
let _current_user = users::Model::find_by_pid(&ctx.db, &auth.claims.pid).await?;
let mut protected = None;
@@ -83,7 +129,13 @@ pub async fn add(auth: &auth::JWT, ctx: &AppContext, mut payload: Multipart) ->
.ok_or_else(|| ModelError::Any("Failed to get file name".into()))?;
let ext = String::from(og_file_name.split('.').last().unwrap_or("txt"));
let temp_file_name = uuid::Uuid::new_v4().to_string();
let temp_file_name = if uuid_name {
let temp_file_name = uuid::Uuid::new_v4().to_string();
format!("{}.{}", temp_file_name, ext)
} else {
og_file_name.to_string()
};
let temp_file_name = format!("{}.{}", temp_file_name, ext);
file_name = Some(temp_file_name.clone());

View File

@@ -0,0 +1,38 @@
use loco_rs::prelude::*;
use crate::services;
pub struct AddDataFile;
#[async_trait]
impl Task for AddDataFile {
fn task(&self) -> TaskInfo {
TaskInfo {
name: "add_data_file".to_string(),
detail: "Task for adding a new data file".to_string(),
}
}
async fn run(&self, app_context: &AppContext, vars: &task::Vars) -> Result<()> {
let file_path = vars.cli_arg("file_path")?;
let file_name = vars.cli_arg("file_name")?;
let protected = vars.cli_arg("protected")?;
let uuid_name = vars.cli_arg("uuid_name")?;
let protected = protected.parse::<bool>().unwrap_or(false);
let uuid_name = uuid_name.parse::<bool>().unwrap_or(false);
services::data::add_data_file_from_path(
&app_context,
file_path,
file_name,
protected,
uuid_name,
)
.await?;
tracing::info!("Data file added successfully");
Ok(())
}
}

View File

@@ -1,3 +1,4 @@
pub mod add_data_file;
pub mod create_job;
pub mod create_skill;
pub mod create_user;