From f93d2b3cddb97605a6ac9190be90357d7e16e76c Mon Sep 17 00:00:00 2001 From: Kremilly Date: Thu, 12 Dec 2024 18:04:52 -0300 Subject: [PATCH] refactor: enhance Pastebin share method with file and API key validation --- src/plugins/pastebin.rs | 37 ++++++++++++++++++++++++++++--------- src/utils/file.rs | 4 ++++ 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/plugins/pastebin.rs b/src/plugins/pastebin.rs index dad1bdb..ba5c214 100644 --- a/src/plugins/pastebin.rs +++ b/src/plugins/pastebin.rs @@ -27,22 +27,41 @@ impl Pastebin { } } - pub async fn share(&self) -> Result<(), Box> { - let ext = FileUtils::extension(&self.file); + fn privacy(&self) -> String { + match self.privacy.as_str() { + "public" => "0", + "unlisted" => "1", + "private" => "2", + _ => "1", + }.to_string() + } + + fn file_exists(&self) -> Result<(), Box> { + if !FileUtils::exists(&self.file) { + ShareAlerts::error("File does not exist"); + return Ok(()); + } + + Ok(()) + } + fn api_key_exists(&self) -> Result<(), Box> { if self.api_key.trim().is_empty() { ShareAlerts::error("API key is missing or empty"); return Ok(()); } + Ok(()) + } + + pub async fn share(&self) -> Result<(), Box> { + let ext = FileUtils::extension(&self.file); + + self.file_exists()?; + self.api_key_exists()?; + if ["sql", "txt", "csv", "json", "html"].iter().any(|&e| e == ext) { - let privacy = match self.privacy.as_str() { - "public" => "0", - "unlisted" => "1", - "private" => "2", - _ => "1", - }.to_string(); - + let privacy = &self.privacy(); let api_option = "paste".to_string(); let name = format!("{}: {}", Global::APP_NAME, &self.file); let content = FileUtils::content(&self.file); diff --git a/src/utils/file.rs b/src/utils/file.rs index 11dbd9b..0f4bac6 100644 --- a/src/utils/file.rs +++ b/src/utils/file.rs @@ -28,4 +28,8 @@ impl FileUtils { .unwrap_or_default() } + pub fn exists(file_path: &str) -> bool { + fs::metadata(file_path).is_ok() + } + }