From da735aa00b4c867c58b3028aeaac2f832a857e16 Mon Sep 17 00:00:00 2001 From: Chris Cochrun Date: Mon, 14 Oct 2024 10:58:21 -0500 Subject: [PATCH] Error handling..... grrr --- src/rust/core/file.rs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/rust/core/file.rs b/src/rust/core/file.rs index 8a9aa9e..e3d5c8f 100644 --- a/src/rust/core/file.rs +++ b/src/rust/core/file.rs @@ -1,8 +1,8 @@ use tar::{Archive, Builder}; use tracing::error; use zstd::Encoder; -use std::{fs::{self, File}, future::Future, iter, path::{Path, PathBuf}}; -use color_eyre::eyre::{eyre, Result}; +use std::{fs::{self, File}, iter, path::{Path, PathBuf}}; +use color_eyre::eyre::{eyre, Context, Result}; use serde_json::Value; use sqlx::{query, query_as, FromRow, SqliteConnection}; use crate::{images::{get_image_from_db, Image}, kinds::ServiceItemKind, model::get_db, presentations::{get_presentation_from_db, PresKind, Presentation}, service_items::ServiceItem, slides::Background, songs::{get_song_from_db, Song}, videos::{get_video_from_db, Video}}; @@ -66,17 +66,23 @@ async fn store_service_items(items: &Vec, db: &mut SqliteConnection }; if let Some(file) = audio { let audio_file = temp_dir.join(file.file_name().expect("Audio file couldn't be added to temp_dir")); - match fs::File::create(&audio_file) { - Ok(_) => Ok(fs::copy(file, &audio_file)?), - Err(e) => Err(eyre!("Couldn't create audio file: {e}")), - }?; + if let Ok(file) = file.strip_prefix("file://") { + fs::File::create(&audio_file).wrap_err("Couldn't create audio file")?; + fs::copy(file, audio_file).wrap_err("Audio file could not be copied, the source file doesn't exist not be found"); + } else { + fs::File::create(&audio_file).wrap_err("Couldn't create audio file")?; + fs::copy(file, audio_file).wrap_err("Audio file could not be copied, the source file doesn't exist not be found"); + } }; if let Some(file) = background { let background_file = temp_dir.join(file.path.file_name().expect("Background file couldn't be added to temp_dir")); - match fs::File::create(&background_file) { - Ok(_) => Ok(fs::copy(file.path, &background_file)?), - Err(e) => Err(eyre!("Couldn't create background file: {e}")), - }?; + if let Ok(file) = file.path.strip_prefix("file://") { + fs::File::create(&background_file).wrap_err("Couldn't create background file")?; + fs::copy(file, background_file).wrap_err("Background file could not be copied, the source file doesn't exist not be found"); + } else { + fs::File::create(&background_file).wrap_err("Couldn't create background file")?; + fs::copy(file.path, background_file).wrap_err("Background file could not be copied, the source file doesn't exist not be found"); + } } } Ok(()) @@ -157,7 +163,7 @@ mod test { use fs::canonicalize; use sqlx::Connection; - use pretty_assertions::{assert_eq, assert_ne}; + use pretty_assertions::assert_eq; use tracing::debug; use super::*;