summaryrefslogtreecommitdiff
path: root/src/files.rs
blob: 045c5b48868ac988ec3890da2cbaff67736c3400 (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use anyhow::{Result, anyhow};
use futures_util::StreamExt;
use pin_utils::pin_mut;
use std::{
    fmt::Display,
    path::{Path, PathBuf},
    time::UNIX_EPOCH,
};
use tokio::{fs::read_dir, task::JoinSet};

use crate::{db::Database, flac::handle_encode};

#[derive(Debug)]
pub struct FileError {
    file: PathBuf,
    error: anyhow::Error,
}

impl FileError {
    pub fn new(file: impl AsRef<Path>, error: anyhow::Error) -> Self {
        FileError {
            file: file.as_ref().to_path_buf(),
            error,
        }
    }
}

impl Display for FileError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "error: {}\t on file {}",
            self.error,
            self.file.to_string_lossy()
        )
    }
}

async fn handle_file(file: PathBuf, conn: Database) -> Result<()> {
    match conn.check_file(&file).await {
        Ok(true) => {
            let modtime = file
                .metadata()?
                .modified()?
                .duration_since(UNIX_EPOCH)?
                .as_secs();
            let db_time = conn.get_modtime(&file).await?;
            if modtime != db_time {
                if let Err(error) = conn.update_file(&file).await {
                    return Err(anyhow!(FileError::new(file, error)));
                };
            }
            return Ok(());
        }
        Err(error) => return Err(anyhow!(FileError::new(file, error))),
        _ => {}
    }

    if let Err(error) = conn.insert_file(&file).await {
        return Err(anyhow!(FileError::new(file, error)));
    }

    Ok(())
}

pub async fn index_files_recursively(path: impl AsRef<Path>, conn: &Database) -> Result<()> {
    if !path.as_ref().is_dir() {
        return Err(anyhow!("Invalid root directory"));
    }
    let abspath = path.as_ref().canonicalize()?;
    let mut tasks = JoinSet::new();

    let mut dirs = vec![abspath];

    let mut counter: u64 = 0;

    while let Some(dir) = dirs.pop() {
        let mut read_dir = read_dir(dir).await?;

        while let Some(entry) = read_dir.next_entry().await? {
            let path = entry.path();
            if path.is_dir() {
                dirs.push(path);
            } else if path.is_file() {
                if let Some(ext) = path.extension() {
                    if ext == "flac" {
                        let newconn = conn.clone();
                        tasks.spawn(async move { handle_file(path, newconn).await });
                    }
                }
            }
        }
    }

    while let Some(task) = tasks.join_next().await {
        match task {
            Ok(Err(error)) => eprintln!("{error}"),
            Err(error) => eprintln!("Error encountered:\t{}", error),
            _ => {
                counter += 1;
                print!("\rParsed files:\t{counter}");
            }
        }
    }
    Ok(())
}

fn check_path(folderpath: Option<&PathBuf>) -> Result<(PathBuf, bool)> {
    if let Some(real_path) = folderpath {
        Ok((real_path.canonicalize()?, false))
    } else {
        Ok((PathBuf::new(), true))
    }
}

pub async fn reencode_files(folderpath: Option<&PathBuf>, conn: &Database) -> Result<()> {
    let (path, nocheck) = check_path(folderpath)?;

    let stream = conn.get_toencode_stream().await?;
    pin_mut!(stream);

    let mut tasks = JoinSet::new();

    let mut counter: u64 = 0;

    while let Some(Ok(row)) = stream.next().await {
        if let Some(file) = row.get_value(0)?.as_text() {
            let filename = Path::new(file).canonicalize()?;
            if nocheck || filename.starts_with(&path) {
                tasks.spawn_blocking(move || handle_encode(filename));
            }
        }
    }

    let mut update_tasks = JoinSet::new();

    while let Some(task) = tasks.join_next().await {
        match task {
            Ok(Ok(path)) => {
                let newconn = conn.clone();
                update_tasks.spawn(async move { newconn.update_file(path).await });
                counter += 1;
                print!("\rReencoded files:\t{counter}")
            }
            Ok(Err(error)) => eprintln!("{error}"),
            Err(error) => eprintln!("Error encountered:\t{}", error),
        }
    }

    while let Some(task) = update_tasks.join_next().await {
        match task {
            Ok(Err(error)) => eprintln!("{error}"),
            Err(error) => eprintln!("Error encountered:\t{}", error),
            _ => {}
        }
    }

    Ok(())
}

pub async fn count_reencode_files(folderpath: Option<&PathBuf>, conn: &Database) -> Result<u64> {
    let (path, nocheck) = check_path(folderpath)?;

    let mut counter: u64 = 0;
    let stream = conn.get_toencode_stream().await?;
    pin_mut!(stream);

    while let Some(Ok(row)) = stream.next().await {
        if let Some(file) = row.get_value(0)?.as_text() {
            let filename = Path::new(file).canonicalize()?;
            if nocheck || filename.starts_with(&path) {
                counter += 1;
            }
        }
    }

    Ok(counter)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_index_lots_of_files() {
        let conn = Database::new("temp3.db").await.unwrap();
        index_files_recursively(Path::new("./testfiles"), &conn)
            .await
            .unwrap();

        std::fs::remove_file("temp3.db").unwrap();
    }

    #[tokio::test]
    async fn test_reencode_lots_of_files() {
        let conn = Database::new("temp4.db").await.unwrap();
        let path = PathBuf::from("./testfiles");
        index_files_recursively(Path::new("./testfiles"), &conn)
            .await
            .unwrap();
        println!("\n{}", count_reencode_files(None, &conn).await.unwrap());
        reencode_files(Some(&path), &conn).await.unwrap();
        std::fs::remove_file("temp4.db").unwrap();
    }
}