summaryrefslogtreecommitdiffstats
path: root/libimagmail
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2016-09-21 19:26:08 +0200
committerMatthias Beyer <mail@beyermatthias.de>2016-10-12 19:17:41 +0200
commit953f53767063194120e04bfecfa283e1ec088bb0 (patch)
treead3dd44df74cc27d7843a8c3ef6cae5e28139993 /libimagmail
parentc7f890d0d4b014c82925514a1679ccc5a9c42d98 (diff)
Create internal file buffer when storing/loading
Diffstat (limited to 'libimagmail')
-rw-r--r--libimagmail/src/mail.rs40
1 files changed, 35 insertions, 5 deletions
diff --git a/libimagmail/src/mail.rs b/libimagmail/src/mail.rs
index 3eb07fa0..dd98345d 100644
--- a/libimagmail/src/mail.rs
+++ b/libimagmail/src/mail.rs
@@ -1,6 +1,8 @@
use std::result::Result as RResult;
use std::path::Path;
use std::path::PathBuf;
+use std::fs::File;
+use std::io::Read;
use libimagstore::store::{FileLockEntry, Store};
use libimagref::reference::Ref;
@@ -26,7 +28,7 @@ impl From<String> for Buffer {
}
}
-pub struct Mail<'a>(Ref<'a>);
+pub struct Mail<'a>(Ref<'a>, Buffer);
impl<'a> Mail<'a> {
@@ -38,15 +40,43 @@ impl<'a> Mail<'a> {
Ref::create_with_hasher(store, p, f, h)
.map_err_into(MEK::RefCreationError)
- .map(|r| Mail(r))
+ .and_then(|reference| {
+ reference.fs_file()
+ .map_err_into(MEK::RefHandlingError)
+ .and_then(|path| File::open(path).map_err_into(MEK::IOError))
+ .and_then(|mut file| {
+ let mut s = String::new();
+ file.read_to_string(&mut s)
+ .map(|_| s)
+ .map_err_into(MEK::IOError)
+ })
+ .map(Buffer::from)
+ .map(|buffer| Mail(reference, buffer))
+ })
}
/// Opens a mail by the passed hash
pub fn open<S: AsRef<str>>(store: &Store, hash: S) -> Result<Option<Mail>> {
- Ref::get_by_hash(store, String::from(hash.as_ref()))
- .map(|opt| opt.map(|r| Mail(r)))
+ let r = try!(Ref::get_by_hash(store, String::from(hash.as_ref()))
.map_err_into(MEK::FetchByHashError)
- .map_err_into(MEK::FetchError)
+ .map_err_into(MEK::FetchError));
+
+ if r.is_none() {
+ return Ok(None);
+ }
+ let r = r.unwrap();
+
+ r.fs_file()
+ .map_err_into(MEK::RefHandlingError)
+ .and_then(|path| File::open(path).map_err_into(MEK::IOError))
+ .and_then(|mut file| {
+ let mut s = String::new();
+ file.read_to_string(&mut s)
+ .map(|_| s)
+ .map_err_into(MEK::IOError)
+ })
+ .map(Buffer::from)
+ .map(|buffer| Some(Mail(r, buffer)))
}
pub fn get_field<S: AsRef<str>>(&self, field: S) -> Result<Option<&str>> {