Skip to content

Commit

Permalink
refactor: enhance Pastebin share method with file and API key validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Kremilly committed Dec 12, 2024
1 parent 348694f commit f93d2b3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
37 changes: 28 additions & 9 deletions src/plugins/pastebin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,41 @@ impl Pastebin {
}
}

pub async fn share(&self) -> Result<(), Box<dyn Error>> {
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<dyn Error>> {
if !FileUtils::exists(&self.file) {
ShareAlerts::error("File does not exist");
return Ok(());
}

Ok(())
}

fn api_key_exists(&self) -> Result<(), Box<dyn Error>> {
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<dyn Error>> {
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);
Expand Down
4 changes: 4 additions & 0 deletions src/utils/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ impl FileUtils {
.unwrap_or_default()
}

pub fn exists(file_path: &str) -> bool {
fs::metadata(file_path).is_ok()
}

}

0 comments on commit f93d2b3

Please sign in to comment.