2024-03-10 04:53:46 +00:00
|
|
|
use crate::{
|
|
|
|
error_code::ErrorCode, file::File, future::WithPollTimer, store::Store, stream::LocalBoxStream,
|
|
|
|
};
|
2021-10-23 04:48:56 +00:00
|
|
|
use actix_web::web::Bytes;
|
2023-08-23 16:59:42 +00:00
|
|
|
use futures_core::Stream;
|
2021-10-23 04:48:56 +00:00
|
|
|
use std::{
|
|
|
|
path::{Path, PathBuf},
|
2023-09-02 23:30:45 +00:00
|
|
|
sync::Arc,
|
2021-10-23 04:48:56 +00:00
|
|
|
};
|
2022-10-02 02:17:18 +00:00
|
|
|
use tracing::Instrument;
|
2021-10-23 04:48:56 +00:00
|
|
|
|
2023-06-20 20:59:08 +00:00
|
|
|
use super::StoreError;
|
|
|
|
|
2021-10-23 04:48:56 +00:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
pub(crate) enum FileError {
|
2022-03-26 21:49:23 +00:00
|
|
|
#[error("Failed to read or write file")]
|
2021-10-23 04:48:56 +00:00
|
|
|
Io(#[from] std::io::Error),
|
|
|
|
|
2023-09-03 23:21:46 +00:00
|
|
|
#[error("Couldn't strip root dir")]
|
|
|
|
PrefixError,
|
|
|
|
|
2023-09-02 23:30:45 +00:00
|
|
|
#[error("Couldn't convert Path to String")]
|
|
|
|
StringError,
|
2021-10-23 04:48:56 +00:00
|
|
|
|
|
|
|
#[error("Tried to save over existing file")]
|
|
|
|
FileExists,
|
|
|
|
}
|
|
|
|
|
2023-09-02 01:50:10 +00:00
|
|
|
impl FileError {
|
|
|
|
pub(super) const fn error_code(&self) -> ErrorCode {
|
|
|
|
match self {
|
|
|
|
Self::Io(_) => ErrorCode::FILE_IO_ERROR,
|
|
|
|
Self::FileExists => ErrorCode::FILE_EXISTS,
|
2023-09-03 23:21:46 +00:00
|
|
|
Self::StringError | Self::PrefixError => ErrorCode::FORMAT_FILE_ID_ERROR,
|
2023-09-02 01:50:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 04:48:56 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub(crate) struct FileStore {
|
|
|
|
root_dir: PathBuf,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Store for FileStore {
|
2023-12-16 04:34:45 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(self))]
|
2023-07-07 17:05:42 +00:00
|
|
|
async fn health_check(&self) -> Result<(), StoreError> {
|
|
|
|
tokio::fs::metadata(&self.root_dir)
|
|
|
|
.await
|
|
|
|
.map_err(FileError::from)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-03-10 04:53:46 +00:00
|
|
|
async fn save_stream<S>(
|
2023-06-20 20:59:08 +00:00
|
|
|
&self,
|
2024-03-10 04:53:46 +00:00
|
|
|
stream: S,
|
2023-07-14 19:53:37 +00:00
|
|
|
_content_type: mime::Mime,
|
2024-02-28 02:41:25 +00:00
|
|
|
extension: Option<&str>,
|
2023-09-02 23:30:45 +00:00
|
|
|
) -> Result<Arc<str>, StoreError>
|
2021-10-23 04:48:56 +00:00
|
|
|
where
|
2024-03-10 04:53:46 +00:00
|
|
|
S: Stream<Item = std::io::Result<Bytes>>,
|
2021-10-23 04:48:56 +00:00
|
|
|
{
|
2024-02-28 02:41:25 +00:00
|
|
|
let path = self.next_file(extension);
|
2021-10-23 04:48:56 +00:00
|
|
|
|
2024-03-11 03:02:27 +00:00
|
|
|
if let Err(e) = self
|
|
|
|
.safe_save_stream(&path, crate::stream::error_injector(stream))
|
|
|
|
.await
|
|
|
|
{
|
2021-10-23 04:48:56 +00:00
|
|
|
self.safe_remove_file(&path).await?;
|
2022-03-27 01:45:12 +00:00
|
|
|
return Err(e.into());
|
2021-10-23 04:48:56 +00:00
|
|
|
}
|
|
|
|
|
2022-03-27 01:45:12 +00:00
|
|
|
Ok(self.file_id_from_path(path)?)
|
2021-10-23 04:48:56 +00:00
|
|
|
}
|
|
|
|
|
2023-09-02 23:30:45 +00:00
|
|
|
fn public_url(&self, _identifier: &Arc<str>) -> Option<url::Url> {
|
2023-07-14 20:23:07 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2023-09-03 23:21:46 +00:00
|
|
|
#[tracing::instrument(skip(self))]
|
2021-10-23 04:48:56 +00:00
|
|
|
async fn to_stream(
|
|
|
|
&self,
|
2023-09-02 23:30:45 +00:00
|
|
|
identifier: &Arc<str>,
|
2021-10-23 04:48:56 +00:00
|
|
|
from_start: Option<u64>,
|
|
|
|
len: Option<u64>,
|
2023-09-02 23:30:45 +00:00
|
|
|
) -> Result<LocalBoxStream<'static, std::io::Result<Bytes>>, StoreError> {
|
2021-10-23 04:48:56 +00:00
|
|
|
let path = self.path_from_file_id(identifier);
|
|
|
|
|
2022-04-07 02:40:49 +00:00
|
|
|
let file_span = tracing::trace_span!(parent: None, "File Stream");
|
|
|
|
let file = file_span
|
|
|
|
.in_scope(|| File::open(path))
|
|
|
|
.instrument(file_span.clone())
|
2023-06-20 20:59:08 +00:00
|
|
|
.await
|
|
|
|
.map_err(FileError::from)?;
|
2022-04-07 02:40:49 +00:00
|
|
|
|
|
|
|
let stream = file_span
|
|
|
|
.in_scope(|| file.read_to_stream(from_start, len))
|
|
|
|
.instrument(file_span)
|
2021-10-23 04:48:56 +00:00
|
|
|
.await?;
|
|
|
|
|
2024-03-11 03:02:27 +00:00
|
|
|
Ok(Box::pin(crate::stream::error_injector(stream)))
|
2021-10-23 04:48:56 +00:00
|
|
|
}
|
|
|
|
|
2023-09-03 23:21:46 +00:00
|
|
|
#[tracing::instrument(skip(self))]
|
2023-09-02 23:30:45 +00:00
|
|
|
async fn len(&self, identifier: &Arc<str>) -> Result<u64, StoreError> {
|
2021-10-23 04:48:56 +00:00
|
|
|
let path = self.path_from_file_id(identifier);
|
|
|
|
|
2023-06-20 20:59:08 +00:00
|
|
|
let len = tokio::fs::metadata(path)
|
|
|
|
.await
|
|
|
|
.map_err(FileError::from)?
|
|
|
|
.len();
|
2021-10-23 04:48:56 +00:00
|
|
|
|
|
|
|
Ok(len)
|
|
|
|
}
|
|
|
|
|
2023-09-03 23:21:46 +00:00
|
|
|
#[tracing::instrument(skip(self))]
|
2023-09-02 23:30:45 +00:00
|
|
|
async fn remove(&self, identifier: &Arc<str>) -> Result<(), StoreError> {
|
2021-10-23 04:48:56 +00:00
|
|
|
let path = self.path_from_file_id(identifier);
|
|
|
|
|
|
|
|
self.safe_remove_file(path).await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FileStore {
|
2024-02-26 21:43:30 +00:00
|
|
|
pub(crate) async fn build(root_dir: PathBuf) -> color_eyre::Result<Self> {
|
2023-07-08 15:42:03 +00:00
|
|
|
tokio::fs::create_dir_all(&root_dir).await?;
|
2023-07-07 17:05:42 +00:00
|
|
|
|
2024-02-26 21:43:30 +00:00
|
|
|
Ok(FileStore { root_dir })
|
2021-10-23 04:48:56 +00:00
|
|
|
}
|
|
|
|
|
2023-09-02 23:30:45 +00:00
|
|
|
fn file_id_from_path(&self, path: PathBuf) -> Result<Arc<str>, FileError> {
|
2023-09-03 23:21:46 +00:00
|
|
|
path.strip_prefix(&self.root_dir)
|
|
|
|
.map_err(|_| FileError::PrefixError)?
|
|
|
|
.to_str()
|
|
|
|
.ok_or(FileError::StringError)
|
|
|
|
.map(Into::into)
|
2023-09-02 23:30:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn path_from_file_id(&self, file_id: &Arc<str>) -> PathBuf {
|
|
|
|
self.root_dir.join(file_id.as_ref())
|
|
|
|
}
|
|
|
|
|
2024-02-28 02:41:25 +00:00
|
|
|
fn next_file(&self, extension: Option<&str>) -> PathBuf {
|
2024-03-02 18:20:50 +00:00
|
|
|
crate::file_path::generate_disk(self.root_dir.clone(), extension)
|
2021-10-23 04:48:56 +00:00
|
|
|
}
|
|
|
|
|
2023-12-16 04:34:45 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(self, path), fields(path = ?path.as_ref()))]
|
2021-10-23 04:48:56 +00:00
|
|
|
async fn safe_remove_file<P: AsRef<Path>>(&self, path: P) -> Result<(), FileError> {
|
|
|
|
tokio::fs::remove_file(&path).await?;
|
|
|
|
self.try_remove_parents(path.as_ref()).await;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn try_remove_parents(&self, mut path: &Path) {
|
|
|
|
while let Some(parent) = path.parent() {
|
2023-12-28 17:58:38 +00:00
|
|
|
tracing::trace!("try_remove_parents: looping");
|
|
|
|
|
2021-10-23 04:48:56 +00:00
|
|
|
if parent.ends_with(&self.root_dir) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if tokio::fs::remove_dir(parent).await.is_err() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
path = parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-10 04:53:46 +00:00
|
|
|
async fn safe_save_stream<P: AsRef<Path>>(
|
2021-10-23 04:48:56 +00:00
|
|
|
&self,
|
|
|
|
to: P,
|
2024-03-10 04:53:46 +00:00
|
|
|
input: impl Stream<Item = std::io::Result<Bytes>>,
|
2021-10-23 04:48:56 +00:00
|
|
|
) -> Result<(), FileError> {
|
|
|
|
safe_create_parent(&to).await?;
|
|
|
|
|
|
|
|
if let Err(e) = tokio::fs::metadata(&to).await {
|
|
|
|
if e.kind() != std::io::ErrorKind::NotFound {
|
|
|
|
return Err(e.into());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(FileError::FileExists);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut file = File::create(to).await?;
|
|
|
|
|
2024-03-10 04:53:46 +00:00
|
|
|
file.write_from_stream(input)
|
|
|
|
.with_poll_timer("write-from-stream")
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
file.close().await?;
|
2021-10-23 04:48:56 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn safe_create_parent<P: AsRef<Path>>(path: P) -> Result<(), FileError> {
|
|
|
|
if let Some(path) = path.as_ref().parent() {
|
|
|
|
tokio::fs::create_dir_all(path).await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for FileStore {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.debug_struct("FileStore")
|
|
|
|
.field("root_dir", &self.root_dir)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|