From de8f62f831402a997f4eba5f9a69b403ee2f004d Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Thu, 15 Jun 2023 17:42:31 +1000 Subject: [PATCH] Don't fail silently when an empty csv is imported --- ftl/core/importing.ftl | 1 + rslib/src/import_export/mod.rs | 2 ++ rslib/src/import_export/text/csv/metadata.rs | 7 ++++++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/ftl/core/importing.ftl b/ftl/core/importing.ftl index 6f70629e3..8fb270bf6 100644 --- a/ftl/core/importing.ftl +++ b/ftl/core/importing.ftl @@ -115,6 +115,7 @@ importing-cards-added = [one] { $count } card added. *[other] { $count } cards added. } +importing-file-empty = The file you selected is empty. ## NO NEED TO TRANSLATE. This text is no longer used by Anki, and will be removed in the future. diff --git a/rslib/src/import_export/mod.rs b/rslib/src/import_export/mod.rs index 8f86e070c..3b070def3 100644 --- a/rslib/src/import_export/mod.rs +++ b/rslib/src/import_export/mod.rs @@ -137,6 +137,7 @@ pub enum ImportError { TooNew, MediaImportFailed { info: String }, NoFieldColumn, + EmptyFile, } impl ImportError { @@ -148,6 +149,7 @@ impl ImportError { tr.importing_failed_to_import_media_file(info) } ImportError::NoFieldColumn => tr.importing_file_must_contain_field_column(), + ImportError::EmptyFile => tr.importing_file_empty(), } .into() } diff --git a/rslib/src/import_export/text/csv/metadata.rs b/rslib/src/import_export/text/csv/metadata.rs index ef9c34f52..bc465c592 100644 --- a/rslib/src/import_export/text/csv/metadata.rs +++ b/rslib/src/import_export/text/csv/metadata.rs @@ -46,7 +46,12 @@ impl Collection { is_html: Option, ) -> Result { let mut reader = open_file(path)?; - self.get_reader_metadata(&mut reader, delimiter, notetype_id, deck_id, is_html) + let meta = + self.get_reader_metadata(&mut reader, delimiter, notetype_id, deck_id, is_html)?; + if meta.preview.is_empty() { + return Err(ImportError::EmptyFile.into()); + } + Ok(meta) } fn get_reader_metadata(