about summary refs log tree commit diff
path: root/kittybox-rs/src/media/storage
diff options
context:
space:
mode:
Diffstat (limited to 'kittybox-rs/src/media/storage')
-rw-r--r--kittybox-rs/src/media/storage/file.rs30
1 files changed, 4 insertions, 26 deletions
diff --git a/kittybox-rs/src/media/storage/file.rs b/kittybox-rs/src/media/storage/file.rs
index c554d9e..1e0ff0e 100644
--- a/kittybox-rs/src/media/storage/file.rs
+++ b/kittybox-rs/src/media/storage/file.rs
@@ -29,38 +29,16 @@ impl From<tokio::io::Error> for MediaStoreError {
     }
 }
 
-
 impl FileStore {
     pub fn new<T: Into<PathBuf>>(base: T) -> Self {
         Self { base: base.into() }
     }
 
     async fn mktemp(&self) -> Result<(PathBuf, BufWriter<tokio::fs::File>)> {
-        use rand::{Rng, distributions::Alphanumeric};
-        tokio::fs::create_dir_all(self.base.as_path()).await?;
-        loop {
-            let filename = self.base.join(format!("temp.{}", {
-                let string = rand::thread_rng()
-                    .sample_iter(&Alphanumeric)
-                    .take(16)
-                    .collect::<Vec<u8>>();
-                String::from_utf8(string).unwrap()
-            }));
-
-            match OpenOptions::new()
-                .create_new(true)
-                .write(true)
-                .open(&filename)
-                .await
-            {
-                // TODO: determine if BufWriter provides benefit here
-                Ok(file) => return Ok((filename, BufWriter::with_capacity(BUF_CAPACITY, file))),
-                Err(err) => match err.kind() {
-                    std::io::ErrorKind::AlreadyExists => continue,
-                    _ => return Err(err.into())
-                }
-            }
-        }
+        kittybox_util::fs::mktemp(&self.base, "temp", 16)
+            .await
+            .map(|(name, file)| (name, BufWriter::new(file)))
+            .map_err(Into::into)
     }
 }