2023-11-10 00:20:59 +00:00
|
|
|
use std::{
|
|
|
|
ops::Deref,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
sync::Arc,
|
|
|
|
};
|
2021-10-23 19:14:12 +00:00
|
|
|
use tokio::io::AsyncRead;
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
2023-10-07 00:42:24 +00:00
|
|
|
pub(crate) type ArcTmpDir = Arc<TmpDir>;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) struct TmpDir {
|
2023-10-07 16:36:49 +00:00
|
|
|
path: Option<PathBuf>,
|
2023-10-07 00:42:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TmpDir {
|
|
|
|
pub(crate) async fn init() -> std::io::Result<Arc<Self>> {
|
|
|
|
let path = std::env::temp_dir().join(Uuid::new_v4().to_string());
|
|
|
|
tokio::fs::create_dir(&path).await?;
|
2023-10-07 16:36:49 +00:00
|
|
|
Ok(Arc::new(TmpDir { path: Some(path) }))
|
2023-10-07 00:42:24 +00:00
|
|
|
}
|
|
|
|
|
2023-11-10 00:20:59 +00:00
|
|
|
fn build_tmp_file(&self, ext: Option<&str>) -> Arc<Path> {
|
2023-10-07 00:42:24 +00:00
|
|
|
if let Some(ext) = ext {
|
2023-11-10 00:20:59 +00:00
|
|
|
Arc::from(self.path.as_ref().expect("tmp path exists").join(format!(
|
|
|
|
"{}{}",
|
|
|
|
Uuid::new_v4(),
|
|
|
|
ext
|
|
|
|
)))
|
2023-10-07 00:42:24 +00:00
|
|
|
} else {
|
2023-11-10 00:20:59 +00:00
|
|
|
Arc::from(
|
|
|
|
self.path
|
|
|
|
.as_ref()
|
|
|
|
.expect("tmp path exists")
|
|
|
|
.join(Uuid::new_v4().to_string()),
|
|
|
|
)
|
2023-10-07 00:42:24 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-07 16:36:49 +00:00
|
|
|
|
2023-11-10 00:20:59 +00:00
|
|
|
pub(crate) fn tmp_file(&self, ext: Option<&str>) -> TmpFile {
|
|
|
|
TmpFile(self.build_tmp_file(ext))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn tmp_folder(&self) -> std::io::Result<TmpFolder> {
|
|
|
|
let path = self.build_tmp_file(None);
|
|
|
|
tokio::fs::create_dir(&path).await?;
|
|
|
|
Ok(TmpFolder(path))
|
|
|
|
}
|
|
|
|
|
2023-10-07 16:36:49 +00:00
|
|
|
pub(crate) async fn cleanup(self: Arc<Self>) -> std::io::Result<()> {
|
|
|
|
if let Some(path) = Arc::into_inner(self).and_then(|mut this| this.path.take()) {
|
|
|
|
tokio::fs::remove_dir_all(path).await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-10-07 00:42:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for TmpDir {
|
|
|
|
fn drop(&mut self) {
|
2023-10-07 16:36:49 +00:00
|
|
|
if let Some(path) = self.path.as_ref() {
|
|
|
|
std::fs::remove_dir_all(path).expect("Removed directory");
|
|
|
|
}
|
2023-10-07 00:42:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-10 00:20:59 +00:00
|
|
|
#[must_use]
|
|
|
|
pub(crate) struct TmpFolder(Arc<Path>);
|
|
|
|
|
|
|
|
impl TmpFolder {
|
|
|
|
pub(crate) fn reader<R: AsyncRead>(self, reader: R) -> TmpFolderCleanup<R> {
|
|
|
|
TmpFolderCleanup {
|
|
|
|
inner: reader,
|
|
|
|
folder: self,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<Path> for TmpFolder {
|
|
|
|
fn as_ref(&self) -> &Path {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for TmpFolder {
|
|
|
|
type Target = Path;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for TmpFolder {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
crate::sync::spawn(
|
|
|
|
"remove-tmpfolder",
|
|
|
|
tokio::fs::remove_dir_all(self.0.clone()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[must_use]
|
|
|
|
pub(crate) struct TmpFile(Arc<Path>);
|
|
|
|
|
|
|
|
impl TmpFile {
|
|
|
|
pub(crate) fn reader<R: AsyncRead>(self, reader: R) -> TmpFileCleanup<R> {
|
|
|
|
TmpFileCleanup {
|
|
|
|
inner: reader,
|
|
|
|
file: self,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<Path> for TmpFile {
|
|
|
|
fn as_ref(&self) -> &Path {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for TmpFile {
|
|
|
|
type Target = Path;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
2021-10-23 19:14:12 +00:00
|
|
|
|
|
|
|
impl Drop for TmpFile {
|
|
|
|
fn drop(&mut self) {
|
2023-10-21 00:08:11 +00:00
|
|
|
crate::sync::spawn("remove-tmpfile", tokio::fs::remove_file(self.0.clone()));
|
2021-10-23 19:14:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pin_project_lite::pin_project! {
|
|
|
|
pub(crate) struct TmpFileCleanup<R> {
|
|
|
|
#[pin]
|
|
|
|
inner: R,
|
|
|
|
|
|
|
|
file: TmpFile,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-10 00:20:59 +00:00
|
|
|
pin_project_lite::pin_project! {
|
|
|
|
pub(crate) struct TmpFolderCleanup<R> {
|
|
|
|
#[pin]
|
|
|
|
inner: R,
|
|
|
|
|
|
|
|
folder: TmpFolder,
|
2021-10-23 19:14:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<R: AsyncRead> AsyncRead for TmpFileCleanup<R> {
|
|
|
|
fn poll_read(
|
|
|
|
mut self: std::pin::Pin<&mut Self>,
|
|
|
|
cx: &mut std::task::Context<'_>,
|
|
|
|
buf: &mut tokio::io::ReadBuf<'_>,
|
|
|
|
) -> std::task::Poll<std::io::Result<()>> {
|
|
|
|
let this = self.as_mut().project();
|
|
|
|
|
|
|
|
this.inner.poll_read(cx, buf)
|
|
|
|
}
|
|
|
|
}
|
2023-11-10 00:20:59 +00:00
|
|
|
|
|
|
|
impl<R: AsyncRead> AsyncRead for TmpFolderCleanup<R> {
|
|
|
|
fn poll_read(
|
|
|
|
mut self: std::pin::Pin<&mut Self>,
|
|
|
|
cx: &mut std::task::Context<'_>,
|
|
|
|
buf: &mut tokio::io::ReadBuf<'_>,
|
|
|
|
) -> std::task::Poll<std::io::Result<()>> {
|
|
|
|
let this = self.as_mut().project();
|
|
|
|
|
|
|
|
this.inner.poll_read(cx, buf)
|
|
|
|
}
|
|
|
|
}
|