2016-04-08 21:35:04 +00:00
|
|
|
/// External linking is a complex implementation to be able to serve a clean and easy-to-use
|
|
|
|
/// interface.
|
|
|
|
///
|
|
|
|
/// Internally, there are no such things as "external links" (plural). Each Entry in the store can
|
|
|
|
/// only have _one_ external link.
|
|
|
|
///
|
|
|
|
/// This library does the following therefor: It allows you to have several external links with one
|
|
|
|
/// entry, which are internally one file in the store for each link, linked with "internal
|
|
|
|
/// linking".
|
|
|
|
///
|
|
|
|
/// This helps us greatly with deduplication of URLs.
|
|
|
|
///
|
|
|
|
|
2016-04-09 13:49:50 +00:00
|
|
|
use std::ops::DerefMut;
|
|
|
|
use std::collections::BTreeMap;
|
2016-02-21 13:28:55 +00:00
|
|
|
|
2016-02-15 12:28:43 +00:00
|
|
|
use libimagstore::store::Entry;
|
2016-04-08 21:35:04 +00:00
|
|
|
use libimagstore::store::FileLockEntry;
|
|
|
|
use libimagstore::store::Store;
|
|
|
|
use libimagstore::storeid::StoreId;
|
2016-04-09 13:49:50 +00:00
|
|
|
use libimagstore::storeid::IntoStoreId;
|
2016-08-03 09:10:56 +00:00
|
|
|
use libimagutil::debug_result::*;
|
2016-02-03 14:47:14 +00:00
|
|
|
|
2016-04-08 21:35:04 +00:00
|
|
|
use error::LinkError as LE;
|
|
|
|
use error::LinkErrorKind as LEK;
|
2016-08-03 09:10:56 +00:00
|
|
|
use error::MapErrInto;
|
2016-02-03 14:47:14 +00:00
|
|
|
use result::Result;
|
2016-04-08 21:35:04 +00:00
|
|
|
use internal::InternalLinker;
|
2016-04-09 13:49:50 +00:00
|
|
|
use module_path::ModuleEntryPath;
|
2016-02-03 14:47:14 +00:00
|
|
|
|
2016-02-03 18:59:22 +00:00
|
|
|
use toml::Value;
|
2016-02-23 10:50:42 +00:00
|
|
|
use url::Url;
|
2016-04-16 12:26:53 +00:00
|
|
|
use crypto::sha1::Sha1;
|
|
|
|
use crypto::digest::Digest;
|
2016-02-03 18:59:22 +00:00
|
|
|
|
2016-05-03 21:10:32 +00:00
|
|
|
/// "Link" Type, just an abstraction over `FileLockEntry` to have some convenience internally.
|
2016-05-26 16:18:21 +00:00
|
|
|
pub struct Link<'a> {
|
2016-04-08 21:35:04 +00:00
|
|
|
link: FileLockEntry<'a>
|
2016-02-21 13:28:55 +00:00
|
|
|
}
|
|
|
|
|
2016-04-08 21:35:04 +00:00
|
|
|
impl<'a> Link<'a> {
|
2016-02-21 13:28:55 +00:00
|
|
|
|
2016-04-08 21:35:04 +00:00
|
|
|
pub fn new(fle: FileLockEntry<'a>) -> Link<'a> {
|
|
|
|
Link { link: fle }
|
2016-02-21 13:28:55 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 21:10:32 +00:00
|
|
|
/// Get a link Url object from a `FileLockEntry`, ignore errors.
|
2016-04-08 21:35:04 +00:00
|
|
|
fn get_link_uri_from_filelockentry(file: &FileLockEntry<'a>) -> Option<Url> {
|
2016-04-21 11:19:37 +00:00
|
|
|
file.get_header()
|
2016-09-05 16:49:12 +00:00
|
|
|
.read("imag.content.url")
|
2016-04-08 21:35:04 +00:00
|
|
|
.ok()
|
2016-06-11 04:53:46 +00:00
|
|
|
.and_then(|opt| match opt {
|
2016-09-05 16:51:35 +00:00
|
|
|
Some(Value::String(s)) => {
|
|
|
|
debug!("Found url, parsing: {:?}", s);
|
|
|
|
Url::parse(&s[..]).ok()
|
|
|
|
},
|
2016-06-11 04:53:46 +00:00
|
|
|
_ => None
|
2016-04-08 21:35:04 +00:00
|
|
|
})
|
2016-02-21 13:28:55 +00:00
|
|
|
}
|
|
|
|
|
2016-04-08 21:35:04 +00:00
|
|
|
pub fn get_url(&self) -> Result<Option<Url>> {
|
|
|
|
let opt = self.link
|
|
|
|
.get_header()
|
2016-09-05 16:49:12 +00:00
|
|
|
.read("imag.content.url");
|
2016-04-08 21:35:04 +00:00
|
|
|
|
|
|
|
match opt {
|
|
|
|
Ok(Some(Value::String(s))) => {
|
|
|
|
Url::parse(&s[..])
|
2016-05-03 21:10:32 +00:00
|
|
|
.map(Some)
|
2016-04-08 21:35:04 +00:00
|
|
|
.map_err(|e| LE::new(LEK::EntryHeaderReadError, Some(Box::new(e))))
|
|
|
|
},
|
|
|
|
Ok(None) => Ok(None),
|
|
|
|
_ => Err(LE::new(LEK::EntryHeaderReadError, None))
|
|
|
|
}
|
2016-02-21 13:28:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-04-08 21:35:04 +00:00
|
|
|
pub trait ExternalLinker : InternalLinker {
|
2016-02-21 13:28:55 +00:00
|
|
|
|
2016-04-08 21:35:04 +00:00
|
|
|
/// Get the external links from the implementor object
|
2016-04-09 13:15:13 +00:00
|
|
|
fn get_external_links(&self, store: &Store) -> Result<Vec<Url>>;
|
2016-02-21 13:28:55 +00:00
|
|
|
|
2016-04-08 21:35:04 +00:00
|
|
|
/// Set the external links for the implementor object
|
2016-04-09 13:49:50 +00:00
|
|
|
fn set_external_links(&mut self, store: &Store, links: Vec<Url>) -> Result<()>;
|
2016-02-21 13:28:55 +00:00
|
|
|
|
2016-04-08 21:35:04 +00:00
|
|
|
/// Add an external link to the implementor object
|
2016-04-10 15:51:17 +00:00
|
|
|
fn add_external_link(&mut self, store: &Store, link: Url) -> Result<()>;
|
2016-02-03 18:59:22 +00:00
|
|
|
|
2016-04-08 21:35:04 +00:00
|
|
|
/// Remove an external link from the implementor object
|
2016-04-10 16:43:02 +00:00
|
|
|
fn remove_external_link(&mut self, store: &Store, link: Url) -> Result<()>;
|
2016-02-03 18:59:22 +00:00
|
|
|
|
2016-02-03 14:47:14 +00:00
|
|
|
}
|
|
|
|
|
2016-04-09 13:15:13 +00:00
|
|
|
/// Check whether the StoreId starts with `/link/external/`
|
2016-05-12 15:11:58 +00:00
|
|
|
pub fn is_external_link_storeid(id: &StoreId) -> bool {
|
2016-09-05 16:52:42 +00:00
|
|
|
debug!("Checking whether this is a 'links/external/': '{:?}'", id);
|
|
|
|
id.local().starts_with("links/external")
|
2016-04-09 13:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_external_link_from_file(entry: &FileLockEntry) -> Result<Url> {
|
|
|
|
Link::get_link_uri_from_filelockentry(entry) // TODO: Do not hide error by using this function
|
|
|
|
.ok_or(LE::new(LEK::StoreReadError, None))
|
|
|
|
}
|
|
|
|
|
2016-05-03 21:10:32 +00:00
|
|
|
/// Implement `ExternalLinker` for `Entry`, hiding the fact that there is no such thing as an external
|
2016-04-08 21:35:04 +00:00
|
|
|
/// link in an entry, but internal links to other entries which serve as external links, as one
|
|
|
|
/// entry in the store can only have one external link.
|
|
|
|
impl ExternalLinker for Entry {
|
2016-02-07 03:40:43 +00:00
|
|
|
|
2016-04-08 21:35:04 +00:00
|
|
|
/// Get the external links from the implementor object
|
2016-04-09 13:15:13 +00:00
|
|
|
fn get_external_links(&self, store: &Store) -> Result<Vec<Url>> {
|
2016-04-08 21:35:04 +00:00
|
|
|
// Iterate through all internal links and filter for FileLockEntries which live in
|
|
|
|
// /link/external/<SHA> -> load these files and get the external link from their headers,
|
|
|
|
// put them into the return vector.
|
2016-04-09 13:15:13 +00:00
|
|
|
self.get_internal_links()
|
|
|
|
.map(|vect| {
|
2016-04-12 20:27:38 +00:00
|
|
|
debug!("Getting external links");
|
2016-04-09 13:15:13 +00:00
|
|
|
vect.into_iter()
|
2016-05-12 15:11:58 +00:00
|
|
|
.filter(is_external_link_storeid)
|
2016-04-09 13:15:13 +00:00
|
|
|
.map(|id| {
|
2016-04-12 20:27:38 +00:00
|
|
|
debug!("Retrieving entry for id: '{:?}'", id);
|
|
|
|
match store.retrieve(id.clone()) {
|
2016-09-05 16:51:35 +00:00
|
|
|
Ok(f) => {
|
|
|
|
debug!("Store::retrieve({:?}) succeeded", id);
|
|
|
|
debug!("getting external link from file now");
|
|
|
|
get_external_link_from_file(&f)
|
|
|
|
.map_err(|e| { debug!("URL -> Err = {:?}", e); e })
|
|
|
|
},
|
2016-04-12 20:27:38 +00:00
|
|
|
Err(e) => {
|
|
|
|
debug!("Retrieving entry for id: '{:?}' failed", id);
|
|
|
|
Err(LE::new(LEK::StoreReadError, Some(Box::new(e))))
|
|
|
|
}
|
2016-04-09 13:15:13 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.filter_map(|x| x.ok()) // TODO: Do not ignore error here
|
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
.map_err(|e| LE::new(LEK::StoreReadError, Some(Box::new(e))))
|
2016-02-07 03:40:43 +00:00
|
|
|
}
|
|
|
|
|
2016-04-08 21:35:04 +00:00
|
|
|
/// Set the external links for the implementor object
|
2016-04-09 13:49:50 +00:00
|
|
|
fn set_external_links(&mut self, store: &Store, links: Vec<Url>) -> Result<()> {
|
2016-04-08 21:35:04 +00:00
|
|
|
// Take all the links, generate a SHA sum out of each one, filter out the already existing
|
|
|
|
// store entries and store the other URIs in the header of one FileLockEntry each, in
|
|
|
|
// the path /link/external/<SHA of the URL>
|
2016-04-09 13:49:50 +00:00
|
|
|
|
2016-04-12 20:27:38 +00:00
|
|
|
debug!("Iterating {} links = {:?}", links.len(), links);
|
2016-04-09 13:49:50 +00:00
|
|
|
for link in links { // for all links
|
2016-04-16 12:26:53 +00:00
|
|
|
let hash = {
|
|
|
|
let mut s = Sha1::new();
|
2016-05-12 14:40:06 +00:00
|
|
|
s.input_str(&link.as_str()[..]);
|
2016-04-16 12:26:53 +00:00
|
|
|
s.result_str()
|
|
|
|
};
|
2016-08-25 16:49:26 +00:00
|
|
|
let file_id = try!(
|
|
|
|
ModuleEntryPath::new(format!("external/{}", hash)).into_storeid()
|
|
|
|
.map_err_into(LEK::StoreWriteError)
|
|
|
|
.map_dbg_err(|_| {
|
|
|
|
format!("Failed to build StoreId for this hash '{:?}'", hash)
|
|
|
|
})
|
|
|
|
);
|
2016-04-09 13:49:50 +00:00
|
|
|
|
2016-04-12 20:27:38 +00:00
|
|
|
debug!("Link = '{:?}'", link);
|
|
|
|
debug!("Hash = '{:?}'", hash);
|
|
|
|
debug!("StoreId = '{:?}'", file_id);
|
|
|
|
|
2016-04-16 13:34:32 +00:00
|
|
|
// retrieve the file from the store, which implicitely creates the entry if it does not
|
|
|
|
// exist
|
2016-08-03 09:10:56 +00:00
|
|
|
let mut file = try!(store
|
|
|
|
.retrieve(file_id.clone())
|
|
|
|
.map_err_into(LEK::StoreWriteError)
|
|
|
|
.map_dbg_err(|_| {
|
|
|
|
format!("Failed to create or retrieve an file for this link '{:?}'", link)
|
|
|
|
}));
|
2016-04-16 13:34:32 +00:00
|
|
|
|
|
|
|
debug!("Generating header content!");
|
|
|
|
{
|
|
|
|
let mut hdr = file.deref_mut().get_header_mut();
|
|
|
|
|
|
|
|
let mut table = match hdr.read("imag.content") {
|
|
|
|
Ok(Some(Value::Table(table))) => table,
|
|
|
|
Ok(Some(_)) => {
|
|
|
|
warn!("There is a value at 'imag.content' which is not a table.");
|
|
|
|
warn!("Going to override this value");
|
|
|
|
BTreeMap::new()
|
|
|
|
},
|
|
|
|
Ok(None) => BTreeMap::new(),
|
|
|
|
Err(e) => return Err(LE::new(LEK::StoreWriteError, Some(Box::new(e)))),
|
|
|
|
};
|
|
|
|
|
2016-05-12 14:40:06 +00:00
|
|
|
let v = Value::String(link.into_string());
|
2016-04-16 13:34:32 +00:00
|
|
|
|
|
|
|
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");
|
2016-04-09 13:49:50 +00:00
|
|
|
}
|
2016-04-16 13:34:32 +00:00
|
|
|
}
|
2016-04-09 13:49:50 +00:00
|
|
|
|
|
|
|
// 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()) {
|
2016-04-12 20:27:38 +00:00
|
|
|
debug!("Error adding internal link");
|
2016-04-09 13:49:50 +00:00
|
|
|
return Err(LE::new(LEK::StoreWriteError, Some(Box::new(e))));
|
|
|
|
}
|
|
|
|
}
|
2016-04-12 20:27:38 +00:00
|
|
|
debug!("Ready iterating");
|
2016-04-09 13:49:50 +00:00
|
|
|
Ok(())
|
2016-02-07 03:40:43 +00:00
|
|
|
}
|
2016-02-15 12:22:10 +00:00
|
|
|
|
2016-04-08 21:35:04 +00:00
|
|
|
/// Add an external link to the implementor object
|
2016-04-10 15:51:17 +00:00
|
|
|
fn add_external_link(&mut self, store: &Store, link: Url) -> Result<()> {
|
2016-04-08 21:35:04 +00:00
|
|
|
// get external links, add this one, save them
|
2016-04-12 20:27:38 +00:00
|
|
|
debug!("Getting links");
|
2016-04-10 15:51:17 +00:00
|
|
|
self.get_external_links(store)
|
|
|
|
.and_then(|mut links| {
|
2016-04-12 20:27:38 +00:00
|
|
|
debug!("Adding link = '{:?}' to links = {:?}", link, links);
|
2016-04-10 15:51:17 +00:00
|
|
|
links.push(link);
|
2016-04-12 20:27:38 +00:00
|
|
|
debug!("Setting {} links = {:?}", links.len(), links);
|
2016-04-10 15:51:17 +00:00
|
|
|
self.set_external_links(store, links)
|
|
|
|
})
|
2016-02-15 12:28:43 +00:00
|
|
|
}
|
|
|
|
|
2016-04-08 21:35:04 +00:00
|
|
|
/// Remove an external link from the implementor object
|
2016-04-10 16:43:02 +00:00
|
|
|
fn remove_external_link(&mut self, store: &Store, link: Url) -> Result<()> {
|
2016-04-08 21:35:04 +00:00
|
|
|
// get external links, remove this one, save them
|
2016-04-10 16:43:02 +00:00
|
|
|
self.get_external_links(store)
|
2016-04-16 20:04:08 +00:00
|
|
|
.and_then(|links| {
|
2016-04-12 20:27:38 +00:00
|
|
|
debug!("Removing link = '{:?}' from links = {:?}", link, links);
|
2016-04-10 16:43:02 +00:00
|
|
|
let links = links.into_iter()
|
2016-05-12 14:40:06 +00:00
|
|
|
.filter(|l| l.as_str() != link.as_str())
|
2016-04-10 16:43:02 +00:00
|
|
|
.collect();
|
|
|
|
self.set_external_links(store, links)
|
|
|
|
})
|
2016-02-15 12:28:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-04-08 21:35:04 +00:00
|
|
|
|