Skip to content

Commit

Permalink
address requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Chethan-rao committed Jan 18, 2024
1 parent 300f4cd commit e5d5905
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 142 deletions.
32 changes: 14 additions & 18 deletions Cargo.lock

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

14 changes: 3 additions & 11 deletions config/config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -509,14 +509,6 @@ client_secret = "paypal_secret_key" # Secret key for PayPal onboarding
partner_id = "paypal_partner_id" # Partner ID for PayPal onboarding
enabled = true # Switch to enable or disable PayPal onboarding

[frm]
enabled = true

[paypal_onboarding]
client_id = "paypal_client_id" # Client ID for PayPal onboarding
client_secret = "paypal_secret_key" # Secret key for PayPal onboarding
partner_id = "paypal_partner_id" # Partner ID for PayPal onboarding
enabled = true # Switch to enable or disable PayPal onboarding

[events]
source = "logs" # The event sink to push events supports kafka or logs (stdout)
Expand All @@ -531,9 +523,9 @@ connector_logs_topic = "topic" # Kafka topic to be used for connector
outgoing_webhook_logs_topic = "topic" # Kafka topic to be used for outgoing webhook events

# File storage configuration
[file_storage_config]
file_storage_scheme = "aws_s3" # File storage scheme to be used
[file_storage]
file_storage_backend = "aws_s3" # File storage backend to be used

[file_storage_config.aws_s3]
[file_storage.aws_s3]
region = "us-east-1" # The AWS region used by the AWS S3 for file storage
bucket_name = "bucket1" # The AWS S3 bucket name for file storage
3 changes: 3 additions & 0 deletions config/development.toml
Original file line number Diff line number Diff line change
Expand Up @@ -542,3 +542,6 @@ client_id = ""
client_secret = ""
partner_id = ""
enabled = true

[file_storage]
file_storage_backend = "file_system"
3 changes: 3 additions & 0 deletions config/docker_compose.toml
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,6 @@ enabled = true

[events]
source = "logs"

