Skip to content

Commit

Permalink
ui: remove file when an error occurs during text export...
Browse files Browse the repository at this point in the history
And check TOC availability before hand.
  • Loading branch information
fengalin committed Sep 11, 2020
1 parent ab70b25 commit 4a9374c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
6 changes: 0 additions & 6 deletions metadata/src/cue_sheet_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ macro_rules! write_fmt(

impl Writer for CueSheetFormat {
fn write(&self, info: &MediaInfo, destination: &mut dyn Write) -> Result<(), String> {
if info.toc.is_none() {
let msg = gettext("The table of contents is empty");
error!("{}", msg);
return Err(msg);
}

let media_title = info.media_title();
if let Some(title) = &media_title {
write_fmt!(destination, "TITLE \"{}\"\n", title);
Expand Down
6 changes: 0 additions & 6 deletions metadata/src/mkvmerge_text_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,6 @@ macro_rules! write_fmt(

impl Writer for MKVMergeTextFormat {
fn write(&self, info: &MediaInfo, destination: &mut dyn Write) -> Result<(), String> {
if info.toc.is_none() {
let msg = gettext("The table of contents is empty");
error!("{}", msg);
return Err(msg);
}

let mut index = 0;
let mut toc_visitor = TocVisitor::new(info.toc.as_ref().unwrap());
while let Some(chapter) = toc_visitor.next_chapter() {
Expand Down
30 changes: 19 additions & 11 deletions ui/src/export_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use futures::channel::mpsc as async_mpsc;

use gettextrs::gettext;
use gtk::prelude::*;
use log::warn;
use log::{error, warn};

use std::{
fs::File,
fs,
path::Path,
rc::Rc,
sync::{Arc, RwLock},
Expand Down Expand Up @@ -164,18 +164,26 @@ impl MediaProcessor for ExportControllerImpl {
.format;
let res = match format {
Format::MKVMergeText | Format::CueSheet => {
self.export_file_info = None;

let src_info = self.src_info.as_ref().unwrap().read().unwrap();
if src_info.toc.is_none() {
let msg = gettext("The table of contents is empty");
error!("{}", msg);
return Err(msg);
}

// export toc as a standalone file
File::create(&path)
fs::File::create(&path)
.map_err(|_| gettext("Failed to create the file for the table of contents"))
.and_then(|mut output_file| {
let res = {
let src_info = self.src_info.as_ref().unwrap().read().unwrap();
metadata::Factory::writer(format).write(&src_info, &mut output_file)
};
res.map(|_| {
self.export_file_info = None;
ProcessingState::DoneWithCurrent
})
metadata::Factory::writer(format)
.write(&src_info, &mut output_file)
.map(|_| ProcessingState::DoneWithCurrent)
.map_err(|msg| {
let _ = fs::remove_file(&path);
msg
})
})
}
Format::Matroska => {
Expand Down

0 comments on commit 4a9374c

Please sign in to comment.