Skip to content

Commit

Permalink
Add FileStorage module for file management with upload, retrieval, an…
Browse files Browse the repository at this point in the history
…d deletion functionalities
  • Loading branch information
jamesatomc committed Jan 30, 2025
1 parent 87c5b21 commit b10cd80
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
2 changes: 1 addition & 1 deletion monaos/mona-storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async-trait = "0.1"
serde = { version = "1.0.217", features = ["derive"] }
serde_json = "1.0.134"
tokio = { version = "1.0", features = ["full"] }

uuid = { version = "1.0", features = ["v4"] }
rocksdb.workspace = true
bincode.workspace = true
thiserror.workspace = true
89 changes: 89 additions & 0 deletions monaos/mona-storage/src/FileStorage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use std::path::PathBuf;
use std::time::SystemTime;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use tokio::fs;
use uuid::Uuid;

#[derive(Error, Debug)]
pub enum StorageError {
#[error("File not found: {0}")]
FileNotFound(String),
#[error("File size exceeds limit of {size}MB")]
FileSizeExceeded { size: u64 },
#[error("Upload timeout")]
UploadTimeout,
#[error("IO error: {0}")]
IoError(#[from] std::io::Error),
}

#[derive(Debug, Serialize, Deserialize)]
pub struct FileMetadata {
file_name: String,
file_size: u64,
mime_type: String,
created_at: Option<SystemTime>,
file_id: String,
}

impl Default for FileMetadata {
fn default() -> Self {
Self {
file_name: String::new(),
file_size: 0,
mime_type: String::from("application/octet-stream"),
created_at: Some(SystemTime::now()),
file_id: String::new(),
}
}
}

#[derive(Debug)]
pub struct FileStorage {
storage_path: PathBuf,
max_file_size: u64,
}

impl FileStorage {
pub fn new(storage_path: PathBuf) -> Self {
Self {
storage_path,
max_file_size: 100 * 1024 * 1024, // 100MB
}
}

pub async fn upload_file(&self, file_path: PathBuf) -> Result<String, StorageError> {
let metadata = fs::metadata(&file_path).await?;

if metadata.len() > self.max_file_size {
return Err(StorageError::FileSizeExceeded {
size: metadata.len() / (1024 * 1024)
});
}

let file_id = Uuid::new_v4().to_string();
let dest_path = self.storage_path.join(&file_id);

fs::create_dir_all(&self.storage_path).await?;
fs::copy(file_path, dest_path).await?;

Ok(file_id)
}

pub async fn get_file(&self, file_id: &str) -> Result<PathBuf, StorageError> {
let file_path = self.storage_path.join(file_id);
if !file_path.exists() {
return Err(StorageError::FileNotFound(file_id.to_string()));
}
Ok(file_path)
}

pub async fn delete_file(&self, file_id: &str) -> Result<(), StorageError> {
let file_path = self.storage_path.join(file_id);
if !file_path.exists() {
return Err(StorageError::FileNotFound(file_id.to_string()));
}
fs::remove_file(file_path).await?;
Ok(())
}
}
1 change: 1 addition & 0 deletions monaos/mona-storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{fs, path::PathBuf, time::Duration};
use thiserror::Error;
use rocksdb::{DB, Error as RocksError};
use bincode;
mod FileStorage;

#[derive(Error, Debug)]
pub enum StorageError {
Expand Down
2 changes: 1 addition & 1 deletion monaos/mona-types/src/gas_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use move_core_types::{
pub const MIST_PER_KARI: u64 = 1_000_000_000;

/// Total supply denominated in KARI
pub const TOTAL_SUPPLY_KARI: u64 = 10_000_000_000;
pub const TOTAL_SUPPLY_KARI: u64 = 100_000_000;

// Note: cannot use checked arithmetic here since `const unwrap` is still unstable.
/// Total supply denominated in Mist
Expand Down

0 comments on commit b10cd80

Please sign in to comment.