Error handling..... grrr

This commit is contained in:
Chris Cochrun 2024-10-14 10:58:21 -05:00
parent 6fe0bf8fe2
commit da735aa00b

View file

@ -1,8 +1,8 @@
use tar::{Archive, Builder}; use tar::{Archive, Builder};
use tracing::error; use tracing::error;
use zstd::Encoder; use zstd::Encoder;
use std::{fs::{self, File}, future::Future, iter, path::{Path, PathBuf}}; use std::{fs::{self, File}, iter, path::{Path, PathBuf}};
use color_eyre::eyre::{eyre, Result}; use color_eyre::eyre::{eyre, Context, Result};
use serde_json::Value; use serde_json::Value;
use sqlx::{query, query_as, FromRow, SqliteConnection}; 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}}; 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<ServiceItem>, db: &mut SqliteConnection
}; };
if let Some(file) = audio { if let Some(file) = audio {
let audio_file = temp_dir.join(file.file_name().expect("Audio file couldn't be added to temp_dir")); 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) { if let Ok(file) = file.strip_prefix("file://") {
Ok(_) => Ok(fs::copy(file, &audio_file)?), fs::File::create(&audio_file).wrap_err("Couldn't create audio file")?;
Err(e) => Err(eyre!("Couldn't create audio file: {e}")), 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 { 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")); 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) { if let Ok(file) = file.path.strip_prefix("file://") {
Ok(_) => Ok(fs::copy(file.path, &background_file)?), fs::File::create(&background_file).wrap_err("Couldn't create background file")?;
Err(e) => Err(eyre!("Couldn't create background file: {e}")), 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(()) Ok(())
@ -157,7 +163,7 @@ mod test {
use fs::canonicalize; use fs::canonicalize;
use sqlx::Connection; use sqlx::Connection;
use pretty_assertions::{assert_eq, assert_ne}; use pretty_assertions::assert_eq;
use tracing::debug; use tracing::debug;
use super::*; use super::*;