Refactored common filename function.
This commit is contained in:
parent
fe29841ffe
commit
d3f53422df
@ -117,6 +117,11 @@ impl MoreThanText {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn filename(&self, id: &str) -> String {
|
||||||
|
let filename = Path::new(&self.dir).join(&id);
|
||||||
|
filename.into_os_string().into_string().unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
async fn add_entry(&self, entry: CacheType) -> Result<String, DBError> {
|
async fn add_entry(&self, entry: CacheType) -> Result<String, DBError> {
|
||||||
let mut id: String = "".to_string();
|
let mut id: String = "".to_string();
|
||||||
let mut dup = true;
|
let mut dup = true;
|
||||||
@ -124,7 +129,7 @@ impl MoreThanText {
|
|||||||
id = thread_rng().sample_iter(&Alphanumeric).take(32).collect();
|
id = thread_rng().sample_iter(&Alphanumeric).take(32).collect();
|
||||||
dup = Path::new(&self.dir).join(&id).as_path().exists().await;
|
dup = Path::new(&self.dir).join(&id).as_path().exists().await;
|
||||||
}
|
}
|
||||||
match write(Path::new(&self.dir).join(&id), entry.to_bytes()).await {
|
match write(Path::new(&self.filename(&id)), entry.to_bytes()).await {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
let mut error = DBError::new("data write");
|
let mut error = DBError::new("data write");
|
||||||
@ -145,7 +150,7 @@ impl MoreThanText {
|
|||||||
entry.touch();
|
entry.touch();
|
||||||
Ok(entry.clone())
|
Ok(entry.clone())
|
||||||
}
|
}
|
||||||
None => match read(Path::new(&self.dir).join(&id)).await {
|
None => match read(Path::new(&self.filename(id))).await {
|
||||||
Ok(content) => {
|
Ok(content) => {
|
||||||
let data = CacheEntry::new(CacheType::from_bytes(content));
|
let data = CacheEntry::new(CacheType::from_bytes(content));
|
||||||
cache.insert(id.to_string(), data.clone());
|
cache.insert(id.to_string(), data.clone());
|
||||||
@ -161,7 +166,7 @@ impl MoreThanText {
|
|||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(err) => return Err(err),
|
Err(err) => return Err(err),
|
||||||
}
|
}
|
||||||
match write(Path::new(&self.dir).join(&id), entry.to_bytes()).await {
|
match write(Path::new(&self.filename(id)), entry.to_bytes()).await {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
let mut error = DBError::new("data write");
|
let mut error = DBError::new("data write");
|
||||||
|
Loading…
Reference in New Issue
Block a user