-
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathfile_handle_wrapper.rs
More file actions
131 lines (129 loc) · 6.35 KB
/
file_handle_wrapper.rs
File metadata and controls
131 lines (129 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use crate::adapters::file::file_handler::{FileHandler, FileHandlerType, FileRequest};
use crate::adapters::file::local_file_handler::LocalFileHandler;
use crate::adapters::file::s3_file_handler::S3Handler;
use crate::models::podcast_episode::PodcastEpisode;
use crate::models::podcasts::Podcast;
use crate::utils::error::CustomError;
use std::future::Future;
use std::pin::Pin;
pub struct FileHandleWrapper;
impl FileHandleWrapper {
pub fn write_file(
path: &str,
content: &mut [u8],
download_location: &FileHandlerType,
) -> Result<(), CustomError> {
match download_location {
FileHandlerType::Local => LocalFileHandler::write_file(path, content),
FileHandlerType::S3 => S3Handler::write_file(path, content),
}
}
pub fn write_file_async<'a>(
path: &'a str,
content: &'a mut [u8],
download_location: &FileHandlerType,
) -> Pin<Box<dyn Future<Output = Result<(), CustomError>> + Send + 'a>> {
match download_location {
FileHandlerType::Local => LocalFileHandler::write_file_async(path, content),
FileHandlerType::S3 => S3Handler::write_file_async(path, content),
}
}
pub fn create_dir(path: &str, download_location: &FileHandlerType) -> Result<(), CustomError> {
match download_location {
FileHandlerType::Local => LocalFileHandler::create_dir(path),
FileHandlerType::S3 => S3Handler::create_dir(path),
}
}
pub fn path_exists(path: &str, req: FileRequest, download_location: &FileHandlerType) -> bool {
match download_location {
FileHandlerType::Local => LocalFileHandler::path_exists(path, req),
FileHandlerType::S3 => S3Handler::path_exists(path, req),
}
}
pub fn remove_dir(podcast: &Podcast) -> Result<(), CustomError> {
match FileHandlerType::from(podcast.download_location.clone()) {
FileHandlerType::Local => {
// Remove the directory
LocalFileHandler::remove_dir(&podcast.directory_name)?;
PodcastEpisode::get_episodes_by_podcast_id(podcast.id)?
.iter()
.for_each(|episode| {
// Remove the episode directory
if let Some(download_type) = &episode.download_location {
let file_type = FileHandlerType::from(download_type.as_str());
if FileHandlerType::S3 == file_type {
if let Some(file_path) = &episode.file_image_path {
if let Err(e) = S3Handler::remove_file(file_path) {
log::error!(
"Error removing file: {} with reason {}",
file_path,
e
);
}
}
if let Some(file_path) = &episode.file_episode_path {
if let Err(e) = S3Handler::remove_file(file_path) {
log::error!(
"Error removing file: {} with reason {e}",
file_path
);
}
}
}
}
});
Ok(())
}
FileHandlerType::S3 => {
let image_url = urlencoding::decode(&podcast.image_url).unwrap().to_string();
S3Handler::remove_file(&image_url)?;
PodcastEpisode::get_episodes_by_podcast_id(podcast.id)?
.iter()
.for_each(|episode| {
// Remove the episode directory
if let Some(download_type) = &episode.download_location {
let file_type = FileHandlerType::from(download_type.as_str());
if FileHandlerType::S3 == file_type {
if let Some(file_path) = &episode.file_image_path {
if let Err(e) = S3Handler::remove_file(file_path) {
log::error!(
"Error removing file: {} with reason {}",
file_path,
e
);
}
}
if let Some(file_path) = &episode.file_episode_path {
if let Err(e) = S3Handler::remove_file(file_path) {
log::error!("Error removing file: {} {e}", file_path);
}
}
} else {
if let Some(file_path) = &episode.file_image_path {
if let Err(e) = LocalFileHandler::remove_file(file_path) {
log::error!(
"Error removing file: {} with reason {}",
file_path,
e
);
}
}
if let Some(file_path) = &episode.file_episode_path {
if let Err(e) = LocalFileHandler::remove_file(file_path) {
log::error!("Error removing file: {} {e}", file_path);
}
}
}
}
});
Ok(())
}
}
}
pub fn remove_file(path: &str, download_location: &FileHandlerType) -> Result<(), CustomError> {
match download_location {
FileHandlerType::Local => LocalFileHandler::remove_file(path),
FileHandlerType::S3 => S3Handler::remove_file(path),
}
}
}