2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2018-02-07 01:48:53 +00:00
|
|
|
// Copyright (C) 2015-2018 Matthias Beyer <mail@beyermatthias.de> and contributors
|
2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; version
|
|
|
|
// 2.1 of the License.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
//
|
|
|
|
|
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;
|
2017-02-22 09:53:27 +00:00
|
|
|
use std::fmt::Debug;
|
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::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::*;
|
2018-10-30 17:40:51 +00:00
|
|
|
use libimagerror::errors::ErrorMsg as EM;
|
2016-02-03 14:47:14 +00:00
|
|
|
|
2017-08-26 15:53:08 +00:00
|
|
|
use toml_query::read::TomlValueReadExt;
|
2018-01-12 15:32:18 +00:00
|
|
|
use toml_query::read::TomlValueReadTypeExt;
|
2017-09-07 20:13:00 +00:00
|
|
|
use toml_query::insert::TomlValueInsertExt;
|
2018-10-30 17:40:51 +00:00
|
|
|
use failure::Error;
|
|
|
|
use failure::Fallible as Result;
|
|
|
|
use failure::ResultExt;
|
|
|
|
use failure::err_msg;
|
2017-08-26 15:53:08 +00:00
|
|
|
|
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-10-15 13:35:47 +00:00
|
|
|
use self::iter::*;
|
|
|
|
|
2016-02-03 18:59:22 +00:00
|
|
|
use toml::Value;
|
2016-02-23 10:50:42 +00:00
|
|
|
use url::Url;
|
2018-07-18 16:51:05 +00:00
|
|
|
use sha1::{Sha1, Digest};
|
|
|
|
use hex;
|
2016-02-03 18:59:22 +00:00
|
|
|
|
2017-08-28 08:31:13 +00:00
|
|
|
pub trait Link {
|
2016-02-21 13:28:55 +00:00
|
|
|
|
2017-08-28 08:31:13 +00:00
|
|
|
fn get_link_uri_from_filelockentry(&self) -> Result<Option<Url>>;
|
2016-02-21 13:28:55 +00:00
|
|
|
|
2017-08-28 08:31:13 +00:00
|
|
|
fn get_url(&self) -> Result<Option<Url>>;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Link for Entry {
|
2016-02-21 13:28:55 +00:00
|
|
|
|
2017-08-28 08:31:13 +00:00
|
|
|
fn get_link_uri_from_filelockentry(&self) -> Result<Option<Url>> {
|
|
|
|
self.get_header()
|
2018-01-12 15:32:18 +00:00
|
|
|
.read_string("links.external.content.url")
|
2018-10-30 17:40:51 +00:00
|
|
|
.map_err(Error::from)
|
|
|
|
.context(EM::EntryHeaderReadError)
|
|
|
|
.map_err(Error::from)
|
2016-06-11 04:53:46 +00:00
|
|
|
.and_then(|opt| match opt {
|
2018-01-12 15:32:18 +00:00
|
|
|
None => Ok(None),
|
|
|
|
Some(ref s) => {
|
2016-09-05 16:51:35 +00:00
|
|
|
debug!("Found url, parsing: {:?}", s);
|
2018-10-30 17:40:51 +00:00
|
|
|
Url::parse(&s[..])
|
|
|
|
.map_err(Error::from)
|
|
|
|
.context(err_msg("Invalid URI"))
|
|
|
|
.map_err(Error::from)
|
|
|
|
.map(Some)
|
2016-09-05 16:51:35 +00:00
|
|
|
},
|
2016-04-08 21:35:04 +00:00
|
|
|
})
|
2018-10-30 17:40:51 +00:00
|
|
|
.map_err(Error::from)
|
2016-02-21 13:28:55 +00:00
|
|
|
}
|
|
|
|
|
2017-08-28 08:31:13 +00:00
|
|
|
fn get_url(&self) -> Result<Option<Url>> {
|
2018-01-12 15:32:18 +00:00
|
|
|
match self.get_header().read_string("links.external.url")? {
|
|
|
|
None => Ok(None),
|
|
|
|
Some(ref s) => Url::parse(&s[..])
|
|
|
|
.map(Some)
|
2018-10-30 17:40:51 +00:00
|
|
|
.map_err(Error::from)
|
|
|
|
.context(EM::EntryHeaderReadError)
|
|
|
|
.map_err(Error::from),
|
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 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-10-15 13:35:47 +00:00
|
|
|
fn get_external_links<'a>(&self, store: &'a Store) -> Result<UrlIter<'a>>;
|
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-10-15 12:41:20 +00:00
|
|
|
pub mod iter {
|
2016-10-15 13:20:17 +00:00
|
|
|
//! Iterator helpers for external linking stuff
|
|
|
|
//!
|
|
|
|
//! Contains also helpers to filter iterators for external/internal links
|
|
|
|
//!
|
2016-10-15 13:33:44 +00:00
|
|
|
//!
|
|
|
|
//! # Warning
|
|
|
|
//!
|
|
|
|
//! This module uses `internal::Link` as link type, so we operate on _store ids_ here.
|
|
|
|
//!
|
|
|
|
//! Not to confuse with `external::Link` which is a real `FileLockEntry` under the hood.
|
|
|
|
//!
|
2016-10-15 13:20:17 +00:00
|
|
|
|
2016-10-15 12:41:20 +00:00
|
|
|
use libimagutil::debug_result::*;
|
|
|
|
use libimagstore::store::Store;
|
|
|
|
|
|
|
|
use internal::Link;
|
|
|
|
use internal::iter::LinkIter;
|
2018-10-30 17:40:51 +00:00
|
|
|
use failure::Fallible as Result;
|
2016-10-15 12:41:20 +00:00
|
|
|
|
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
/// Helper for building `OnlyExternalIter` and `NoExternalIter`
|
|
|
|
///
|
|
|
|
/// The boolean value defines, how to interpret the `is_external_link_storeid()` return value
|
|
|
|
/// (here as "pred"):
|
|
|
|
///
|
2018-05-11 12:55:23 +00:00
|
|
|
/// ```ignore
|
2016-10-15 12:41:20 +00:00
|
|
|
/// pred | bool | xor | take?
|
|
|
|
/// ---- | ---- | --- | ----
|
|
|
|
/// 0 | 0 | 0 | 1
|
|
|
|
/// 0 | 1 | 1 | 0
|
2016-12-05 16:17:26 +00:00
|
|
|
/// 1 | 0 | 1 | 0
|
2016-10-15 12:41:20 +00:00
|
|
|
/// 1 | 1 | 0 | 1
|
2018-05-11 12:55:23 +00:00
|
|
|
/// ```
|
2016-10-15 12:41:20 +00:00
|
|
|
///
|
|
|
|
/// If `bool` says "take if return value is false", we take the element if the `pred` returns
|
|
|
|
/// false... and so on.
|
|
|
|
///
|
|
|
|
/// As we can see, the operator between these two operants is `!(a ^ b)`.
|
2016-10-15 13:20:36 +00:00
|
|
|
pub struct ExternalFilterIter(LinkIter, bool);
|
2016-10-15 12:41:20 +00:00
|
|
|
|
|
|
|
impl Iterator for ExternalFilterIter {
|
|
|
|
type Item = Link;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
use super::is_external_link_storeid;
|
|
|
|
|
|
|
|
while let Some(elem) = self.0.next() {
|
2017-09-15 21:01:26 +00:00
|
|
|
trace!("Check whether is external: {:?}", elem);
|
2016-10-15 12:41:20 +00:00
|
|
|
if !(self.1 ^ is_external_link_storeid(&elem)) {
|
2017-09-15 21:01:26 +00:00
|
|
|
trace!("Is external id: {:?}", elem);
|
2016-10-15 12:41:20 +00:00
|
|
|
return Some(elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-15 13:20:36 +00:00
|
|
|
/// Helper trait to be implemented on `LinkIter` to select or deselect all external links
|
|
|
|
///
|
|
|
|
/// # See also
|
|
|
|
///
|
|
|
|
/// Also see `OnlyExternalIter` and `NoExternalIter` and the helper traits/functions
|
|
|
|
/// `OnlyInteralLinks`/`only_internal_links()` and `OnlyExternalLinks`/`only_external_links()`.
|
|
|
|
pub trait SelectExternal {
|
|
|
|
fn select_external_links(self, b: bool) -> ExternalFilterIter;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SelectExternal for LinkIter {
|
|
|
|
fn select_external_links(self, b: bool) -> ExternalFilterIter {
|
|
|
|
ExternalFilterIter(self, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-15 12:41:20 +00:00
|
|
|
pub struct OnlyExternalIter(ExternalFilterIter);
|
|
|
|
|
|
|
|
impl OnlyExternalIter {
|
|
|
|
pub fn new(li: LinkIter) -> OnlyExternalIter {
|
|
|
|
OnlyExternalIter(ExternalFilterIter(li, true))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn urls<'a>(self, store: &'a Store) -> UrlIter<'a> {
|
|
|
|
UrlIter(self, store)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for OnlyExternalIter {
|
|
|
|
type Item = Link;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
self.0.next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct NoExternalIter(ExternalFilterIter);
|
|
|
|
|
|
|
|
impl NoExternalIter {
|
|
|
|
pub fn new(li: LinkIter) -> NoExternalIter {
|
|
|
|
NoExternalIter(ExternalFilterIter(li, false))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for NoExternalIter {
|
|
|
|
type Item = Link;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
self.0.next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait OnlyExternalLinks : Sized {
|
|
|
|
fn only_external_links(self) -> OnlyExternalIter ;
|
|
|
|
|
|
|
|
fn no_internal_links(self) -> OnlyExternalIter {
|
|
|
|
self.only_external_links()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OnlyExternalLinks for LinkIter {
|
|
|
|
fn only_external_links(self) -> OnlyExternalIter {
|
|
|
|
OnlyExternalIter::new(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait OnlyInternalLinks : Sized {
|
|
|
|
fn only_internal_links(self) -> NoExternalIter;
|
|
|
|
|
|
|
|
fn no_external_links(self) -> NoExternalIter {
|
|
|
|
self.only_internal_links()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OnlyInternalLinks for LinkIter {
|
|
|
|
fn only_internal_links(self) -> NoExternalIter {
|
|
|
|
NoExternalIter::new(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct UrlIter<'a>(OnlyExternalIter, &'a Store);
|
|
|
|
|
|
|
|
impl<'a> Iterator for UrlIter<'a> {
|
|
|
|
type Item = Result<Url>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
2017-08-28 08:31:13 +00:00
|
|
|
use external::Link;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let next = self.0
|
|
|
|
.next()
|
|
|
|
.map(|id| {
|
|
|
|
debug!("Retrieving entry for id: '{:?}'", id);
|
|
|
|
self.1
|
|
|
|
.retrieve(id.clone())
|
|
|
|
.map_dbg_err(|_| format!("Retrieving entry for id: '{:?}' failed", id))
|
2017-09-23 13:03:29 +00:00
|
|
|
.map_err(From::from)
|
2017-08-28 08:31:13 +00:00
|
|
|
.and_then(|f| {
|
|
|
|
debug!("Store::retrieve({:?}) succeeded", id);
|
|
|
|
debug!("getting external link from file now");
|
|
|
|
f.get_link_uri_from_filelockentry()
|
2017-09-15 21:01:26 +00:00
|
|
|
.map_dbg_str("Error happened while getting link URI from FLE")
|
2017-08-28 08:31:13 +00:00
|
|
|
.map_dbg_err(|e| format!("URL -> Err = {:?}", e))
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
match next {
|
|
|
|
Some(Ok(Some(link))) => return Some(Ok(link)),
|
|
|
|
Some(Ok(None)) => continue,
|
|
|
|
Some(Err(e)) => return Some(Err(e)),
|
|
|
|
None => return None
|
|
|
|
}
|
|
|
|
}
|
2016-10-15 12:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-09 13:15:13 +00:00
|
|
|
/// Check whether the StoreId starts with `/link/external/`
|
2017-02-22 09:53:27 +00:00
|
|
|
pub fn is_external_link_storeid<A: AsRef<StoreId> + Debug>(id: A) -> bool {
|
2016-09-05 16:52:42 +00:00
|
|
|
debug!("Checking whether this is a 'links/external/': '{:?}'", id);
|
2017-02-22 09:53:27 +00:00
|
|
|
id.as_ref().local().starts_with("links/external")
|
2016-04-09 13:15:13 +00:00
|
|
|
}
|
|
|
|
|
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-10-15 13:35:47 +00:00
|
|
|
fn get_external_links<'a>(&self, store: &'a Store) -> Result<UrlIter<'a>> {
|
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()
|
2016-10-13 16:30:16 +00:00
|
|
|
.map(|iter| {
|
2016-04-12 20:27:38 +00:00
|
|
|
debug!("Getting external links");
|
2016-10-15 13:35:47 +00:00
|
|
|
iter.only_external_links().urls(store)
|
2016-04-09 13:15:13 +00:00
|
|
|
})
|
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
|
2018-07-18 16:51:05 +00:00
|
|
|
let hash = hex::encode(Sha1::digest(&link.as_str().as_bytes()));
|
2017-10-30 19:17:21 +00:00
|
|
|
let file_id =
|
2016-08-25 16:49:26 +00:00
|
|
|
ModuleEntryPath::new(format!("external/{}", hash)).into_storeid()
|
|
|
|
.map_dbg_err(|_| {
|
|
|
|
format!("Failed to build StoreId for this hash '{:?}'", hash)
|
|
|
|
})
|
2017-10-30 19:17:21 +00:00
|
|
|
?;
|
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
|
2017-10-30 19:17:21 +00:00
|
|
|
let mut file = store
|
2016-08-03 09:10:56 +00:00
|
|
|
.retrieve(file_id.clone())
|
|
|
|
.map_dbg_err(|_| {
|
|
|
|
format!("Failed to create or retrieve an file for this link '{:?}'", link)
|
2017-10-30 19:17:21 +00:00
|
|
|
})?;
|
2016-04-16 13:34:32 +00:00
|
|
|
|
|
|
|
debug!("Generating header content!");
|
|
|
|
{
|
2017-08-29 15:22:58 +00:00
|
|
|
let hdr = file.deref_mut().get_header_mut();
|
2016-04-16 13:34:32 +00:00
|
|
|
|
2017-10-30 19:17:21 +00:00
|
|
|
let mut table = match hdr.read("links.external.content")? {
|
2017-09-23 13:03:29 +00:00
|
|
|
Some(&Value::Table(ref table)) => table.clone(),
|
|
|
|
Some(_) => {
|
2017-09-07 20:13:00 +00:00
|
|
|
warn!("There is a value at 'links.external.content' which is not a table.");
|
2016-04-16 13:34:32 +00:00
|
|
|
warn!("Going to override this value");
|
|
|
|
BTreeMap::new()
|
|
|
|
},
|
2017-09-23 13:03:29 +00:00
|
|
|
None => BTreeMap::new(),
|
2016-04-16 13:34:32 +00:00
|
|
|
};
|
|
|
|
|
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);
|
|
|
|
|
2017-10-30 19:17:21 +00:00
|
|
|
let _ = hdr.insert("links.external.content", Value::Table(table))?;
|
2017-09-23 13:03:29 +00:00
|
|
|
debug!("Setting URL worked");
|
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
|
2017-10-30 19:17:21 +00:00
|
|
|
let _ = self.add_internal_link(file.deref_mut())?;
|
2017-09-23 13:03:29 +00:00
|
|
|
debug!("Error adding internal link");
|
2016-04-09 13:49:50 +00:00
|
|
|
}
|
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)
|
2016-10-15 13:35:47 +00:00
|
|
|
.and_then(|links| {
|
|
|
|
// TODO: Do not ignore errors here
|
|
|
|
let mut links = links.filter_map(Result::ok).collect::<Vec<_>>();
|
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-10-15 13:35:47 +00:00
|
|
|
debug!("Removing link = '{:?}'", link);
|
|
|
|
let links = links
|
|
|
|
.filter_map(Result::ok)
|
2016-05-12 14:40:06 +00:00
|
|
|
.filter(|l| l.as_str() != link.as_str())
|
2016-10-15 13:35:47 +00:00
|
|
|
.collect::<Vec<_>>();
|
2016-04-10 16:43:02 +00:00
|
|
|
self.set_external_links(store, links)
|
|
|
|
})
|
2016-02-15 12:28:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-04-08 21:35:04 +00:00
|
|
|
|
2017-09-15 20:49:40 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use std::path::PathBuf;
|
2018-05-01 18:53:07 +00:00
|
|
|
use std::sync::Arc;
|
2017-09-15 20:49:40 +00:00
|
|
|
|
|
|
|
use libimagstore::store::Store;
|
|
|
|
|
|
|
|
fn setup_logging() {
|
|
|
|
use env_logger;
|
2017-12-07 21:07:01 +00:00
|
|
|
let _ = env_logger::try_init();
|
2017-09-15 20:49:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_store() -> Store {
|
|
|
|
use libimagstore::file_abstraction::InMemoryFileAbstraction;
|
2018-05-01 18:53:07 +00:00
|
|
|
let backend = Arc::new(InMemoryFileAbstraction::default());
|
2017-12-22 10:24:04 +00:00
|
|
|
Store::new_with_backend(PathBuf::from("/"), &None, backend).unwrap()
|
2017-09-15 20:49:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_simple() {
|
|
|
|
setup_logging();
|
|
|
|
let store = get_store();
|
|
|
|
let mut e = store.retrieve(PathBuf::from("base-test_simple")).unwrap();
|
|
|
|
let url = Url::parse("http://google.de").unwrap();
|
|
|
|
|
|
|
|
assert!(e.add_external_link(&store, url.clone()).is_ok());
|
|
|
|
|
|
|
|
assert_eq!(1, e.get_external_links(&store).unwrap().count());
|
|
|
|
assert_eq!(url, e.get_external_links(&store).unwrap().next().unwrap().unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|