Skip to content

Commit

Permalink
Upload xmls
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosthe19916 committed Jan 4, 2024
1 parent ee63c9b commit e50e69d
Show file tree
Hide file tree
Showing 15 changed files with 610 additions and 127 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion openubl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ docker-compose -f openubl/deploy/compose/compose.yaml up
```

```shell
RUST_LOG=info cargo watch -x 'run -p openubl-cli -- server --db-user user --db-password password --oidc-auth-server-url http://localhost:9001/realms/openubl --storage-type minio --storage-minio-host http://localhost:9000 --storage-minio-access-key accesskey --storage-minio-secret-key secretkey'
RUST_LOG=info cargo watch -x 'run -p openubl-cli -- server --db-user user --db-password password --oidc-auth-server-url http://localhost:9001/realms/openubl --storage-type minio --storage-minio-host http://localhost:9002 --storage-minio-bucket openubl --storage-minio-access-key admin --storage-minio-secret-key password'
```
7 changes: 3 additions & 4 deletions openubl/deploy/compose/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,13 @@ services:

minio:
image: quay.io/minio/minio:latest
command: server /data
command: server --console-address ":9001" /data
ports:
- "9000:9000"
- "9002:9000"
- "9003:9001"
environment:
MINIO_ROOT_USER: "admin"
MINIO_ROOT_PASSWORD: "password"
MINIO_ACCESS_KEY: "accesskey"
MINIO_SECRET_KEY: "secretkey"
MINIO_NOTIFY_NATS_ENABLE_OPENUBL: "on"
MINIO_NOTIFY_NATS_ADDRESS_OPENUBL: "nats:4222"
MINIO_NOTIFY_NATS_SUBJECT_OPENUBL: "openubl"
Expand Down
1 change: 1 addition & 0 deletions openubl/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ actix-web-httpauth = "0.8.1"
actix-4-jwt-auth = { version = "1.2.0" }
actix-multipart = "0.6.1"
minio = "0.1.0"
zip = "0.6.6"
6 changes: 5 additions & 1 deletion openubl/server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use openubl_api::system::InnerSystem;
use openubl_common::config::Database;
use openubl_storage::StorageSystem;

use crate::server::{health, project};
use crate::server::{health, project, files};

pub mod server;

Expand Down Expand Up @@ -95,8 +95,12 @@ pub struct AppState {
pub fn configure(config: &mut web::ServiceConfig) {
config.service(health::liveness);
config.service(health::readiness);

config.service(project::list_projects);
config.service(project::create_project);
config.service(project::get_project);
config.service(project::update_project);
config.service(project::delete_project);

config.service(files::upload_file);
}
18 changes: 10 additions & 8 deletions openubl/server/src/server/files.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use actix_4_jwt_auth::AuthenticatedUser;
use actix_multipart::form::tempfile::TempFile;
use actix_multipart::form::MultipartForm;
use actix_web::{post, web, Responder};
use actix_multipart::form::tempfile::TempFile;
use actix_web::{post, Responder, web};

use openubl_oidc::UserClaims;
use xsender::prelude::{FromPath, UblFile};

use crate::server::Error;
use crate::AppState;
use crate::server::Error;

#[derive(Debug, MultipartForm)]
struct UploadForm {
Expand All @@ -16,18 +16,20 @@ struct UploadForm {
}

#[utoipa::path(responses((status = 200, description = "Upload a file")))]
#[post("/projects/files")]
#[post("/projects/{project_id}/files")]
pub async fn upload_file(
state: web::Data<AppState>,
_path: web::Path<i32>,
MultipartForm(form): MultipartForm<UploadForm>,
_user: AuthenticatedUser<UserClaims>,
) -> Result<impl Responder, Error> {
for f in form.files {
let ubl_file = UblFile::from_path(f.file.path())?;
for temp_file in form.files {
let ubl_file = UblFile::from_path(temp_file.file.path())?;
ubl_file.metadata()?;

let filename = f.file.path().to_str().expect("hello");
state.storage.upload(filename).await?;
let file_path = temp_file.file.path().to_str().expect("Could not find filename");
let filename = temp_file.file_name.unwrap_or("file.xml".to_string());
state.storage.upload(file_path, &filename).await?;
}

Ok("Live")
Expand Down
22 changes: 22 additions & 0 deletions openubl/server/src/server/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,28 @@ pub async fn create_project(
}
}

#[utoipa::path(responses((status = 200, description = "Get project")))]
#[get("/projects/{project_id}")]
pub async fn get_project(
state: web::Data<AppState>,
path: web::Path<i32>,
user: AuthenticatedUser<UserClaims>,
) -> Result<impl Responder, Error> {
let project_id = path.into_inner();

match state
.system
.get_project(project_id, &user.claims.user_id(), Transactional::None)
.await
.map_err(Error::System)?
{
None => Ok(HttpResponse::NotFound().finish()),
Some(ctx) => {
Ok(HttpResponse::Ok().json(ctx.project))
}
}
}

#[utoipa::path(responses((status = 204, description = "Update project")))]
#[put("/projects/{project_id}")]
pub async fn update_project(
Expand Down
1 change: 1 addition & 0 deletions openubl/storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ serde = { version = "1.0.193", features = ["derive"] }
anyhow = "1.0.78"
uuid = { version = "1.6.1", features = ["v4"] }
thiserror = "1.0.53"
zip = "0.6.6"
2 changes: 1 addition & 1 deletion openubl/storage/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct MinioStorage {
#[arg(id = "storage-minio-host", long, env = "STORAGE_MINIO_HOST")]
pub host: String,
#[arg(
id = "minio-bucket",
id = "storage-minio-bucket",
long,
env = "STORAGE_MINIO_BUCKET",
default_value = "openubl"
Expand Down
62 changes: 54 additions & 8 deletions openubl/storage/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
use anyhow::anyhow;
use std::fs::rename;
use std::fs;
use std::fs::{File, rename};
use std::io::{Read, Write};
use std::path::Path;
use std::str::FromStr;

use anyhow::anyhow;
use minio::s3::args::UploadObjectArgs;
use minio::s3::client::Client;
use minio::s3::creds::StaticProvider;
use minio::s3::http::BaseUrl;
use uuid::Uuid;
use zip::result::{ZipError, ZipResult};
use zip::write::FileOptions;
use zip::ZipWriter;

use crate::config::Storage;

Expand All @@ -24,6 +29,8 @@ pub enum StorageSystemErr {
Filesystem(std::io::Error),
#[error(transparent)]
Minio(minio::s3::error::Error),
#[error(transparent)]
Zip(zip::result::ZipError),
}

impl From<std::io::Error> for StorageSystemErr {
Expand All @@ -38,6 +45,12 @@ impl From<minio::s3::error::Error> for StorageSystemErr {
}
}

impl From<zip::result::ZipError> for StorageSystemErr {
fn from(e: zip::result::ZipError) -> Self {
Self::Zip(e)
}
}

impl StorageSystem {
pub fn new(config: &Storage) -> anyhow::Result<Self> {
match config.storage_type.as_str() {
Expand All @@ -57,19 +70,52 @@ impl StorageSystem {
}
}

pub async fn upload(&self, filename: &str) -> Result<String, StorageSystemErr> {
let object_id = Uuid::new_v4().to_string();
pub async fn upload(&self, file_path: &str, filename: &str) -> Result<String, StorageSystemErr> {
let zip_name = format!("{}.zip", Uuid::new_v4());
let zip_path = zip_file(&zip_name, file_path, filename)?;

match self {
StorageSystem::FileSystem(workspace) => {
let new_path = Path::new(workspace).join(&object_id);
rename(filename, new_path)?;
Ok(object_id.clone())
let new_path = Path::new(workspace).join(&zip_name);
rename(zip_path, new_path)?;
Ok(zip_name.clone())
}
StorageSystem::Minio(bucket, client) => {
let object = &UploadObjectArgs::new(bucket, &object_id, filename)?;
let object = &UploadObjectArgs::new(bucket, &zip_name, &zip_path)?;
let response = client.upload_object(object).await?;

// Clear temp files
fs::remove_file(file_path)?;
fs::remove_file(zip_path)?;

Ok(response.object_name)
}
}
}
}


pub fn zip_file(zip_filename: &str, full_path_of_file_to_be_zipped: &str, file_name_to_be_used_in_zip: &str) -> ZipResult<String> {
let mut file = File::open(full_path_of_file_to_be_zipped)?;
let file_path = Path::new(full_path_of_file_to_be_zipped);
let file_directory = file_path.parent().ok_or(ZipError::InvalidArchive("Could not find the parent folder of given file"))?;

let zip_path = file_directory.join(zip_filename);
let zip_file = File::create(zip_path.as_path())?;
let mut zip = ZipWriter::new(zip_file);

let file_options = FileOptions::default()
.compression_method(zip::CompressionMethod::Bzip2)
.unix_permissions(0o755);

zip.start_file(file_name_to_be_used_in_zip, file_options)?;

let mut buff = Vec::new();
file.read_to_end(&mut buff)?;
zip.write_all(&buff)?;

zip.finish()?;

let result = zip_path.to_str().ok_or(ZipError::InvalidArchive("Could not determine with zip filename"))?;
Ok(result.to_string())
}
8 changes: 7 additions & 1 deletion openubl/ui/client/src/app/api/rest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios from "axios";
import axios, { AxiosRequestConfig } from "axios";
import { HubPaginatedResult, HubRequestParams, New, Project } from "./models";
import { serializeRequestParamsForHub } from "@app/hooks/table-controls";

Expand Down Expand Up @@ -43,3 +43,9 @@ export const updateProject = (obj: Project) =>

export const deleteProject = (id: number | string) =>
axios.delete<void>(`${PROJECTS}/${id}`);

export const uploadFile = (
projectId: number | string,
formData: FormData,
config?: AxiosRequestConfig
) => axios.post<void>(`${PROJECTS}/${projectId}/files`, formData, config);
Loading

0 comments on commit e50e69d

Please sign in to comment.