2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2019-01-03 01:32:07 +00:00
|
|
|
// Copyright (C) 2015-2019 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-09 13:49:50 +00:00
|
|
|
use std::ops::DerefMut;
|
2016-02-21 13:28:55 +00:00
|
|
|
|
2016-04-08 21:35:04 +00:00
|
|
|
use libimagstore::storeid::StoreId;
|
2019-05-29 16:03:46 +00:00
|
|
|
use libimagstore::store::Store;
|
|
|
|
use libimagstore::store::Entry;
|
|
|
|
use libimagutil::debug_result::DebugResult;
|
2019-06-21 19:33:22 +00:00
|
|
|
use libimagentrylink::linker::Linkable;
|
2016-02-03 14:47:14 +00:00
|
|
|
|
2018-10-30 17:40:51 +00:00
|
|
|
use failure::Fallible as Result;
|
2016-02-03 18:59:22 +00:00
|
|
|
use toml::Value;
|
2019-05-29 16:03:46 +00:00
|
|
|
use toml::map::Map;
|
|
|
|
use toml_query::read::TomlValueReadExt;
|
|
|
|
use toml_query::insert::TomlValueInsertExt;
|
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
|
|
|
|
2019-05-29 16:03:46 +00:00
|
|
|
use crate::iter::UrlIter;
|
2016-02-21 13:28:55 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
pub trait UrlLinker : Linkable {
|
2016-02-21 13:28:55 +00:00
|
|
|
|
2016-04-08 21:35:04 +00:00
|
|
|
/// Get the external links from the implementor object
|
2019-05-30 09:00:43 +00:00
|
|
|
fn get_urls<'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
|
2019-05-30 09:00:43 +00:00
|
|
|
fn set_urls(&mut self, store: &Store, links: Vec<Url>) -> Result<Vec<StoreId>>;
|
2016-02-21 13:28:55 +00:00
|
|
|
|
2016-04-08 21:35:04 +00:00
|
|
|
/// Add an external link to the implementor object
|
2019-05-30 09:00:43 +00:00
|
|
|
fn add_url(&mut self, store: &Store, link: Url) -> Result<Vec<StoreId>>;
|
2016-02-03 18:59:22 +00:00
|
|
|
|
2016-04-08 21:35:04 +00:00
|
|
|
/// Remove an external link from the implementor object
|
2019-05-30 09:00:43 +00:00
|
|
|
fn remove_url(&mut self, store: &Store, link: Url) -> Result<Vec<StoreId>>;
|
2016-02-03 18:59:22 +00:00
|
|
|
|
2016-02-03 14:47:14 +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.
|
2019-05-29 16:03:46 +00:00
|
|
|
impl UrlLinker 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
|
2019-05-30 09:00:43 +00:00
|
|
|
fn get_urls<'a>(&self, store: &'a Store) -> Result<UrlIter<'a>> {
|
2019-05-29 16:03:46 +00:00
|
|
|
use crate::iter::OnlyExternalLinks;
|
|
|
|
|
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.
|
2019-06-21 19:33:22 +00:00
|
|
|
self.links()
|
2016-10-13 16:30:16 +00:00
|
|
|
.map(|iter| {
|
2016-04-12 20:27:38 +00:00
|
|
|
debug!("Getting external links");
|
2019-05-30 09:00:43 +00:00
|
|
|
iter.only_urls().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
|
2018-10-06 11:15:29 +00:00
|
|
|
///
|
|
|
|
/// # Return Value
|
|
|
|
///
|
|
|
|
/// Returns the StoreIds which were newly created for the new external links, if there are more
|
|
|
|
/// external links than before.
|
|
|
|
/// If there are less external links than before, an empty vec![] is returned.
|
|
|
|
///
|
2019-05-30 09:00:43 +00:00
|
|
|
fn set_urls(&mut self, store: &Store, links: Vec<Url>) -> Result<Vec<StoreId>> {
|
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);
|
2018-10-06 11:15:29 +00:00
|
|
|
links.into_iter().map(|link| {
|
2018-07-18 16:51:05 +00:00
|
|
|
let hash = hex::encode(Sha1::digest(&link.as_str().as_bytes()));
|
2019-02-28 16:28:32 +00:00
|
|
|
let file_id = crate::module_path::new_id(format!("external/{}", hash))
|
2019-04-09 17:19:40 +00:00
|
|
|
.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);
|
|
|
|
|
2018-10-06 11:15:29 +00:00
|
|
|
let link_already_exists = store.get(file_id.clone())?.is_some();
|
|
|
|
|
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");
|
2019-05-29 10:34:47 +00:00
|
|
|
Map::new()
|
2016-04-16 13:34:32 +00:00
|
|
|
},
|
2019-05-29 10:34:47 +00:00
|
|
|
None => Map::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
|
2019-06-21 19:33:22 +00:00
|
|
|
let _ = self.add_link(file.deref_mut())?;
|
2019-05-30 09:35:42 +00:00
|
|
|
debug!("Added internal link");
|
2018-10-06 11:15:29 +00:00
|
|
|
|
|
|
|
Ok((link_already_exists, file_id))
|
|
|
|
})
|
|
|
|
.filter_map(|res| match res {
|
|
|
|
Ok((exists, entry)) => if exists { Some(Ok(entry)) } else { None },
|
|
|
|
Err(e) => Some(Err(e))
|
|
|
|
})
|
|
|
|
.collect()
|
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
|
2018-10-06 11:15:29 +00:00
|
|
|
///
|
|
|
|
/// # Return Value
|
|
|
|
///
|
2019-05-30 09:00:43 +00:00
|
|
|
/// (See ExternalLinker::set_urls())
|
2018-10-06 11:15:29 +00:00
|
|
|
///
|
|
|
|
/// Returns the StoreIds which were newly created for the new external links, if there are more
|
|
|
|
/// external links than before.
|
|
|
|
/// If there are less external links than before, an empty vec![] is returned.
|
|
|
|
///
|
2019-05-30 09:00:43 +00:00
|
|
|
fn add_url(&mut self, store: &Store, link: Url) -> Result<Vec<StoreId>> {
|
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");
|
2019-05-30 09:00:43 +00:00
|
|
|
self.get_urls(store)
|
2016-10-15 13:35:47 +00:00
|
|
|
.and_then(|links| {
|
2019-03-09 11:39:56 +00:00
|
|
|
let mut links = links.collect::<Result<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);
|
2019-03-09 11:39:56 +00:00
|
|
|
|
2016-04-12 20:27:38 +00:00
|
|
|
debug!("Setting {} links = {:?}", links.len(), links);
|
2019-05-30 09:00:43 +00:00
|
|
|
self.set_urls(store, links)
|
2016-04-10 15:51:17 +00:00
|
|
|
})
|
2016-02-15 12:28:43 +00:00
|
|
|
}
|
|
|
|
|
2016-04-08 21:35:04 +00:00
|
|
|
/// Remove an external link from the implementor object
|
2018-10-06 11:15:29 +00:00
|
|
|
///
|
|
|
|
/// # Return Value
|
|
|
|
///
|
2019-05-30 09:00:43 +00:00
|
|
|
/// (See ExternalLinker::set_urls())
|
2018-10-06 11:15:29 +00:00
|
|
|
///
|
|
|
|
/// Returns the StoreIds which were newly created for the new external links, if there are more
|
|
|
|
/// external links than before.
|
|
|
|
/// If there are less external links than before, an empty vec![] is returned.
|
|
|
|
///
|
2019-05-30 09:00:43 +00:00
|
|
|
fn remove_url(&mut self, store: &Store, link: Url) -> Result<Vec<StoreId>> {
|
2016-04-08 21:35:04 +00:00
|
|
|
// get external links, remove this one, save them
|
2019-05-30 09:00:43 +00:00
|
|
|
self.get_urls(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<_>>();
|
2019-05-30 09:00:43 +00:00
|
|
|
self.set_urls(store, links)
|
2016-04-10 16:43:02 +00:00
|
|
|
})
|
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;
|
|
|
|
|
|
|
|
use libimagstore::store::Store;
|
|
|
|
|
|
|
|
fn setup_logging() {
|
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 {
|
2018-12-02 22:52:14 +00:00
|
|
|
Store::new_inmemory(PathBuf::from("/"), &None).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();
|
|
|
|
|
2019-05-30 09:00:43 +00:00
|
|
|
assert!(e.add_url(&store, url.clone()).is_ok());
|
2017-09-15 20:49:40 +00:00
|
|
|
|
2019-05-30 09:00:43 +00:00
|
|
|
assert_eq!(1, e.get_urls(&store).unwrap().count());
|
|
|
|
assert_eq!(url, e.get_urls(&store).unwrap().next().unwrap().unwrap());
|
2017-09-15 20:49:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|