[file_storage]
file_storage_backend = "file_system"
51 changes: 32 additions & 19 deletions crates/common_utils/src/fs_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//!
use std::{
fmt::{Display, Formatter},
fs::{remove_file, File},
io::{Read, Write},
path::PathBuf,
Expand All @@ -15,12 +14,14 @@ use crate::errors::CustomResult;

/// Constructs the file path for a given file key within the file system.
/// The file path is generated based on the workspace path and the provided file key.
pub fn get_file_path(file_key: String) -> PathBuf {
fn get_file_path(file_key: impl AsRef<str>) -> PathBuf {
let mut file_path = PathBuf::new();
#[cfg(feature = "logs")]
file_path.push(router_env::env::workspace_path());
#[cfg(not(feature = "logs"))]
file_path.push(std::env::current_dir());
file_path.push("files");
file_path.push(file_key);
file_path.push(file_key.as_ref());
file_path
}

Expand All @@ -32,56 +33,68 @@ impl FileSystem {
/// Saves the provided file data to the file system under the specified file key.
pub fn save_file_to_fs(
&self,
file_key: String,
file_key: impl AsRef<str>,
file_data: Vec<u8>,
) -> CustomResult<(), FileSystemStorageError> {
let file_path = get_file_path(file_key);
let mut file = File::create(file_path)
.into_report()
.change_context(FileSystemStorageError("Failed to create file"))?;
.change_context(FileSystemStorageError::CreateFailure)?;
file.write_all(&file_data)
.into_report()
.change_context(FileSystemStorageError("Failed while writing into file"))?;
.change_context(FileSystemStorageError::WriteFailure)?;
Ok(())
}

/// Deletes the file associated with the specified file key from the file system.
pub fn delete_file_from_fs(
&self,
file_key: String,
file_key: impl AsRef<str>,
) -> CustomResult<(), FileSystemStorageError> {
let file_path = get_file_path(file_key);
remove_file(file_path)
.into_report()
.change_context(FileSystemStorageError("Failed while deleting the file"))?;
.change_context(FileSystemStorageError::DeleteFailure)?;
Ok(())
}

/// Retrieves the file content associated with the specified file key from the file system.
pub fn retrieve_file_from_fs(
&self,
file_key: String,
file_key: impl AsRef<str>,
) -> CustomResult<Vec<u8>, FileSystemStorageError> {
let mut received_data: Vec<u8> = Vec::new();
let file_path = get_file_path(file_key);
let mut file = File::open(file_path)
.into_report()
.change_context(FileSystemStorageError("Failed while opening the file"))?;
.change_context(FileSystemStorageError::FileOpenFailure)?;
file.read_to_end(&mut received_data)
.into_report()
.change_context(FileSystemStorageError("Failed while reading the file"))?;
.change_context(FileSystemStorageError::ReadFailure)?;
Ok(received_data)
}
}

/// Represents an error that can occur during file system storage operations locally.
#[derive(Debug)]
pub struct FileSystemStorageError(&'static str);
/// Represents an error that can occur during local file system storage operations.
#[derive(Debug, thiserror::Error)]
pub enum FileSystemStorageError {
/// Error indicating opening a file failed
#[error("Failed while opening the file")]
FileOpenFailure,

impl std::error::Error for FileSystemStorageError {}
/// Error indicating file creation failed.
#[error("Failed to create file")]
CreateFailure,

impl Display for FileSystemStorageError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "Local file system storage error: {}", self.0)
}
/// Error indicating reading a file failed.
#[error("Failed while reading the file")]
ReadFailure,

/// Error indicating writing to a file failed.
#[error("Failed while writing into file")]
WriteFailure,

/// Error indicating file deletion failed.
#[error("Failed while deleting the file")]
DeleteFailure,
}
2 changes: 0 additions & 2 deletions crates/external_services/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ aws-smithy-client = "0.55.3"
base64 = "0.21.2"
dyn-clone = "1.0.11"
error-stack = "0.3.1"
futures = "0.3.28"
once_cell = "1.18.0"
serde = { version = "1.0.193", features = ["derive"] }
thiserror = "1.0.40"
Expand All @@ -35,4 +34,3 @@ hyper = "0.14.26"
common_utils = { version = "0.1.0", path = "../common_utils" }
masking = { version = "0.1.0", path = "../masking" }
router_env = { version = "0.1.0", path = "../router_env", features = ["log_extra_implicit_fields", "log_custom_entries_to_extra"] }
storage_impl = { version = "0.1.0", path = "../storage_impl", default-features = false }
44 changes: 26 additions & 18 deletions crates/external_services/src/file_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
//! Module for managing file storage operations with support for multiple storage schemes.
//!
use std::fmt::{Display, Formatter};

use common_utils::{
errors::{CustomResult, FileStorageError},
fs_utils,
};
use error_stack::ResultExt;
use storage_impl::errors::ApplicationError;

#[cfg(feature = "aws_s3")]
use crate::file_storage::aws_s3::{AwsFileStorageClient, AwsFileStorageConfig};
Expand All @@ -18,7 +19,7 @@ pub mod aws_s3;

/// Enum representing different file storage configurations, allowing for multiple storage schemes.
#[derive(Debug, Clone, Default, serde::Deserialize)]
#[serde(tag = "file_storage_scheme")]
#[serde(tag = "file_storage_backend")]
#[serde(rename_all = "snake_case")]
pub enum FileStorageConfig {
/// AWS S3 storage configuration.
Expand All @@ -34,24 +35,13 @@ pub enum FileStorageConfig {

impl FileStorageConfig {
/// Validates the file storage configuration.
pub fn validate(&self) -> Result<(), ApplicationError> {
pub fn validate(&self) -> Result<(), InvalidFileStorageConfig> {
match self {
#[cfg(feature = "aws_s3")]
Self::AwsS3 { aws_s3 } => aws_s3.validate(),
Self::FileSystem => Ok(()),
}
}

/// Retrieves the appropriate file storage client based on the configuration.
pub async fn get_file_storage_client(&self) -> FileStorageScheme {
match self {
#[cfg(feature = "aws_s3")]
Self::AwsS3 { aws_s3 } => FileStorageScheme::AwsS3 {
client: aws_s3.get_aws_file_storage_client().await,
},
Self::FileSystem => FileStorageScheme::FileSystem(fs_utils::FileSystem),
}
}
}

/// Enum representing different file storage clients.
Expand All @@ -61,7 +51,7 @@ pub enum FileStorageScheme {
#[cfg(feature = "aws_s3")]
AwsS3 {
/// AWS S3 file storage client.
client: &'static AwsFileStorageClient,
client: AwsFileStorageClient,
},
/// Local file system storage client.
FileSystem(fs_utils::FileSystem),
Expand All @@ -71,7 +61,7 @@ impl FileStorageScheme {
/// Uploads a file to the selected storage scheme.
pub async fn upload_file(
&self,
file_key: String,
file_key: impl AsRef<str>,
file: Vec<u8>,
) -> CustomResult<(), FileStorageError> {
match self {
Expand All @@ -87,7 +77,10 @@ impl FileStorageScheme {
}

/// Deletes a file from the selected storage scheme.
pub async fn delete_file(&self, file_key: String) -> CustomResult<(), FileStorageError> {
pub async fn delete_file(
&self,
file_key: impl AsRef<str>,
) -> CustomResult<(), FileStorageError> {
match self {
#[cfg(feature = "aws_s3")]
Self::AwsS3 { client } => client
Expand All @@ -101,7 +94,10 @@ impl FileStorageScheme {
}

/// Retrieves a file from the selected storage scheme.
pub async fn retrieve_file(&self, file_key: String) -> CustomResult<Vec<u8>, FileStorageError> {
pub async fn retrieve_file(
&self,
file_key: impl AsRef<str>,
) -> CustomResult<Vec<u8>, FileStorageError> {
match self {
#[cfg(feature = "aws_s3")]
Self::AwsS3 { client } => client
Expand All @@ -114,3 +110,15 @@ impl FileStorageScheme {
}
}
}

/// Error thrown when the file storage config is invalid
#[derive(Debug, Clone)]
pub struct InvalidFileStorageConfig(&'static str);

impl std::error::Error for InvalidFileStorageConfig {}

impl Display for InvalidFileStorageConfig {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "file_storage: {}", self.0)
}
}
Loading

0 comments on commit e5d5905

Please sign in to comment.