Rewrite set_external_links()

This commit is contained in:
Matthias Beyer 2016-04-16 15:34:32 +02:00
parent f482c2564b
commit 22a64dbc30

View file

@ -171,45 +171,42 @@ impl ExternalLinker for Entry {
debug!("Hash = '{:?}'", hash); debug!("Hash = '{:?}'", hash);
debug!("StoreId = '{:?}'", file_id); debug!("StoreId = '{:?}'", file_id);
let mut file = { // retrieve the file from the store, which implicitely creates the entry if it does not
if let Ok(mut file) = store.retrieve(file_id.clone()) { // retrieve the file from the store // exist
debug!("Woha, there is already a file for this link: '{:?}'", link); let file = store.retrieve(file_id.clone());
file if file.is_err() {
} else { // or debug!("Failed to create or retrieve an file for this link '{:?}'", link);
debug!("Generating file for link = '{:?}' on id = {:?}", link, file_id); return Err(LE::new(LEK::StoreWriteError, Some(Box::new(file.err().unwrap()))));
let res = store.create(file_id) // create it }
.and_then(|mut file| { let mut file = file.unwrap();
{
debug!("Generating header content!"); debug!("Generating header content!");
{
let mut hdr = file.deref_mut().get_header_mut(); let mut hdr = file.deref_mut().get_header_mut();
// Write the URI into the header let mut table = match hdr.read("imag.content") {
match hdr.set("imag.content", Value::Table(BTreeMap::new())) { Ok(Some(Value::Table(table))) => table,
Ok(_) => { Ok(Some(_)) => {
let v = Value::String(link.serialize()); warn!("There is a value at 'imag.content' which is not a table.");
debug!("setting URL = '{:?}", v); warn!("Going to override this value");
hdr.set("imag.content.uri", v); BTreeMap::new()
Ok(())
}, },
Err(e) => { Ok(None) => BTreeMap::new(),
debug!("Failed to generate a table in header at 'imag.content'"); Err(e) => return Err(LE::new(LEK::StoreWriteError, Some(Box::new(e)))),
Err(e)
},
}
}.map(|_| file)
})
.map_err(|e| LE::new(LEK::StoreWriteError, Some(Box::new(e))));
// And if that fails we can error
if let Err(e) = res {
debug!("Failed to create or retrieve an file for this link '{:?}'", link);
return Err(e);
}
debug!("Success creating or retrieving an file for this link '{:?}'", link);
res.unwrap()
}
}; };
let v = Value::String(link.serialize());
debug!("setting URL = '{:?}", v);
table.insert(String::from("url"), v);
if let Err(e) = hdr.set("imag.content", Value::Table(table)) {
return Err(LE::new(LEK::StoreWriteError, Some(Box::new(e))));
} else {
debug!("Setting URL worked");
}
}
// then add an internal link to the new file or return an error if this fails // then add an internal link to the new file or return an error if this fails
if let Err(e) = self.add_internal_link(file.deref_mut()) { if let Err(e) = self.add_internal_link(file.deref_mut()) {
debug!("Error adding internal link"); debug!("Error adding internal link");