|
| 1 | +use async_recursion::async_recursion; |
| 2 | +use std::path::PathBuf; |
| 3 | +use tokio::io::AsyncWriteExt; |
| 4 | + |
| 5 | +type Error = Box<dyn std::error::Error + Send + Sync + 'static>; |
| 6 | + |
| 7 | +impl crate::application::CoLink { |
| 8 | + async fn _sm_fs_get_path( |
| 9 | + &self, |
| 10 | + path_key_name: &str, |
| 11 | + path_suffix: &str, |
| 12 | + ) -> Result<PathBuf, Error> { |
| 13 | + let path_key = format!("{}:path", path_key_name); |
| 14 | + let mut path = String::from_utf8(self.read_entry(&path_key).await?)?; |
| 15 | + if !path_suffix.is_empty() { |
| 16 | + let path_suffix = path_suffix.replace(':', "/"); |
| 17 | + path += "/"; |
| 18 | + path += &path_suffix; |
| 19 | + } |
| 20 | + println!("path: {}", path); |
| 21 | + let path = PathBuf::from(path); |
| 22 | + tokio::fs::create_dir_all(path.parent().unwrap()).await?; |
| 23 | + Ok(path) |
| 24 | + } |
| 25 | + |
| 26 | + #[async_recursion] |
| 27 | + pub(crate) async fn _create_entry_fs( |
| 28 | + &self, |
| 29 | + path_key_name: &str, |
| 30 | + path_suffix: &str, |
| 31 | + payload: &[u8], |
| 32 | + ) -> Result<String, Error> { |
| 33 | + let path = self._sm_fs_get_path(path_key_name, path_suffix).await?; |
| 34 | + let mut file = tokio::fs::OpenOptions::new() |
| 35 | + .write(true) |
| 36 | + .create_new(true) |
| 37 | + .open(path) |
| 38 | + .await?; |
| 39 | + file.write_all(payload).await?; |
| 40 | + Ok("ok".to_string()) |
| 41 | + } |
| 42 | + |
| 43 | + #[async_recursion] |
| 44 | + pub(crate) async fn _read_entry_fs( |
| 45 | + &self, |
| 46 | + path_key_name: &str, |
| 47 | + path_suffix: &str, |
| 48 | + ) -> Result<Vec<u8>, Error> { |
| 49 | + let path = self._sm_fs_get_path(path_key_name, path_suffix).await?; |
| 50 | + let data = tokio::fs::read(path).await?; |
| 51 | + Ok(data) |
| 52 | + } |
| 53 | + |
| 54 | + #[async_recursion] |
| 55 | + pub(crate) async fn _update_entry_fs( |
| 56 | + &self, |
| 57 | + path_key_name: &str, |
| 58 | + path_suffix: &str, |
| 59 | + payload: &[u8], |
| 60 | + ) -> Result<String, Error> { |
| 61 | + let path = self._sm_fs_get_path(path_key_name, path_suffix).await?; |
| 62 | + let mut file = tokio::fs::File::create(path).await?; |
| 63 | + file.write_all(payload).await?; |
| 64 | + Ok("ok".to_string()) |
| 65 | + } |
| 66 | + |
| 67 | + #[async_recursion] |
| 68 | + pub(crate) async fn _append_entry_fs( |
| 69 | + &self, |
| 70 | + path_key_name: &str, |
| 71 | + path_suffix: &str, |
| 72 | + payload: &[u8], |
| 73 | + ) -> Result<String, Error> { |
| 74 | + let path = self._sm_fs_get_path(path_key_name, path_suffix).await?; |
| 75 | + let lock_token = self.lock(&path.to_string_lossy()).await?; |
| 76 | + // use a closure to prevent locking forever caused by errors |
| 77 | + let res = async { |
| 78 | + let mut file = tokio::fs::OpenOptions::new() |
| 79 | + .append(true) |
| 80 | + .open(path) |
| 81 | + .await?; |
| 82 | + file.write_all(payload).await?; |
| 83 | + Ok::<String, Error>("ok".to_string()) |
| 84 | + } |
| 85 | + .await; |
| 86 | + self.unlock(lock_token).await?; |
| 87 | + res |
| 88 | + } |
| 89 | + |
| 90 | + #[async_recursion] |
| 91 | + pub(crate) async fn _delete_entry_fs( |
| 92 | + &self, |
| 93 | + path_key_name: &str, |
| 94 | + path_suffix: &str, |
| 95 | + ) -> Result<String, Error> { |
| 96 | + let path = self._sm_fs_get_path(path_key_name, path_suffix).await?; |
| 97 | + tokio::fs::remove_file(path).await?; |
| 98 | + Ok("ok".to_string()) |
| 99 | + } |
| 100 | +} |
0 commit comments