feat: enable script upload
This commit is contained in:
parent
001be86948
commit
de9500bd16
@ -6,7 +6,7 @@ description = "PowerShell Scripts for EVERYONE!"
|
|||||||
repository = "https://git.itsscb.de/itsscb/BefehlsWerk"
|
repository = "https://git.itsscb.de/itsscb/BefehlsWerk"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
axum = { version = "0.8.4", features = ["ws"] }
|
axum = { version = "0.8.4", features = ["multipart", "ws"] }
|
||||||
chrono = "0.4.41"
|
chrono = "0.4.41"
|
||||||
futures = "0.3.31"
|
futures = "0.3.31"
|
||||||
serde = { version = "1.0.219", features = ["derive", "rc"] }
|
serde = { version = "1.0.219", features = ["derive", "rc"] }
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use axum::routing::{delete, get, get_service};
|
use axum::routing::{delete, get, get_service, post};
|
||||||
use routes::scripts::{delete_script, get_script_by_id, get_scripts};
|
use routes::scripts::{delete_script, get_script_by_id, get_scripts, upload_script};
|
||||||
use sqlx::SqlitePool;
|
use sqlx::SqlitePool;
|
||||||
use tower_http::services::ServeDir;
|
use tower_http::services::ServeDir;
|
||||||
mod routes;
|
mod routes;
|
||||||
@ -8,6 +8,7 @@ pub fn new_router(db: SqlitePool) -> axum::Router {
|
|||||||
axum::Router::new()
|
axum::Router::new()
|
||||||
.fallback_service(get_service(ServeDir::new("assets")))
|
.fallback_service(get_service(ServeDir::new("assets")))
|
||||||
.route("/scripts", get(get_scripts))
|
.route("/scripts", get(get_scripts))
|
||||||
|
.route("/scripts", post(upload_script))
|
||||||
.route("/scripts/{id}", get(get_script_by_id))
|
.route("/scripts/{id}", get(get_script_by_id))
|
||||||
.route("/scripts/{id}", delete(delete_script))
|
.route("/scripts/{id}", delete(delete_script))
|
||||||
.with_state(db)
|
.with_state(db)
|
||||||
|
@ -1,12 +1,38 @@
|
|||||||
use axum::{
|
use axum::{
|
||||||
extract::{Path, State},
|
extract::{Multipart, Path, State},
|
||||||
http::StatusCode,
|
http::StatusCode,
|
||||||
response::{Html, IntoResponse, Redirect},
|
response::{Html, IntoResponse, Redirect},
|
||||||
};
|
};
|
||||||
use sqlx::SqlitePool;
|
use sqlx::SqlitePool;
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
|
use tokio::io::AsyncWriteExt;
|
||||||
use tracing::{error, info, instrument, warn};
|
use tracing::{error, info, instrument, warn};
|
||||||
|
|
||||||
|
#[allow(clippy::unwrap_used)]
|
||||||
|
#[instrument(skip_all)]
|
||||||
|
pub async fn upload_script(
|
||||||
|
// State(database): State<SqlitePool>,
|
||||||
|
mut multipart: Multipart,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
while let Some(field) = multipart.next_field().await.unwrap() {
|
||||||
|
// Borrow file_name as &str
|
||||||
|
let file_name = field.file_name().map(|name| name.to_string());
|
||||||
|
|
||||||
|
// Now you can move or consume `field` safely
|
||||||
|
let data = field.bytes().await.unwrap();
|
||||||
|
|
||||||
|
if let Some(name) = file_name {
|
||||||
|
let _ = tokio::fs::create_dir("./uploads").await;
|
||||||
|
// Use `name` (owned String) and `data` here
|
||||||
|
let path = format!("./uploads/{}", name);
|
||||||
|
let mut file = tokio::fs::File::create(path).await.unwrap();
|
||||||
|
file.write_all(&data).await.unwrap();
|
||||||
|
// tokio::fs::write(path, &data).await.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Html("File uploaded successfully")
|
||||||
|
}
|
||||||
|
|
||||||
#[instrument(skip_all, fields(id = ?Path(&id)))]
|
#[instrument(skip_all, fields(id = ?Path(&id)))]
|
||||||
pub async fn delete_script(
|
pub async fn delete_script(
|
||||||
State(database): State<SqlitePool>,
|
State(database): State<SqlitePool>,
|
||||||
@ -88,7 +114,12 @@ pub async fn get_scripts(State(database): State<SqlitePool>) -> impl IntoRespons
|
|||||||
});
|
});
|
||||||
|
|
||||||
let output = format!(
|
let output = format!(
|
||||||
r##"<section id="scripts"><ul>{content}</ul><button hx-get="/scripts" hx-swap="outerHTML" hx-target="#scripts">Refresh</button></section>"##
|
r##"<section id="scripts"><ul>{content}</ul><button hx-get="/scripts" hx-swap="outerHTML" hx-target="#scripts">Refresh</button><form hx-post="/scripts" hx-target="#upload-status" enctype="multipart/form-data" method="post">
|
||||||
|
<input type="file" name="file" />
|
||||||
|
<button type="submit">Upload</button>
|
||||||
|
</form>
|
||||||
|
<div id="upload-status"></div>
|
||||||
|
</section>"##
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(Html(output))
|
Ok(Html(output))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user