feat: upload package

This commit is contained in:
Ahmet Kaan GÜMÜŞ 2024-11-20 00:16:31 +03:00
parent 9c64df93fd
commit e5e3bf2de5
7 changed files with 60 additions and 11 deletions

View file

@ -1,4 +1,8 @@
use crate::{database, routing};
use axum::extract::Multipart;
use tokio::{fs::File, io::AsyncWriteExt};
use tokio_util::io::ReaderStream;
use crate::{database, routing, PACKAGE_PATH};
use super::package::Package;
@ -19,3 +23,36 @@ pub async fn update_package(package_name: String, package: routing::Package) ->
pub async fn delete_package(package_name: String) -> Option<Package> {
database::delete_package(package_name).await
}
pub async fn download_package(package_name: String) -> Option<ReaderStream<File>> {
if let Some(package) = crate::package::utils::read_package(package_name).await {
if let Some(package_file_stream) = package.serve().await {
return Some(package_file_stream);
}
}
None
}
pub async fn upload_package(mut package_file: Multipart) -> Option<Package> {
if let Ok(package_file_part_unchecked) = package_file.next_field().await {
if let Some(package_file_part) = package_file_part_unchecked {
if let Some(package_file_name) = package_file_part.file_name() {
let package_file_name = package_file_name.to_owned();
let file_location = format!("./{}/{}", PACKAGE_PATH, package_file_name);
if let Ok(package_file_data) = package_file_part.bytes().await {
if let Some(mut package) =
crate::package::utils::read_package(package_file_name.to_owned()).await
{
if let Ok(mut file_descriptor) = File::create_new(&file_location).await {
if let Ok(_) = file_descriptor.write_all(&package_file_data).await {
package.set_location(&file_location);
return Some(package);
}
}
}
}
}
}
}
None
}