|
1 | | -use std::fs::{self, File}; |
| 1 | +use std::fs::{self, File as StdFile}; |
2 | 2 | use std::path::Path; |
3 | | -use crate::types::{MediaFile, TextFile}; |
| 3 | +use crate::types::{MediaFile, TextFile, File}; |
4 | 4 | use std::io::Write; |
5 | 5 |
|
| 6 | +/// Saves a [`File`] into a directory named `cached_files_{notification_from}`. |
| 7 | +/// |
| 8 | +/// The function writes the [`TextFile`] content and appends a line |
| 9 | +/// for each attached [`MediaFile`]. |
| 10 | +/// |
| 11 | +/// # Errors |
| 12 | +/// |
| 13 | +/// Returns an error if the directory cannot be created or if the file cannot |
| 14 | +/// be created or written to. |
| 15 | +pub fn save_file(notification_from: &u8, file: &File) -> std::io::Result<()> { |
| 16 | + let dir_name = format!("cached_files_{notification_from}"); |
| 17 | + let dir_path = Path::new(&dir_name); |
| 18 | + fs::create_dir_all(dir_path)?; |
| 19 | + |
| 20 | + let file_name = format!("{}_{}", file.text_file.id, file.text_file.title); |
| 21 | + let file_path = dir_path.join(file_name); |
| 22 | + |
| 23 | + let mut f = StdFile::create(file_path)?; |
| 24 | + writeln!(f, "{}", file.text_file.content)?; |
| 25 | + for media_file in &file.media_files { |
| 26 | + writeln!(f, "MediaFile attached: {}_{}", media_file.id, media_file.title)?; |
| 27 | + } |
| 28 | + Ok(()) |
| 29 | +} |
| 30 | + |
| 31 | +/// Saves a list of [`File`]s into `cached_files_{notification_from}` by |
| 32 | +/// delegating to [`save_file`] for each element. |
| 33 | +/// |
| 34 | +/// # Errors |
| 35 | +/// |
| 36 | +/// Returns an error if saving any single file fails. |
| 37 | +pub fn save_files(notification_from: &u8, files: &Vec<File>) -> std::io::Result<()> { |
| 38 | + for file in files { |
| 39 | + save_file(notification_from, file)?; |
| 40 | + } |
| 41 | + Ok(()) |
| 42 | +} |
| 43 | + |
| 44 | +/// Saves a [`TextFile`] into `cached_files_{notification_from}`. |
| 45 | +/// |
| 46 | +/// The function writes the text content and appends a line for each |
| 47 | +/// attached [`MediaReference`]. |
| 48 | +/// |
| 49 | +/// # Errors |
| 50 | +/// |
| 51 | +/// Returns an error if the directory cannot be created or if the file cannot |
| 52 | +/// be created or written to. |
6 | 53 | pub fn save_text_file(notification_from: &u8, file: &TextFile) -> std::io::Result<()> { |
7 | | - let dir_name = format!("cached_files_{}", notification_from); |
| 54 | + let dir_name = format!("cached_files_{notification_from}"); |
8 | 55 | let dir_path = Path::new(&dir_name); |
9 | 56 | fs::create_dir_all(dir_path)?; |
10 | 57 |
|
11 | 58 | let file_name = format!("{}_{}", file.id, file.title); |
12 | 59 | let file_path = dir_path.join(file_name); |
13 | 60 |
|
14 | | - let mut f = File::create(file_path)?; |
| 61 | + let mut f = StdFile::create(file_path)?; |
15 | 62 | writeln!(f, "{}", file.content)?; |
16 | 63 | for media_ref in &file.media_refs { |
17 | 64 | writeln!(f, "MediaFile attached: {}_{}", media_ref.location, media_ref.id)?; |
18 | 65 | } |
19 | 66 | Ok(()) |
20 | 67 | } |
21 | 68 |
|
| 69 | +/// Saves a list of [`TextFile`]s by delegating to [`save_text_file`]. |
| 70 | +/// |
| 71 | +/// # Errors |
| 72 | +/// |
| 73 | +/// Returns an error if saving any single file fails. |
22 | 74 | pub fn save_text_files(notification_from: &u8, files: &Vec<TextFile>) -> std::io::Result<()> { |
23 | 75 | for file in files { |
24 | 76 | save_text_file(notification_from, file)?; |
25 | 77 | } |
26 | 78 | Ok(()) |
27 | 79 | } |
28 | 80 |
|
| 81 | +/// Saves a single [`MediaFile`] into `cached_files_{notification_from}`. |
| 82 | +/// |
| 83 | +/// The file is written as `{id}_{title}` and its binary content flushed. |
| 84 | +/// |
| 85 | +/// # Errors |
| 86 | +/// |
| 87 | +/// Returns an error if the directory cannot be created or if the file cannot |
| 88 | +/// be created or written to. |
29 | 89 | pub fn save_media_file(notification_from: &u8, file: &MediaFile) -> std::io::Result<()> { |
30 | | - let dir_name = format!("cached_files_{}", notification_from); |
| 90 | + let dir_name = format!("cached_files_{notification_from}"); |
31 | 91 | let dir_path = Path::new(&dir_name); |
32 | 92 | fs::create_dir_all(dir_path)?; |
33 | 93 |
|
34 | 94 | let file_name = format!("{}_{}", file.id, file.title); |
35 | 95 | let file_path = dir_path.join(file_name); |
36 | 96 |
|
37 | | - let mut f = File::create(file_path)?; |
| 97 | + let mut f = StdFile::create(file_path)?; |
38 | 98 | for chunk in &file.content { |
39 | 99 | f.write_all(chunk)?; |
40 | 100 | } |
41 | 101 | Ok(()) |
42 | 102 | } |
43 | 103 |
|
| 104 | +/// Saves a list of [`MediaFile`]s by delegating to [`save_media_file`]. |
| 105 | +/// |
| 106 | +/// # Errors |
| 107 | +/// |
| 108 | +/// Returns an error if saving any single file fails. |
44 | 109 | pub fn save_media_files(notification_from: &u8, files: &[MediaFile]) -> std::io::Result<()> { |
45 | 110 | for file in files { |
46 | 111 | save_media_file(notification_from, file)?; |
|
0 commit comments