1
0
Fork 0
mirror of https://gitlab.com/famedly/conduit.git synced 2025-06-27 16:35:59 +00:00

refactor(service/media): make all fs operations async

This commit is contained in:
AndSDev 2025-06-06 10:46:58 +03:00
parent 0b5e93eeff
commit e7822f0332

View file

@ -1,5 +1,5 @@
mod data; mod data;
use std::{fs, io::Cursor, sync::Arc}; use std::{io::Cursor, sync::Arc};
pub use data::Data; pub use data::Data;
use futures_util::{stream, StreamExt}; use futures_util::{stream, StreamExt};
@ -26,7 +26,7 @@ pub struct DbFileMeta {
} }
use tokio::{ use tokio::{
fs::File, fs::{self, File},
io::{AsyncReadExt, AsyncWriteExt}, io::{AsyncReadExt, AsyncWriteExt},
}; };
@ -662,7 +662,7 @@ pub async fn create_file(sha256_hex: &str, file: &[u8]) -> Result<()> {
// Create all directories leading up to file // Create all directories leading up to file
if let Some(parent) = path.parent() { if let Some(parent) = path.parent() {
fs::create_dir_all(&parent).inspect_err(|e| error!("Error creating leading directories for media with sha256 hash of {sha256_hex}: {e}"))?; fs::create_dir_all(&parent).await.inspect_err(|e| error!("Error creating leading directories for media with sha256 hash of {sha256_hex}: {e}"))?;
} }
let mut f = File::create(path).await?; let mut f = File::create(path).await?;
@ -727,7 +727,7 @@ async fn delete_file(sha256_hex: &str) -> Result<()> {
.globals .globals
.get_media_path(path, directory_structure, sha256_hex)?; .get_media_path(path, directory_structure, sha256_hex)?;
if let Err(e) = fs::remove_file(&path) { if let Err(e) = fs::remove_file(&path).await {
// Multiple files with the same filehash might be requseted to be deleted // Multiple files with the same filehash might be requseted to be deleted
if e.kind() != std::io::ErrorKind::NotFound { if e.kind() != std::io::ErrorKind::NotFound {
error!("Error removing media from filesystem: {e}"); error!("Error removing media from filesystem: {e}");
@ -742,7 +742,7 @@ async fn delete_file(sha256_hex: &str) -> Result<()> {
// Here at the start so that the first time, the file gets removed from the path // Here at the start so that the first time, the file gets removed from the path
path.pop(); path.pop();
if let Err(e) = fs::remove_dir(&path) { if let Err(e) = fs::remove_dir(&path).await {
if e.kind() == std::io::ErrorKind::DirectoryNotEmpty { if e.kind() == std::io::ErrorKind::DirectoryNotEmpty {
break; break;
} else { } else {