use std::{
path::{Path, PathBuf},
process::Command,
};
use crate::{io::home_dir_from_os, pkg::ArcPkgReq, util::open_link};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ManualFile {
pub filename: String,
pub url: String,
pub req: Option<ArcPkgReq>,
}
pub fn get_scan_dir_from_os(os: &str) -> PathBuf {
home_dir_from_os(os)
.unwrap_or_else(|_| PathBuf::from("/"))
.join("Downloads")
}
pub fn scan(dir: &Path, files: &[ManualFile]) -> Vec<String> {
let mut found_files = Vec::new();
for file in files {
let path = dir.join(&file.filename);
if path.exists() {
found_files.push(file.filename.clone());
}
}
found_files
}
pub fn open_all(files: &[ManualFile]) {
if files.len() > 1 {
if try_new_window(files).is_ok() {
return;
}
}
for file in files {
let _ = open_link(&file.url);
}
}
fn try_new_window(files: &[ManualFile]) -> anyhow::Result<()> {
if files.is_empty() {
return Ok(());
}
let to_try: &[&str] = if cfg!(target_os = "windows") {
&["firefox.exe", "chrome.exe", "msedge.exe"]
} else {
&["firefox", "google-chrome"]
};
for exe in to_try {
let first_file = &files[0];
let command = Command::new(exe)
.arg("--new-window")
.arg(&first_file.url)
.args(files.iter().skip(1).map(|x| &x.url))
.spawn();
if command.is_ok() {
return Ok(());
}
}
Err(anyhow::anyhow!("Failed to open a new window"))
}