| 1 | use crate::archive_ui::{Entry, Kind}; |
| 2 | use std::collections::{BTreeMap, BTreeSet}; |
| 3 | |
| 4 | #[derive(Clone, Debug)] |
| 5 | pub struct Row { |
| 6 | pub path: String, |
| 7 | pub name: String, |
| 8 | pub directory: bool, |
| 9 | pub ids: BTreeSet<u32>, |
| 10 | pub bytes: Option<u64>, |
| 11 | pub encrypted: bool, |
| 12 | } |
| 13 | |
| 14 | pub fn normalized(name: &str) -> Option<String> { |
| 15 | let name = name.replace('\\', "/"); |
| 16 | let name = name.trim_end_matches('/'); |
| 17 | if name.is_empty() |
| 18 | || name.starts_with('/') |
| 19 | || name.contains(':') |
| 20 | || name |
| 21 | .split('/') |
| 22 | .any(|part| part.is_empty() || part == "." || part == ".." || part.contains('\0')) |
| 23 | { |
| 24 | return None; |
| 25 | } |
| 26 | Some(name.to_owned()) |
| 27 | } |
| 28 | |
| 29 | pub fn rows(entries: &[Entry], current: &str, query: &str) -> Vec<Row> { |
| 30 | let prefix = if current.is_empty() { |
| 31 | String::new() |
| 32 | } else { |
| 33 | format!("{current}/") |
| 34 | }; |
| 35 | let mut rows = BTreeMap::<String, Row>::new(); |
| 36 | for entry in entries { |
| 37 | let Some(path) = normalized(&entry.name) else { |
| 38 | continue; |
| 39 | }; |
| 40 | let Some(relative) = path.strip_prefix(&prefix) else { |
| 41 | continue; |
| 42 | }; |
| 43 | if relative.is_empty() { |
| 44 | continue; |
| 45 | } |
| 46 | let (name, implicit) = relative |
| 47 | .split_once('/') |
| 48 | .map_or((relative, false), |(first, _)| (first, true)); |
| 49 | let directory = implicit || entry.kind == Kind::Directory; |
| 50 | let row = rows.entry(name.to_owned()).or_insert_with(|| Row { |
| 51 | path: format!("{prefix}{name}"), |
| 52 | name: name.to_owned(), |
| 53 | directory, |
| 54 | ids: BTreeSet::new(), |
| 55 | bytes: None, |
| 56 | encrypted: false, |
| 57 | }); |
| 58 | row.directory |= directory; |
| 59 | if matches!(entry.kind, Kind::File | Kind::Directory) { |
| 60 | row.ids.insert(entry.id); |
| 61 | } |
| 62 | if !directory { |
| 63 | row.bytes = entry.bytes; |
| 64 | } else if entry.kind == Kind::File { |
| 65 | // 폴더 크기는 안에 있는 모든 파일 크기의 합으로 표시함. |
| 66 | let total = row.bytes.unwrap_or(0); |
| 67 | row.bytes = Some(total.saturating_add(entry.bytes.unwrap_or(0))); |
| 68 | } else if row.bytes.is_none() { |
| 69 | row.bytes = Some(0); |
| 70 | } |
| 71 | row.encrypted |= entry.encrypted; |
| 72 | } |
| 73 | let query = query.to_lowercase(); |
| 74 | let mut rows = rows |
| 75 | .into_values() |
| 76 | .filter(|row| row.name.to_lowercase().contains(&query)) |
| 77 | .collect::<Vec<_>>(); |
| 78 | rows.sort_by(|a, b| { |
| 79 | b.directory |
| 80 | .cmp(&a.directory) |
| 81 | .then_with(|| a.name.cmp(&b.name)) |
| 82 | }); |
| 83 | rows |
| 84 | } |
| 85 | |
| 86 | pub fn expand_selection(entries: &[Entry], selected: &BTreeSet<u32>) -> BTreeSet<u32> { |
| 87 | let directories = entries |
| 88 | .iter() |
| 89 | .filter(|entry| entry.kind == Kind::Directory && selected.contains(&entry.id)) |
| 90 | .filter_map(|entry| normalized(&entry.name).map(|path| format!("{path}/"))) |
| 91 | .collect::<Vec<_>>(); |
| 92 | entries |
| 93 | .iter() |
| 94 | .filter(|entry| { |
| 95 | selected.contains(&entry.id) |
| 96 | || normalized(&entry.name) |
| 97 | .is_some_and(|name| directories.iter().any(|prefix| name.starts_with(prefix))) |
| 98 | }) |
| 99 | .filter(|entry| matches!(entry.kind, Kind::File | Kind::Directory)) |
| 100 | .map(|entry| entry.id) |
| 101 | .collect() |
| 102 | } |
| 103 | |
| 104 | pub fn remove_selection(entries: &[Entry], selected: &mut BTreeSet<u32>, removed: &BTreeSet<u32>) { |
| 105 | let paths = entries |
| 106 | .iter() |
| 107 | .filter(|entry| removed.contains(&entry.id)) |
| 108 | .filter_map(|entry| normalized(&entry.name)) |
| 109 | .collect::<Vec<_>>(); |
| 110 | let ancestors = entries |
| 111 | .iter() |
| 112 | .filter(|entry| entry.kind == Kind::Directory) |
| 113 | .filter_map(|entry| { |
| 114 | let prefix = format!("{}/", normalized(&entry.name)?); |
| 115 | paths |
| 116 | .iter() |
| 117 | .any(|path| path.starts_with(&prefix)) |
| 118 | .then_some(entry.id) |
| 119 | }) |
| 120 | .collect::<BTreeSet<_>>(); |
| 121 | selected.retain(|id| !removed.contains(id) && !ancestors.contains(id)); |
| 122 | } |
| 123 | |
| 124 | /// 찾기 창의 결과 한 줄. |
| 125 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 126 | pub struct Found { |
| 127 | /// 압축파일 안 전체 경로 (앞뒤 '/' 없음). |
| 128 | pub path: String, |
| 129 | pub name: String, |
| 130 | pub directory: bool, |
| 131 | } |
| 132 | |
| 133 | /// 찾기 결과를 너무 많이 그리지 않도록 정한 최대 개수. |
| 134 | pub const FIND_LIMIT: usize = 5000; |
| 135 | |
| 136 | /// 압축파일 안에서 검색어가 들어간 파일과 폴더를 찾음. 대소문자는 구분하지 않음. |
| 137 | /// |
| 138 | /// - `whole_path`가 false면(Ctrl+F) `base` 폴더 아래의 모든 하위 폴더를 찾아 내려가며 |
| 139 | /// 이름에 검색어가 들어간 항목을 찾음. |
| 140 | /// - `whole_path`가 true면(Ctrl+Shift+F) 압축파일 전체에서 전체 경로에 검색어가 들어간 |
| 141 | /// 항목을 찾음. 이때 `base`는 쓰지 않음. |
| 142 | /// |
| 143 | /// 압축파일에 따로 적혀 있지 않은 상위 폴더도 결과에 포함함. 결과는 경로 순서이며 |
| 144 | /// `limit`개를 넘으면 잘라 내고 두 번째 값으로 true를 돌려줌. |
| 145 | pub fn find( |
| 146 | entries: &[Entry], |
| 147 | base: &str, |
| 148 | query: &str, |
| 149 | whole_path: bool, |
| 150 | limit: usize, |
| 151 | ) -> (Vec<Found>, bool) { |
| 152 | let query = query.trim().to_lowercase(); |
| 153 | if query.is_empty() { |
| 154 | return (Vec::new(), false); |
| 155 | } |
| 156 | let base = base.trim_matches('/'); |
| 157 | let prefix = if whole_path || base.is_empty() { |
| 158 | String::new() |
| 159 | } else { |
| 160 | format!("{base}/") |
| 161 | }; |
| 162 | let mut paths = BTreeMap::<String, bool>::new(); |
| 163 | for entry in entries { |
| 164 | if !matches!(entry.kind, Kind::File | Kind::Directory) { |
| 165 | continue; |
| 166 | } |
| 167 | let Some(path) = normalized(&entry.name) else { |
| 168 | continue; |
| 169 | }; |
| 170 | if !path.starts_with(&prefix) { |
| 171 | continue; |
| 172 | } |
| 173 | // 상위 폴더는 압축파일에 따로 없어도 폴더로 넣음. |
| 174 | let mut end = prefix.len(); |
| 175 | while let Some(slash) = path[end..].find('/') { |
| 176 | end += slash; |
| 177 | paths.insert(path[..end].to_owned(), true); |
| 178 | end += 1; |
| 179 | } |
| 180 | let directory = entry.kind == Kind::Directory; |
| 181 | *paths.entry(path).or_insert(directory) |= directory; |
| 182 | } |
| 183 | let mut found = Vec::new(); |
| 184 | for (path, directory) in paths { |
| 185 | let name = path.rsplit('/').next().unwrap_or(&path); |
| 186 | let haystack = if whole_path { path.as_str() } else { name }; |
| 187 | if !haystack.to_lowercase().contains(&query) { |
| 188 | continue; |
| 189 | } |
| 190 | if found.len() == limit { |
| 191 | return (found, true); |
| 192 | } |
| 193 | found.push(Found { |
| 194 | name: name.to_owned(), |
| 195 | path, |
| 196 | directory, |
| 197 | }); |
| 198 | } |
| 199 | (found, false) |
| 200 | } |